关闭边栏,然后处理表单提交 [英] Close sidebar then process form submission

查看:109
本文介绍了关闭边栏,然后处理表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Google Spreadsheet中,用户可以单击自定义菜单项,该菜单项会在边栏中显示HTML表单,以便他们可以输入数据.处理形成数据的功能大约需要25秒钟才能完成. 是否可以关闭边栏,然后处理表单数据?,在脚本完成时,用户将屏幕的一部分阻塞了半分钟真是令人讨厌.

In my Google Spreadsheet, the user can click a custom menu item that brings up an HTML form in the sidebar so they can enter data. The function that processes that form data takes ~25 seconds to complete. Is it possible to close the sidebar THEN process the form data? It's annoying for the user to have part of the screen blocked for half a minute while the script finishes.

当前我正在从<form onSubmit="handleFormSubmit(this)">调用此函数,并且按预期,只有在脚本完成后,侧边栏才会关闭:

Currently I'm calling this function from <form onSubmit="handleFormSubmit(this)"> and as expected the sidebar only closes after the script is finished:

function handleFormSubmit(formObject) {
  google.script.run.withSuccessHandler(google.script.close).processForm(formObject)
  }

修改 我尝试了@Jack_Brown的添加睡眠延迟的策略,但是不幸的是,"setTimeout"在Apps脚本中不可用.此外,我也不认为Apps Script中也没有异步"函数声明.

Edit I tried @Jack_Brown's strategy of adding a sleep delay and unfortunately "setTimeout" is not available in Apps Script. Also, I don't think the "async" function declaration is available in Apps Script either.

推荐答案

您可以致电此处

You can call google.script.host.close() right after you call google.script.run.processForm(formObject) to close your sidebar. As mentioned here,

google.script.run是异步客户端JavaScript API

google.script.run is asynchronous client-side JavaScript API

这意味着客户端的javascript在继续执行代码的下一行之前不会等待执行结束.因此,您可以在运行API之后立即调用google.script.host.close().像这样

Which, means javascript on the client side doesn't wait for the execution to be over before proceeding to the next line in the code. Hence, you can call google.script.host.close() right after the run API. Like so

function handleFormSubmit(formObject) {
  google.script.run.processForm(formObject)
  google.script.host.close()
  }

由于您过早关闭了客户端,因此不需要successHandler.
注意:但是,如果运行失败,您将不会收到任何通知!

Since you are closing the client side prematurely, you dont need a successHandler.
Note: However, you will get no notification if the run fails!

编辑:在调用运行后立即调用google.script.host.close()似乎会影响异步调用.但是,增加延迟可以使其在关闭侧边栏之前完成API调用.下面的功能在关闭侧边栏之前会休眠2秒钟.

The call to the google.script.host.close() right after the call to run, seems to affect the asynchronous call. However, adding delay allows it to complete the API call before closing the sidebar. The below function sleeps for 2 seconds before closing the sidebar.

function handleFormSubmit(formObject) {
    google.script.run.processForm(formObject)
    setTimeout(google.script.host.close, 2000)
  }

参考:
setTimeout()

Reference:
setTimeout()

这篇关于关闭边栏,然后处理表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆