Search code examples
google-sheetsgoogle-apps-scriptweb-applicationsdialog

Use google apps script to close a dialogue box launched via SpreadsheetApp.getUi().showModalDialog(html, 'Alert Dialog');


Using GAS, I would like to launch a message box, run code, and then close the message box on the other side with code. I understand that this cannot be done with ui.alerts but believe it can be done via html. Here's my attempt. The box opens, of course, but I can't find any way to close it.

function testing() {

  var html = HtmlService.createHtmlOutput('<div><h2>Message</h2><p>This is an alert message.</p></div>');
  var dialog = SpreadsheetApp.getUi().showModalDialog(html, 'Alert Dialog');

  Logger.log('Intervening code etc.')

  dialog.close();

};

That last command (dialog.close()) does not work.

Thanks for the assistance.


Solution

  • Closing a Dialog

    Using google.script.host.close() and a short delay through onSuccessHandler

    It's in the javascript of your html

    function testing() {
      var html = HtmlService.createHtmlOutput('<div><h2>Message</h2><p>This is an alert message.</p></div><script>window.onload = () => { google.script.run.withSuccessHandler(() => {google.script.host.close()}).dela()}</script>');
      var dialog = SpreadsheetApp.getUi().showModalDialog(html, 'Alert Dialog');
      Logger.log('Intervening code etc.');
    }
    
    function dela() {
      //Utilities.sleep(2000);//optional for longer delays
      return;
    }
    

    Note: google.script.host.close() does not work on web apps