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

Adding some color to the events #243

Merged
merged 4 commits into from
Dec 14, 2021
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
9 changes: 6 additions & 3 deletions Code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

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://www.calendarlabs.com/ical-calendar/ics/76/US_Holidays.ics", "US Holidays"]
// Or with colors following mapping https://developers.google.com/apps-script/reference/calendar/event-color,
// for instance: ["https://www.calendarlabs.com/ical-calendar/ics/76/US_Holidays.ics", "US Holidays", "11"]
["icsUrl1", "targetCalendar1"],
["icsUrl2", "targetCalendar2"],
["icsUrl3", "targetCalendar1"]
Expand Down Expand Up @@ -151,16 +153,17 @@ function startSync(){

targetCalendarName = calendar[0];
var sourceCalendarURLs = calendar[1];
var colorId = calendar[2];
var vevents;

//------------------------ Fetch URL items ------------------------
var responses = fetchSourceCalendars(sourceCalendarURLs);
Logger.log("Syncing " + responses.length + " calendars to " + targetCalendarName);

//------------------------ Get target calendar information------------------------
var targetCalendar = setupTargetCalendar(targetCalendarName);
var targetCalendar = setupTargetCalendar(targetCalendarName, colorId);
targetCalendarId = targetCalendar.id;
Logger.log("Working on calendar: " + targetCalendarId);
Logger.log("Working on calendar: " + targetCalendarId + colorId !== undefined ? " with colorId: " + colorId : "");

//------------------------ Parse existing events --------------------------
if(addEventsToCalendar || modifyExistingEvents || removeEventsFromCalendar){
Expand Down Expand Up @@ -200,7 +203,7 @@ function startSync(){
}, defaultMaxRetries);

vevents.forEach(function(e){
processEvent(e, calendarTz);
processEvent(e, calendarTz, colorId);
});

Logger.log("Done processing events");
Expand Down
15 changes: 10 additions & 5 deletions Helpers.gs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function condenseCalendarMap(calendarMap){
if (index > -1)
result[index][1].push(mapping[0]);
else
result.push([ mapping[1], [ mapping[0] ] ]);
result.push([ mapping[1], [ mapping[0] ], mapping[2] ]);
derekantrican marked this conversation as resolved.
Show resolved Hide resolved
}

return result;
Expand Down Expand Up @@ -106,7 +106,7 @@ function fetchSourceCalendars(sourceCalendarURLs){
* @param {string} targetCalendarName - The name of the calendar to return
* @return {Calendar} The calendar retrieved or created
*/
function setupTargetCalendar(targetCalendarName){
function setupTargetCalendar(targetCalendarName, colorId){
var targetCalendar = Calendar.CalendarList.list({showHidden: true}).items.filter(function(cal) {
return ((cal.summaryOverride || cal.summary) == targetCalendarName) &&
(cal.accessRole == "owner" || cal.accessRole == "writer");
Expand All @@ -117,6 +117,7 @@ function setupTargetCalendar(targetCalendarName){
targetCalendar = Calendar.newCalendar();
targetCalendar.summary = targetCalendarName;
targetCalendar.description = "Created by GAS";
targetCalendar.colorId = colorId;
targetCalendar.timeZone = Calendar.Settings.get("timezone").value;
targetCalendar = Calendar.Calendars.insert(targetCalendar);
}
Expand Down Expand Up @@ -191,10 +192,11 @@ function parseResponses(responses){
*
* @param {ICAL.Component} event - The event to process
* @param {string} calendarTz - The timezone of the target calendar
* @param {string} colorId - The colorId for the target calendar
*/
function processEvent(event, calendarTz){
function processEvent(event, calendarTz, colorId){
//------------------------ Create the event object ------------------------
var newEvent = createEvent(event, calendarTz);
var newEvent = createEvent(event, calendarTz, colorId);
if (newEvent == null)
return;

Expand Down Expand Up @@ -244,9 +246,10 @@ function processEvent(event, calendarTz){
*
* @param {ICAL.Component} event - The event to process
* @param {string} calendarTz - The timezone of the target calendar
* @param {string} colorId - The color id for the target calendar events
* @return {?Calendar.Event} The Calendar.Event that will be added to the target calendar
*/
function createEvent(event, calendarTz){
function createEvent(event, calendarTz, colorId){
event.removeProperty('dtstamp');
var icalEvent = new ICAL.Event(event, {strictExceptions: true});
if (onlyFutureEvents && checkSkipEvent(event, icalEvent)){
Expand Down Expand Up @@ -446,6 +449,8 @@ function createEvent(event, calendarTz){
newEvent.recurringEventId = recID.convertToZone(ICAL.TimezoneService.get('UTC')).toString();
newEvent.extendedProperties.private['rec-id'] = newEvent.extendedProperties.private['id'] + "_" + newEvent.recurringEventId;
}

newEvent.colorId = colorId;

return newEvent;
}
Expand Down