如何在此Google Apps脚本中定位多个特定日期

时间:2018-02-15 20:47:55

标签: google-apps-script google-sheets

我有这个脚本,当电子表格在一天中的特定时间内没有被修改时,它会发送一封电子邮件:

function checkWriting() {

  const emailAddress = "your@email.com";

  var now = new Date();
  var day = now.getDay();
  var hours = now.getHours();

//Set the days and between which hours to check
//Sun=0 Mon=1 Tue=2 Wed=3 Thu=4 Fri=5 Sat=6   
if ((day >= 3) && (day <= 5) && (hours >= 18) && (hours <= 19)) {

// Get the dates
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var lastRow = sheet.getLastRow();
var formatDate = sheet.getRange(lastRow, 1).getValue().replace("at", "").replace("AM", "").replace("PM","") + " GMT+0100";
var lastWrite = new Date(formatDate);

var lastWriteDate = lastWrite.getFullYear() + "-" + lastWrite.getMonth() + "-" + lastWrite.getDate();
var nowDate = now.getFullYear() + "-" + now.getMonth() + "-" + now.getDate();

if (nowDate > lastWriteDate) {
  // Send an email out
  MailApp.sendEmail(emailAddress, "You didn’t write today.", "Get on that.");
  }
 }
}

现在我将它设定为在星期三和星期五之间运行。

我的问题是如何定位星期一,星期三,星期五?

我会写它,所以我只有多个独立的功能吗?例如

function checkWritingMonday() {

  const emailAddress = "your@email.com";

  var now = new Date();
  var day = now.getDay();
  var hours = now.getHours();

//Set the days and between which hours to check
//Sun=0 Mon=1 Tue=2 Wed=3 Thu=4 Fri=5 Sat=6   
if ((day = 1) && (hours >= 18) && (hours <= 19)) { 

我意识到我可能也只能在周一,周三或周五进行触发。这就是我将在临时做的事情。

我只是想在脚本中找到一种方法。

1 个答案:

答案 0 :(得分:0)

你可以简单地创建一个更复杂的,如下所示:

//Sun=0 Mon=1 Tue=2 Wed=3 Thu=4 Fri=5 Sat=6   
if ((day == 1 || day == 3 || day == 5) && (hours >= 18) && (hours <= 19)) {