Skip to content

Commit

Permalink
Trigger flexibility (#358)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lonestarjeepin authored Dec 13, 2023
1 parent f88b3d0 commit 3a3f1f1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
28 changes: 21 additions & 7 deletions Code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(){
Expand Down
19 changes: 12 additions & 7 deletions Helpers.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down

0 comments on commit 3a3f1f1

Please sign in to comment.