Google Apps脚本 - 延迟发送电子邮件48小时

时间:2016-02-05 15:40:03

标签: google-apps-script

我需要延迟发送电子邮件48小时。我试过了:

var mail_date_plus_2 = new Date(date.setDate(mail_date+2))

但是我收到了错误:

  

" ReferenceError:" date"没有定义。 (第26行,文件"代码")。

这是脚本:

function sendEmail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var data_sheet = ss.getSheetByName('Sheet1');
  var values = data_sheet.getRange(2, 1, data_sheet.getLastRow(), data_sheet.getLastColumn()).getValues();
  var row = 2;
  var current_time= new Date();
  Logger.log('Current data and time is '+current_time)
  for(var v in values) {    
    var mail_note = data_sheet.getRange(row, 1).getNote();
    if(mail_note=='') {
      var mail_date = values[v][5];
      if(mail_date!='') {        
        if(current_time>mail_date) {
          Logger.log(v+'>>Need to send mail at '+mail_date);
          var name  =  values[v][2];
          var email =  values[v][4];
          var form  =  values[v][1];
          var htmlbody = RESERVATION_EMAIL_HTML;
          var prefix = 'Dear '+name+', '+"<br/><br/>";
          htmlbody = prefix + htmlbody
          if(form=='Reservation') {MailApp.sendEmail({
             to: email,
             subject: EMAIL_SUBJECT,
             htmlBody: htmlbody,        
          });
          }
          var cdate = new Date();
          data_sheet.getRange(row, 1).setNote('Mail has been sent at '+cdate)
        }
      }
    }
    row++;
  }
}

1 个答案:

答案 0 :(得分:0)

date的位置,您需要将其更改为mail_date。但是要确保mail_date是日期类型。您可以使用关键字typeof检查JavaScript变量类型。

var mail_date = values[v][5];
Logger.log('typeof mail_date: ' + typeof mail_date);

如果mail_date不是日期类型,则将其转换为日期:

var mail_date = values[v][5];

if (typeof mail_date !== 'object') {//In JavaScript a date is an "object" type
  mail_date = new Date(mail_date);
};

var mail_date_plus_2 = new Date(mail_date.setDate(mail_date+2))