appscript.dev
Automation Beginner Calendar

Auto-color-code events by keyword

Apply event colours based on title keywords — `[client]` red, `internal` blue.

Published Sep 14, 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, or Interview — 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

  1. recolorEvents builds a time window from now to fourteen days ahead and fetches every event in it from the default calendar.
  2. If the window is empty, it logs a message and stops.
  3. For each event it walks COLOR_RULES in order, testing the event title against each rule’s regular expression.
  4. The first rule that matches sets the event’s colour via setColor and break stops the inner loop — so order matters, and earlier rules take priority over later ones.
  5. Events whose titles match no rule are left with whatever colour they already have.
  6. It logs how many events were recoloured against the total scanned.

Example run

Your calendar for the coming fortnight:

Event titleMatched ruleNew colour
[client] Acme review^\[client\]Red
Internal standupinternal|standupBlue
Interview — designerinterviewGreen
Focus — deep workfocus|deep workGrey
Lunch with Samnoneunchanged

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:

  1. In the Apps Script editor, open Triggers (the clock icon).
  2. Click Add Trigger.
  3. 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 CalendarApp at 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.EventColor offers a fixed set of eleven named colours. You cannot set an arbitrary hex colour, so map your conventions onto the names available.

Related