Skip to content

Instantly share code, notes, and snippets.

@SBoudrias
Last active September 12, 2024 19:45
Show Gist options
  • Save SBoudrias/95a097076b26b633c5f08238793b4cc5 to your computer and use it in GitHub Desktop.
Save SBoudrias/95a097076b26b633c5f08238793b4cc5 to your computer and use it in GitHub Desktop.
Google script to add colours & focus time to your calendar
function ColorEvents() {
const today = new Date();
const later = new Date();
later.setDate(later.getDate() + 20);
Logger.log(`Updating from ${today} to ${later}`);
const calendar = CalendarApp.getCalendarById('[email protected]');
const allEvents = calendar.getEvents(today, later).filter((event) => !event.isAllDayEvent());
function setEventColor(event, color) {
const title = event.getTitle();
// Do not override unless the color isn't the expected one otherwise
// we can hit the API rate limit for modifying events.
if (event.getColor() !== color) {
const colorName = Object.keys(CalendarApp.EventColor).find(key => CalendarApp.EventColor[key] === color);
Logger.log(`Updating ${title} to ${colorName}`);
event.setColor(color);
}
}
function ceilToNext15Min(from) {
const date = new Date(from.getTime());
const minutes = date.getMinutes();
const minutesToTarget = Math.ceil(minutes / 15) * 15;
date.setMinutes(minutesToTarget);
date.setSeconds(0);
date.setMilliseconds(0);
return date;
}
// Color events
allEvents.forEach((event) => {
const title = event.getTitle();
if (title.includes('Interview') || title.startsWith('Remote F2F') || title.startsWith('Debrief') || title.includes('Hiring Committee') || title.includes('Pre-Huddle')|| title.includes('Scorecard')) {
setEventColor(event, CalendarApp.EventColor.PALE_RED);
} else if (title.includes('1:1') || title.includes('1-1') || title.includes('/ Simon') || title.includes('Simon /')) {
setEventColor(event, CalendarApp.EventColor.PALE_GREEN);
} else if (title.startsWith('Flight:') || title.startsWith('Stay:')) {
setEventColor(event, CalendarApp.EventColor.ORANGE);
} else if (title.startsWith('OKR')) {
setEventColor(event, CalendarApp.EventColor.YELLOW);
}
});
// Create scorecard blocks after interviews
allEvents
.filter((event) => {
const title = event.getTitle();
if (title.includes('Interview') || title.startsWith('Remote F2F')) {
return true;
}
return false;
})
.forEach((event) => {
const nextBlockStartTime = ceilToNext15Min(event.getEndTime())
const nextBlockEndTime = new Date(nextBlockStartTime.getTime() + (30 * 60 * 1000));
const nextEvents = calendar.getEvents(nextBlockStartTime, nextBlockEndTime);
const hasScorecard = nextEvents.some((event) => event.getTitle() === 'Scorecard');
if (!hasScorecard) {
const scorecardEvent = calendar.createEvent('Scorecard', nextBlockStartTime, nextBlockEndTime);
setEventColor(scorecardEvent, CalendarApp.EventColor.PALE_RED);
}
});
}
@SBoudrias
Copy link
Author

For Google Scripts: https://script.google.com/

Create a new project. Add a file.

Then set a trigger on any Google calendar update

Screenshot 2023-08-08 at 12 02 47 PM

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