强制询问授权onOpen()(强制弹出) [英] Force ask authorization onOpen() (force popup)
问题描述
摘要:是否可以在onOpen()上请求授权?
详细版本: 我有一个带有按钮的电子表格,该表格已分发给很多人. 当按下任何按钮时,会调用一些需要权限的功能,因此Google Apps脚本会显示此弹出窗口:
DETAILED VERSION: I have a spreadsheet with buttons that gets distributed to a lot of people. When any button is pressed, some functions that require permissions are called, so Google Apps Script shows this popup:
此操作被接受后,一切运行良好,因为它现在已获得授权.但是,当打开工作簿时,我想运行在按下按钮之前需要权限的内容.但是,如果将需要授权的代码放入onEdit或onOpen函数中,则默认情况下,该代码将不具有任何特权运行,并且会中途崩溃,而不是显示弹出窗口并请求权限.
AFTER this is accepted, everything runs well, since it has authorization now. However, I want to run things that require permissions BEFORE a button is pushed, when the Workbook is opened. However, if you place authorization-requiring code into an onEdit or onOpen function, it runs with no privileges by default and crashes halfway, rather than showing the popup and asking for permissions.
以下是一些可以说明这一点的代码-崩溃而不是请求创建触发器的权限(也适用于CalendarApp等):
Here is some code that exemplifies this - crashing instead of asking for permissions to create a trigger (also works for CalendarApp, etc.):
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");
}
推荐答案
注意:简单触发器无法访问需要授权的服务.例如,简单触发器无法发送电子邮件,因为Gmail服务需要授权
Note: A simple trigger cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization
onOpen()
是作为简单触发器的函数的保留函数名称.
onOpen()
is a reserved function name for a function that is a simple trigger.
有一种方法可以检查授权状态,如果需要对代码进行授权,则可以获取授权URL.但是,如果ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL)
方法需要用户授权,则从onOpen()
There is a way to check the authorization status, and then get the authorization URL if the code needs to be authorized. However, if the ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL)
method needs authorization by the user, then it won't work when run from onOpen()
onOpen
方法无法打开侧边栏,因此无法从onOpen
触发器检查授权状态.用户需要先单击打开侧边栏的菜单项或对话框.
The onOpen
method can not open a sidebar, so the authorization status can't be checked from the onOpen
trigger. The user would need to click a menu item that opened the sidebar or a dialog box first.
我不确定代码'ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL)`是否能正常工作.要强制重新授权提示,您可能需要运行将导致评估范围的代码.
I'm not sure that the code 'ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL)` works that well. To force a re-authorization prompt, you may need to run code that would cause the scopes to be evaluated.
function onOpen() {
//Create custom menu or add-on menu
}
function showSidebar() {
var html = HtmlService.createTemplateFromFile('HTML_Sidebar').evaluate()
.setTitle('Log Tools')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
HTML_Sidebar
<!DOCTYPE html>
<html>
<head>
<base target="_blank">
</head>
<body>
<div><?!= getAuthUrl(); ?></div>
</body>
</html>
GS_Sidebar
function getAuthUrl() {
var authInfo,msg;
authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL);
msg = 'This spreadsheet needs permission to use Apps Script Services. Click ' +
'this url to authorize: <br><br>' +
'<a href="' + authInfo.getAuthorizationUrl() +
'">Link to Authorization Dialog</a>' +
'<br><br> This spreadsheet needs to either ' +
'be authorized or re-authorized.';
//return msg;//Use this for testing
//ScriptApp.AuthMode.FULL is the auth mode to check for since no other authorization mode requires
//that users grant authorization
if (authInfo.getAuthorizationStatus() === ScriptApp.AuthorizationStatus.REQUIRED) {
return msg;
} else {
return "No Authorization needed";
}
}
这篇关于强制询问授权onOpen()(强制弹出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!