You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Apps Script处理完成后自动关闭对话框?

问题分析与解决方案

你遗漏了异步调用的成功回调处理。google.script.run是异步执行服务器端函数的,直接调用startDashboardProcessing()后,脚本不会等待函数完成就继续执行,因此需要绑定成功回调函数,在服务器端处理完成后触发对话框关闭操作。

修正后的模态对话框HTML代码:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h4>Processing your request...</h4>
    <script>
      // 绑定成功回调,处理完成后关闭对话框
      google.script.run
        .withSuccessHandler(() => {
          google.script.host.close();
        })
        // 可选:添加失败回调,处理执行出错的情况
        .withFailureHandler((error) => {
          alert(`处理出错:${error.message}`);
          google.script.host.close();
        })
        .startDashboardProcessing();
    </script>
  </body>
</html>

关键说明:

  • withSuccessHandler(callback):当服务器端函数startDashboardProcessing()成功执行完毕后,会调用传入的回调函数,这里我们在回调里执行google.script.host.close()关闭对话框。
  • 可选的withFailureHandler:如果服务器端函数可能抛出错误,可以通过这个方法捕获错误,提示用户后再关闭对话框,提升用户体验。

内容的提问来源于stack exchange,提问作者doublevision

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 02:04:53