How to call multiple sheets with the same script? (Apps Script)

96 views
Skip to first unread message

Alex M

unread,
Mar 7, 2023, 10:24:26 AM3/7/23
to Google Apps Script Community
Hello everyone I have an app script that sends out emails. I have two sheets one is called A and the other B. I can only get one or the other to work. I want to be able to run the code on Sheet A or Sheet B (independently of each other). Any help would be appreciated!

var ss_id = "xxxxxxx";//Update with your own sheet.
var sheetName = "A"//Change the sheet name
var sheetName = "B"//Change the sheet name
var dueDateCell = "C2"//The cell that indicates the due date.
var startRow = 6//First row after your header.
var reminderColStart = 6//What row your reminder starts on.

//Email data
var subject = "URGENT Attendance Reminder "//The email subject

/******************************************************************************************
 * The main run function. It loops thorugh each user and calls the following functions:
 *   - sendEmail()
 *   - openCompleteWindow()
 */

function main() {
  var ss = SpreadsheetApp.openById(ss_id);
  var sheet = ss.getSheetByName(sheetName);
  
  var lastRow = sheet.getLastRow();
  var lastCol = sheet.getLastColumn();
  
  var dueDate = sheet.getRange(dueDateCell)
                     .getValue();
  dueDate = Utilities.formatDate(dueDate"GMT+7""EEE, dd MMM yyyy");
  
  var range = sheet.getRange(startRow,1,lastRow - startRow+1lastCol); 
  var rangeVals = range.getValues();
  
  var badEmailList = [];
  var reminder = [];
  
  //Loop through range values. If tick box is not ticked continue.
  for(row in rangeVals){
    
    if(rangeVals[row][2]){
      
      var badEmail = sendEmail(rangeVals[row], ss.getUrldueDate);
      
      
      //Update Redminder Sent column F(5) and Last Reminder Date column G(6);
      if(!badEmail){
        rangeVals[row][5] += 1;
        rangeVals[row][6] = new Date();
      }else{
        badEmailList.push(badEmail);
      };
         
      
    };
    reminder.push([rangeVals[row][5],rangeVals[row][6]])
  }; 
  
  var reminderRange = sheet.getRange(startRowreminderColStart,lastRow - startRow+12);
  reminderRange.setValues(reminder);
  
  var complete = openCompleteWindow(badEmailList);
};

/******************************************************************************************
 * Google trigger function. When the sheet is opened, a custom menu is produced.
 * 
 */
 
function onOpen(){

  var ui = SpreadsheetApp.getUi();
  ui.createMenu("Send Reminder")
  .addItem("Send Reminder""main")
  .addToUi();

};

/******************************************************************************************
 * Sends HTML email to user who has not completed task. 
 * The HTML template is created from the email.html file. 
 *
 * @param {array} overDueStaff - 2d array of overdue staff with full row data.
 * @param {string} sheetURL - url of current Spreadsheet.
 * @param {date} dateDue - formatted date.
 *
 * @return {array} - Returns array of email addresses that could not be attemplted due to errors.
 */
function sendEmail(overDueStaffsheetURLdateDue){
  
  //Staff Details
  var staff = {
    "name"overDueStaff[0],
    "email"overDueStaff[1],
    "time"overDueStaff[8],
    "ZoomLink"overDueStaff[9],
    "clientName"overDueStaff[10],
    "dueDate"dateDue,
    "daysOverdue"overDueStaff[4]
  };
  staff.daysOverdue = (staff.daysOverdue > 1) ? staff.daysOverdue + " days" : staff.daysOverdue + " day";

  
  var subject1 = staff.name;
  
  var subj = subject.concat(subject1);
  
  
  var body = HtmlService.createTemplateFromFile("email");
  body.name = staff.name;
  body.due = staff.dueDate;
  body.overdue = staff.daysOverdue;
  body.time = staff.time;
  body.ZoomLink = staff.ZoomLink;
  body.clientName = staff.clientName;
  body.Tasktype = staff.Tasktype;
  
  
  try{
      MailApp.sendEmail({
      tostaff.email,
      subjectsubj,
      htmlBodybody.evaluate().getContent(),
      });
  }
  catch(error){
    return staff.email;
  }
};

/******************************************************************************************
 * Opens a completion alert once the script is run with a list of emails that could not be sent.
 *
 * @param {string} badEmailList - array of email addresses that could not be attemplted due to errors.
 *
 */
function openCompleteWindow(badEmailList){

  SpreadsheetApp.getUi().alert(
    "Emails Sent\n"+
    "The following emails could not be sent:\n"+
    badEmailList.join("\n")

  );
};
Reply all
Reply to author
Forward
0 new messages