2

I'm working for a non-profit and they want me to schedule their google calendar in such a way that once you create one event, let's say event A, it should automatically create events B,C,D and E.

For example- If I create an event "Anne's birthday", then the calendar should automatically create events (or sub-events) like Birthday plan meeting 1 week before the due date as event "B", Cake order 4 days before the due date as event "C", Party prep Shopping 2 days before as event "D" and Contacting invitees 1 day before as event "E" at specified dates and times before the due date and should be applicable for all similar future events.

Subhransu Nanda
  • 119
  • 1
  • 3
  • 11

1 Answers1

2

You should create a trigger event in your script that will fire your function when a new event is added to your calendar.

In order to do that, you have to synchronize your script to your calendar. You can follow these steps:

  1. Create a script and enable the Calendar advance service.
  2. Get your nextSyncToken for the calendar you want to track created events from, using Events.list() (run this function at the beginning, only once):
function initialSync() {
  var nextSyncToken = Calendar.Events.list('your_calendar_id')["nextSyncToken"];
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  sheet.getRange('A1').setValue(nextSyncToken)
}

You have to store this nextSyncToken somewhere. I have used a spreadsheet for that in this sample, but you can use a database or whatever suits you (actually a spreadsheet is not the most secure way to store data, so I'd recommend using something else).

  1. Create a trigger event that will fire whenever you update an event in your calendar (create, edit or delete an event). Also run this function only once, you only need a trigger:
function createTrigger() {
    var t = ScriptApp.newTrigger("createSubevents")
         .forUserCalendar("your_calendar_id")
         .onEventUpdated()
         .create();
}
  1. Create function that will create 'subevents' every time a new event is created. This function should have the same name as the one you invoke in the trigger you just created. In the following function, only a 'subevent' is created, repeat the process or create a function to do the same for how many subevents you want to create (you should run this function at least once to get all needed permissions):
function createSubevents() {
  // Getting current syncToken from spreadsheet
  var params = {
    syncToken: sheet.getRange('A1').getValue()
  }
  // Retrieve events that were updated since last call
  var events = Calendar.Events.list('your_calendar_id', params);
  var items = events["items"];
  // Update sync token in spreadsheet
  sheet.getRange('A1').setValue(events["nextSyncToken"]);
  // Loop through updated items (in this case, it should only be one)
  for(var i = 0; i < items.length; i++) {
    var item = items[i];
    var mainStartTime = new Date(item.start.date);
    var mainEndTime = new Date(item.end.date);
    var dateCreated = new Date(item.created);
    var dateUpdated = new Date(item.updated);
    // Check if updated event was created (not deleted nor merely edited)
    if (dateUpdated - dateCreated < 1000 && item.status != "cancelled") {
      var mainEventTitle = item.summary;
      // Setting event B title
      var eventBTitle = 'whatever_event_B_is_for ' + mainEventTitle;
      // Setting event B dates
      var numDaysB = 7 // Number of days before main event, edit according to each event
      var startTimeB = mainStartTime;
      startTimeB.setDate(mainStartTime.getDate() - numDaysB);
      var endTimeB = mainEndTime;
      endTimeA.setDate(mainEndTime.getDate() - numDaysB);
      // Creating event B
      createEvent(eventBTitle, startTimeB, endTimeB)
    }
  }
}

// This function receives a title, a start time and an end time and creates the corresponding event
function createEvent(title, startTime, endTime) {
  try {
    var calendar = CalendarApp.getCalendarById('your_calendar_id');
    var event = calendar.createEvent(title, startTime, endTime)
    Logger.log(event)
  } catch(err) {
    Logger.log(err)
  }
}

I hope this is useful to you!

Iamblichus
  • 18,540
  • 2
  • 11
  • 27