Last active
April 9, 2023 22:39
-
-
Save ttrahan/46d5315c2daef004d916d14f9b8f687e to your computer and use it in GitHub Desktop.
Google Apps Script to color code Calendar events based on one to many keywords in Event Title
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
// This script will color code Events in Google Calendar. For setup, follow the instructions posted here: | |
// https://www.linkedin.com/pulse/automate-color-coding-your-google-calendar-marguerite-thibodeaux-acc/?trk=articles_directory | |
function colorEvents() { | |
const today = new Date(); | |
const endDate = new Date(); | |
// Set the end date for calendar events - set below at 31 days from current date | |
endDate.setDate(endDate.getDate() + 31); | |
Logger.log(today + " " + endDate); | |
var calendars = CalendarApp.getAllOwnedCalendars(); | |
Logger.log("found number of calendars: " + calendars.length); | |
for (var i=0; i<calendars.length; i++) { | |
const calendar = calendars[i]; | |
const events = calendar.getEvents(today, endDate); | |
// Terms to look for in calendar events by type of event | |
// Add as many different event types by creating a new line below (or delete extras) | |
const personalMeetings = ["Personal", "BLOCK"]; | |
const interviewMeetings = ["Interview"]; | |
const execTeamMemberMeetings = ["Jim", "Rob", "John", "Thomas", "Dave", "Mary"]; | |
const execMeetings = ["Exec"]; | |
const bdTeamMemberMeetings = ["Ella", "Larry", "Lou", "Julie", "William"]; | |
const salesTeamMemberMeetings = ["Phil", "Ed", "Jane", "Don"]; | |
const salesMeetings = ["Revenue", "Sales"]; | |
// Process each calendar event | |
for (let j=0; j<events.length; j++) { | |
let e = events[j]; | |
let title = e.getTitle(); | |
let currentColor = e.getColor(); | |
let description = e.getDescription(); | |
// Evaluate for each keyword for each type of meeting | |
// Must have one below for each event type above. Note that the constant name following the = sign must match | |
// the constant name assigned above in lines 20-25 | |
const personal = personalMeetings.some(event => title.includes(event)); | |
const interview = interviewMeetings.some(event => title.includes(event)); | |
const execTeam = execTeamMemberMeetings.some(event => title.includes(event)); | |
const exec = execMeetings.some(event => title.includes(event)); | |
const bdTeam = bdTeamMemberMeetings.some(event => title.includes(event)); | |
const salesTeam = salesTeamMemberMeetings.some(event => title.includes(event)); | |
const sales = salesMeetings.some(event => title.includes(event)); | |
// Set the colors based on event type | |
// Only change the calendar event if the color needs to change | |
// Customize colors by changing the color in ALL CAPS | |
// Colors available listed here: https://developers.google.com/apps-script/reference/calendar/event-color | |
if (personal) { | |
if (currentColor != 8) { | |
e.setColor(CalendarApp.EventColor.GRAY); | |
} | |
} | |
if (interview) { | |
if (currentColor != 11) { | |
e.setColor(CalendarApp.EventColor.RED); | |
} | |
} | |
if (execTeam) { | |
if (currentColor != 4) { | |
e.setColor(CalendarApp.EventColor.PALE_RED); | |
} | |
} | |
if (exec) { | |
if (currentColor != 11) { | |
//Logger.log("RED: " && currentColor); // 11 | |
e.setColor(CalendarApp.EventColor.RED); | |
} | |
} | |
if (bdTeam) { | |
if (currentColor != 2) { | |
e.setColor(CalendarApp.EventColor.PALE_GREEN); | |
} | |
} | |
if (salesTeam) { | |
if (currentColor != 1) { | |
e.setColor(CalendarApp.EventColor.PALE_BLUE); | |
} | |
} | |
if (sales) { | |
if (currentColor != 9) { | |
e.setColor(CalendarApp.EventColor.BLUE); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment