Created
July 27, 2016 07:43
-
-
Save KainokiKaede/583d364fa99b9fafdcaf5e9287047ca7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From Resources > Advanced Google Services, enable Calendar API, | |
// and follow the link "Google Developers Console" and enable Calendar API there, too. | |
// | |
// Original: https://github.com/kdotsaito/GAS/blob/master/calender_merge.gs | |
// Calendar ID to concatenate to. | |
var outCal_id = "@group.calendar.google.com"; | |
// Calendar IDs to concatenate. | |
var inCal_ids = [ | |
"@group.calendar.google.com", | |
"@group.calendar.google.com", | |
"@group.calendar.google.com", | |
"@group.calendar.google.com" ]; | |
function main(){ | |
var months = 4; | |
var date_start = new Date(); | |
var date_end = new Date; | |
date_end.setTime(date_start.getTime() + months*30*24*60*60*1000); | |
Logger.log("Retrieving output calendar data."); | |
var outCal = CalendarApp.getCalendarById(outCal_id); | |
var old_Future_Ev = [] | |
old_Future_Ev = old_Future_Ev.concat(CalendarApp.getCalendarById(outCal_id).getEvents(date_start,date_end)); | |
Logger.log("Retrieving input calendar data."); | |
var new_Future_Ev = []; | |
var calendar_title = []; | |
for(var i = 0; i < inCal_ids.length; i++){ | |
var cal_name = CalendarApp.getCalendarById(inCal_ids[i]).getName() | |
var events_to_add = CalendarApp.getCalendarById(inCal_ids[i]).getEvents(date_start,date_end); | |
new_Future_Ev = new_Future_Ev.concat(events_to_add); | |
for(var j = 0; j < events_to_add.length; j++){ | |
calendar_title.push(cal_name); | |
} | |
} | |
var remove_flag = []; | |
for(var j = 0; j < old_Future_Ev.length; j++){ remove_flag[j] = true; } // init | |
var add_flag = []; | |
for(var i = 0; i < new_Future_Ev.length; i++){ add_flag[i] = true; } // init | |
Logger.log("Looking for new/updated events."); | |
for(var i = 0; i < new_Future_Ev.length; i++){ | |
for(var j = 0; j < old_Future_Ev.length; j++){ | |
if( isSameEvent(calendar_title[i], new_Future_Ev[i], old_Future_Ev[j]) ){ | |
remove_flag[j] = false; | |
add_flag[i] = false; | |
break; | |
} | |
} | |
} | |
for(var j = 0; j < old_Future_Ev.length; j++){ | |
if(remove_flag[j] == true){ | |
Utilities.sleep(500); // Avoid deleting events too quickly. | |
Logger.log("Deleting event: " + old_Future_Ev[j].getTitle()); | |
old_Future_Ev[j].deleteEvent(); | |
} | |
} | |
for(var i = 0; i < new_Future_Ev.length; i++){ | |
if(add_flag[i] == true){ | |
Utilities.sleep(500); // Avoid adding events too quickly. | |
Logger.log("Adding event: "+ new_Future_Ev[i].getTitle()); | |
if(new_Future_Ev[i].isAllDayEvent()){ | |
createAllDaysEvent(createTitle(calendar_title[i], new_Future_Ev[i].getTitle()), | |
new_Future_Ev[i].getAllDayStartDate(), | |
new_Future_Ev[i].getAllDayEndDate(), | |
new_Future_Ev[i].getDescription(), | |
new_Future_Ev[i].getLocation()); | |
}else{ | |
outCal.createEvent(createTitle(calendar_title[i], new_Future_Ev[i].getTitle()), | |
new_Future_Ev[i].getStartTime(), | |
new_Future_Ev[i].getEndTime(), | |
{description:new_Future_Ev[i].getDescription(), | |
location:new_Future_Ev[i].getLocation()}); | |
} | |
} | |
} | |
} | |
function createAllDaysEvent(summary, start, end, description, location) { | |
var calendarId = outCal_id; | |
var event = { | |
summary: summary, | |
location: location, | |
description: description, | |
start: { | |
date: add9Hours(start).toISOString().substring(0, 10) | |
}, | |
end: { | |
date: add9Hours(end).toISOString().substring(0, 10) | |
} | |
}; | |
event = Calendar.Events.insert(event, calendarId); | |
} | |
function add9Hours(d){return new Date(d.valueOf()+9*60*60*1000)} | |
function createTitle(calendar_title, event_title){ | |
return "<" + calendar_title + ">" + event_title; | |
} | |
function isSameEvent(event1_calendar_title, event1, event2){ | |
if (! (createTitle(event1_calendar_title, event1.getTitle()) === event2.getTitle()) ) {return false;} | |
else if (! (event1.getStartTime().getTime() === event2.getStartTime().getTime()) ) {return false;} | |
else if (! (event1.getEndTime().getTime() === event2.getEndTime().getTime()) ) {return false;} | |
else if (! (event1.getDescription === event2.getDescription) ) {return false;} | |
else if (! (event1.getLocation() === event2.getLocation()) ) {return false;} | |
else {return true;} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment