From e5cd8e02417ee38a04eadbd795201822dbac2e2d Mon Sep 17 00:00:00 2001 From: Alice Jacka Date: Sat, 25 Mar 2023 02:56:04 +1100 Subject: [PATCH] add option to not delete past events (#317) * add option to not delete past events #117 * Remove unnessacary variable passed to processEventCleanup * Fix date comparison * add check of start.date * Make ln 644 easier to understand --- Code.gs | 59 +++++++++++++++++++++++++++--------------------------- Helpers.gs | 10 ++++++++- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/Code.gs b/Code.gs index 44e4981..2ce91c3 100644 --- a/Code.gs +++ b/Code.gs @@ -2,7 +2,7 @@ *========================================= * INSTALLATION INSTRUCTIONS *========================================= -* +* * 1) Make a copy: * New Interface: Go to the project overview icon on the left (looks like this: ⓘ), then click the "copy" icon on the top right (looks like two files on top of each other) * Old Interface: Click in the menu "File" > "Make a copy..." and make a copy to your Google Drive @@ -23,31 +23,32 @@ var sourceCalendars = [ // The ics/ical urls that you want to get events from along with their target calendars (list a new row for each mapping of ICS url to Google Calendar) // For instance: ["https://p24-calendars.icloud.com/holidays/us_en.ics", "US Holidays"] - // Or with colors following mapping https://developers.google.com/apps-script/reference/calendar/event-color, + // Or with colors following mapping https://developers.google.com/apps-script/reference/calendar/event-color, // for instance: ["https://p24-calendars.icloud.com/holidays/us_en.ics", "US Holidays", "11"] ["icsUrl1", "targetCalendar1"], ["icsUrl2", "targetCalendar2"], ["icsUrl3", "targetCalendar1"] - + ]; -var howFrequent = 15; // What interval (minutes) to run this script on to check for new events -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 -var removeEventsFromCalendar = true; // If you turn this to "true", any event created by the script that is not found in the feed will be removed. -var addAlerts = "yes"; // Whether to add the ics/ical alerts as notifications on the Google Calendar events or revert to the calendar's default reminders ("yes", "no", "default"). -var addOrganizerToTitle = false; // Whether to prefix the event name with the event organiser for further clarity -var descriptionAsTitles = false; // Whether to use the ics/ical descriptions as titles (true) or to use the normal titles as titles (false) -var addCalToTitle = false; // Whether to add the source calendar to title -var addAttendees = false; // Whether to add the attendee list. If true, duplicate events will be automatically added to the attendees' calendar. -var defaultAllDayReminder = -1; // Default reminder for all day events in minutes before the day of the event (-1 = no reminder, the value has to be between 0 and 40320) - // See https://github.com/derekantrican/GAS-ICS-Sync/issues/75 for why this is neccessary. -var overrideVisibility = ""; // Changes the visibility of the event ("default", "public", "private", "confidential"). Anything else will revert to the class value of the ICAL event. +var howFrequent = 15; // What interval (minutes) to run this script on to check for new events +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 +var removeEventsFromCalendar = true; // If you turn this to "true", any event created by the script that is not found in the feed will be removed. +var removePastEventsFromCalendar = true; // If you turn this to "false", any event that is in the past will not be removed. +var addAlerts = "yes"; // Whether to add the ics/ical alerts as notifications on the Google Calendar events or revert to the calendar's default reminders ("yes", "no", "default"). +var addOrganizerToTitle = false; // Whether to prefix the event name with the event organiser for further clarity +var descriptionAsTitles = false; // Whether to use the ics/ical descriptions as titles (true) or to use the normal titles as titles (false) +var addCalToTitle = false; // Whether to add the source calendar to title +var addAttendees = false; // Whether to add the attendee list. If true, duplicate events will be automatically added to the attendees' calendar. +var defaultAllDayReminder = -1; // Default reminder for all day events in minutes before the day of the event (-1 = no reminder, the value has to be between 0 and 40320) + // See https://github.com/derekantrican/GAS-ICS-Sync/issues/75 for why this is neccessary. +var overrideVisibility = ""; // Changes the visibility of the event ("default", "public", "private", "confidential"). Anything else will revert to the class value of the ICAL event. var addTasks = false; -var emailSummary = false; // Will email you when an event is added/modified/removed to your calendar -var email = ""; // OPTIONAL: If "emailSummary" is set to true or you want to receive update notifications, you will need to provide your email address +var emailSummary = false; // Will email you when an event is added/modified/removed to your calendar +var email = ""; // OPTIONAL: If "emailSummary" is set to true or you want to receive update notifications, you will need to provide your email address /* *========================================= @@ -103,7 +104,7 @@ function install(){ //Schedule sync routine to explicitly repeat and schedule the initial sync ScriptApp.newTrigger("startSync").timeBased().everyMinutes(getValidTriggerFrequency(howFrequent)).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(); } @@ -133,15 +134,15 @@ function startSync(){ Logger.log("Another iteration is currently running! Exiting..."); return; } - + PropertiesService.getUserProperties().setProperty('LastRun', new Date().getTime()); - + if (onlyFutureEvents) startUpdateTime = new ICAL.Time.fromJSDate(new Date()); - - //Disable email notification if no mail adress is provided + + //Disable email notification if no mail adress is provided emailSummary = emailSummary && email != ""; - + sourceCalendars = condenseCalendarMap(sourceCalendars); for (var calendar of sourceCalendars){ //------------------------ Reset globals ------------------------ @@ -158,12 +159,12 @@ function startSync(){ //------------------------ Fetch URL items ------------------------ var responses = fetchSourceCalendars(sourceCalendarURLs); Logger.log("Syncing " + responses.length + " calendars to " + targetCalendarName); - + //------------------------ Get target calendar information------------------------ var targetCalendar = setupTargetCalendar(targetCalendarName); targetCalendarId = targetCalendar.id; Logger.log("Working on calendar: " + targetCalendarId); - + //------------------------ Parse existing events -------------------------- if(addEventsToCalendar || modifyExistingEvents || removeEventsFromCalendar){ var eventList = @@ -192,7 +193,7 @@ function startSync(){ vevents = parseResponses(responses, icsEventsIds); Logger.log("Parsed " + vevents.length + " events from ical sources"); } - + //------------------------ Process ical events ------------------------ if (addEventsToCalendar || modifyExistingEvents){ Logger.log("Processing " + vevents.length + " events"); @@ -200,14 +201,14 @@ function startSync(){ callWithBackoff(function(){ return Calendar.Settings.get("timezone").value; }, defaultMaxRetries); - + vevents.forEach(function(e){ processEvent(e, calendarTz); }); Logger.log("Done processing events"); } - + //------------------------ Remove old events from calendar ------------------------ if(removeEventsFromCalendar){ Logger.log("Checking " + calendarEvents.length + " events for removal"); diff --git a/Helpers.gs b/Helpers.gs index fcc5119..4cfe250 100644 --- a/Helpers.gs +++ b/Helpers.gs @@ -651,7 +651,15 @@ function processEventCleanup(){ var currentID = calendarEventsIds[i]; var feedIndex = icsEventsIds.indexOf(currentID); - if(feedIndex == -1 && calendarEvents[i].recurringEventId == null){ + if(feedIndex == -1 // Event is no longer in source + && calendarEvents[i].recurringEventId == null // And it's not a recurring event + && ( // And one of: + removePastEventsFromCalendar // We want to remove past events + || new Date(calendarEvents[i].start.dateTime) > new Date() // Or the event is in the future + || new Date(calendarEvents[i].start.date) > new Date() // (2 different ways event start can be stored) + ) + ) + { Logger.log("Deleting old event " + currentID); callWithBackoff(function(){ Calendar.Events.remove(targetCalendarId, calendarEvents[i].id);