总结:是否可以在 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 做出了一些政策更改,现在通过使用像 onOpen
这样的简单触发器来阻止该脚本自动打开 UI 元素,因此并不总是可以强制要求授权 onOpen。 .
这对于电子表格的所有者可能是可能的,但对于编辑者来说是不可能的。
来自 my answer (快一岁了,至今0票)到onOpen not executing?
A simple trigger open function can't open a sidebar because it runs on AuthMode.LIMITED. You should use a function that runs on Auth.Mode.FULL to open a sidebar, like an installable trigger.
I know that this isn't about add-ons but the following quote applies
From https://developers.google.com/apps-script/add-ons/lifecycle#authorization_modes
Note: Add-ons can't open sidebars or dialogs while executing in AuthMode.LIMITED. You can use menu items to open sidebars and dialogs since these run in AuthMode.FULL.
规范引用是
https://developers.google.com/apps-script/guides/triggers/ https://developers.google.com/apps-script/guides/triggers/events
关于google-apps-script - 强制要求授权 onOpen()(强制弹出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44892964/