我有一个脚本,用于根据一栏中提到的特定日期自动发送电子邮件提醒。我的目的是在提醒日期起7天前发送提醒。该脚本可以完美运行并在我运行时发送电子邮件,但在提醒日期的第7天不能自动运行。
请帮助纠正它
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails3() {
var today = new Date().toLocaleDateString(); // Today's date, without
time
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3; // First row of data to process
var numRows = sheet.getLastRow()-1 // Get last row, -1 because your
startrow is 3
// Fetch the range of cells A3:B999
var dataRange = sheet.getRange(startRow, 1, numRows, 999)
// 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 emailAddressTo = row[8]; // POC Email column D
var emailAddressCC = row[9];
var subject = " Case PEC V/s " +row[2]; // Subject column F
var message = "Dear Sir, Please ensure to attend the hearing in the
captioned matter and update the status of hearing. Kindly contact with the
advocate for the courtroom and item number. Hearing date is " +row[6] +"
. Case no is: " +row[3] + " . Advocate M/s " +row[4]; // Message column
G and Column H
var emailSent = row[13]; // Output the message is sent in column H
var cellValue,reminderDate;//Define variables at top of function
cellValue = row[10];// date specified in cell k
if (!cellValue) {continue;}//If there is no cell value continue looping
if (typeof cellValue === 'object') {
reminderDate = cellValue.toLocaleDateString();
} else {
cellValue = new Date(cellValue);//Convert date as string to object
reminderDate = cellValue.toLocaleDateString();
}
if (reminderDate != today) // Skip this reminder if not for today
continue;
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
MailApp.sendEmail(emailAddressTo, subject, message);
MailApp.sendEmail(emailAddressCC, subject, message);
sheet.getRange(startRow + i, 15).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is
interrupted
SpreadsheetApp.flush();
}
}
}