0
votes

I have this script, it sends an email when the spreadsheet hasn't been modified by a specific time on set of days:

function checkWriting() {

  const emailAddress = "[email protected]";

  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.");
  }
 }
}

Right now I have it set to run between Wednesday and Friday.

My question is how do I target lets say Monday, Wednesday, Friday?

Would I write it so I just have multiple separate functions? For example

function checkWritingMonday() {

  const emailAddress = "[email protected]";

  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)) { 

I realize I can probably also just have my trigger run only on Monday, Wed, or Friday. That's what I'm going to do in the interim.

I am just looking for a way to do it in the script.

1

1 Answers

0
votes

You could simply create a more complex if, like this:

//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)) {