Search code examples
google-sheetsgoogle-apps-scriptweb-applications

Run multiple scripts when we open an HTML web app


How to run multiple scripts when we open an HTML web app?

Below is my example script, when user opens the HTML web apps the second (function getMail) script should run and capture the email id in defined sheet:

function doGet() {
  return HtmlService
      .createTemplateFromFile('index')
   .evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
  return html;
}

  function getMail() {
  Logger.log(Session.getActiveUser().getEmail());
  var value = Logger.getLog();
  var ss = SpreadsheetApp.openById("1IRpIzJKdc50dz99hYHoUVUBuwwhZI4NGdEKbc2QWqS8").getActiveSheet();
  ss.getRange(ss.getLastRow()+1,1).setValue(value);
}

function getData() {
  return SpreadsheetApp
      .openById('1P0Z_njzrwjiPHrynDW37OTjwaPPFvntpiyBYSNBXt40')
      .getDataRange()
      .getValues();
}

Solution

  • You can call Google Apps Script function getMail from index.html template using next syntax:

    <? getMail() ?>
    

    Detailed docs: https://developers.google.com/apps-script/guides/html/templates#calling_apps_script_functions_from_a_template