Sarah S
Sarah S

Reputation: 1

How to run Google Apps Script - Weekdays only

I have a trigger setup to send a Google doc to colleagues via email, daily between 9-10pm. It works perfectly. However, I need this to only run on weekdays.

Below is my code. At the very top I attempted to "return false" on Sunday and Saturday for "isbusinessday" definition, but this is still running all 7 days. What am I doing wrong / how can I fix?

function isBusinessDay(date){
  if (date.getDay() == 0 || date.getDay() == 6) {
    return false;
  }
  return true;
}

function setTrigger() {
  var setTime = new Date();
  setTime.setHours(21);
  setTime.setMinutes(00); 
  if (isBusinessDay(setTime)) {
  ScriptApp.newTrigger('sendMyEmail').timeBased().at(setTime).create();
  }
}

function delTrigger() {
  var triggers = ScriptApp.getProjectTriggers();
  for(var i=0; i < triggers.length; i++) {
    if (triggers[i].getHandlerFunction() == "myFunction") {
      ScriptApp.deleteTrigger(triggers[i]);
    }
  }
}

function getNextBusinessDay(date) {
  date = new Date(+date);
  do {
    date.setDate(date.getDate() + 1);
  } while (!(date.getDay() % 6))
  return date;
}

function sendMyEmail() {
  delTrigger();
  var doc = DocumentApp.getActiveDocument();
  var nextBusinessDay = getNextBusinessDay(new Date())
  var month = ("0"+(nextBusinessDay.getMonth()+1)).slice(-2)
  var day = ("0"+(nextBusinessDay.getDate())).slice(-2)
  var formattedDate = nextBusinessDay.getYear() + "." + month + "." + day;
  var subject = " --document name-- " + formattedDate;
  var emailTo = " --receipient emails-- ";
  var message = "Please see today's agenda attached and in redline.";
  
  var docFile = getBlobViaURL_(doc.getId(), MimeType.MICROSOFT_WORD);
  docFile.setName(" --document name--" + formattedDate + ".docx");
  //var mailObj = {attachments:[docFile], bcc:" --my email-- "};
  var mailObj = {attachments:[docFile]};
  MailApp.sendEmail(emailTo, subject, message, mailObj);
}

function getBlobViaURL_(id, mimeType) {
  var url = " --url for my google doc-- "+id+"/export?mimeType="+ mimeType;
  var resp = UrlFetchApp.fetch(url, {
    headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken()}
  });
  return resp.getBlob();
}

Upvotes: -1

Views: 47

Answers (1)

Tedinoz
Tedinoz

Reputation: 8069

You want to send an email (sendMyEmail) every weekday between 9pm and 10pm.

The OP should note that triggers can be adapted to almost every circumstance and there are many precedents on StackOverflow for triggers. In this case, given that the OP is new to scripting, the trigger and script are designed to be as simple as possible so that the OP can better understand the process.

Try this answer:
There are two scripts:

  • createTimeDrivenTrigger - this will create a time-driven trigger for every day at around 21:30 (9:30pm).
  • sendMyEmail - the script to be executed

Note that the first few lines of sendMyEmail evaluate whether the day is 0 (Sunday) or 6 (Saturday)

  • if no, the code proceeds as normal
  • if yes, then the code stops

Note: The OP should manually delete any existing trigger for sendMyEmail.


function sendMyEmail() {

  //Skip week-ends
  var day = new Date();
  if (day.getDay()==0 || day.getDay()==6) {// 0=Sunday, 6=Saturday
      return;
  }

  // send the email
  var doc = DocumentApp.getActiveDocument();
  var nextBusinessDay = getNextBusinessDay(new Date())
  var month = ("0"+(nextBusinessDay.getMonth()+1)).slice(-2)
  var day = ("0"+(nextBusinessDay.getDate())).slice(-2)
  var formattedDate = nextBusinessDay.getYear() + "." + month + "." + day;
  var subject = " --document name-- " + formattedDate;
  var emailTo = " --receipient emails-- ";
  var message = "Please see today's agenda attached and in redline.";
  
  var docFile = getBlobViaURL_(doc.getId(), MimeType.MICROSOFT_WORD);
  docFile.setName(" --document name--" + formattedDate + ".docx");
  //var mailObj = {attachments:[docFile], bcc:" --my email-- "};
  var mailObj = {attachments:[docFile]};
  MailApp.sendEmail(emailTo, subject, message, mailObj);
}

function createTimeDrivenTrigger() {
    // Trigger every Day at 21:30
    ScriptApp.newTrigger('sendMyEmail')
        .timeBased()
        .everyDays(1)
        .atHour(21)
        .nearMinute(30)  
        .create(); 
}

Trigger - snapshot

trigger

Upvotes: 1

Related Questions