我想在将新行添加到评论电子表格时尝试使用脚本发送电子邮件。在" Send single email with values from all new rows in a spreadsheet"" repository"但我发现当使用触发器onEdit
进行以下脚本时,即使第9列中的所有行(通知已发送列),我仍会在进行任何编辑时收到空白电子邮件标记为"已发送"。
有没有办法添加if
语句,只有在有一个空白列9的新行时才发送电子邮件(因此我没有收到onEdit
的空白电子邮件触发)?
function sendEmail() {
//setup function
var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
if (ActiveSheet.getName() == 'Review Tracker') {
var StartRow = 6;
var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,11);
var AllValues = WholeRange.getValues();
for (i in AllValues) {
var row = AllValues[i];
if (row[7] === "Ready for Review") {
var message = "";
//iterate loop
for (i in AllValues) {
//set current row
var CurrentRow = AllValues[i];
//define column to check if sent
var EmailSent = CurrentRow[10];
//if row has been sent, then continue to next iteration
if (EmailSent == "sent")
continue;
//set HTML template for information
message +=
"<p><b>Name: </b>" + CurrentRow[0] + "</p>" +
"<p><b>Client: </b>" + CurrentRow[1] + "</p>" +
"<p><b>Deliverable Title: </b>" + CurrentRow[2] + "</p>" +
"<p><b>Link to Review Thread: </b>" + CurrentRow[3] + "</p>" +
"<p><b>Deadline for Review: </b>" + CurrentRow[4] + "</p>" +
"<p><b>Delivery to Client: </b>" + CurrentRow[5] + "</p>" +
"<p><b>Notes: </b>" + CurrentRow[6] + "</p>" +
"<p><b>Status: </b>" + CurrentRow[7] + "</p>" + "</p><br><br>";
//set the row to look at
var setRow = parseInt(i) + StartRow;
//mark row as "sent"
ActiveSheet.getRange(setRow, 10).setValue("sent");
}
//define who to send emails to
var SendTo = "email@address.com";
//set subject line
var Subject = "New Deliverable to Review for " + CurrentRow[1];
//send the actual email
MailApp.sendEmail({
to: SendTo,
subject: Subject,
htmlBody: message,
});
}
}
}
}
答案 0 :(得分:0)
从
更改var Subject = "New Deliverable to Review for " + CurrentRow[1];
//send the actual email If message is not empty
if (message) {// Added
MailApp.sendEmail({
to: SendTo,
subject: Subject,
htmlBody: message,
});
}//Added
到
function sendEmail() {
//setup function
var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
if (ActiveSheet.getName() == 'Review Tracker') {
var StartRow = 6;
var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,11);
var AllValues = WholeRange.getValues();
var message = "";
//iterate loop
for (i in AllValues) {
//set current row
var CurrentRow = AllValues[i];
if (CurrentRow[7] == "Ready for Review" && CurrentRow[9] != "sent") {
//define column to check if sent
//var EmailSent = CurrentRow[10];
//if row has been sent, then continue to next iteration
//if (EmailSent == "sent") {
//continue;}
//set HTML template for information
message +=
"<p><b>Name: </b>" + CurrentRow[0] + "</p>" +
"<p><b>Client: </b>" + CurrentRow[1] + "</p>" +
"<p><b>Deliverable Title: </b>" + CurrentRow[2] + "</p>" +
"<p><b>Link to Review Thread: </b>" + CurrentRow[3] + "</p>" +
"<p><b>Deadline for Review: </b>" + CurrentRow[4] + "</p>" +
"<p><b>Delivery to Client: </b>" + CurrentRow[5] + "</p>" +
"<p><b>Notes: </b>" + CurrentRow[6] + "</p>" +
"<p><b>Status: </b>" + CurrentRow[7] + "</p>" + "</p><br><br>";
//set the row to look at
var setRow = parseInt(i) + StartRow;
//mark row as "sent"
ActiveSheet.getRange(setRow, 10).setValue("sent");
}//if review ready
}//For loop close
//define who to send emails to
var SendTo = "email@address.com";
//set subject line
var Subject = "New Deliverable to Review for " + CurrentRow[1];
//send the actual email if message is not empty
if (message) {
MailApp.sendEmail({
to: SendTo,
subject: Subject,
htmlBody: message,
});
}//if message
}//if sheetName Review
}//End Func
修改后的脚本:
{{1}}