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

你。 提交于 2019-12-04 12:51:37

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!