Introduction to Google Apps Script: Automate Your Workflows and Boost Productivity

Creating and Managing Calendar Events with Apps Script

Section 3

Working with Google Forms and Calendar

Introduction to Google Apps Script: Automate Your Workflows and Boost ProductivityWorking with Google Forms and Calendar

Google Apps Script provides a powerful way to interact with Google Calendar, allowing you to automate event creation, updates, and deletions. This is incredibly useful for tasks such as scheduling meetings based on form submissions, creating recurring events, or sending reminders. Let's explore how to harness this capability.

The CalendarApp service is your gateway to Google Calendar. You can access it directly in your Apps Script projects. The first step is often to get a specific calendar you want to work with.

function createEvent() {
  var calendar = CalendarApp.getDefaultCalendar(); // Get the default calendar
  // Alternatively, get a calendar by its ID:
  // var calendar = CalendarApp.getCalendarById('YOUR_CALENDAR_ID');

  // ... rest of the code will go here
}

Once you have a calendar object, you can create new events. The createEvent() method is versatile and can take various arguments, including event title, start time, and end time. You can also specify details like location, description, and guests.

function createEvent() {
  var calendar = CalendarApp.getDefaultCalendar();

  var title = 'Team Meeting';
  var startTime = new Date(); // Today
  var endTime = new Date(startTime.getTime() + 60 * 60 * 1000); // 1 hour from now
  var description = 'Discuss project progress';
  var location = 'Conference Room A';
  var guests = 'guest1@example.com, guest2@example.com';

  var event = calendar.createEvent(title, startTime, endTime, {
    description: description,
    location: location,
    guests: guests
  });

  Logger.log('Event created: %s', event.getTitle());
}

You can also create all-day events. For these, you only need to provide the date, not a specific time.

function createAllDayEvent() {
  var calendar = CalendarApp.getDefaultCalendar();
  var title = 'Project Deadline';
  var date = new Date(); // Today

  var event = calendar.createAllDayEvent(title, date);
  Logger.log('All-day event created: %s on %s', event.getTitle(), event.getAllDayDates());
}

Working with existing events is just as straightforward. You can search for events within a specific date range or retrieve a single event by its ID.

チャプターへ戻る