Search code examples
javascriptarraysgoogle-apps-scriptweb-applicationsmessage

How To Create A Message Box From Google App Script To Web App


I'm trying to create a message box from my .gs to my web app, I tried these but the message box couldn't show up:

var html = HtmlService.createHtmlOutputFromFile('View');
SpreadsheetApp.getUi().showModalDialog(html, 'Dialog title');

Browser.msgBox('Hello!', Browser.Buttons.OK);

HtmlService.createHtmlOutput("Hello!");

Are those methods the right way to do it? Your response will be appreciated :)


Solution

  • The methods you are trying to use are designed for use in the context of a Google Spreadsheet

    • You cannot use them within a WebApp, even if your WebApp is bound to a spreadsheet.
    • It would make little sense anyway, since when you deploy a WebApp, you interact with the WebApp interface, not with the spreadsheet UI.
    • Instead you need to use Javascript methods that are accessible from the clientside of your WebApp, see here.

    Sample:

    Code.js

    function doGet() {
      return HtmlService.createHtmlOutputFromFile("index");
    }
    
    
    function called(){
      
      Logger.log("I was called");
      //the rest of your code
    }
    

    index.html

    <!DOCTYPE html>
    <html>
      <head>
        <base target="_top">
      </head>
      <body>
        <script>
        if (confirm("Please confirm, are you sure you want to continue?")) {
         alert( "google.script.run will be called");
         google.script.run.called();
       } else {
        alert( "You cancelled!");
       } 
        </script>
      </body>
    </html>