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:
- Create a script and enable the Calendar advance service.
- 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).
- 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();
}
- 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!