How to Call Google Apps Script Function from Web App

In this post, I demonstrate how to call Google Apps Script Functions from Web App. I also show how to pass parameters in and out of functions.

How to Video:

Video Notes:

  • Legacy Apps Script Editor Used in Video
  • Apps Script (Script Editor) is now located under tab ‘Extensions’ instead of ‘Tools’ on Google Sheets
  • For Further Details on Deploying a Web App Click Here

Code in Video:

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('WebApp');
}

function getOneCellData() { 
  var ss= SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1"); 
  var data = sheet.getRange('A1').getValue(); 
  return data;  
}

function getMultiCellData() { 
  var ss= SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1"); 
  var data = sheet.getRange('A1:C1').getValues(); 
  return data;  
}

function getInOutData(inData) { 
  var outData = "IN DATA: " + inData; 
  return outData;  
}
<!DOCTYPE html>
<html>
  <head>
  <base target="_top">
  <script>
  function callOneCellData()
  {
    google.script.run.withSuccessHandler(function(returnData) 
    {
       document.getElementById("oneCellData").innerHTML = returnData;
    }).getOneCellData();
  }
  
  function callMultiCellData()
  {
    google.script.run.withSuccessHandler(function(returnArray) 
    {
        console.log(returnArray);
        var stringArray = returnArray.toString();
        document.getElementById("multiData").innerHTML = stringArray;
    }).getMultiCellData();
  }
  
  function callInOutData()
  {
    var inData = document.getElementById("inData").value
    google.script.run.withSuccessHandler(function(outData) 
    {
        document.getElementById("outData").innerHTML = outData;
    }).getInOutData(inData);
  }
  
  function callData()
  {
    callOneCellData();
    callMultiCellData();
    callInOutData();
  }  
  </script>
  </head>
  <body>
  <label>One Cell Data</label><br><textarea id="oneCellData" ></textarea><br>
  <label>Multi Data</label><br><textarea id="multiData" ></textarea><br>
  <label>In Data</label><br><input type="text" id="inData" /><br>
  <label>Out Data</label><br><textarea id="outData" ></textarea><br>
  <input type="button" onclick="callData()" value="Get Data" />
  </body>
</html>

Related Posts

Create Custom HTML Form Google Sheets Create Custom HTML Form for Google Sheets - In this post, I demonstrate the steps on creating a Custom HTML Form for Google Sheets using Google Apps Script.
Link Web App Pages How to Link Between Google Web App Pages - In this post, I demonstrate how to link Web App HTML pages together and pass parameters to the Web App HTML pages using Google Apps Script.
Create Web App Form on Google Sheets How to Create Web App Form on Google Sheets - In this post, I demonstrate how to create a simple Web App form that populates Google Sheets using Google Apps Script.
Multi Select Web App Google Sheets Create Multi-Select Dropdown in Web App on Google Sheets - In this post, I demonstrate how to use a multi-select dropdown in a Web App on Google Sheets. This will in turn store the multiple values in a cell on Google Sheets.
File Loader Google Web App Create File Loader Google Web App to Google Drive - In this post, I demonstrate how to create a File Loader Google Web App to Google Drive on Google Sheets. It works with such files as PNG, JPG, DOC, XLS, PDF, and many more.
Dependent Drop Down Web App WS Create Dependent Drop Down on Google Web App - In this post, I demonstrate how to create a dependent drop-down list in a Google Web App using Google Apps Script on Google Sheets.