Google doesn't have a simple way to combine different calendars into a single view. So, you have to select which calendars you want to look at. This isn't a huge problem for small groups, or personal use, however, when you begin dealing with larger groups of people, all with individual calendars, it quickly becomes quite difficult to manage.
The first problem I ran across was that each newly created event gets its own ID. I am sure there is a dynamic way to access the events by ID, but I haven't figured it out yet.
Because each event, old and new, is unique, it becomes hard to actually tell which events have been added and which events have not been added.
The work around I decided to go with was to delete everything from the calendar, and update it from scratch. Using Google Scripts, you can easially schedule triggers for the events based on time. So, every night, at midnight, the script will automatically run, deleting the old calendar, and updating all the current events. Of course, the downside to this is that you won't be able to see when events have been updated in real time. But for now its good enough.
// Deletes all events in specified calendar
function deleteAllEvents() {
// Specify calendar by calendar ID
var calendar = CalendarApp.getCalendarById("YOUR_CALENDAR_ID_HERE@group.calendar.google.com");
// Set start time from which you want to clear events
var startTime = new Date("January 01, 2022");
// Set end time for end of clearing
var endTime = new Date("December 31, 2025");
// Loops through each event. Not Optimized
var allEvents = calendar.getEvents(startTime, endTime);
for ( i = 0; i < allEvents.length; i ++){
allEvents[i].deleteEvent();
// Because too many calls to the calendar will cause error if too many events
// need to introduce sleep to function.
Utilities.sleep(333);
}
};
The code above creates a JavaScript Object of all the events in the calendar. Then, loops through each event ID and deletes it. Because of the volume of deleted events, Google will stip the script from running unless you add a sleep timer to the function. However, if you have fewer events, and a shorter timeframe from which you need to delete events, you could likely remove the sleep function, greating imporving the speed of the script.
The script could also be combined with a UI function that adds a menu selection and allows the user to add in start and end dates, or select which calendar to remove events from.
Once you have a clean slate, you can add the code to add the events from external calendars.
My first intention was to make a json object for all the calendars I wanted to add, including ID, Name, and Colors, but I wasn't able to figure out what I was doing wrong. So, I just made a list of IDs and a list of Names. Each ID should look a lot like an email, with an @ mark in the middle. Add the IDs you want to combine to the list. Make sure to wrap the IDs in quotes and seperate each ID with a comma. Do the same for the Names. Names can be arbitrary, but must match the number of instances of unique IDs.
The list of colors is just numbers 1-9 for now, and as long as you don't add more than 9 IDs to the list, there should not be any errors. However, if you decide to use more than 10 calendars, you will want to add additional colors. The colors are currrently just the basic Google Event Colors as enumerators. However, I believe that you can also insert Hex Codes for the colors. So, what I'm saying is that if you don't like the order of the colors, feel free to change as you like.
const teamIds = [] // Create a list of Calendar IDs you want to combine
const teamNames = [] // Create a list of Names for each Calendar ID. Must have more Names than IDs
const teamColors = ["1","2","3","4","5","6","7","8","9", "10"] // Colors are enumerated in the Calendar script.
Once you have your lists set up, you can begin to make your for loops to add the events. The same problem with too many call backs to the calendar will cause Google to quit on you. So, we have added a sleep timer to these loops as well.
Again, if you have only a few events, or are only combining a small number of events, you could proabaly delete the sleep timer and greatly improve the speed of this program.
function eventList(cal, name_id, id_color) {
// Add the calendar ID for the calendar you want add events to
var calToAdd = CalendarApp.getCalendarById("YOUR_CALENDAR_ID_HERE@group.calendar.google.com")
// Will take ID from the teamIDs list above
var calendar = CalendarApp.getCalendarById(cal);
// Choose a start date and end date for the addtions
startDate = new Date('January 1, 2022');
endDate = new Date('December 31, 2022');
// Creates a JavaScript object of Event IDs
var events = calendar.getEvents(startDate, endDate);
//Loop through the events and add to calendar. (Not optimized)
for (var i = 0; i < events.length; i++) {
start_time = events[i].getStartTime();
end_time = events[i].getEndTime();
title = events[i].getTitle();
id = events[i].getId();
var newEvent = calToAdd.createEvent(title, start_time, end_time,
{description: name_id});
newEvent.setColor(id_color);
// Need to add Delays to prevent Google from stopping you from doing too much at once.
// If you don't have too many events, you can delete.
Utilities.sleep(333);
}
};
// Function loops through ID values and executes function above.
function combineCals(){
for (var j=0; j < teamIds.length; j++) {
eventList(teamIds[j], teamNames[j], teamColors[j]);
// Too many calls to the Calendar will cause Google to stop you.
// If you don't have too many events, you can delete.
Utilities.sleep(1000);
}
};
I could have combined these two functions into one function, but I didn't. I understand that this is the prefered way to input the code.
If you use this code and get some use out of it, please let me know.
I don't know why google doesn't have this as a defalut option, but it seems like a lot of people I have talked to really have no use for this program.
But I'm lazy, and solving problems is fun. So, here it is.
// Deletes all events in specified calendar
function deleteAllEvents() {
// Specify calendar by calendar ID
var calendar = CalendarApp.getCalendarById("YOUR_CALENDAR_ID_HERE@group.calendar.google.com");
// Set start time from which you want to clear events
var startTime = new Date("January 01, 2022");
// Set end time for end of clearing
var endTime = new Date("December 31, 2025");
// Loops through each event. Not Optimized
var allEvents = calendar.getEvents(startTime, endTime);
for ( i = 0; i < allEvents.length; i ++){
allEvents[i].deleteEvent();
// Because too many calls to the calendar will cause error if too many events
// need to introduce sleep to function.
Utilities.sleep(333);
}
};
const teamIds = [] // Create a list of Calendar IDs you want to combine
const teamNames = [] // Create a list of Names for each Calendar ID. Must have more Names than IDs
const teamColors = ["1","2","3","4","5","6","7","8","9", "10"] // Colors are enumerated in the Calendar script.
function eventList(cal, name_id, id_color) {
// Add the calendar ID for the calendar you want add events to
var calToAdd = CalendarApp.getCalendarById("YOUR_CALENDAR_ID_HERE@group.calendar.google.com")
// Will take ID from the teamIDs list above
var calendar = CalendarApp.getCalendarById(cal);
// Choose a start date and end date for the addtions
startDate = new Date('January 1, 2022');
endDate = new Date('December 31, 2022');
// Creates a JavaScript object of Event IDs
var events = calendar.getEvents(startDate, endDate);
//Loop through the events and add to calendar. (Not optimized)
for (var i = 0; i < events.length; i++) {
start_time = events[i].getStartTime();
end_time = events[i].getEndTime();
title = events[i].getTitle();
id = events[i].getId();
var newEvent = calToAdd.createEvent(title, start_time, end_time,
{description: name_id});
newEvent.setColor(id_color);
// Need to add Delays to prevent Google from stopping you from doing too much at once.
// If you don't have too many events, you can delete.
Utilities.sleep(333);
}
};
// Function loops through ID values and executes function above.
function combineCals(){
for (var j=0; j < teamIds.length; j++) {
eventList(teamIds[j], teamNames[j], teamColors[j]);
// Too many calls to the Calendar will cause Google to stop you.
// If you don't have too many events, you can delete.
Utilities.sleep(1000);
}
};