Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To create a Google Calendar event using Google Apps Script without duplicating it, follow these steps:

  1. Open the script editor by clicking on Tools > Script editor in your Google Calendar.
  2. Write the code to create your event using the Calendar API. The code should include the start and end times of the event, the event title, and any other relevant details.

Here is an example code:

function createEvent() {
  var calendar = CalendarApp.getDefaultCalendar();
  var title = "Meeting with John";
  var startTime = new Date("October 5, 2021 09:00:00");
  var endTime = new Date("October 5, 2021 10:00:00");
  var event = calendar.createEvent(title, startTime, endTime);
  Logger.log('Event ID: ' + event.getId());
}
  1. Before creating the event, you can search for events with the same title or start and end time by using the getEvents() method.

Here is an example that checks for events on the same day:

function createEvent() {
  var calendar = CalendarApp.getDefaultCalendar();
  var title = "Meeting with John";
  var startTime = new Date("October 5, 2021 09:00:00");
  var endTime = new Date("October 5, 2021 10:00:00");

  var events = calendar.getEventsForDay(startTime);

  // Iterate through the events and check for duplicates
  var eventExists = false;
  for (var i = 0; i < events.length; i++) {
    var event = events[i];
    if (event.getTitle() == title && event.getStartTime() == startTime && event.getEndTime() == endTime) {
      eventExists = true;
    }
  }

  if (!eventExists) {
    var event = calendar.createEvent(title, startTime, endTime);
    Logger.log('Event ID: ' + event.getId());
  } else {
    Logger.log('Event already exists!');
  }
}
  1. Run the code to create your event or to check for duplicates. You can run the code manually or set up a trigger to run it automatically on a schedule.