How can you interact with a modal from apps script after it has already run?

20 Views Asked by At

Here's the scenario: I want to create a templated modal in Google Sheets that displays information about a script that's running in the background. As the script runs, I want it to update the modal. Sort of like a console window.

The html is simple.

test.html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      function update_info (info) {
        document.getElementById("information").innerText = info;
      }
    </script>
  </head>
  <body>
    <div id = "information"></div>
  </body>
</html>

I create this with a function that is called during the execution of the main script:

function show_popup () {
    let htmlTemplate = HtmlService.createTemplateFromFile('test.html');

    let html = htmlTemplate
      .evaluate()
      .setWidth(500)
      .setHeight(100);
    
    SpreadsheetApp.getUi()
      .showModalDialog(html, ' ');
}

The script continues running in the background. As it reaches certain conditions, I want to update the modal dialog.

You can do the reverse with google.script.run from a script tag in the html. But I want something like html.script.run or html.document.getElementById("information").

I'm aware that you can pass a variable to a templated html object, but I need to do this dynamically.

Currently as a workaround I have a hidden sheet storing the information, and the popup checks the sheet every 10 seconds for an update using google.script.run. BUT I hate this solution.

Is there a better way?

0

There are 0 best solutions below