[英]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.我相信你的目标如下。
For this, how about this answer?为此,这个答案怎么样?
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.
此解决方法的流程如下。
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);
}
}
log
.log
的 function 中。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.