简体   繁体   English

无法使用 changeType INSERT_GRID 触发器获取名称()新添加的谷歌表

[英]Cannot getname() newly added google sheet using changeType INSERT_GRID trigger

Looking to get the name of the newly added sheet (manually added) using an installable trigger.希望使用可安装的触发器获取新添加的工作表(手动添加)的名称。 ChangeType with INSERT_GRID worked fine for me (tested by outputting some random value when sheet gets added) but when I try getting the name of the newly added sheet, it instead gives me the value of the FIRST sheet's name in the document.带有 INSERT_GRID 的 ChangeType 对我来说工作得很好(通过在添加工作表时输出一些随机值进行测试)但是当我尝试获取新添加的工作表的名称时,它反而给了我文档中第一个工作表名称的值。

Any alternatives?有什么选择吗? Have heard that this might be a previous bug in App Script.听说这可能是 App Script 中的先前错误。

function log(e)
{
if (e.changeType === "INSERT_GRID") {  
     var news = e.source.getActiveSheet().getName(); //fails to get correct value
     e.source.getSheetByName("Client").getRange(1, 1).setValue(news);
}}

I believe your goal as follows.我相信你的目标如下。

  • You want to detect the sheet name of added sheet using the OnChange event trigger.您想使用 OnChange 事件触发器检测添加工作表的工作表名称。
  • In your current issue, you cannot retrieve the sheet name of added sheet.在您当前的问题中,您无法检索已添加工作表的工作表名称。 When this is the bug or specification, you want to know about the alternatives for achieving your goal.当这是错误或规范时,您想了解实现目标的替代方案。

For this, how about this answer?为此,这个答案怎么样?

Issue and workaround:问题和解决方法:

Unfortunately, in the current stage, it seems that the event object of OnChange event trigger has not information about the added sheet.不幸的是,在当前阶段,OnChange事件触发器的事件object似乎没有关于添加工作表的信息。 By this, when your script is used, the sheet name of the 1st tab is retrieved.这样,当使用您的脚本时,将检索第一个选项卡的工作表名称。 I'm not sure whether this is the bug or the current specification.我不确定这是错误还是当前规范。

So in order to achieve your goal, I would like to propose the following workaround.因此,为了实现您的目标,我想提出以下解决方法。 The flow of this workaround is as follows.此解决方法的流程如下。

  1. At first, the initial sheet names are saved to PropertiesService.首先,初始工作表名称保存到 PropertiesService。
  2. When the sheet is added, the aded sheet is retrieved by comparing the current sheets and initial sheets retrieved from the PropertiesService.添加工作表时,通过比较当前工作表和从 PropertiesService 检索到的初始工作表来检索添加的工作表。

Sample script:示例脚本:

In order to use this script, please close the Google Spreadsheet and open it again.要使用此脚本,请关闭 Google 电子表格并再次打开。 By this, onOpen is run and the initial sheet names are saved to the PropertiesService.这样, onOpen将运行并将初始工作表名称保存到 PropertiesService。 Then, when the sheet is added, the added sheet is retrieved and the sheet name of the added sheet is put to the cell.然后,当添加工作表时,检索添加的工作表并将添加的工作表的工作表名称放入单元格中。

function saveCurrentSheets(prop, spreadsheet) {
  const sheets = spreadsheet.getSheets().reduce((o, s) => Object.assign(o, {[s.getSheetName()]: true}), {});
  prop.setProperty("sheets", JSON.stringify(sheets));
}

function onOpen(e) {
  saveCurrentSheets(PropertiesService.getScriptProperties(), e.source);
}

function log(e) {
  if (e.changeType === "INSERT_GRID") {
    const prop = PropertiesService.getScriptProperties();
    const oldSheets = prop.getProperty("sheets");
    if (!oldSheets) {
      saveCurrentSheets(prop, e.source);
      return;
    }
    const oldSheetsObj = JSON.parse(prop.getProperty("sheets"));
    const addedSheet = e.source.getSheets().filter(s => !oldSheetsObj[s.getSheetName()])[0];
    const news = addedSheet.getSheetName();
    e.source.getSheetByName("Client").getRange(1, 1).setValue(news);
    saveCurrentSheets(prop, e.source);
  } else if (e.changeType === "REMOVE_GRID") {
    saveCurrentSheets(PropertiesService.getScriptProperties(), e.source);
  }
}
  • In this script, please install the OnChange event trigger to the function of log .在此脚本中,请将 OnChange 事件触发器安装到log的 function 中。
  • In this script, when the existing sheet is deleted, the values of PropertiesService are updated by the current sheet names.在此脚本中,当删除现有工作表时,PropertiesService 的值将由当前工作表名称更新。

Note:笔记:

  • When there are a lot of sheets in the Google Spreadsheet, I think that the initial sheet names are required to be a file instead of the PropertiesService, because of "Properties value size is 9kB / val."当 Google 电子表格中有很多工作表时,我认为初始工作表名称必须是文件而不是 PropertiesService,因为“Properties value size is 9kB / val”。 and "Properties total storage is 500kB / property store.".和“属性总存储空间为 500kB / 属性存储。”。 Please be careful this.请注意这一点。

References:参考:

That's the normal behavior.这是正常的行为。 It always returns e.source.getSheets()[0];它总是返回 e.source.getSheets()[0]; Which is the same thing if you get the active sheet when you openById();如果您在 openById(); 时获得活动工作表,这也是同样的事情;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM