Create Google Calendar Event with File Attachment

C
Published in: Google Apps Script - Google Calendar

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);

}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Google Calendar

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube