首页 > 解决方案 > 在未来日期发送自动电子邮件

问题描述

我在谷歌脚本上编写了我的第一个代码,试图在截止日期之前/之后/之后发送一封自动电子邮件。虽然前两个条件有效,但当截止日期是未来日期时,我没能发送电子邮件。

考虑以下代码:

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 < 今天)而不是第三个条件(ActionDeadlines > 今天)错误地激活。
  4. 最后,电子邮件中的时间包括小时数(“Sat Mar 02 2019 00:00:00 GMT+0100 (CET)”)。

我怎样才能保持它到 dd/MM/yyyy?

标签: google-apps-script

解决方案


推荐阅读