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

Overwrite parent calendar name #394

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 23 additions & 9 deletions Code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* 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
* 2) Settings: Change lines 24-50 to be the settings that you want to use
* 2) Settings: Change lines 24-67 to be the settings that you want to use
* 3) Install:
* New Interface: Make sure your toolbar says "install" to the right of "Debug", then click "Run"
* Old Interface: Click "Run" > "Run function" > "install"
Expand All @@ -21,14 +21,28 @@
*=========================================
*/

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,
// for instance: ["https://p24-calendars.icloud.com/holidays/us_en.ics", "US Holidays", "11"]
["icsUrl1", "targetCalendar1"],
["icsUrl2", "targetCalendar2"],
["icsUrl3", "targetCalendar1"]

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 additional Options:
// - colorId: Colors of the Events with the following mapping https://developers.google.com/apps-script/reference/calendar/event-color
// - calendarName: Alternative name for the parent calendar
// for instance:
// ["https://p24-calendars.icloud.com/holidays/us_en.ics", "US Holidays", {
// colorId: "1",
// calendarName: 'Parent Calendar Name'
// }]

["icsUrl1", "targetCalendar1", {
colorId: "1",
calendarName: 'Parent Calendar Name 1'
}],
["icsUrl2", "targetCalendar2", {
calendarName: 'Parent Calendar Name 2'
Comment on lines +39 to +43
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use proper ical property names here:
colorId -> color
calendarName -> name

https://www.rfc-editor.org/rfc/rfc5545
https://www.rfc-editor.org/rfc/rfc7986

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to change 2x parentCal to name at line 460++

}],
["icsUrl3", "targetCalendar3"],
];

var howFrequent = 15; // What interval (minutes) to run this script on to check for new events. Any integer can be used, but will be rounded up to 5, 10, 15, 30 or to the nearest hour after that.. 60, 120, etc. 1440 (24 hours) is the maximum value. Anything above that will be replaced with 1440.
Expand Down
9 changes: 5 additions & 4 deletions Helpers.gs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ function parseResponses(responses){
var result = [];
for (var itm of responses){
var resp = itm[0];
var colorId = itm[1];
var jcalData = ICAL.parse(resp);
var component = new ICAL.Component(jcalData);

Expand All @@ -215,13 +214,15 @@ function parseResponses(responses){
ICAL.TimezoneService.register(tz);
}

var colorId = itm[1] && itm[1].colorId
var allEvents = component.getAllSubcomponents("vevent");
if (colorId != undefined)
allEvents.forEach(function(event){event.addPropertyWithValue("color", colorId);});

var calName = component.getFirstPropertyValue("x-wr-calname") || component.getFirstPropertyValue("name");
if (calName != null)
allEvents.forEach(function(event){event.addPropertyWithValue("parentCal", calName); });
var calName = (itm[1] && itm[1].calendarName) || component.getFirstPropertyValue("x-wr-calname") || component.getFirstPropertyValue("name");
if (calName != null) {
component.getAllSubcomponents("vevent").forEach(function(event){event.addPropertyWithValue("parentCal", calName); });
}
Comment on lines +217 to +225
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In preparation of future changes, let's make this less 'hardcoded'.
I'd suggest

let overrides = itm[1];
let allEvents = component.getAllSubcomponents("vevent");
let calName = component.getFirstPropertyValue("x-wr-calname") || component.getFirstPropertyValue("name");
if (calName != null)
  allEvents.forEach(function(event){event.addPropertyWithValue("name", calName); });

if (overrides){
  Logger.log(`Applying ${Object.keys(overrides).length} override(s)`);
  allEvents.forEach(function(event){
    for(let overrideProp in overrides){
      event.updatePropertyWithValue(overrideProp, overrides[overrideProp]);
    }
  });
}


result = [].concat(allEvents, result);
}
Expand Down