这个问题我已经琢磨了好几个小时了.
这就是我正在努力实现的目标. 最终目标是能够使用Google Sheets中的侧边栏修改作业(job)名称. 可以使用自定义菜单打开侧边栏. 在侧栏中,我有一个字段,它从一个特定的范围中提取所有的工作名称. 在下面的字段中,用户可以键入更新的名称.
然后,使用textFinder()
函数,我希望找到原来的名称,如 Select 的字段,并替换为新的名称.
下面是我的代码:
code.gs
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Manager Menu')
.addItem('Rename Job', 'showNewJobSidebar')
.addToUi();
SpreadsheetApp.getUi();
}
function showNewJobSidebar() {
var html = HtmlService
.createTemplateFromFile('sidebar');
// Add the dropdown lists to the template
html.dropdown = SpreadsheetApp.getActiveSheet().getRange("C6:C").getValues().filter(String);
html = html.evaluate()
.setTitle('Change Job Position Name')
SpreadsheetApp.getUi().showSidebar(html);
}
function updateJobName(form) {
let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getActiveSheet();
let value1 = form.Current_Job;
let value2 = form.New_Job;
sheet.getRange("C6:C").createTextFinder(value1).replaceAllWith(value2);
}
html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link
rel="stylesheet"
href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"
/>
<style>
.container {
margin: 5px 5px 5px 5px;
}
.input-Dropdown {
width: 50%;
text-align: left;
float: left;
padding: 5px 5px 5px 5px;
}
.input-Text {
width: 50%;
text-align: left;
float: left;
padding: 5px 5px 5px 5px;
}
.custom-heading {
font-weight: bold;
}
</style>
<script>
function submitForm() {
google.script.run.updateJobName(document.getElementById("jobForm"));
}
</script>
</head>
<body>
<div class="container">
<h1 class="custom-heading"><center>Rename Job</center></h1><br><br>
<form id ="jobForm " onkeydown="return event.key != 'Enter'">
<!-- Create input fields to accept values from the user -->
Current Job Name:<br><br>
<select class="input-Dropdown" id="Current_Job"/>
<? for (let i in dropdown) { ?>
<option value="<?=dropdown[i]?>"><?=dropdown[i]?></option>
<? } ?>
</select>
<br>
<br>
<br>
New Job Name:<br><br>
<input class="input-Text" type="text" id="New_Job"/>
<br>
<br>
<br>
<button class="action" onclick="submitForm();">Save</button>
<button onclick="google.script.host.close();">Close</button>
</form>
</div>
</body>
</html>
菜单和侧栏功能正常. 作为一个视觉效果,这里是一个示例工作表的屏幕截图,其中侧边栏处于活动状态:
正如您所看到的,在活动工作表中,从C6:C成功创建了NTFS. 但是,当我点击"保存"时,虽然按钮发生了变化,表示按钮已被按下,但Electron 表格不会更新,即将原始作业(job)名称更改为更新后的作业(job)名称. 有人能指出我做错了什么吗?
这是我得到的错误:
TypeError:无法读取null的属性(读取'Current_Job') at updateJobName(工作时间:27:21)