简体   繁体   中英

Capture onClose event for Spreadsheet App Modal Dialog in Google Apps Script

I want to do something when the Modal Dialog (opened using showModalDialog() ) is closed from the Spreadsheet App.

However I cannot find the reference of such event, in the API documents provided by Google. I found how to capture this event in a Alert Box , and by using this piece of code I can capture how the user closed the Alert Box, but I cannot use this in a Modal Dialog or a Modeless Dialog.

Is there a way to do this? Please kindly answer if you do.

This is not possible. And you should write your script in a way that this does not matter. For example, by showing a big action button in the dialog, making it clear to the user that he must click there for the script to continue.

But if you really want to make this happen, I guess you could use an HtmlService dialog that make regular async calls to the backend and each call waits for the next one before quitting, then if the "next" call does not get in time, it can assume the dialog got closed and execute your close procedure instead of simply quitting.

Here's an alternative solution. You can use google hosted jquery in the HTML (served by GAS) to track unload events when the page is closed. Here is some sample code:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <!-- page content -->
  </body>

  <!-- Minified google hosted jquery (see https://developers.google.com/speed/libraries/)-->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  <!-- And here's where the magic happens -->
  <script type="text/javascript">
    $(document).ready( e => {
      console.log('-- DOM ready --');

      /** Add jquery unload listener */
      $(window).on('unload', e => {
        console.log("Invoked just before unload");
        // do pre-unload stuff
      });
    });
  </script>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM