showModalDialog() 是 运行 即使它的 if() 门没有评估为真
showModalDialog() being run even though the if() gate to it isn't evaluating to true
我有一个程序必须 运行 完全在应用程序脚本中(使用它的用户在他们的计算机上没有插件的安装权限)。该程序根据报价单中的信息制作商业报价单和其他一些东西。
我需要复制引号以保存它们,然后授权代码在这些副本上 运行 以便在需要进行更改时进行更改。
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);
}
}
我的问题是 "if ( authRequired ) { ... }" 中的代码仍然是 运行,即使它不应该如此。 (即,当 authStatus 为 NOT_REQUIRED 且 authRequired 为 false 时)此外,尽管当该代码为 运行 时日志应显示 "Authorization required",但事实并非如此。该语句中仅有的 运行 行是 "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>
(还有,有没有更简单的提示授权的方法?甚至可以通过编程方式获取授权,而不提示用户?我已经找了一个星期了,还没找到。)
我发现这个相关 issue 677 - Allow end users to provide OAuth credentials posted in google-apps-script-issues and based from that thread there are workarounds which you can also try and one was posted in GitHub - OAuthService for Google Apps Script 其中开发人员将 oAuth 流程包装在一个库中,您也可以将其添加到您自己的脚本中。
此 SO post 中给出的解决方案 - Google API Authorization when 'Executing the app as me.' 关于 google 应用程序脚本中的授权也可能有帮助。
我有一个程序必须 运行 完全在应用程序脚本中(使用它的用户在他们的计算机上没有插件的安装权限)。该程序根据报价单中的信息制作商业报价单和其他一些东西。
我需要复制引号以保存它们,然后授权代码在这些副本上 运行 以便在需要进行更改时进行更改。
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);
}
}
我的问题是 "if ( authRequired ) { ... }" 中的代码仍然是 运行,即使它不应该如此。 (即,当 authStatus 为 NOT_REQUIRED 且 authRequired 为 false 时)此外,尽管当该代码为 运行 时日志应显示 "Authorization required",但事实并非如此。该语句中仅有的 运行 行是 "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>
(还有,有没有更简单的提示授权的方法?甚至可以通过编程方式获取授权,而不提示用户?我已经找了一个星期了,还没找到。)
我发现这个相关 issue 677 - Allow end users to provide OAuth credentials posted in google-apps-script-issues and based from that thread there are workarounds which you can also try and one was posted in GitHub - OAuthService for Google Apps Script 其中开发人员将 oAuth 流程包装在一个库中,您也可以将其添加到您自己的脚本中。
此 SO post 中给出的解决方案 - Google API Authorization when 'Executing the app as me.' 关于 google 应用程序脚本中的授权也可能有帮助。