我正在查看是否在提交Google表单时触发自动电子邮件。它工作正常,但是我在下面的代码中遇到3个问题
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow(); // Number of rows to process
var dataRange = sheet.getRange(numRows, 2, 1, sheet.getLastColumn());
// 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[5]; // First column
var message = 'Hello,'+ "\n"
+ "\n"
+ 'We have received an inquiry from your customer in Inbound'+ ".\n"
+ "\n"
+ 'Lead No is' + " - "
+ row[1] + "\n"
+ "\n"
+ 'Kindly arrange a callback'+ "\n"
+ "\n"
+ 'Regards,'+ "\n"
+ 'Team Inbound' + "\n"
+ "\n"
+ 'This is an auto-generated email'; // Second column
var emailSent = row[7]; // Third column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Inbound Inquiry"+ " - " + row[1];
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 8).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
以这种方式尝试:
function sendEmails() {
var sheet=SpreadsheetApp.getActiveSheet();
var startRow=2;
var dataRange=sheet.getRange(startRow,1,sheet.getLastRow()-startRow+1,sheet.getLastColumn());//I think startRow is a better fit for the first parameter and my guess is that you did't really intend for data.length to be only 1 row.
var data=dataRange.getValues();
for (var i=0;i<data.length;++i) {
var row=data[i];
var emailAddress=row[5];//column 6
var message=Utilities.formatString('Hello,\n\nWe have received an inquiry from your customer in Inbound.\n\nLead No is - %s\n\nKindly arrange a callback.\n\nRegards,\nTeam Inbound\n\nThis is an auto-generated email.',row[1]);
var emailSent = row[7];//column 8
if (emailSent != "EMAIL_SENT") { // Prevents sending duplicates
var subject = "Inbound Inquiry"+ " - " + row[1];//column 2
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 8).setValue("EMAIL_SENT");//column 8 and I don't think it's necessary to flush in the loop
}
}
}
检查评论以确保我获得正确的列。
Read Sheet.getRange(row, column, number of rows, number of columns)