自动关闭模式对话框-完成服务器代码后,在Google Spreadsheet中关闭对话框

尼克·伯克

我有一个Google Sheet,它运行一些Apps Script服务器代码以连接到SQL Server。我想在刷新数据时在模式对话框中显示消息“正在加载...”。我可以弹出模态,但是我想在代码完成后立即自动关闭对话框。

我设置的示例是:

function testpop () {
  var htmlOutput = HtmlService
    .createHtmlOutput('<p> This box will close when the data has finished loading.</p>')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .setWidth(250)
    .setHeight(200);
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Loading...');
  sleep(1000);
//close the dialog
}

我知道这可以在客户端调用,但需要在GS中进行处理,以便在代码完成后触发。

艾伦·威尔斯

事件流可能是:

  • 用户做某事
  • 触发模式对话框
  • onLoad 模态对话框事件触发客户端代码
  • 客户端google.script.run触发服务器端.gs功能运行
  • .gs脚本文件中的服务器功能运行。
  • 服务器已更新数据库。
  • 服务器代码将返回值发送回对话框
  • 对话框中的“ withSuccessHandler()”检测到服务器的返回
  • 运行“ withSuccessHandler()”并关闭对话框 google.script.host.close();

您将<script>在模态对话框中需要一个标签。

<script>
  window.onload = function() {    
    //console.log('window.onload ran!');

    google.script.run
      .withSuccessHandler(closeDialog)
      .theFunctionNameToUpdateDatabase()
  };

  window.closeDialog = function() {
    google.script.host.close();
  };
</script>

现在您正在使用:

HtmlService.createHtmlOutput(the HTML here)

您可以改用文件创建HTML:

HtmlService.createHtmlOutputFromFile(filename)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章