Skip to content

Commit e5cd8e0

Browse files
authored
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
1 parent 514c294 commit e5cd8e0

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

Code.gs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*=========================================
33
* INSTALLATION INSTRUCTIONS
44
*=========================================
5-
*
5+
*
66
* 1) Make a copy:
77
* 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)
88
* Old Interface: Click in the menu "File" > "Make a copy..." and make a copy to your Google Drive
@@ -23,31 +23,32 @@
2323

2424
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)
2525
// For instance: ["https://p24-calendars.icloud.com/holidays/us_en.ics", "US Holidays"]
26-
// Or with colors following mapping https://developers.google.com/apps-script/reference/calendar/event-color,
26+
// Or with colors following mapping https://developers.google.com/apps-script/reference/calendar/event-color,
2727
// for instance: ["https://p24-calendars.icloud.com/holidays/us_en.ics", "US Holidays", "11"]
2828
["icsUrl1", "targetCalendar1"],
2929
["icsUrl2", "targetCalendar2"],
3030
["icsUrl3", "targetCalendar1"]
31-
31+
3232
];
3333

34-
var howFrequent = 15; // What interval (minutes) to run this script on to check for new events
35-
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)
36-
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
37-
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
38-
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.
39-
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").
40-
var addOrganizerToTitle = false; // Whether to prefix the event name with the event organiser for further clarity
41-
var descriptionAsTitles = false; // Whether to use the ics/ical descriptions as titles (true) or to use the normal titles as titles (false)
42-
var addCalToTitle = false; // Whether to add the source calendar to title
43-
var addAttendees = false; // Whether to add the attendee list. If true, duplicate events will be automatically added to the attendees' calendar.
44-
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)
45-
// See https://github.com/derekantrican/GAS-ICS-Sync/issues/75 for why this is neccessary.
46-
var overrideVisibility = ""; // Changes the visibility of the event ("default", "public", "private", "confidential"). Anything else will revert to the class value of the ICAL event.
34+
var howFrequent = 15; // What interval (minutes) to run this script on to check for new events
35+
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)
36+
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
37+
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
38+
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.
39+
var removePastEventsFromCalendar = true; // If you turn this to "false", any event that is in the past will not be removed.
40+
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").
41+
var addOrganizerToTitle = false; // Whether to prefix the event name with the event organiser for further clarity
42+
var descriptionAsTitles = false; // Whether to use the ics/ical descriptions as titles (true) or to use the normal titles as titles (false)
43+
var addCalToTitle = false; // Whether to add the source calendar to title
44+
var addAttendees = false; // Whether to add the attendee list. If true, duplicate events will be automatically added to the attendees' calendar.
45+
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)
46+
// See https://github.com/derekantrican/GAS-ICS-Sync/issues/75 for why this is neccessary.
47+
var overrideVisibility = ""; // Changes the visibility of the event ("default", "public", "private", "confidential"). Anything else will revert to the class value of the ICAL event.
4748
var addTasks = false;
4849

49-
var emailSummary = false; // Will email you when an event is added/modified/removed to your calendar
50-
var email = ""; // OPTIONAL: If "emailSummary" is set to true or you want to receive update notifications, you will need to provide your email address
50+
var emailSummary = false; // Will email you when an event is added/modified/removed to your calendar
51+
var email = ""; // OPTIONAL: If "emailSummary" is set to true or you want to receive update notifications, you will need to provide your email address
5152

5253
/*
5354
*=========================================
@@ -103,7 +104,7 @@ function install(){
103104
//Schedule sync routine to explicitly repeat and schedule the initial sync
104105
ScriptApp.newTrigger("startSync").timeBased().everyMinutes(getValidTriggerFrequency(howFrequent)).create();
105106
ScriptApp.newTrigger("startSync").timeBased().after(1000).create();
106-
107+
107108
//Schedule sync routine to look for update once per day
108109
ScriptApp.newTrigger("checkForUpdate").timeBased().everyDays(1).create();
109110
}
@@ -133,15 +134,15 @@ function startSync(){
133134
Logger.log("Another iteration is currently running! Exiting...");
134135
return;
135136
}
136-
137+
137138
PropertiesService.getUserProperties().setProperty('LastRun', new Date().getTime());
138-
139+
139140
if (onlyFutureEvents)
140141
startUpdateTime = new ICAL.Time.fromJSDate(new Date());
141-
142-
//Disable email notification if no mail adress is provided
142+
143+
//Disable email notification if no mail adress is provided
143144
emailSummary = emailSummary && email != "";
144-
145+
145146
sourceCalendars = condenseCalendarMap(sourceCalendars);
146147
for (var calendar of sourceCalendars){
147148
//------------------------ Reset globals ------------------------
@@ -158,12 +159,12 @@ function startSync(){
158159
//------------------------ Fetch URL items ------------------------
159160
var responses = fetchSourceCalendars(sourceCalendarURLs);
160161
Logger.log("Syncing " + responses.length + " calendars to " + targetCalendarName);
161-
162+
162163
//------------------------ Get target calendar information------------------------
163164
var targetCalendar = setupTargetCalendar(targetCalendarName);
164165
targetCalendarId = targetCalendar.id;
165166
Logger.log("Working on calendar: " + targetCalendarId);
166-
167+
167168
//------------------------ Parse existing events --------------------------
168169
if(addEventsToCalendar || modifyExistingEvents || removeEventsFromCalendar){
169170
var eventList =
@@ -192,22 +193,22 @@ function startSync(){
192193
vevents = parseResponses(responses, icsEventsIds);
193194
Logger.log("Parsed " + vevents.length + " events from ical sources");
194195
}
195-
196+
196197
//------------------------ Process ical events ------------------------
197198
if (addEventsToCalendar || modifyExistingEvents){
198199
Logger.log("Processing " + vevents.length + " events");
199200
var calendarTz =
200201
callWithBackoff(function(){
201202
return Calendar.Settings.get("timezone").value;
202203
}, defaultMaxRetries);
203-
204+
204205
vevents.forEach(function(e){
205206
processEvent(e, calendarTz);
206207
});
207208

208209
Logger.log("Done processing events");
209210
}
210-
211+
211212
//------------------------ Remove old events from calendar ------------------------
212213
if(removeEventsFromCalendar){
213214
Logger.log("Checking " + calendarEvents.length + " events for removal");

Helpers.gs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,15 @@ function processEventCleanup(){
651651
var currentID = calendarEventsIds[i];
652652
var feedIndex = icsEventsIds.indexOf(currentID);
653653

654-
if(feedIndex == -1 && calendarEvents[i].recurringEventId == null){
654+
if(feedIndex == -1 // Event is no longer in source
655+
&& calendarEvents[i].recurringEventId == null // And it's not a recurring event
656+
&& ( // And one of:
657+
removePastEventsFromCalendar // We want to remove past events
658+
|| new Date(calendarEvents[i].start.dateTime) > new Date() // Or the event is in the future
659+
|| new Date(calendarEvents[i].start.date) > new Date() // (2 different ways event start can be stored)
660+
)
661+
)
662+
{
655663
Logger.log("Deleting old event " + currentID);
656664
callWithBackoff(function(){
657665
Calendar.Events.remove(targetCalendarId, calendarEvents[i].id);

0 commit comments

Comments
 (0)