自动关闭模式对话框 - 完成服务器代码后,关闭Google电子表格中的对话框

Nic*_*rke 9 google-sheets showmodaldialog google-apps-script

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

我设置的一个例子是:

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
}
Run Code Online (Sandbox Code Playgroud)

我知道这可以在客户端调用,但是需要在GS中处理它,以便在代码完成时触发它.

Ala*_*lls 11

事件的流程可能是:

  • 用户做了一些事
  • 触发模态对话框
  • 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>
Run Code Online (Sandbox Code Playgroud)

现在你正在使用:

HtmlService.createHtmlOutput(the HTML here)
Run Code Online (Sandbox Code Playgroud)

您可以从文件创建HTML:

HtmlService.createHtmlOutputFromFile(filename)
Run Code Online (Sandbox Code Playgroud)


pcw*_*211 9

//显示对话框

var output = HtmlService.createHtmlOutput('<b>Please wait...</b>');
  SpreadsheetApp.getUi().showModalDialog(output, 'Saving...');
Run Code Online (Sandbox Code Playgroud)

一些代码

//关闭对话框

  var output = HtmlService.createHtmlOutput('<script>google.script.host.close();</script>');
  SpreadsheetApp.getUi().showModalDialog(output, 'Loading...');
Run Code Online (Sandbox Code Playgroud)

*可选的

  ui.alert("Saved Successfully!")
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢这个。我认为这是一个更清洁的解决方案。谢谢! (2认同)
  • 这比(当前)接受的答案更容易和更强大。所有的分数都给你。 (2认同)