From 3a3f1f1e34e3f80705db260093e8c4c8222e1615 Mon Sep 17 00:00:00 2001 From: Lonestarjeepin <54453237+Lonestarjeepin@users.noreply.github.com> Date: Wed, 13 Dec 2023 10:18:08 -0600 Subject: [PATCH] Trigger flexibility (#358) Adding more flexibility to script trigger to allow for 5, 10, 15, 30, but also any hour from 1-24 with 24 as max trigger. --- Code.gs | 28 +++++++++++++++++++++------- Helpers.gs | 19 ++++++++++++------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/Code.gs b/Code.gs index 2ce91c3..d0c0ae9 100644 --- a/Code.gs +++ b/Code.gs @@ -31,7 +31,7 @@ var sourceCalendars = [ // The ics/ical urls that you want to get ]; -var howFrequent = 15; // What interval (minutes) to run this script on to check for new events +var howFrequent = 15; // What interval (minutes) to run this script on to check for new events. Any integer can be used, but will be rounded up to 5, 10, 15, 30 or to the nearest hour after that.. 60, 120, etc. 1440 (24 hours) is the maximum value. Anything above that will be replaced with 1440. var onlyFutureEvents = false; // If you turn this to "true", past events will not be synced (this will also removed past events from the target calendar if removeEventsFromCalendar is true) var addEventsToCalendar = true; // If you turn this to "false", you can check the log (View > Logs) to make sure your events are being read correctly before turning this on var modifyExistingEvents = true; // If you turn this to "false", any event in the feed that was modified after being added to the calendar will not update @@ -97,16 +97,30 @@ var email = ""; // OPTIONAL: If "emailSummary" is set var defaultMaxRetries = 10; // Maximum number of retries for api functions (with exponential backoff) -function install(){ - //Delete any already existing triggers so we don't create excessive triggers +function install() { + // Delete any already existing triggers so we don't create excessive triggers deleteAllTriggers(); - //Schedule sync routine to explicitly repeat and schedule the initial sync - ScriptApp.newTrigger("startSync").timeBased().everyMinutes(getValidTriggerFrequency(howFrequent)).create(); + // Schedule sync routine to explicitly repeat and schedule the initial sync + var adjustedMinutes = getValidTriggerFrequency(howFrequent); + if (adjustedMinutes >= 60) { + ScriptApp.newTrigger("startSync") + .timeBased() + .everyHours(adjustedMinutes / 60) + .create(); + } else { + ScriptApp.newTrigger("startSync") + .timeBased() + .everyMinutes(adjustedMinutes) + .create(); + } ScriptApp.newTrigger("startSync").timeBased().after(1000).create(); - //Schedule sync routine to look for update once per day - ScriptApp.newTrigger("checkForUpdate").timeBased().everyDays(1).create(); + // Schedule sync routine to look for update once per day using everyDays + ScriptApp.newTrigger("checkForUpdate") + .timeBased() + .everyDays(1) + .create(); } function uninstall(){ diff --git a/Helpers.gs b/Helpers.gs index 3cd5575..89fd36d 100644 --- a/Helpers.gs +++ b/Helpers.gs @@ -12,15 +12,20 @@ function getValidTriggerFrequency(origFrequency) { return 15; } - var adjFrequency = Math.round(origFrequency/5) * 5; // Set the number to be the closest divisible-by-5 - adjFrequency = Math.max(adjFrequency, 1); // Make sure the number is at least 1 (0 is not valid for the trigger) - adjFrequency = Math.min(adjFrequency, 15); // Make sure the number is at most 15 (will check for the 30 value below) + // Limit the original frequency to 1440 + origFrequency = Math.min(origFrequency, 1440); - if((adjFrequency == 15) && (Math.abs(origFrequency-30) < Math.abs(origFrequency-15))) - adjFrequency = 30; // If we adjusted to 15, but the original number is actually closer to 30, set it to 30 instead + var acceptableValues = [5, 10, 15, 30].concat( + Array.from({ length: 24 }, (_, i) => (i + 1) * 60) + ); // [5, 10, 15, 30, 60, 120, ..., 1440] - Logger.log("Intended frequency = "+origFrequency+", Adjusted frequency = "+adjFrequency); - return adjFrequency; + // Find the smallest acceptable value greater than or equal to the original frequency + var roundedUpValue = acceptableValues.find(value => value >= origFrequency); + + Logger.log( + "Intended frequency = " + origFrequency + ", Adjusted frequency = " + roundedUpValue + ); + return roundedUpValue; } String.prototype.includes = function(phrase){