2016-02-05 209 views
0

我需要延遲48小時發送電子郵件。我已經試過:Google Apps腳本 - 延遲48小時發送電子郵件

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

,但我得到的錯誤:

"ReferenceError: "date" is not defined. (line 26, file "Code").

下面是腳本:

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++; 
    } 
} 

回答

0

其中date是,你需要將其更改爲mail_date。但firest確保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)) 
+0

謝謝大家的響應。更改後,我收到另一個錯誤: TypeError:在對象中找不到函數setDate。 (第28行,文件「代碼」) –

+0

查看更新的答案。 –

相關問題