Opening Dialog Box during Calculation and Retrieving Calculated Result using Google Apps Script

Gists

  1. When it starts a calculation, open a dialog box.
  2. When the calculation is finished, close the dialog and retrieve the calculated result.

This is a sample script for achieving above flow. This sample script supposes to use the container-bound script of Spreadsheet. When you use this, please run the function of run().

Sample script:

function doSomething(e) {

  // Scripts for calculating.

  Utilities.sleep(3000); // This is a sample wait time.
  var data = "data";
  main({result: data});
}

function openDialogue() {
  var html = "<script>google.script.run.withSuccessHandler(function() {google.script.host.close()}).doSomething();</script>";
  var h = HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModalDialog(h, "Sample");
}

function main(e) {
  if ("result" in e) return e.result;
  openDialogue(e);
}

// Please run this function.
function run() {
  var res = main({});
    Logger.log(res);
}

Note:

  • This can be also used for the sidebar and other Google Docs.

 Share!