Categories
Code

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); 
  
}
Categories
Code

Daily Email Summary of your Google Calendar Events

What’s the agenda for today? The Google Script will send you a daily email digest of all the events that are scheduled for today. The email includes the event’s title and the start and end dates but you can extend it to include the event’s location, description and other details.

function sendCalendarAgenda() {
  
  /* Get the default calendar */
  var cal = CalendarApp.getDefaultCalendar();
  
  /* Get all events for today */  
  var events = cal.getEventsForDay(new Date());
  
  /* Email address for the events digest */
  var mailto = "email@labnol.org"
  
  if(events.length > 0) {
    
    var body = "Google Calendar - Events" + "\n\n";
    var tz = Session.getScriptTimeZone();
    
    /* Concatenate the events list */
    for(var i=0; i < events.length; i++){
      body += Utilities.formatDate(events[i].getStartTime(),tz,"MM:dd HH:mm") + " ~ ";  
      body += Utilities.formatDate(events[i].getEndTime(),tz,"MM:dd HH:mm") + " :";  
      body += events[i].getTitle() + "\n";
    }
    
    /* Send email digest */
    MailApp.sendEmail(mailto, "Google Calender - Summary", body);
    
  }
}