> ## Documentation Index
> Fetch the complete documentation index at: https://developers.scoutworks.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Programme to calendar

> Put this term’s planned meetings, with what each evening holds, in a calendar feed parents subscribe to. It updates itself as leaders plan.

> [!NOTE]
>
> * **For:** section leaders, and the parents they share it with
> * **You get:** a calendar feed of every planned meeting this term, with its activities, in Google, Outlook or Apple Calendar
> * **Time:** about ten minutes
> * **Scopes:** `programme:read`

Parents ask “what are they doing on Tuesday?” all term. This feed answers it. It lists every meeting in your programme plans, from four weeks ago to the end of the term, with the evening’s activities in the description. When a leader moves a meeting or changes what it holds in the programme planner, the change reaches every subscriber’s calendar on its own.

It works beside [Events in your calendar](https://developers.scoutworks.app/recipes/calendar-feed.md). A meeting a leader has already published as an event is left out here, so nobody sees it twice: subscribe to both feeds, or to this one alone while the term is still being planned.

## 1. Try the request

```bash theme={null}
curl "https://api-demo.scoutworks.app/api/v1/planned-meetings?limit=5" \
  -H "Authorization: Bearer sw_test_473adcc6c7236916021f0f24559db36576a53bab071d0f0c08a025adb510b6ab"
```

Each meeting has a `date`, a `title`, its `activities`, and an `eventId` once it has been published. Its notes for leaders are never sent.

## 2. Add the script

Go to [script.google.com](https://script.google.com) and choose **New project**. Call it **Scoutworks programme**. Delete what is there, paste the script below and choose **Save**.

```javascript Code.gs theme={null}
// Scoutworks → programme to calendar. https://developers.scoutworks.app/recipes/programme-calendar

const BASE = 'https://api-demo.scoutworks.app/api/v1';
const DAY = 24 * 60 * 60 * 1000;
const MEETING_MINUTES = 90; // How long to show each meeting for.

// Calendar apps call this address. Add ?plan=cubs for one plan only.
function doGet(e) {
  const plan = ((e && e.parameter && e.parameter.plan) || '').toLowerCase().slice(0, 50);

  // Serve a copy made in the last hour. Repeated requests then cost no API calls.
  const cache = CacheService.getScriptCache();
  const cached = cache.get('programme:' + plan);
  if (cached) return ContentService.createTextOutput(cached).setMimeType(ContentService.MimeType.ICAL);

  const now = new Date();
  const from = new Date(now.getTime() - 28 * DAY).toISOString();

  // Plan names, such as "Cubs spring term", to filter on and to show.
  const plans = {};
  readAll('/programme-plans').forEach(function (p) { plans[p.id] = p.name; });

  const meetings = readAll('/planned-meetings?from=' + encodeURIComponent(from)).filter(function (m) {
    if (m.eventId) return false; // Already on the calendar as an event.
    return !plan || (plans[m.planId] || '').toLowerCase().indexOf(plan) !== -1;
  });

  const lines = [
    'BEGIN:VCALENDAR',
    'VERSION:2.0',
    'PRODID:-//Scoutworks programme recipe//EN',
    'CALSCALE:GREGORIAN',
    'METHOD:PUBLISH',
    'X-WR-CALNAME:' + text(plan ? 'Programme (' + plan + ')' : 'Programme'),
  ];
  meetings.forEach(function (m) {
    const start = new Date(m.date);
    const end = new Date(start.getTime() + MEETING_MINUTES * 60 * 1000);
    const activities = m.activities.map(function (a) { return '• ' + a.title; }).join('\n');
    lines.push(
      'BEGIN:VEVENT',
      'UID:' + m.id + '@scoutworks.app',
      'DTSTAMP:' + stamp(now),
      'DTSTART:' + stamp(start),
      'DTEND:' + stamp(end),
      'SUMMARY:' + text(m.title),
      'DESCRIPTION:' + text((plans[m.planId] || 'Programme') + (activities ? '\n\n' + activities : '')),
      'END:VEVENT'
    );
  });
  lines.push('END:VCALENDAR');

  const ics = lines.map(fold).join('\r\n') + '\r\n';
  try {
    cache.put('programme:' + plan, ics, 60 * 60);
  } catch (err) {
    // A feed over 100 KB is too big to keep. Serve it without the copy.
  }
  return ContentService.createTextOutput(ics).setMimeType(ContentService.MimeType.ICAL);
}

// 2026-09-24T18:30:00.000Z → 20260924T183000Z
function stamp(date) {
  return date.toISOString().replace(/[-:]/g, '').replace(/\.\d{3}/, '');
}

// Escapes the characters that have a meaning in a calendar file.
function text(value) {
  return String(value)
    .replace(/\\/g, '\\\\')
    .replace(/;/g, '\\;')
    .replace(/,/g, '\\,')
    .replace(/\r?\n/g, '\\n');
}

// Calendar files keep each line short. A long line continues after a space.
function fold(line) {
  const parts = [];
  while (line.length > 73) {
    parts.push(line.slice(0, 73));
    line = ' ' + line.slice(73);
  }
  parts.push(line);
  return parts.join('\r\n');
}

// Every item in a list, following nextCursor to the last page.
function readAll(path) {
  const items = [];
  let cursor = null;
  do {
    let url = BASE + path + (path.indexOf('?') === -1 ? '?' : '&') + 'limit=200';
    if (cursor) url += '&cursor=' + encodeURIComponent(cursor);
    const body = fetchScoutworks(url);
    items.push.apply(items, body.data);
    cursor = body.nextCursor;
  } while (cursor);
  return items;
}

// Calls the API. Waits and tries again if the rate limit is reached.
function fetchScoutworks(url) {
  const apiKey = PropertiesService.getScriptProperties().getProperty('SCOUTWORKS_API_KEY');
  if (!apiKey) throw new Error('Add SCOUTWORKS_API_KEY in Project Settings → Script Properties.');
  for (let attempt = 0; attempt < 3; attempt++) {
    const res = UrlFetchApp.fetch(url, {
      headers: { Authorization: 'Bearer ' + apiKey },
      muteHttpExceptions: true,
    });
    const body = JSON.parse(res.getContentText());
    if (res.getResponseCode() === 200) return body;
    if (res.getResponseCode() === 429) {
      Utilities.sleep((body.retryAfter || 30) * 1000);
      continue;
    }
    throw new Error(body.error.code + ': ' + body.error.message);
  }
  throw new Error('Scoutworks is still busy. Try again in a minute.');
}
```

## 3. Add your key

In the script editor, choose **Project Settings** (the cog), then **Script Properties → Add script property**. Property: `SCOUTWORKS_API_KEY`. Value: the [sandbox key](https://developers.scoutworks.app/guides/sandbox.md) to try it, or your own key.

## 4. Publish it and subscribe

Publish the script as a web app and subscribe to its address exactly as [Events in your calendar](https://developers.scoutworks.app/recipes/calendar-feed.md#3-publish-it-as-a-web-app) does: **Deploy → New deployment → Web app**, **Execute as: Me**, **Who has access: Anyone**, then add the `/exec` address to your calendar **from URL**.

For one section, add `?plan=` and a word from the plan’s name: `…/exec?plan=cubs`.

> [!WARNING]
>
> Anyone with the address can see your programme: dates, titles and activities. It never holds a young person’s name, or the leaders’ notes. Share it only with your parents and leaders.

## 5. Switch to your group

In the script, change `api-demo.scoutworks.app` to `api.scoutworks.app`, and replace the sandbox key with your own. Your key needs `programme:read` and nothing else. After a change, choose **Deploy → Manage deployments → Edit**, set **Version** to **New version** and choose **Deploy**, so the address stays the same.

## Variations

* **Show the badge work.** Each meeting’s `badgeRequirements` lists the badge requirements it covers. Add them to the description so parents know what their child is working towards.
* **Longer meetings.** Change `MEETING_MINUTES` to your section’s meeting length.

## If something goes wrong

See [Errors](https://developers.scoutworks.app/guides/errors.md) for every code. A `403` means the key does not have `programme:read`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.