关闭包含表单的 App 脚本侧边栏

Closing an App Script sidebar containing a form

我在边栏中有一个 Google Apps 脚本 运行 一个 html 表单。 表单答案用于生成文档。我的问题是我想在程序完成后(点击提交后)关闭侧边栏。

<form id="myForm">
    <input name="name" type="text"/>
    <input type="button" value="Submit" onclick="google.script.run.withFailureHandler(fail).
                                        withSuccessHandler(google.script.host.close()).
                                        doStuff(this.parentNode)"/>

</form>

如果我删除 withSuccessHandler,程序会按预期运行,否则不会。有没有办法让我在 doStuff() 结束时关闭侧边栏?

Documentation:侧边栏可以通过在 HTML-服务接口的客户端调用 google.script.host.close() 或在UI-服务接口。侧边栏不能被其他界面关闭,只能由用户自己关闭。

UiInstance 已标记为已弃用。

onclick()属性可以有多个功能。您可以将 google.script.host.close() 语句放在所有代码之后。只需确保在所有 google.script.run 内容的末尾放置一个分号:

<input type="button" value="Submit" 
  onclick="google.script.run.withFailureHandler(fail)
  .doStuff(this.parentNode); google.script.host.close();"/>

您将大量代码打包到 onclick() 属性中。你可以有一个单独的功能:

onclick="serverCall();"

window.serverCall = function() {
  google.script.run
    .withFailureHandler(fail)
    .doStuff(this.parentNode);

  google.script.host.close();
};