Google Apps Script – How to Log Use of Close Button on Modal Dialogue

google sheetsgoogle-apps-script

I'm working on a spreadsheet which opens a modal dialogue when a script begins to execute. Inside it plays a google-style "working" animation made with CSS:

https://i.imgur.com/kQ8muyi.gif

If I could remove the close button so that the user has to sit at this dialogue box until the script finishes, I would. I don't want the user to be able to edit the spreadsheet while the script runs, but my understanding is that I can't remove it.

So, is it instead possible to register when the user clicks the close button so that I can cancel execution of the script?


This is the code that generates the Modal Dialogue:

Code.gs

function createpage()
{

var output=HtmlService.createTemplateFromFile('Page').evaluate();
SpreadsheetApp.getUi().showModalDialog(output, 'Generating... please wait');

}

function getContent(filename) {
    return HtmlService.createTemplateFromFile(filename).getRawContent();
}

Page.html

<html>
  <head>
    <base target="_top">
    <?!= getContent("CSS") ?>
  </head>
  <body>

<h1>

</h1>

<p>To cancel, click the close button.</p>

<div class="google-loader">
    <span></span>
    <span></span>
    <span></span>
    <span></span>
</div>

  </body>
</html>

There is also a CSS.HTML file that is pulled into Page.HTML by <?!= getContent("CSS") ?>.

Best Answer

I don't think that it's possible to log the click of the built-in close button in a Google Apps Script custom dialog.

The alternative is to add a custom button in your custom dialog that does what you are looking for before closing the dialog by using google.script.host.close().