简体   繁体   English

重命名工作表时如何更新 Google Sheet Add-on 侧边栏下拉列表?

[英]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.收到所有建议。

Answer:回答:

There is no direct way that this can be done, though using an onChange() trigger you can reload the sidebar.没有直接的方法可以做到这一点,尽管使用onChange()触发器可以重新加载侧边栏。

More Information:更多信息:

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()事件, changeTypeOTHER When this is detected by onChange() you can set your sidebar to reload.onChange()检测到这一点时,您可以将侧边栏设置为重新加载。

Code:代码:

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);
}

Setting up an Installable Trigger:设置可安装触发器:

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按钮,设置触发器设置如下:

  • Choose which function to run: onChange选择要运行的 function: onChange
  • Choose which deployment should run: Head选择应该运行的部署: Head
  • Select event source: From spreadsheet Select 事件源: From spreadsheet
  • Select event type: On change Select 事件类型: 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!我希望这对你有帮助!

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在侧边栏的下拉列表中获取所有工作表的列表 - How to get the list of all sheet in a dropdown list in a sidebar 将自定义Google表格功能转化为附加组件 - Turning custom Google Sheets functions into an add-on Google表格加载项中的自定义功能 - Custom Function in Google Sheets Add-On 以管理员身份运行 Google Sheet Add-On - Run Google Sheet Add-On as Admin Google 文档插件 - setSelection() 通过侧边栏按钮设置文本 - 如何使其成为活动选择? - Google document add-on - setSelection() of text by sidebar button - how to make it an active selection? 如何根据谷歌表格中的范围自动更新谷歌表单中的下拉列表? - How to automatically update dropdown in a Google Form based on range in Google Sheets? 有什么方法可以将 google 脚本代码打包为 google sheet 的附加组件? - Is there any way to package google script code as add-on to google sheet? 我编写了一个 nodejs 应用程序来操作 Google 表格。 如何将其转换为附加组件? - I have written a nodejs application to manipulate Google sheets. How to convert this to an add-on? Google表格附加组件,未添加自定义菜单? - Google Sheets Add-On, custom menu not being added? 是否可以使用 AppScript 运行 Google 表格插件? - Is it possible to run a Google Sheets Add-on using an AppScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM