我从Google表格教程中获得了此代码,并且可以正常工作,我想在同行填写Google表单后发送电子邮件回复。该电子邮件有效,因此第一步已完成,但是即使我指定它不会发送重复的电子邮件,它仍将继续发送电子邮件。我的工作表如下:
Time Stamp/Email Address/Message/Complete for columns A-D
date-time/test@email.com/ Test1/ EMAIL_SENT
date-time/ test@email.com/ Test2/ EMAIL_SENT
date-time/test@email.com/ Test3/ EMAIL_SENT
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';
/**
* Sends non-duplicate emails with data from the current spreadsheet.
*/
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 3; // Number of rows to process
// Fetch the range of cells B1:D3
var dataRange = sheet.getRange(startRow, 1, numRows, 3);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[1]; // Second column
var message = row[2]; // Third column
var emailSent = row[3]; // Fourth column
if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
var subject = 'Your PFB Request';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 4).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
答案 0 :(得分:2)
var dataRange = sheet.getRange(startRow, 1, numRows, 3);
getRange
不是基于零的方法。您的定义定义的范围只有3列宽。它不包含“完成”列。
将其更改为:
var dataRange = sheet.getRange(startRow, 1, numRows, 4);
“完成”列将等于行[3](从零开始)。当前row [3]没有任何值,可能是“未定义”,它将解释为什么“ IF”语句不起作用。