Auto close modal dialog - After server code is done, close dialog in Google Spreadsheet

情到浓时终转凉″ 提交于 2019-12-05 03:50:04

The flow of events could be:

  • User does something
  • Triggers modal dialog
  • onLoad event of modal dialog triggers client side code
  • Client side google.script.run triggers a server side .gs function to run
  • Server function in .gs script file runs.
  • database updated from server.
  • server code sends a return value back to dialog
  • "withSuccessHandler()" in dialog detects the return from the server
  • "withSuccessHandler()" runs and closes the dialog using google.script.host.close();

You'll need a <script> tag in your modal dialog.

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

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

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

Right now you are using:

HtmlService.createHtmlOutput(the HTML here)

You could create the HTML from a file instead:

HtmlService.createHtmlOutputFromFile(filename)

//show dialog

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

some code

//close dialog

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

*optional

  ui.alert("Saved Successfully!")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!