【发布时间】:2018-02-21 08:18:22
【问题描述】:
我有一个关于在谷歌电子表格中作为绑定函数运行的一些 JavaScript 代码 - 所以是服务器端 - 和作为客户端的对话框(恰好是模态,但无模态是相同的)之间的控制流的问题。
虽然下面的代码示例工作正常,因为对话框按照下面的行成功调用了服务器端函数,并且 withSuccessHandler 也可以工作。
google.script.run.withSuccessHandler(success_callback).getCredentials(this.parentNode)
但我真正想要实现的是,一旦对话结束,一些服务器端代码继续执行;理想情况下,从调用 .showModalDialog() 函数的那一刻起,我很乐意将控制权交还给任何服务器端函数。
下面是一些示例软件;不要忘记这是有效的,只是不是我想要的!本质上,由 OnOpen() 函数创建的菜单项的事件处理程序调用模式对话框来提示用户输入安全凭证。
var html = HtmlService.createHtmlOutputFromFile('authorization_dialog');
SpreadsheetApp.getUi()
.showModalDialog(html, 'Authorization Dialog');
HTML 文件:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form>
Authorization Code:
<input type="text" name="authorization_code"><br><br>
Account ID:
<input type="text" name="account_id"><br><br>
Enter account details...
<br>
<br><br>
<input type="button" value="OK"
onclick="google.script.run.withSuccessHandler(success_callback).getCredentials(this.parentNode)" />
<input type="button" value="Close"
onclick="google.script.host.close()" />
</form>
<script>
// Using this call back prevents the need to hit the Close Button after OK.
function success_callback() {
google.script.host.close(); // Close the dialog.
}
</script>
</body>
</html>
【问题讨论】: