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

ⅰ亾dé卋堺 提交于 2019-12-21 20:22:19

问题


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.


回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/26092149/capture-onclose-event-for-spreadsheet-app-modal-dialog-in-google-apps-script

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