Create Google Calendar Event with File Attachment

The Google Script will create a new event in the specified Google Calendar and will attach a file from Google Drive into the event.

Google Calendar API only allows file attachments from Google Drive and you can include a maximum of 25 attachment per event. The attachments can be specified either by File ID or by File URL. The Advanced Calendar API should be enabled from your Google Console.

// Credits / References
// https://developers.google.com/google-apps/calendar/v3/reference/events
// http://stackoverflow.com/questions/34853043

function createEvent() {
  var calendarId = '{{Google Calendar Id}}';

  // April 20, 2016 10:00:00 AM
  var start = new Date(2016, 3, 20, 10, 0, 0);

  // April 20, 2016 10:30:00 AM
  var end = new Date(2016, 3, 20, 10, 30, 0);

  var fileName = 'Appraisal Guidlines.pdf';

  // Get the Drive ID of the file attachments
  // Only Google Drive file are support in Google Calendar
  var fileId = DriveApp.getFilesByName(fileName).next().getId();

  var calendarEvent = {
    summary: 'Performance Appraisal',
    description: 'Submit Appraisal Document for March.',
    location: '10 Hanover Square, NY 10005',
    start: {
      dateTime: start.toISOString(),
    },
    end: {
      dateTime: end.toISOString(),
    },
    attachments: [
      {
        fileId: fileId,
        title: fileName,
      },
    ],
    attendees: [
      {
        email: 'employee1@ctrlq.org',
      },
      {
        email: 'employee2@labnol.org',
      },
    ],
  };

  // Set supportsAttachments to true
  // if the calendarEvent object has one or more file attachments
  calendarEvent = Calendar.Events.insert(calendarEvent, calendarId, {
    supportsAttachments: true,
  });

  Logger.log('Event with attachment created. Event ID is %s' + calendarEvent.getId());

  // For debugging the output
  Logger.log(calendarEvent);
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & recognition

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻