Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
Adding some color to the events (#243)
Browse files Browse the repository at this point in the history
* Supporting event colors

* Going for the initial examples

* Simplifying code

* Removing color from array example
  • Loading branch information
leog authored and pull[bot] committed Dec 14, 2021
1 parent 833bb78 commit 9fc2924
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
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 @@ -152,16 +154,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 @@ -201,7 +204,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] ]);
}

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 @@ -201,10 +202,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 @@ -254,9 +256,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 @@ -456,6 +459,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

0 comments on commit 9fc2924

Please sign in to comment.