Automating Google Calendar: Adding Events with Google Apps Script

Google Apps Script is a powerful tool that enables you to automate tasks within Google Workspace, including Google Calendar. In this blog post, we will guide you through an exercise to write a script that adds a new event to your Google Calendar with a title, description, start time, and end time. Additionally, we will explore how you can create and manage Google Calendar events using Apps Script.

Prerequisites

Before we begin, ensure you have:

  • A Google account
  • Basic knowledge of JavaScript
  • Access to Google Calendar

Step 1: Setting Up the Script

  1. Open the Apps Script Editor:
    • Go to your Google Calendar, click on the gear icon for settings, and select Settings.
    • In the left-hand menu, click on Add-ons > Manage Apps and then select Google Apps Script to open the script editor.
  2. Create a New Script:
    • In the Apps Script editor, replace any existing code with the following code to create a new calendar event:

function createCalendarEvent() {

  // Define the event details

  const title = ‘Project Meeting’;

  const description = ‘Discuss project milestones and deliverables.’;

  const startTime = new Date(‘2023-07-01T10:00:00’);

  const endTime = new Date(‘2023-07-01T11:00:00’);

  // Get the default calendar

  const calendar = CalendarApp.getDefaultCalendar();

  // Create the event

  calendar.createEvent(title, startTime, endTime, {description: description});

  Logger.log(‘Event created: ‘ + title);

}

Step 2: Running the Script

  1. Save and Run the Script:
    • Save your script by clicking the floppy disk icon or pressing Ctrl+S (Windows) or Cmd+S (Mac).
    • Click on the Run button (the play icon) to execute the createCalendarEvent function.
  2. Authorize the Script:
    • The first time you run the script, you will need to authorize it. Click on Review Permissions and follow the steps to grant the necessary permissions.
  3. Check Your Google Calendar:
    • After running the script, open your Google Calendar to see the new event added at the specified date and time.

Step 3: Customizing the Script

  1. Dynamic Event Details:
    • Modify the script to accept dynamic input for event details. You can use prompts or form inputs to allow users to enter event details interactively.

function createCustomCalendarEvent(title, description, startTime, endTime) {

  const calendar = CalendarApp.getDefaultCalendar();

  calendar.createEvent(title, new Date(startTime), new Date(endTime), {description: description});

  Logger.log(‘Event created: ‘ + title);

}

  1. Automating Repetitive Events:
    • Create recurring events by setting up repeat rules.

function createRecurringEvent() {

  const title = ‘Weekly Team Meeting’;

  const description = ‘Weekly sync-up with the team.’;

  const startTime = new Date(‘2023-07-03T09:00:00’);

  const endTime = new Date(‘2023-07-03T10:00:00’);

  const calendar = CalendarApp.getDefaultCalendar();

  const eventSeries = calendar.createEvent(title, startTime, endTime, {description: description})

    .repeatWeekly()

    .until(new Date(‘2023-12-31’));

  Logger.log(‘Recurring event created: ‘ + title);

}

  1. Managing Events:
    • List, update, and delete events using Apps Script functions.

function listCalendarEvents() {

  const calendar = CalendarApp.getDefaultCalendar();

  const events = calendar.getEvents(new Date(‘2023-07-01T00:00:00’), new Date(‘2023-07-31T23:59:59’));

  events.forEach(event => {

    Logger.log(‘Event: ‘ + event.getTitle() + ‘, Start Time: ‘ + event.getStartTime());

  });

}

function updateCalendarEvent(eventId, newTitle) {

  const calendar = CalendarApp.getDefaultCalendar();

  const event = calendar.getEventById(eventId);

  if (event) {

    event.setTitle(newTitle);

    Logger.log(‘Event updated: ‘ + newTitle);

  } else {

    Logger.log(‘Event not found’);

  }

}

function deleteCalendarEvent(eventId) {

  const calendar = CalendarApp.getDefaultCalendar();

  const event = calendar.getEventById(eventId);

  if (event) {

    event.deleteEvent();

    Logger.log(‘Event deleted’);

  } else {

    Logger.log(‘Event not found’);

  }

}

Conclusion

With this exercise, you’ve learned how to use Google Apps Script to add a new event to Google Calendar, along with various ways to manage calendar events programmatically. By automating calendar tasks, you can enhance productivity and streamline your scheduling process. Explore further customizations and integrations to tailor your automation scripts to your specific needs.