I made a small web app that opens when the user clicks a link in a spreadsheet. The link will take them to web app, which makes changes to a spreadsheet. I want the browser window to close automatically once the code is finished running.
function doGet(e){
// code make changes to spreadsheet
// here I want browser window to close
return HtmlService.createHtmlOutput(uniqueid + " is marked complete");
};
Thanks for help
Use the following at the end of the script portion of the HTML file to close a currently open HTML window in Google Sheets:
I came across your question long after you asked it, but in case you or anyone else is looking for the answer, try this:
To get the browser window to close, create a simple HTML file that instructs itself to close with the instruction
window.top.close()
like this:Close Window.html
You need to return this as an HTML output so that when the Web App runs, it runs on the client side (as Sandy said in the previous answer). In your scenario, you are already returning an HTML output to inform that some "[uniqueid] is marked complete", and it is assumed that you DON'T wish to close this window after the script runs, else the user will not receive the prompt you intended. That is why you need to keep the instruction
window.top.close()
outside of the same HTML output that gives this alert. To achieve that, just comma-separate both HTML outputs as two return values.Here are the two other files that I came up with to model a solution for your use case. FYI: I packaged the user-facing HTML in an email to provide a convenient point of execution, and the code will request an email address for delivery.
Code.gs
HTML Email to run Web App
Obviously, there are some unanswered questions--like why is it a requirement to automatically close a window while your Web App appears to open only a window that carries a message for the user--but I trust your use case is more complicated than that, and you and others can intelligently leverage the principals in this example to your advantage.
A distinction needs to be made between a Web App in a browser tab, and a sidebar or dialog box inside of a Google document (Sheet, Form, Doc).
The question is about a Web App in a browser tab. If you want to close a sidebar or dialog box, then just use:
But this question is for a Web App. You can try putting a script tag in the HTML with code that runs automatically when the window is opened.
If you want a delay:
You can try using
window.onload
.If you don't care whether the user sees anything or not, you could just run Apps Script Content Service.
Instead of using two HtmlServices separated with coma in return statement of your doGet(e) function (that has not worked for me - I got only one return value)
you can put window.top.close(); in onClick event of your submit button like this:
Replace:
With:
and add somewhere in your html:
I hope this will be of some use to someone somewhere someday :) Cheers :)