【问题标题】:Force ask authorization onOpen() (force popup)强制询问授权 onOpen()(强制弹出)
【发布时间】: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


    【解决方案1】:

    注意:简单的触发器无法访问需要授权的服务。例如,简单的触发器无法发送电子邮件,因为 Gmail 服务需要授权

    onOpen() 是简单触发器函数的保留函数名称。

    有一种方法可以查看授权状态,如果代码需要授权,再获取授权URL。但是,如果ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL) 方法需要用户授权,那么从onOpen() 运行时它将不起作用

    onOpen 方法无法打开侧边栏,因此无法从onOpen 触发器检查授权状态。用户需要先单击打开侧边栏或对话框的菜单项。

    我不确定“ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL)”代码是否能正常工作。要强制重新授权提示,您可能需要运行会导致评估范围的代码。

    代码.gs

    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";
      }
    }
    

    【讨论】:

    • 感谢您提供的信息。脚本有时会失去授权,这是一个持续存在的问题。如果脚本失去其授权,那么它将失败。虽然我遇到过授权错误导致我的代码失败的情况,但它仍然会向我发送带有UrlFetchApp.fetch(my url) 的错误消息所以,我真的不明白到底发生了什么。
    • 澄清了持续存在的问题:2018 年,Google 对简单触发器增加了限制。
    • 侧边栏不能再用简单的触发器打开了。
    【解决方案2】:

    现在并不总是可以强制要求授权 onOpen,因为 Google 进行了一些政策更改,现在阻止该脚本通过使用像 onOpen 这样的简单触发器自动打开 UI 元素。

    这对电子表格的所有者来说是可能的,但对编辑者来说却不是。

    my answer(差不多一岁,目前0票)到onOpen not executing?

    一个简单的触发器打开功能无法打开侧边栏,因为它运行在 AuthMode.LIMITED。你应该使用一个运行在 Auth.Mode.FULL 打开一个侧边栏,就像一个可安装的触发器。

    我知道这与附加组件无关,但以下引用适用

    https://developers.google.com/apps-script/add-ons/lifecycle#authorization_modes

    注意:加载项在 AuthMode.LIMITED 中执行时无法打开侧边栏或对话框。您可以使用菜单项打开侧边栏和对话框 因为它们在 AuthMode.FULL 中运行。

    规范的参考是

    【讨论】:

      猜你喜欢
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      • 2013-01-18
      • 2012-10-05
      • 1970-01-01
      • 2013-07-27
      相关资源
      最近更新 更多