0

I have a Google Sheets spreadsheet and a script needs to update it automatically. Need to run a Google Script only weekdays, every hour, between certain hours - using time-based trigger? Thus run script Monday to Friday from 8am to 5pm, every hour.

Petrus
  • 309
  • 4
  • 12
  • quite same as this one https://stackoverflow.com/questions/68482786/how-to-automate-google-sheet-importhtml-trigger-from-11-3-every-day-except-frida/68484149#68484149 – Mike Steelson Jul 24 '21 at 13:06

2 Answers2

1

Just add a condition that stops execution if the day is Saturday or Sunday OR the hour is before 8 or after 17.

function myFunction () {
  var today = new Date();
  var day = today.getDay();
  
  var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
  
  if(day == 6 || day == 0) {
    message.innerHTML = 'Today is ' + days[day] + '. So I\'ll do nothing'
    return;
  } else {
    var hour = today.getHours();
    var minute = today.getMinutes();
    if(hour < 8 || hour > 17) {
      message.innerHTML = 'Today is ' + days[day] + ' but the time is ' + hour + ':' + minute + '. So I\'ll do nothing'
      return;
    }
   
    message.innerHTML = 'Today is ' + days[day] + ' and the time is ' + hour + ':' + minute + '. So I\'ll do something'
    
  }
}

myFunction()
<span id="message"></span>

With this implementation, you just need to create a trigger to run the function once per hour. The condition in the if statements will make sure that any code written below that will not run if it doesn't meet your runtime criteria.

ale13
  • 5,679
  • 3
  • 10
  • 25
Muhammet Yunus
  • 541
  • 4
  • 14
  • Thanks, and to add every hour? Then it can work with triggers to trigger script to run? – Petrus Jul 24 '21 at 10:25
  • 2
    With this implementation, you just need to create a trigger to run the function once per hour. The condition in the if statements will make sure that any code written below that will not run if it doesn't meet your runtime criteria. – Lle.4 Jul 24 '21 at 12:44
  • 1
    Work great, combined with the trigger per hour. Thanks! – Petrus Jul 26 '21 at 06:29
0

This is what I was trying but not working...

 function setWeekdaysTrigger() {

  var startHour = 8;
  var endHour   = 17;
  var daysWeek  = []; //put Enums for days of the week here;

  //set trigger to specific week days;
  daysWeek.forEach(function(day){
    //set trigger for 9AM to 13PM;
    for(var h=startHour; h<endHour; h++) {
      var tg = ScriptApp.newTrigger('TheScriptToRun').timeBased();
          tg.onWeekDay(day);
          tg.atHour(h).nearMinute(1).create(); //arbitrary precision (minutes);
    }        
  });

}
Petrus
  • 309
  • 4
  • 12
  • See @AmitAgarwal's [answer](https://stackoverflow.com/a/19230690/12968627) please. But you'd like to add hour timers from 8 to 17, this means 10 trigger/day. Too many triggers. It's best if you add a single hour timer and put some conditions to the beginning of your function in my opinion. – Muhammet Yunus Jul 24 '21 at 10:57
  • Sure, but needs to execute every hour between 8am to 5pm, weekdays only... – Petrus Jul 24 '21 at 12:12
  • In this case, you should add 50 triggers, but Google allows a maximum of 20 triggers per script. – Muhammet Yunus Jul 24 '21 at 12:22