Thanks to lots of reading on this site, I successfully built a script that does what I wanted it to do: Upon running, the script sends an individual email for each new row in a spreadsheet, then mark that row "sent." Yay!
But now I need the script to compile all the rows into multiple sections in a single email. So, I need the script to check for new rows, use a template to add the values from each new row into an individual section, compile all the new sections into an email, send the email, then mark all new rows "sent".
I'm stuck because it seems that I would need to have each newly added row define a relative variable, i.e. first unsent row = sectionOne, second = sectionTwo, etc. But the length of the email and the number of variables to define would depend on the number of new rows. So I don't really know how to get the script to go through a loop and add only (but all of) the new content to the email body.
Here's what I have, anyone know how to get to the goal here?
function sendEmail() {
//setup function
var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var StartRow = 3;
var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,11);
var AllValues = WholeRange.getValues();
//iterate loop
for (i in AllValues) {
//set current row
var CurrentRow = AllValues[i];
//set subject line
var Subject = "Found by " + CurrentRow[1];
//set HTML template for information
var message =
"<p><b>Title: </b>" + CurrentRow[2] + "</p>" +
"<p><b>Agency: </b>" + CurrentRow[3] + "</p>" +
"<p><b>Summary: </b>" + CurrentRow[4] + "</p>" +
"<p><b>Due: </b>" + CurrentRow[5] + "</p>" +
"<p><b>Posted: </b>" + CurrentRow[6] + "</p>" +
"<p><b>Total Funding: </b>" + CurrentRow[7] + "</p>" +
"<p><b>Announcement Number: </b>" + CurrentRow[8] + "</p>" +
"<p><b>Useful Links: </b>" + CurrentRow[9] + "</p>";
//define column to check if sent
var EmailSent = CurrentRow[11];
//define who to send grants to
var SendTo = "[email protected]" + "," + "[email protected]";
//if row has not been sent, then...
if (emailsent != "sent") {
//set the row to look at
var setRow = parseInt(i) + startRow;
//mark row as "sent"
ActiveSheet.getRange(setRow, 11).setValue("sent");
//send the actual email
MailApp.sendEmail({
to: SendTo,
cc: "",
subject: subject,
htmlBody: message,
});
}
}
}