如何在创建新工作表或更改工作表名称或复制工作表或从google散页中删除工作表时,通过google应用脚本自动刷新当前的工作表列表
*我需要名单名称:
*:
表名列表应该显示在第二个工作表上,代码表达式为sheet1。
下面的代码运行良好。但它并不是通过添加工作表或删除工作表来刷新
function sheetnames()
{
return SpreadsheetApp.getActiveSpreadsheet().getSheets().map(function(x) {return x.getName();});
}
发布于 2020-07-09 05:20:02
我相信你的处境和目标如下。
sheetnames()
函数作为Google电子表格上的自定义函数。sheetnames()
功能正常工作。为了达到上述目的,我想提出以下方法。
用法:
1.编写脚本。
在本例中,用于刷新电子表格中sheetnames()
的自定义函数的示例脚本由OnChange事件触发器运行。为此,请将以下示例脚本复制并粘贴到电子表格的容器绑定脚本中,并保存脚本。
function onChange(e) {
var lock = LockService.getDocumentLock();
if (lock.tryLock(10000)) {
try {
const prop = PropertiesService.getScriptProperties();
if ((e.changeType === "OTHER" || e.changeType === "REMOVE_GRID" || e.changeType === "INSERT_GRID") && !prop.getProperty("run")) {
const formula = "=sheetnames"; // <--- Please set the function name of the custom function.
const ss = e.source;
const tempFormula = "=sampleFormula";
ss.createTextFinder("^\\" + formula).matchFormulaText(true).useRegularExpression(true).replaceAllWith(tempFormula);
ss.createTextFinder("^\\" + tempFormula).matchFormulaText(true).useRegularExpression(true).replaceAllWith(formula);
prop.setProperty("run", "done");
} else {
prop.deleteProperty("run");
}
} catch(e) {
throw new Error(e);
} finally {
lock.releaseLock();
}
}
}
LockService
。PropertiesService
。2.安装OnChange事件触发器。
为了执行onChange
的函数,请将OnChange事件触发器安装到函数onChange
上。您可以在这份正式文件上看到安装该程序的方法。
3.测试
为了测试上面的脚本,例如,在您将函数onChange
安装为可安装的OnChange事件触发器后,请插入新的工作表。这样,您就可以确认自定义函数sheetnames()
是刷新的。
参考文献:
https://stackoverflow.com/questions/62807234
复制相似问题