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 plete");
};
Thanks for help
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 plete");
};
Thanks for help
Share Improve this question edited Jul 27, 2024 at 21:02 Wicket 38.3k9 gold badges77 silver badges192 bronze badges asked Jan 27, 2015 at 1:18 Sandeep SinghSandeep Singh 8972 gold badges8 silver badges20 bronze badges4 Answers
Reset to default 6A 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:
google.script.host.close()
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.
<script>
window.top.close();
</script>
If you want a delay:
<script>
setTimeout(function(){ window.top.close(); }, 3000);
</script>
You can try using window.onload
.
<script>
window.onload=function(){
console.log("This onload did run");
setTimeout(function(){ window.top.close(); }, 3000);
};
</script>
If you don't care whether the user sees anything or not, you could just run Apps Script Content Service.
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
<!DOCTYPE html>
<html>
<script>
window.top.close();
</script>
</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 plete", 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 ma-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
function doGet(e){
// code make changes to spreadsheet
var ss = SpreadsheetApp.openById([The key for the spreadsheet you wish you modify])
var sheet = ss.getActiveSheet()
var lastRow = sheet.getLastRow()
var params = JSON.stringify(e);
var paramsArray = JSON.parse(params)
sheet.getRange(lastRow + 1, 1).setValue('Code made changes to spreadsheet at ' + Date())
sheet.getRange(lastRow + 1, 2).setValue(paramsArray.parameter.change)
// here I want browser window to close
var uniqueid = "Someuniqueid"
return HtmlService.createHtmlOutputFromFile('Close Window'), HtmlService.createHtmlOutput(uniqueid + " is marked plete")
};
function mailIt() {
var emailAddress = Browser.inputBox('What email address do you want to send the WebApp to?')
var html = HtmlService.createTemplateFromFile('HTML Email to run Web App')
var htmlCode = html.getRawContent()
Logger.log(htmlCode)
MailApp.sendEmail({
name: "Publish WebApp for survey embed Test",
to: emailAddress,
subject: "Publish WebApp for survey embed Test",
htmlBody: htmlCode
})
}
HTML Email to run Web App
<!DOCTYPE html>
<html>
<form action=[Your current Web App URL in quotes]>
Send text to spreadsheet: <input type="text" name="change"><br>
<input type="submit" value="Submit">
</form>
</html>
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 plicated than that, and you and others can intelligently leverage the principals in this example to your advantage.
Use the following at the end of the script portion of the HTML file to close a currently open HTML window in Google Sheets:
google.script.host.close();
Instead of using two HtmlServices separated with a 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:
<input type="submit" value="Submit">
With:
<input type="submit"
onclick="this.value='Magic ...';
google.script.run.withSuccessHandler(closeWindow); ">
and add somewhere in your html:
<script>
function closeWindow() { window.top.close(); }
</script>
I hope this will be of some use to someone somewhere someday :) Cheers :)