在将来的日期发送自动电子邮件

时间:2019-02-25 16:04:09

标签: google-apps-script

我在google脚本上写了我的第一个代码,试图在截止日期之前/之后/之后发送自动电子邮件。尽管前两个条件都可行,但是当截止日期是将来的日期时,我没有设法发送电子邮件。

考虑以下代码:

function EmailReminder() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A2:C4");
var UserData = range.getValues();
var today = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");

for (i in UserData){
  var row = UserData[i];
  var Contact = row[0];
  var ActionNeeded = row[1];
  var ActionDeadline = row[2];
  var ActionDeadlines = Utilities.formatDate(new Date(ActionDeadline),"GMT+1","dd/MM/yyyy"); 
  if  (ActionDeadlines  < today) { 
    MailApp.sendEmail(row[0], "Action pending for your project ","Dear PM,\n\nFor your project, the deadline to complete the following action was on " + ActionDeadline + ": \n\n*****\n\n"  + ActionNeeded + "\n\n*****\n\nKindly take appropriate action as soon as possible.");
}  
  else if (ActionDeadlines == today) { 
    MailApp.sendEmail(row[0], "Action pending for your project ","Dear PM,\n\nFor your project, the deadline to complete the following action is today: \n\n*****\n\n"  + ActionNeeded + "\n\n*****\n\nKindly take appropriate action as soon as possible.");
}  
  else if (ActionDeadlines > today) { 
    MailApp.sendEmail(row[0], "Action pending for your project ","Dear PM,\n\nFor your project, there is an upcoming deadline due on the " + ActionDeadline + ": \n\n*****\n\n"  + ActionNeeded + "\n\n*****\n\nKindly take appropriate action as soon as possible."); 
}
}
}

样本数据:

Contact               ActionNeeded                  Deadline
Insert email    function for a past deadline       =today()-5 
Insert email    function for a current deadline    =today() 
Insert email    function for a future deadline     =today()+5 

结果:

  1. 对于过去日期的截止日期:发送的电子邮件很好
  2. 对于截止日期为今天的日期:发送的电子邮件很好
  3. 对于具有未来日期的截止日期:它给出了正确的ActionNeeded,但是它似乎被第一个If条件(ActionDeadlines <今天)而不是第三个If条件(ActionDeadlines> Today)错误地激活了。
  4. 最后,电子邮件中的时间包括小时数(“ Sat Mar 02 2019 00:00:00 GMT + 0100(CET)”)。

如何将其保留为dd / MM / yyyy?

0 个答案:

没有答案