0
以下脚本的目的是从Google工作表发送电子邮件,但我只希望在满足条件时发送电子邮件。Google Apps脚本中的if条件从Google工作表发送电子邮件不起作用
在我的电子表格M列(第12行)中,值为TRUE或FALSE。单元格基于在单元内运行的AND脚本显示TRUE或FALSE。例如,TRUE的一个条件是列P是空的(而不是显示EMAIL_SENT)。 AND语句正常工作。
但是当我运行脚本时,它只是发送电子邮件而不管这种情况。所以有什么错的条件...
我基于它的教程here ...但我只是这样跑它,原来似乎并没有为我工作,这并没有帮助我猜测!
// Create a date object for the current date and time.
var now = new Date();
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";
var SENDYESNO = "FALSE";
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 16)
// 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 title = row[0];
var name = row[2];
var link = row[9];
var emailAddress = row[10]; // First column
var message = row[14]; // Second column
var emailSent = row[16]; // Third column
var date = row[17];
var yesNo = row[12];
if (yesNo != SENDYESNO) { // Prevents sending duplicates
var subject = "Sending emails from a Spreadsheet";
GmailApp.sendEmail(emailAddress, subject, "Dear " + title + " " + name + ", \n\nThis is an email report of your link. \n\nYour link is " + link + " \n\nKind regards,\nName ", {
from: "[email protected]",
name: "My name"});
sheet.getRange(startRow + i, 16).setValue(EMAIL_SENT);
sheet.getRange(startRow + i, 17).setValue(now);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
是的,它的工作原理 - 非常感谢! – touto