Skip to content Skip to sidebar Skip to footer

Close Sidebar Then Process Form Submission

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

Solution 1:

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 is asynchronous client-side JavaScript API

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

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

Edit: 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)
  }

Reference: setTimeout()

Post a Comment for "Close Sidebar Then Process Form Submission"