我需要检测 Apps 脚本中对话框何时关闭(当用户单击对话框右上角的 X 按钮时)
这个方法我试过了,还是不行
window.addEventListener('beforeunload', function (e) {
e.preventDefault();
google.script.host.close()
});
那怎么办呢?谢谢。
最佳答案
- 您的问题涉及 custom modal dialog 的关闭
- 这种对话框在脚本的 html 部分定义并在客户端运行
- 这意味着要检测此类对话框的关闭,您需要实现客户端事件监听器 - 即 Javascript 方法
- 一种可能是使用 window event handler与事件 window.unload
示例:
Code.gs
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('Custom Menu')
.addItem('Show dialog', 'showDialog')
.addToUi();
}
function showDialog() {
var html = HtmlService.createHtmlOutputFromFile('index')
.setWidth(400)
.setHeight(300);
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'My custom dialog');
}
index.html
<html>
<head>
<base target="_top">
</head>
<body>
Hello, world! <input type="button" value="Close" onclick="google.script.host.close()" />
<script>
window.addEventListener("unload", function() {console.log("It works"); });
</script>
</body>
</html>
Note: The event listener in the sample will notice when the user closes the dialog, however it cannot detect the difference between closing by pressing the
X
and pressing theCLOSE
button.
关于google-apps-script - Apps 脚本 - 如何检测对话框是否关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59485690/