I have a Spreadsheet that stores Task on on Sheet 1. When a task is completed a email is sent. Sheet 2 holds the specific email addresses for send to, cc, and reply to. I am able to loop through sheet 2 and get the email addresses for each column. I want to be able to get those email address into my options for the send email in MailApp. I am unable to get the email addresses out of the loop for sheet 2. See code below. Any help would be appreciate.
var EMAIL_SENT = "EMAIL_SENT";
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheets()[0];
var sheet2 = ss.getSheets()[1];
var startRow = 2;
var lastRow1 = sheet1.getLastRow()-1;
var lastCol1 = sheet1.getLastColumn();
var lastRow2 = sheet2.getLastRow()-1;
var lastCol2 = sheet2.getLastColumn();
var sh1Range = sheet1.getRange(startRow, 1, lastRow1, lastCol1).getValues();
var sh2Range = sheet2.getRange(startRow, 1, lastRow2, lastCol2).getValues();
var subject = "Test Email";
//sheet2 Email Loop
for (var i = 0; i < sh2Range.length; ++i){
var emails = sh2Range[i];
var to = emails[0];
var cc = emails[1];
var replyTo = emails[2];
}
//sheet1 Data Loop and Send Email
for (var j = 0; j < sh1Range.length; ++j){
var data = sh1Range[j];
var pName = data[0];
var pID = data[1];
var pm = data[2];
var dd = Utilities.formatDate(new Date(data[3]), "America/New_York", "MMMM dd, yyyy");
var team1 = data[4];
var status = data[7];
if (team1 == "Task Completed" && status !== EMAIL_SENT){
var htmlBody;
htmlBody = "Project Name: "+pName+"<br>"+"Project ID: "+pID+"<br>"+"Project Manager: "+pm+"<br>"+"Due Date: "+dd+"<br>";
var optAdvancedArgs = {replyTo: replyTo, cc: cc, name: "Test Email", htmlBody: htmlBody};
//Logger.log(htmlBody);
//Logger.log(optAdvancedArgs);
}
}
}
sh2Range.length
loops the values in sheet 2 andsh1Range.length
loops the values in sheet 1. Are you saying the variable names should be changed? Can you show an example to loop through sheet1 and sheet2 in onefor
loop? – WallyG