多亏了在这个网站上的大量阅读,我成功地构建了一个脚本来完成我想要它做的事情:运行时,脚本为电子表格中的每个新行发送一封单独的电子邮件,然后将该行标记为“已发送”。耶!
但现在我需要脚本将所有行编译成单个电子邮件中的多个部分。因此,我需要脚本来检查新行,使用模板将每个新行中的值添加到单个部分中,将所有新部分编译成电子邮件,发送电子邮件,然后将所有新行标记为“已发送”。
我被卡住了,因为似乎我需要让每个新添加的行定义一个相对变量,即第一个未发送的行 = sectionOne,第二个 = sectionTwo 等。但是电子邮件的长度和要定义的变量数量将取决于关于新行的数量。所以我真的不知道如何让脚本通过循环并将新内容(但全部)添加到电子邮件正文中。
这就是我所拥有的,有人知道如何到达这里的目标吗?
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,
});
}
}
}
最佳答案
无需过多修改您的代码,这就是我为您准备的:
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();
var message = "";
//iterate loop
for (i in AllValues) {
//set current row
var CurrentRow = AllValues[i];
//define column to check if sent (starts from "0" not "1")
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>Found by: </b>" + CurrentRow[1] + "</p>" +
"<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><br><br>";
//set the row to look at
var setRow = parseInt(i) + StartRow;
//mark row as "sent"
ActiveSheet.getRange(setRow, 11).setValue("sent");
}
//define who to send grants to
var SendTo = "[email protected]" + "," + "[email protected]";
//set subject line
var Subject = "Grants";
//send the actual email
MailApp.sendEmail({
to: SendTo,
cc: "",
subject: Subject,
htmlBody: message,
});
}
所以本质上我们将 message 变量的范围更改到 for 循环之外,以便在每次迭代期间可以使用相同的内容访问它。然后,我们将每个新记录的附加 HTML 内容附加到消息变量中。我更改了对电子邮件是否已经发送到顶部的检查,这实际上会稍微提高速度,但也不会干扰我们的消息变量。最后,我将电子邮件发送部分移出了 for 循环,因为我们在循环内收集了我们的数据。
如果我多花一点时间,我可以更改此代码并使其更易于理解,并且还可能将 HTML 内容更改为表格格式,但现在应该可以完成这项工作。
我还没有测试过这段代码,因为我正在路上这样做,但我相信它应该没问题。如果您需要进一步的帮助,请告诉我。
关于email - 使用电子表格中所有新行的值发送一封电子邮件(Google Script/GAS),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36415273/