Auto-color-code events by keyword
Apply event colours based on title keywords — `[client]` red, `internal` blue.
Publié le 14 sept. 2025
A colour-coded calendar tells you the shape of your week at a glance — red for client work, blue for internal meetings, green for interviews. At Northwind the convention exists, but nobody sets the colour when they create an event, so the calendar stays a uniform block and the glance-value is lost.
This automation reads the next two weeks of events and recolours each one by
matching its title against a list of rules. The first rule that matches wins,
so a [client] standup is still red rather than blue. Run it on a schedule
and the colour convention enforces itself.
What you’ll need
- Access to your default Google Calendar — the script uses
CalendarApp.getDefaultCalendar(). - A naming convention worth matching: titles like
[client] Acme review,Internal standup, orInterview — designer. Adjust the rules to your own conventions.
The script
// Colour rules, checked in order — the first matching rule wins.
// Each rule is [regular expression, CalendarApp.EventColor].
const COLOR_RULES = [
[/^\[client\]/i, CalendarApp.EventColor.RED],
[/internal|standup/i, CalendarApp.EventColor.BLUE],
[/interview/i, CalendarApp.EventColor.GREEN],
[/focus|deep work/i, CalendarApp.EventColor.GRAY],
];
// How many days ahead to recolour.
const LOOKAHEAD_DAYS = 14;
/**
* Scans the next fortnight of calendar events and sets each event's colour
* based on the first keyword rule its title matches.
*/
function recolorEvents() {
// 1. Define the window: now through LOOKAHEAD_DAYS from now.
const start = new Date();
const end = new Date(start.getTime() + LOOKAHEAD_DAYS * 86400000);
const events = CalendarApp.getDefaultCalendar().getEvents(start, end);
if (!events.length) {
Logger.log('No events in the next ' + LOOKAHEAD_DAYS + ' days.');
return;
}
let recoloured = 0;
for (const event of events) {
// 2. Test the title against each rule in order.
for (const [re, color] of COLOR_RULES) {
if (re.test(event.getTitle())) {
// 3. First match wins — set the colour and stop checking.
event.setColor(color);
recoloured++;
break;
}
}
}
Logger.log('Recoloured ' + recoloured + ' of ' + events.length + ' events.');
}
How it works
recolorEventsbuilds a time window from now to fourteen days ahead and fetches every event in it from the default calendar.- If the window is empty, it logs a message and stops.
- For each event it walks
COLOR_RULESin order, testing the event title against each rule’s regular expression. - The first rule that matches sets the event’s colour via
setColorandbreakstops the inner loop — so order matters, and earlier rules take priority over later ones. - Events whose titles match no rule are left with whatever colour they already have.
- It logs how many events were recoloured against the total scanned.
Example run
Your calendar for the coming fortnight:
| Event title | Matched rule | New colour |
|---|---|---|
| [client] Acme review | ^\[client\] | Red |
| Internal standup | internal|standup | Blue |
| Interview — designer | interview | Green |
| Focus — deep work | focus|deep work | Grey |
| Lunch with Sam | none | unchanged |
Note that Internal standup matches the internal|standup rule, not a
standup-specific one — and a [client] standup would go red, because the
client rule sits first in the list.
Trigger it
This works best running quietly so new events get coloured soon after they appear:
- In the Apps Script editor, open Triggers (the clock icon).
- Click Add Trigger.
- Choose
recolorEvents, event source Time-driven, type Day timer, and pick an early-morning hour.
Watch out for
- The script recolours every matching event on every run. If you set a colour by hand that conflicts with a rule, the next run overrides it — exclude such events with a tag check if that matters.
- Only the default calendar is scanned. Events on a secondary or shared
calendar are untouched unless you point
CalendarAppat that calendar. - Rule order is the whole design. A broad rule placed early will swallow events a more specific later rule was meant to catch — put the most specific patterns first.
- Matching is on the title only. An event mislabelled or named loosely will not match any rule and keeps its existing colour.
CalendarApp.EventColoroffers a fixed set of eleven named colours. You cannot set an arbitrary hex colour, so map your conventions onto the names available.
À voir aussi
Schedule personal habits and routines
Block recurring habits on Awadesh's calendar — gym, walks, deep-work mornings.
Mis à jour le 5 nov. 2025
Sync birthdays and anniversaries to Calendar
Populate recurring personal dates from a Sheet — the Northwind team rituals calendar.
Mis à jour le 1 nov. 2025
Generate recurring events with custom exceptions
Handle complex recurrence rules in code — every Tuesday except UK bank holidays.
Mis à jour le 28 oct. 2025
Auto-reschedule low-priority conflicts
Move flexible Northwind events around fixed ones — focus blocks bend, client calls don't.
Mis à jour le 16 oct. 2025
Build a contract-renewal calendar
Track Northwind's recurring-revenue renewal dates as calendar events for proactive sales.
Mis à jour le 8 oct. 2025