Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BrandenBedoya/455717824632fad7511f0e7a20d6dd82 to your computer and use it in GitHub Desktop.
Save BrandenBedoya/455717824632fad7511f0e7a20d6dd82 to your computer and use it in GitHub Desktop.
Google Calendar - Auto Color Coding - Apps Script
function ColorEvents() {
var today = new Date();
var nextweek = new Date();
nextweek.setDate(nextweek.getDate() + 7);
Logger.log(today + " " + nextweek);
var calendars = CalendarApp.getAllOwnedCalendars();
Logger.log("found number of calendars: " + calendars.length);
// Define keywords and their corresponding colors in order of priority
var keywords = [
//Mandatory Events
{ keyword: 'DR.', color: CalendarApp.EventColor.RED },
{ keyword: 'DOCTOR', color: CalendarApp.EventColor.RED },
{ keyword: 'FLY', color: CalendarApp.EventColor.RED },
{ keyword: 'FLIGHT', color: CalendarApp.EventColor.RED },
//Work Events
{ keyword: 'WORK', color: CalendarApp.EventColor.YELLOW },
{ keyword: 'UPWORK', color: CalendarApp.EventColor.YELLOW },
//Gym and Self Care Events
{ keyword: 'GYM', color: CalendarApp.EventColor.PALE_BLUE },
{ keyword: 'EOS', color: CalendarApp.EventColor.PALE_BLUE },
{ keyword: 'MUAY THAI', color: CalendarApp.EventColor.PALE_BLUE },
{ keyword: 'JIU JITSU', color: CalendarApp.EventColor.PALE_BLUE },
{ keyword: 'BOXING', color: CalendarApp.EventColor.PALE_BLUE },
{ keyword: 'WORKOUT', color: CalendarApp.EventColor.PALE_BLUE },
{ keyword: 'JOURNAL', color: CalendarApp.EventColor.PALE_BLUE },
{ keyword: 'THERAPY', color: CalendarApp.EventColor.PALE_BLUE },
{ keyword: 'MEDITATION', color: CalendarApp.EventColor.PALE_BLUE },
{ keyword: 'NAP', color: CalendarApp.EventColor.PALE_BLUE },
{ keyword: 'MEDITATE', color: CalendarApp.EventColor.PALE_BLUE },
//Dates
{ keyword: 'DATE', color: CalendarApp.EventColor.PALE_RED },
{ keyword: 'GIRL', color: CalendarApp.EventColor.PALE_RED },
{ keyword: 'MOVIES', color: CalendarApp.EventColor.PALE_RED },
{ keyword: 'SHOPPING', color: CalendarApp.EventColor.PALE_RED },
//Reminders
{ keyword: 'BDAY', color: CalendarApp.EventColor.GRAY },
{ keyword: 'BIRTHDAY', color: CalendarApp.EventColor.GRAY },
{ keyword: 'SUBSCRIPTION', color: CalendarApp.EventColor.GRAY },
{ keyword: 'CANCEL', color: CalendarApp.EventColor.GRAY },
{ keyword: 'MONTHLY', color: CalendarApp.EventColor.GRAY },
{ keyword: 'ANNUAL', color: CalendarApp.EventColor.GRAY },
//Optional Events
{ keyword: '?', color: CalendarApp.EventColor.PALE_GREEN }
];
for (var i = 0; i < calendars.length; i++) {
var calendar = calendars[i];
var events = calendar.getEvents(today, nextweek);
for (var j = 0; j < events.length; j++) {
var e = events[j];
var title = e.getTitle().toUpperCase();
for (var k = 0; k < keywords.length; k++) {
if (title.includes(keywords[k].keyword)) {
e.setColor(keywords[k].color);
break; // Stop checking after the first match
}
}
}
}
}
@BrandenBedoya
Copy link
Author

This is my code to auto color code my Google Calendar events after creating one on my phone/computer. One less step needed when creating future calendar events

Steps to implement code:

  1. Go to script.google.com and make a new script. I titled mine "Google Calendar Personal Color Coding"
  2. Copy and paste the code above
  3. Within the code, adjust the:
  • "keyword" (aka keywords you want to adjust color based off of for the calendar event title)
  • "CalendarApp.EventColor.COLOR" to your preferences
  1. Save
  2. Set a "Trigger" to have this run on every new calendar update, to run in real time:
  • Function: ColorEvents
  • Select event source: From calendar
  • Event calendar details: Calendar updated
  • Failure notifications: Notify my daily
  • Save

And done! Have fun organizing your life :)

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