2016-08-03 65 views
1

我有一个程序必须完全在App脚本中运行(使用它的用户没有安装权限计算机的插件)。该计划根据报价中的信息制作商业报价和其他一些内容。showModalDialog()即使if()门没有正在评估为真也运行

我需要复制引号以保存它们,然后授权代码在这些副本上运行,以便如果需要进行更改,它们可以是。

function onOpen(e) { 
    var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL); 
    var authStatus = authInfo.getAuthorizationStatus(); 
    var authRequired = (authStatus == ScriptApp.AuthorizationStatus.REQUIRED); 

    Logger.log(authStatus); 
    Logger.log(authRequired); 

    if (authRequired) { 
    Logger.log('Authorization required'); 
    var authScript = '<script type="text/javascript">'+ 
         'function openAuthWindow() {'+ 
         'window.open("'+authInfo.getAuthorizationUrl()+'");'+ 
         '}'+ 
        '</script>'; 
    var alert = HtmlService.createHtmlOutputFromFile('sidebarHeadOpen') 
     .append(HtmlService.createHtmlOutput(authScript).getContent()) 
     .append(HtmlService.createHtmlOutputFromFile('authorizeAlert').getContent()); 
    SpreadsheetApp.getUi().showModalDialog(alert, 'Authorization Required'); 
    } else { 
    var frontend = e.source; 
    initialize(frontend); 
    } 
} 

我的问题是内部的代号为“如果(authRequired){...}”仍在运行,即使它不应该。 (即当authStatus为NOT_REQUIRED且authRequired为false时)更重要的是,尽管日志应该在运行代码时显示“需要授权”,但它不会。从该语句运行的唯一行是“var authScript = ...;”行,“var alert = ...;”行和“SpreadsheetApp.getUi()。showModalDialog();”线。我试图让不是发生。有没有办法做到这一点?

以供参考,这是alert.html样子:

<html> 
    <head> 
    <base target="_top"> 
    <script type="text/javascript"> 
     function openAuthWindow() { 
     window.open("'+authInfo.getAuthorizationUrl()+'"); 
     } 
    </script> 
    </head> 
    <body> 
    Please authorize this copy of the form to run.<br> 
    <br> 
    Without authorization, it will not do anything automatically.<br> 
    <br> 
    <input type="button" value="Review Permissions" onclick="google.script.host.close(); openAuthWindow(); google.script.run.runInstall();" /> 
    </body> 
</html> 

(此外,有没有一个简单的提示进行授权方式甚至一个刚刚编程方式获取它,而不提示用户? )

+0

这是一个网络应用程序? 'onOpen()'如何运行?从电子表格? –

回答

0

我发现这个相关issue 677 - Allow end users to provide OAuth credentials发表在谷歌应用程序脚本问题和基于该线程有变通办法,你也可以尝试和一个发布在GitHub - OAuthService for Google Apps Script中,其中包含oAuth的开发人员流入库中,您也可以添加到你自己的脚本。

本SO帖子中给出的解决方案 - Google API Authorization when 'Executing the app as me.'关于谷歌应用程序脚本中的授权可能也有帮助。

相关问题