html - 关闭包含表单的应用程序脚本侧边栏

标签 html forms google-apps-script server client

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

<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 服务接口(interface)的客户端中调用 google.script.host.close() 或在 UI 服务接口(interface)中调用 UiInstance.close() 自行关闭。侧边栏不能被其他界面关闭,只能由用户或自己关闭。

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();
};

关于html - 关闭包含表单的应用程序脚本侧边栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34179339/

相关文章:

javascript - 选择下拉选项并显示不同的图像(使用 Javascript)

html - 如何将元素对齐到中心?

python - Django 表单保存问题 AttributeError 'tuple' object has no attribute 'get'

html - Apps 脚本对话框中的 CSS

javascript - 如何使用 JavaScript 在单击鼠标时更改 td 中的 CSS

javascript - 如何使用 JSON API?

google-apps-script - 使用 Google Advanced Drive 服务在带有 Apps Script 的文件夹中创建新文件

javascript - google.script.run 执行但返回 "null"而 code.gs 函数确实有/日志值要返回

javascript - 用js改变z-index

javascript - 在操作时以 HTML 形式调用 php 但不重定向