Skip to content

Instantly share code, notes, and snippets.

@Rio517
Created July 26, 2016 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rio517/47e059cf7daa8f2bb7c1f8b4123c4c83 to your computer and use it in GitHub Desktop.
Save Rio517/47e059cf7daa8f2bb7c1f8b4123c4c83 to your computer and use it in GitHub Desktop.
This script will merge multiple calendars into a separate, isolated calendar.
// Google Calendar Merge Script
// This script will merge multiple calendars into a single calendar. Slightly updated from an
// earlier version: http://techingthetech.blogspot.co.uk/2015/04/merging-google-calendars.html
//
// Usage:
// 1. Grab the calendar name and IDs for your target calendar and the ones you want to merge
// 2. Go to Google Drive and create a new Google Apps Script (it will be under New > More).
// 3. Search for the "App Script" add it and allow it to work.
// 4. Create a new script and copy and past this code in there.
// 5. Add your IDs below for the target calender to import to and to import from.
// 6. From the Google Apps Script page go to Resources > Current Project's Triggers. Click the link
// to add a new trigger. Select "sync", "Time-driven", then the time period you want it to run.
//
// Hints/Notes:
// * You can use the play button the Google Apps Script page to run the app.
// * API documentation is here: https://developers.google.com/apps-script/reference/calendar/
// * Multiday events don't seem to be supported by the API. This script uses normal scheduled events
// to create multiday events, but this can lead to timezone issues. This script ignores those
// issues.
function sync() {
const importCalendarIds = [
// 'name': 'calendar_id',
['SuperSoho', "brh9v6sln9hhhhhhhiookgkgrp8@import.calendar.google.com"], //imported airbnb calendar
['CenterofSoho', "pq1u34v5rhhhhhvgsa3n2p@import.calendar.google.com"], //imported airbnb calendar
['Internal', "cal@webso.co"] //internal user calendar
];
const targetCalendarId = 'hostmaker.co_4tk5ffffff3bs8@group.calendar.google.com';
for (calendarIndex in importCalendarIds) {
var currentCalendar = CalendarApp.getCalendarById(importCalendarIds[calendarIndex][1]);
var today = new Date();
var enddate = new Date();
enddate.setDate(today.getDate() + 90); // only merges out N days, and only future events
var eventsToImport = currentCalendar.getEvents(today, enddate);
var targetCalendar = CalendarApp.getCalendarById(targetCalendarId);
var existingTargetEvents = targetCalendar.getEvents(today, enddate);
var stat = 1;
for (eventIndex in eventsToImport) {
stat = 1;
var currentEvent = eventsToImport[eventIndex];
var currentEventTitle = importCalendarIds[calendarIndex][0] + ': ' + currentEvent.getTitle();
// skip events that already exist in target calendar
for (existingIndex in existingTargetEvents) {
Logger.log(existingTargetEvents[existingIndex].getTitle());
Logger.log(currentEVentTitle);
if (
(existingTargetEvents[existingIndex].getStartTime().getTime() == currentEvent.getStartTime().getTime()) &&
(existingTargetEvents[existingIndex].getEndTime().getTime() == currentEvent.getEndTime().getTime()) &&
existingTargetEvents[existingIndex].getTitle() == currentEventTitle
)
{
stat = 0;
break;
}
}
if (stat == 0) continue;
if (currentEvent.isAllDayEvent() && currentEvent.getStartTime().getDate() == currentEvent.getEndTime().getDate()) {
var startTime = currentEvent.getStartTime();
startTime.setDate(startTime.getDate() + 2);
targetCalendar.createAllDayEvent(currentEventTitle, startTime, {
location: currentEvent.getLocation(),
description: currentEvent.getDescription()
});
} else {
targetCalendar.createEvent(currentEventTitle, currentEvent.getStartTime(), currentEvent.getEndTime(), {
location: currentEvent.getLocation(),
description: currentEvent.getDescription()
});
}
}
}
var calendars = new Array();
for (calendarIndex in importCalendarIds) {
calendars[calendars.length] = CalendarApp.getCalendarById(importCalendarIds[calendarIndex][1]).getEvents(today, enddate);
}
// Mario Note: The following seems to delete events we want created. Maybe issues w/ multi-day events?
// No more time to even look at this. Seems super redundant. Also `sleep(1000)` - takes ages.
// Not useful in our use case.
// checks each event in events against eventIndex to see if they are the same
// if eventIndex does not exist in events delete it
// will have to have existingTargetEvents rotate through the array of importCalendarIds to check all calendars
//
/*
for (eventIndex in existingTargetEvents) {
var count = 0;
Utilities.sleep(1000);
stat = 1;
for (eventList in calendars) {
for (ev in calendars[eventList]) {
count++;
if (
(existingTargetEvents[eventIndex].getStartTime().getTime() == calendars[eventList][ev].getStartTime().getTime()) &&
(existingTargetEvents[eventIndex].getEndTime().getTime() == calendars[eventList][ev].getEndTime().getTime()) &&
(existingTargetEvents[eventIndex].getTitle() == calendars[eventList][ev].getTitle()))
{
stat = 0;
}
}
}
if (stat == 1) {
existingTargetEvents[eventIndex].deleteEvent();
}
}
*/
}​
@undomalum
Copy link

hey! will it merge them in a continuous manner? or just once?

@Rio517
Copy link
Author

Rio517 commented Aug 13, 2020

I regret that I don't remember. It's been ages. This was used as a temporary hack at a small, now defunct company. Good luck. Wish I could be more helpful. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment