8

I have a tool that I have created with Google docs, part of it is that I back up a set of numbers on a daily basis at 11am which I use to create a chart to show progress over time. I have this script running using a trigger that is set to backup the data once a week.

What I would really like to do is only back this data up on weekdays as the weekend data is throwing my averages out. So just Monday - Friday daily at 11am

Does anyone have a way of doing setting the script to run this way programatically?

jbr
  • 6,198
  • 3
  • 30
  • 42
ewebber
  • 83
  • 1
  • 1
  • 3
  • 2
    Find a full example in this post: http://stackoverflow.com/questions/10995308/working-hours-only-trigger/10995530#10995530 – Serge insas Oct 07 '13 at 17:57

4 Answers4

20

I had the same idea as Zig Mandel.

Instead of creating 1 trigger for each day, a simpler method is to stop the function if it is the week-end.

Set the trigger to Daily and add few lines of code at the beginning of the function to do the trick:

function functionName(parameters) {

    //Skip week-end
    var day = new Date();
    if (day.getDay()>5 || day.getDay()==0) {
      return;
    }

  //Code to be executed
 }
Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
  • 1
    getDay = 0 for sunday. your code will only skip saturday. – Tarek Eldeeb Sep 20 '16 at 07:03
  • Sure, but needs to execute every hour between 8am to 5pm, weekdays only? – Petrus Jul 24 '21 at 12:11
  • @Petrus: you can adapt the answer to do a return if the hour is lower than 8 or higher than 5 (and of course, set a Trigger to repeat every hour). You can check the following example: https://stackoverflow.com/a/10995530/1603480 – Jean-Francois T. Jul 25 '21 at 02:05
17

You can use the following function to create Triggers that will run every weekday at 11 AM.

function createTriggers() {
   var days = [ScriptApp.WeekDay.MONDAY, ScriptApp.WeekDay.TUESDAY,
               ScriptApp.WeekDay.WEDNESDAY, ScriptApp.WeekDay.THURSDAY,                                            
               ScriptApp.WeekDay.FRIDAY];
   for (var i=0; i<days.length; i++) {
      ScriptApp.newTrigger("your_function_name")
               .timeBased().onWeekDay(days[i])
               .atHour(11).create();
   }
}
Amit Agarwal
  • 10,910
  • 1
  • 32
  • 43
2

Run it daily and add an if at the beginning of your trigger using js functions to check for the day of week. getDay() method returns the day of the week (from 0 to 6)

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
0

Perhaps with the Time-Driven Triggers accomplish what you need. You can define a trigger for each day/time you need.

You also have the option mentioned here by Zig Mandel.

Community
  • 1
  • 1
wchiquito
  • 16,177
  • 2
  • 34
  • 45