2016-08-03 36 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.'關於谷歌應用程序腳本中的授權可能也有幫助。

相關問題