我正在尝试创建一个脚本,该脚本将为每个包含今天日期的单元格发送一封电子邮件。这是我到目前为止的内容:
function email() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var rowCounter = 1;
var limit = sheet.getLastRow();
// Fetch the estimated dates & Today's date
var estimatedReturnDateRange = sheet.getRange("F:F");
var estimatedReturnDate = estimatedReturnDateRange.getCell(rowCounter, 1);
var todayDateRange = sheet.getRange("S1");
var todayDate = todayDateRange.getValue();
// Check totals sales
for(i=1; i<=limit; i++){
if (estimatedReturnDate = todayDate){
// Fetch the email address
var emailAddress = "maxbkimmel@gmail.com";
// Send Alert Email.
var message = estimatedReturnDate; // Second column
var subject = 'Your Google Spreadsheet Alert';
MailApp.sendEmail(emailAddress, subject, message);
}
rowCounter++;
estimatedReturnDate = estimatedReturnDateRange.getCell(rowCounter, 1);
}
rowCounter =1;
}
这是我设想脚本工作逻辑的方式:
estimatedReturnDate最初获取列F中的第一个单元格,该列是日期列表。
todayDate获取包含今天日期的单元格S1。
然后,一个for循环在工作表的所有行中循环,并检查EstimateReturnDate = todayDate。 如果是这样,则会发送一封电子邮件,其中包含与今天的日期匹配的行号。然后,增加rowCounter,将returnDateDate设置为该行的下一个单元格,然后循环再次运行。
我遇到的问题是,当我运行此脚本时,无论估计的ReturnDate是否与todayDate匹配,都为工作表中的每一行发送一封电子邮件。
有人知道这是什么原因吗?
答案 0 :(得分:2)
function email() {
var ss=SpreadsheetApp.getActive();
var sheet=ss.getSheets()[0];//This is always the left most sheet but not necessarily the same sheet depending how users move the sheets around.
var vA=sheet.getRange(1,5,sheet.getLastRow(),1).getValues()
var dt=new Date();
var toda=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight yesterday
var tmro=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()+1).valueOf();//midnight today
for(var i=0;i<vA.length;i++){
var dt=new Date(vA[i][0]).valueOf();//date from column5 of spreadsheet
//dt is between midnight yesterday and midnight today
if(dt>=toda && dt<=tmro){
var emailAddress = "maxbkimmel@gmail.com";
var message = Utilities.formatDate(dt, Session.getScriptTimeZone(), "E MMM dd, yyyy");
var subject = 'Your Google Spreadsheet Alert';
MailApp.sendEmail(emailAddress, subject, message);
}
}
}