Categories
User Help Web Apps

How to run a different script on multiple Google Sheet tabs?

The code writes an auto email once a trigger is met in the sheet.
There are 3 different tabs pulled from the main sheet and each tab should run the script but with a different message for each tab.

When I duplicate the script and change the tab variable only the last script runs.

Should each tab have its own script or do I need to combine the variables into one script?

Here’s the code:

// This constant is written in column E for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';

function sendEmails2() {
 var sheet = SpreadsheetApp.getActive().getSheetByName('CAN WE R - HS');
  var startRow = 3; // First row of data to process
  var numRows = 1000; // Number of rows to process
  var dataRange = sheet.getRange(startRow, 1, numRows, 10)
   // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[1]; // Second column
    var message1 = 'HS Co 1 / HS co 2 / HS co 3';
    var emailSent = row[4]; // Sixth column
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
      var subject1 = 'Sending emails from a Spreadsheet CAN WE R - HS';
      MailApp.sendEmail(emailAddress, subject1, message1);
      sheet.getRange(startRow + i, 5).setValue(EMAIL_SENT)
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}
var EMAIL_SENT = 'EMAIL_SENT';

function sendEmails2() {
 var sheet = SpreadsheetApp.getActive().getSheetByName('CAN WE R - DRAWING');
  var startRow = 3; // First row of data to process
  var numRows = 1000; // Number of rows to process
  var dataRange = sheet.getRange(startRow, 1, numRows, 10)
   // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[1]; // Second column
    var message1 = 'drawing Co 1 / drawing co 2';
    var emailSent = row[4]; // Sixth column
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
      var subject1 = 'Sending emails from a Spreadsheet CAN WE R - drawing';
      MailApp.sendEmail(emailAddress, subject1, message1);
      sheet.getRange(startRow + i, 5).setValue(EMAIL_SENT)
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *