【发布时间】:2019-06-18 06:06:50
【问题描述】:
总结:是否可以请求 onOpen() 授权?
详细版本: 我有一个带有按钮的电子表格,可以分发给很多人。 当按下任何按钮时,会调用一些需要权限的函数,因此 Google Apps 脚本会显示此弹出窗口:
接受后,一切运行良好,因为它现在已获得授权。但是,当工作簿打开时,我想在按下按钮之前运行需要权限的东西。但是,如果您将需要授权的代码放入 onEdit 或 onOpen 函数中,则默认情况下它在没有权限的情况下运行并中途崩溃,而不是显示弹出窗口并请求权限。
这里有一些代码可以说明这一点 - 崩溃而不是请求创建触发器的权限(也适用于 CalendarApp 等):
function onOpen(e) {
Browser.msgBox("This is shown to the user");
try {
ScriptApp.newTrigger('someFunction').forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet()).onEdit().create();
}
catch (e) {
Browser.msgBox(e);
}
Browser.msgBox("This message is never shown to the user if there is no try catch because it crashes");
}
【问题讨论】:
标签: google-apps-script permissions google-sheets authorization