如何通过Google Apps脚本在电子邮件的正文中设置日期格式?

时间:2019-11-28 02:58:35

标签: google-apps-script google-sheets

我有一个自动的电子邮件脚本,邮件正文中是一个日期,当前表示Thu Nov 28 2019 00:00:00 GMT+1300 (NZDT),但我希望它表示为28 November 2019

下面是我的代码。关于日期的代码是:

 var date = sheet.getRange(i, 7).getValue();
 var formattedDate = Utilities.formatDate(date, "dd MMMMM yyyy")

//Email Alerts for H&S Reviews
function sendEmails() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName("H&S Reviews").activate();
  var lastRow = sheet.getLastRow();
  var message = spreadsheet.getSheetByName("Email Alerts").getRange(1,1).getValue();

  for (var i = 4;i<=lastRow;i++){

    var emailAddress = sheet.getRange(i, 9).getValue();
    var firstName = sheet.getRange(i, 10).getValue();
    var todaysDate = sheet.getRange(1, 11).getValue();
    var date = sheet.getRange(i, 7).getValue();
    var formattedDate = Utilities.formatDate(date, "dd MMMMM yyyy")
    var task = sheet.getRange(i, 3).getValue();
    var description = sheet.getRange(i, 4).getValue();
    var messageBody = message.replace("{name}",firstName).replace("{Task}",task).replace("{Description}",description).replace("{Date}",date);
    var subject = "Health & Safety Review Task"; 
    var sendDate = sheet.getRange(i, 11).getValue();
    var sheetDate = new Date(sendDate);
    Sdate=Utilities.formatDate(todaysDate,"GMT+0200","dd-MM-yyyy")
    SsheetDate=Utilities.formatDate(sheetDate,"GMT+0200", "dd-MM-yyyy")
    
    
    if (Sdate == SsheetDate){
      var subject = "Health & Safety Review Task";
      MailApp.sendEmail(emailAddress, subject, messageBody);
      
    }    
  }
}

我希望对此有所帮助。

1 个答案:

答案 0 :(得分:2)

您已经在脚本中使用Utilities.formatDate()。您可以将其应用于生成“ 2019年11月28日”之类的格式。要获取序数后缀,请考虑this answer

如果您对“ 2019年11月28日”的格式还满意,则只需更改这两行

var formattedDate = Utilities.formatDate(date, "GMT+0200", "dd MMMMM yyyy");
// ... other code ...
var messageBody = message.replace("{name}",firstName).replace("{Task}",task).replace("{Description}",description).replace("{Date}",formattedDate);

您的最终代码为:

//Email Alerts for H&S Reviews
function sendEmails() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName("H&S Reviews").activate();
  var lastRow = sheet.getLastRow();
  var message = spreadsheet.getSheetByName("Email Alerts").getRange(1,1).getValue();

  for (var i = 4;i<=lastRow;i++){

    var emailAddress = sheet.getRange(i, 9).getValue();
    var firstName = sheet.getRange(i, 10).getValue();
    var todaysDate = sheet.getRange(1, 11).getValue();
    var date = sheet.getRange(i, 7).getValue();
    var formattedDate = Utilities.formatDate(date, "GMT+0200", "dd MMMMM yyyy");
    var task = sheet.getRange(i, 3).getValue();
    var description = sheet.getRange(i, 4).getValue();
    var messageBody = message.replace("{name}",firstName).replace("{Task}",task).replace("{Description}",description).replace("{Date}",formattedDate);
    var subject = "Health & Safety Review Task"; 
    var sendDate = sheet.getRange(i, 11).getValue();
    var sheetDate = new Date(sendDate);
    Sdate=Utilities.formatDate(todaysDate,"GMT+0200","dd-MM-yyyy")
    SsheetDate=Utilities.formatDate(sheetDate,"GMT+0200", "dd-MM-yyyy")


    if (Sdate == SsheetDate){
      var subject = "Health & Safety Review Task";
      MailApp.sendEmail(emailAddress, subject, messageBody);

    }    
  }
}