Skip to content

Creating multiple events

Adam Gibbons edited this page Nov 18, 2017 · 7 revisions

To generate multiple events, first create array of events, then map over it with ics.createEvent.

Note - currently, ICS does not generate unique IDs when used in this manner, so make sure that you assign UUIDs to your events before they're passed to ICS (see example).

Example:

const ics = require('ics')
const fs = require('fs')
const uuidv4 = require('uuid/v4')

const events = [
  {
    title: 'First event',
    start: [2018, 5, 30, 6, 30],
    duration: { hours: 1 },
    uid: uuidv4()
  },
  {
    title: 'Second event',
    start: [2018, 6, 30, 6, 30],
    duration: { minutes: 30 },
    uid: uuidv4()
  }
]

events.map((event) => {
  ics.createEvent(event, (error, value) => {
    if (error) throw error

    fs.writeFile(`events/${event.uid}.ics`, value, (error) => {
      if (error) throw error
    })
  })
})
Clone this wiki locally