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

后端 未结 2 1621
误落风尘
误落风尘 2020-12-31 12:53

I have a Google Sheet that runs some Apps Script server code to connect to an SQL server. I want to show the message \"loading...\" in the modal dialog while data is being

相关标签:
2条回答
  • 2020-12-31 13:26

    //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!")
    
    0 讨论(0)
  • 2020-12-31 13:37

    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)
    
    0 讨论(0)
提交回复
热议问题