Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google script trigger weekdays only

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?

like image 427
ewebber Avatar asked Oct 07 '13 11:10

ewebber


People also ask

What is onEdit E?

onEdit(e) The onEdit(e) trigger runs automatically when a user changes the value of any cell in a spreadsheet. Most onEdit(e) triggers use the information in the event object to respond appropriately. For example, the onEdit(e) function below sets a comment on the cell that records the last time it was edited.


2 Answers

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
 }
like image 200
Jean-Francois T. Avatar answered Nov 04 '22 10:11

Jean-Francois T.


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();
   }
}
like image 20
Amit Agarwal Avatar answered Nov 04 '22 09:11

Amit Agarwal