'Stop Google App Script from creating duplicate calendar events

The script below allows me to copy events from one calendar to another using Google App Script and it works great minus the fact I need to run this script say every hour which results in many duplicates being created.

So I was wondering if it is possible to resolve this by adapting the script to somehow check the existing events on the target calendar and only copy new/missing events from the source maybe?

Any help you can provide would be so very much appreciated :)

function copyAppointments() {
  const sourceCalendar = CalendarApp.getCalendarById("[email protected]");
  const targetCalendar = CalendarApp.getCalendarById("[email protected]");
  const dt=new Date();
  const starttime  = new Date(dt.getFullYear()-1,dt.getMonth(),dt.getDate(),0,0,0,0);//one year ago
  const endtime = new Date(dt.getFullYear()+1,dt.getMonth(),dt.getDate(),0,0,0,0);//next year
  const events = sourceCalendar.getEvents(starttime,endtime);
  events.forEach(e =>{
    targetCalendar.createAllDayEvent(e.getTitle(),e.getAllDayStartDate(),e.getAllDayEndDate());
  });
}


Solution 1:[1]

Description

Here is an example of a way to get and store the events that have been processed. The first time through all events within the time range are stored in Script Properties. The next time the script is run during the same session no new events have been added. In your case when this is run periodically and new events are added they will become a list of new events to process and be added to the stored list.

Script

function runCalendarTest () {
  try {
    let calendar = CalendarApp.getCalendarById("[email protected]");
    let events = calendar.getEvents(new Date(2021,0,1),new Date());
    events = checkIds(events);
    if( events ) {
      console.log(events.length);
    }
    else {
      let service = PropertiesService.getScriptProperties();
      let ids = service.getProperty("StoredIds");
      console.log(ids)
    }
  }
  catch(err) {
    console.log("Error in runCalendarTest - "+err);
  }
}

function checkIds(events) {
  try {
    let service = PropertiesService.getScriptProperties();
    let ids = service.getProperty("StoredIds");
    let stored = null;
    if( ids ) {
      ids = ids.split(",");
      events.forEach( event => { let found = ids.find( id => event.getId() === id );
                                 if( !found ) {
                                   stored.push(event);
                                   ids.push(event.getId());
                                 }
                                }
                    );
    }
    else {
      ids = []
      events.forEach( event => ids.push(event.getId()) );
      stored = events;
    }
    ids = ids.join();
    service.setProperty("StoredIds",ids);
    return stored;
  }
  catch(err) {
    console.log("Error in checkIds - "+err)
  }
}

References

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TheWizEd