我成功地从工作表发送了电子邮件,并且它正在填充“已发送”列,然后在再次运行后不再重复发送电子邮件。
我想打开它以便未定义行,以便在信息进入时向该文档添加电子邮件。
随着时间的推移,电子邮件列将被填充,我不想每次都编辑脚本,因为这会适得其反。
以下是工作表:https://docs.google.com/spreadsheets/d/1f6qUjGYHZtRRGCbyh5oNzM7o64B-wlkrTpB1ac64I2U/edit?usp=sharing
由于代码设置为设定的范围,因此不会超出第5行。
如果将范围更改为L100,可能会出现错误,可能是因为第5行之后没有电子邮件地址
var EMAIL_SENT = 'EMAIL_SENT';
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 4;
var numRows = 2;
var dataRange = sheet.getRange("J4:L5");
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0];
var message = row[1];
var emailSent = row[2];
if (emailSent != EMAIL_SENT) {
var subject = 'Non-repeating emails sent from Sheets with no row limit 001';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
我希望能够具有无限数量的行,并且能够重复运行该脚本,并且不发送电子邮件至以前发送过的地址,而仅发送给我新添加的电子邮件运行脚本
答案 0 :(得分:1)
我认为您已经接近了,应该在代码中使用getDataRange()
而不是静态范围。参见下面的版本,进行了一些修改:
var EMAIL_SENT = 'EMAIL_SENT';
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 4;
// var numRows = 2; --> not used
var dataRange = sheet.getDataRange(); // Get the entire range having data
var data = dataRange.getValues();
for (var i = startRow-1; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0];
var message = row[1];
var emailSent = row[2];
if (emailSent != EMAIL_SENT) {
var subject = 'Non-repeating emails sent from Sheets with no row limit 001';
MailApp.sendEmail(emailAddress, subject, message);
data[i][2] = EMAIL_SENT;
}
}
dataRange.setValues(data); // More efficient to only write once
}