Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to not delete past events #317

Merged
merged 6 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions Code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

/*
*=========================================
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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 ------------------------
Expand All @@ -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 =
Expand Down Expand Up @@ -192,22 +193,22 @@ 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");
var calendarTz =
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");
Expand Down
10 changes: 9 additions & 1 deletion Helpers.gs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,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);
Expand Down