Google apps script 应用程序脚本-如何检测对话框已关闭

Google apps script 应用程序脚本-如何检测对话框已关闭,google-apps-script,google-apps-script-addon,Google Apps Script,Google Apps Script Addon,我需要检测应用程序脚本中的对话框何时关闭(当用户单击对话框右上角的X按钮时) 我试过这个方法,但不起作用 window.addEventListener('beforeunload', function (e) { e.preventDefault(); google.script.host.close() }); 那怎么做呢?谢谢。请参考中提供的任何示例代码片段 下面是一个示例,如果您在警报框中使用Yes\u No- function onOpen() { Spreads

我需要检测应用程序脚本中的对话框何时关闭(当用户单击对话框右上角的X按钮时)

我试过这个方法,但不起作用

window.addEventListener('beforeunload', function (e) {
    e.preventDefault();
    google.script.host.close()
});

那怎么做呢?谢谢。

请参考中提供的任何示例代码片段

下面是一个示例,如果您在警报框中使用
Yes\u No
-

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show alert', 'showAlert')
      .addToUi();
}

function showAlert() {
  var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.alert(
     'Please confirm',
     'Are you sure you want to continue?',
      ui.ButtonSet.YES_NO);

  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
    ui.alert('YES - Confirmation received.');
  }  else if (result == ui.Button.NO) {
    // User clicked "No".
    ui.alert('NO - Confirmation received.');
  } else if (result == ui.Button.CLOSE) {
    // User clicked X in the title bar.
    ui.alert('You closed the prompt.');
  }
}
希望这有帮助。

  • 你的问题是关于关闭一家公司
  • 这种对话框在脚本的html部分定义并在客户端运行
  • 这意味着要检测这样一个对话框的关闭,您需要实现一个客户端事件监听器,即Javascript方法
  • 一种可能是在活动中使用
样本: 代码.gs

index.html


你好,世界!
addEventListener(“unload”,function(){console.log(“它工作”);});
注意:示例中的事件侦听器将注意到用户 关闭对话框,但它无法检测 按
X
并按
CLOSE
按钮关闭


您指的是HTML web应用程序还是工作表/文档的内部?请尝试
返回false您在哪个浏览器上试用过?谢谢,但这与我的问题无关。在我的问题中,我指出“当用户单击对话框右上角的X按钮时”的情况“当用户单击对话框右上角的X按钮时”由
if(result==ui.button.CLOSE)
负责。如果这不是您想要的对话/操作类型-请指定。谢谢您的编辑,@ziganotschka:)我的错误。@ziganotschka:谢谢您提供的信息。在我的例子中,该对话框是一个自定义对话框。我找不到任何方法来获取result对象以检查result==ui.Button.CLOSE。自定义对话框的解决方案是什么?
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');
}
<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>