Reputation: 31
I'm trying to build a script that can automatically send email when a new row is updated. I only want to trigger the new updated rows and mark 'sent' after sending them out.
Here's the script that's currently working well. I can run it with a click:
function sendMail()
{
var name = 0;
var request = 1;
var id = 2;
var select = 3;
var time = 4;
var email = 5;
var status = 6;
var error = 7;
var emailTemp = HtmlService.createTemplateFromFile("email");
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email list");
var data = ws.getRange("A2:I" + ws.getLastRow()).getValues();
data = data.filter(function(r){ return r[8] == true });
data.forEach(function(row)
{
emailTemp.fn = row[name];
emailTemp.req = row[request];
emailTemp.id = row[id];
emailTemp.erm = row[error];
emailTemp.ta = row[select];
emailTemp.time = row[time];
var htmlMessage = emailTemp.evaluate().getContent();
GmailApp.sendEmail(row[email],
"Failure message",
"Please submit again.",
{name: "My name", htmlBody: htmlMessage}
);
});
}
I have studied this query but I still have no idea how it works.
Please share some relevant source or some solution, thank you!
Upvotes: 0
Views: 2148
Reputation: 201378
Although I'm not sure about mark 'sent' after sending them out.
, if "sent" is put to the column "J", from I assume it's filled in another column, like col8 or col 9.
of your reply, I guessed that you wanted to put "sent" to the column "J" because the columns 8 and 9 (H and I) have already been used in your script. If my understanding is correct, how about the following modification?
Please copy and paste the following script to the script editor of Google Spreadsheet and save the script. And please install OnEdit trigger to the function sendMail
. By this, when the cell is edited, the script is run.
function sendMail() {
var ws = SpreadsheetApp.getActiveSheet();
if (ws.getSheetName() != "Email list") return;
var name = 0;
var request = 1;
var id = 2;
var select = 3;
var time = 4;
var email = 5;
var status = 6;
var error = 7;
var emailTemp = {};
var emailTemp = HtmlService.createTemplateFromFile("email");
var data = ws.getRange("A2:J" + ws.getLastRow()).getValues();
var ranges = data.map(function (row, i) {
if (row[8] == true && row[9] != "sent") {
emailTemp.fn = row[name];
emailTemp.req = row[request];
emailTemp.id = row[id];
emailTemp.erm = row[error];
emailTemp.ta = row[select];
emailTemp.time = row[time];
var htmlMessage = emailTemp.evaluate().getContent();
GmailApp.sendEmail(row[email],
"Failure message",
"Please submit again.",
{ name: "My name", htmlBody: htmlMessage }
);
return "J" + (i + 2);
}
return "";
}).filter(String);
ws.getRangeList(ranges).setValue("sent");
}
I think that the event object of OnEdit can be used. But from your question, I thought that you might want to directly run the script with the script editor. So I didn't use the event object.
It seems that the columns 8 and 9 (H and I ) are used in your script like emailTemp.erm = row[error];
and data = data.filter(function(r){ return r[8] == true });
. So I put "sent" to the column "J". Please be careful this.
Upvotes: 2