r/GoogleAppsScript • • 7d ago

Unresolved Google Apps Script trigger frequency setting

Google Apps Script trigger frequency setting: Currently, I have hourly triggers set up for 15 functions, but running all of them every hour seems excessive, even though the 90-minute daily quota is still sufficient.

I'm wondering if there's a way to schedule them to run every two hours, specifically at even-numbered hours (e.g., 8 AM, 10 AM, noon, etc.), so the schedule won't be affected by daylight saving time changes.

Alternatively, I could keep hourly triggers but limit them to between 6 PM and midnight, since there's no need for these functions to run during the rest of the day.

I mean setting only, not the script to check hour.

Edit: I follow the comment and created a master trigger. Now I have new question https://www.reddit.com/r/GoogleAppsScript/comments/1xzjvlk/how_to_call_a_function_from_another_project_when/

0 Upvotes

8 comments sorted by

1

u/Borsaid 7d ago

I don't think there's a way to do that in the scheduler. But, you can have a script create a trigger. ie at script end, schedule a new run 2 hours from now.

1

u/VAer1 7d ago

Thanks! I think I’ll keep the hourly schedule for them for now. I’ll look into ways to save time if I need to create more functions for time-driven triggers.

1

u/DrMorris 7d ago

As others said, you could have one function which triggers the rest,, incide that function you can define your rules ie if time is x run a b and c etc. And then just have that trigger set to run every 15 min. You can also set down time period like this so from say 6pm to 7am it triggers, detects the time and end withiut calling anything else.

1

u/VAer1 7d ago

Thanks. Do they have to be in the same project?

1

u/DrMorris 7d ago

You can technically trigger scripts from other projects in one project using deployed web hook. But that's another level of complexity so it depends on your knowledge. You can basically add a doGet function in each project that triggers the scripts in it, and then deploy it and use the deployment address ti gtrigfer it from say Master Trigger script.

Only risk there is the query limit for urlFetch calls.

1

u/AdministrativeGift15 7d ago

You could create daily triggers, with each timed trigger running all 15 functions. Create one daily timed trigger for each hour that you want it to run.

1

u/No-Sheepherder7116 7d ago

Yes, but the two options in Apps Script do not do exactly the same thing, and that is the key point here.

1. Every 2 hours in the trigger settings

In the editor, go to Triggers > Add Trigger, choose Time-driven, then Hour timer. The interval list includes Every 2 hours, as well as 4, 6, 8 and 12 hours. The code equivalent is:

ScriptApp.newTrigger('myFunction')
  .timeBased()
  .everyHours(2)
  .create();

That will halve your runs, but it does not let you choose 8 AM, 10 AM, noon, and so on. It is interval-based: the first run happens within two hours of when you create the trigger, then roughly every two hours after that, and the exact minute is not guaranteed. So creating it at 8 AM gets you roughly even-hour runs, but it is not a promise, and it can shift around a daylight saving change.

2. Exact even hours, DST-proof: daily triggers at set hours

For exact clock times, create a daily trigger for each hour you want:

ScriptApp.newTrigger('myFunction')
  .timeBased()
  .atHour(8)
  .everyDays(1)
  .create();

Repeat that for 10, 12, and so on. These use the script timezone in Project Settings, so they stay on wall-clock time through a daylight saving change - 8 AM stays 8 AM.

The catch is the limit: 20 triggers per user, per script. With 15 separate functions, one daily trigger per function per hour would be far over that limit in a single project - 15 functions x 4 hours is 60 triggers.

So the practical way to do what you asked, without any hour-checking code in the functions themselves, is to consolidate. Put one wrapper function in the project that just calls the 15 functions, then give that wrapper a few daily triggers, for example at 18, 20 and 22 if you want the 6 PM to midnight window. Add 0 to the list if you also want a run at midnight:

function runAllEvenHours() {
  functionOne();
  functionTwo();
  // ... the other 13 functions
}

function setupEvenHourSchedule() {
  ScriptApp.getProjectTriggers().forEach(function(trigger) {
    if (trigger.getHandlerFunction() === 'runAllEvenHours') {
      ScriptApp.deleteTrigger(trigger);
    }
  });

  [18, 20, 22].forEach(function(hour) {
    ScriptApp.newTrigger('runAllEvenHours')
      .timeBased()
      .atHour(hour)
      .everyDays(1)
      .create();
  });
}

Run setupEvenHourSchedule() once. The delete step at the top just stops you creating duplicate triggers if you run the setup again. Also delete the old hourly triggers for those 15 functions, or they will keep running every hour alongside the new schedule.

On your last question: yes, they have to be in the same project for this to work directly. A trigger can only call a function in its own Apps Script project. If your functions are spread across projects, each project needs its own triggers, and each project gets its own 20-trigger limit.

So in short: if roughly every two hours is good enough, use Every 2 hours in the trigger settings. If the exact even hours matter and you want them DST-proof, use the wrapper plus daily atHour triggers.