Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active February 27, 2020 17:46
Embed
What would you like to do?
Opening Dialog Box during Calculation and Retrieving Calculated Result using Google Apps Script

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

  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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment