Closing window after running a Google Script web app?

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:

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 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

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 complete")
};

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 complicated than that, and you and others can intelligently leverage the principals in this example to your advantage.