[英]How to update Google Sheet Add-on sidebar dropdown list when sheets are renamed?
I am creating a Google Sheet Add-on that compares 2 columns in a spreadsheet.我正在创建一个比较电子表格中的 2 列的 Google 表格插件。 The user can choose the sheets that the 2 columns are in from a dropdown list in the sidebar.
用户可以从侧边栏中的下拉列表中选择两列所在的工作表。 When opening then sidebar, the dropdown list is populated using the following code in the sidebar html:
打开侧边栏时,使用侧边栏 html 中的以下代码填充下拉列表:
<select id="sheet1">
<? var sheets=SpreadsheetApp.getActiveSpreadsheet().getSheets(); ?>
<? for(var i=0;i<sheets.length;i++) { ?>
<option><?= sheets[i].getName()?></option>
<? } ?>
};
</select>
However, if the user renames any of the google sheets while the sidebar is open, the list does not get updated.但是,如果用户在侧边栏打开时重命名任何 google 表格,则列表不会更新。 I'm not sure how would be the best way to update the dropdown list.
我不确定如何更新下拉列表的最佳方式。 I'm open to the idea of putting a 'Update sheet names' button next to the dropdown list if that would be better than having to watch for every change to the spreadsheet to make it happen dynamically.
我愿意在下拉列表旁边放置一个“更新工作表名称”按钮的想法,如果这比必须观察电子表格的每次更改以使其动态发生更好的话。 I've tried many possible solutions today without success.
我今天尝试了许多可能的解决方案,但都没有成功。 All suggestions gratefully received.
收到所有建议。
There is no direct way that this can be done, though using an onChange()
trigger you can reload the sidebar.没有直接的方法可以做到这一点,尽管使用
onChange()
触发器可以重新加载侧边栏。
As per the documentation:根据文档:
An installable change trigger runs when a user modifies the structure of a spreadsheet itself—for example, by adding a new sheet or removing a column.
当用户修改电子表格本身的结构时(例如,通过添加新工作表或删除列),将运行可安装的更改触发器。
When a sheet is renamed, an onChange()
event is triggered with a changeType
of OTHER
in the event object.重命名工作表时,会在事件 object 中触发
onChange()
事件, changeType
为OTHER
。 When this is detected by onChange()
you can set your sidebar to reload.当
onChange()
检测到这一点时,您可以将侧边栏设置为重新加载。
function onChange(e) {
if (e.changeType == "OTHER") {
showSidebar();
}
}
function showSidebar() {
var html = HtmlService.createTemplateFromFile('Page').evaluate()
.setTitle('Sidebar title')
.setWidth(300);
SpreadsheetApp.getUi().showSidebar(html);
}
Following the Edit > Current project's triggers menu item, you will have a new page open in the G Suite Developer Hub.在 Edit > Current project's triggers 菜单项之后,您将在 G Suite Developer Hub 中打开一个新页面。 Click the
+ Add Trigger
button in the bottom right and set up the trigger settings as follows:点击右下角的
+ Add Trigger
按钮,设置触发器设置如下:
onChange
onChange
Head
Head
From spreadsheet
From spreadsheet
On change
On change
And press save.然后按保存。
NB: Any changes to the Spreadsheet's structure, except for those in the following list , will also trigger the sidebar to reload:注意:对电子表格结构的任何更改,除了以下列表中的更改,也会触发侧边栏重新加载:
EDIT
: The contents of a range is changed EDIT
:范围的内容已更改INSERT_ROW
- A row is inserted into a sheet INSERT_ROW
- 将一行插入到工作表中INSERT_COLUMN
- A column is inserted into a sheet INSERT_COLUMN
- 将一列插入工作表REMOVE_ROW
- A row is deleted from a sheet (not the same as cleared) REMOVE_ROW
- 从工作表中删除一行(与清除不同)REMOVE_COLUMN
- A column is deleted from a sheet REMOVE_COLUMN
- 从工作表中删除一列INSERT_GRID
- A new sheet is added to the Spreadsheet INSERT_GRID
- 将新工作表添加到电子表格REMOVE_GRID
- A new sheet is deleted from the Spreadsheet REMOVE_GRID
- 从电子表格中删除一个新工作表FORMAT
- The formatting of a range is edited (display formatting, background colour, font, etc) FORMAT
- 编辑范围的格式(显示格式、背景颜色、字体等) I hope this is helpful to you!我希望这对你有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.