javascriptgoogle-sheetsgoogle-apps-scriptweb-applications

Closing window after running a Google Script web app?


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


Solution

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