0

I would like to write a Google Apps Script that will check if there are any conflicts for a recurring events before I send it to Google Calendar from a Google Sheet.

I am able to check on the first date using:

var ConTimes = calendarAll.getEvents(startDate, endDate, {search: roomNumber});
Logger.log('Conflicting Times: ' + ConTimes.length);
Logger.log('Conflicting dates: ' + ConTimes);

If the length is greater than 0, then I know there is a conflict; however, this script will not check any of the recurring events.

It would be nice if there was a simple script that would search for all Calendar events with a specific word, and then return the number of overlapping events between the current Calendar events and the recurring events I would like to schedule. I imagine this ideal script would be similar to the one I use for adding the recurring events:

var recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekdays(weekdays).times(times);  
var eventSeries = calendarSingle.createEventSeries(newEventTitle, startDate, endDate, recurrence);

In my spreadsheet, I have these columns:row-data for example:

  • RoomNumber: 4000;
  • EventName: Geology101;
  • Start DateTime: 7/23/2018 12:00:00;
  • End DateTime: 7/23/2018 14:00:00;
  • RecDaysPerWeek: MONDAY, WEDNESDAY, FRIDAY;
  • NumberOfWeeks: 4.

I'm sure I can write a long script that will give me all the dates for the Mondays, Wednesdays, and Fridays over the next 4 weeks, and then check if each date has a conflict, but this is costly. Has anyone seen if Google has a code written for searching recurrences?

Thanks. +++++++++Some Edits for Clarification+++++++++++ I'm not sure if I explained this clearly enough, so here is a sample of what the costly code would look like and produce:

var startDate=Mon Jul 23 2018 07:22:11 GMT-0400 (EDT);
var endDate=Mon Jul 23 2018 08:22:11 GMT-0400 (EDT);
var times=4;
var recurringDays="MONDAY, WEDNESDAY, FRIDAY";
var room = "5071";

if (recurringDays == "MONDAY, WEDNESDAY, FRIDAY") {

for (var i = 1; i < times; i++)  {   
  //Note: this assumes the first weekday and the start date are the same
  // Then what is the date of the first Monday
  var MondayStartDate = startDate;
  var MondayEndDate = endDate;  
       var ConTimes = calendarAll.getEvents(startDate, endDate, {search: room});
            Logger.log('Conflicting Times: ' + ConTimes.length);   
  //and what are the dates of the first Wednesday
  var WednesdayStartDate = startDate.setDate(startDate.getDate()+2);
  var WednesdayEndDate = endDate.setDate(endDate.getDate()+2);
       var ConTimes = calendarAll.getEvents(startDate, endDate, {search: room});
            Logger.log('Conflicting Times: ' + ConTimes.length); 
  //and what is the date of the first Friday
  var FridayStartDate = startDate.setDate(startDate.getDate()+2);
  var FridayEndDate = endDate.setDate(endDate.getDate()+2); 
       var ConTimes = calendarAll.getEvents(startDate, endDate, {search: room});
            Logger.log('Conflicting Times: ' + ConTimes.length); 
  var     Back2MonStartDate = startDate.setDate(startDate.getDate()+3);
  var     Back2MonEndDate = endDate.setDate(endDate.getDate()+3);      
 } }

The script produces a log that looks like this:

  • [18-07-23 13:36:44:551 EDT] times: 5
  • [18-07-23 13:36:44:552 EDT] room: room 5071
  • [18-07-23 13:36:44:634 EDT] Conflicting Times: 2
  • [18-07-23 13:36:44:682 EDT] Conflicting Times: 2
  • [18-07-23 13:36:44:746 EDT] Conflicting Times: 2
  • [18-07-23 13:36:44:806 EDT] Conflicting Times: 2
  • [18-07-23 13:36:44:878 EDT] Conflicting Times: 2
  • [18-07-23 13:36:44:948 EDT] Conflicting Times: 2
  • [18-07-23 13:36:45:017 EDT] Conflicting Times: 2
  • [18-07-23 13:36:45:091 EDT] Conflicting Times: 2
  • [18-07-23 13:36:45:158 EDT] Conflicting Times: 2
  • [18-07-23 13:36:45:234 EDT] Conflicting Times: 2
  • [18-07-23 13:36:45:322 EDT] Conflicting Times: 2
  • [18-07-23 13:36:45:371 EDT] Conflicting Times: 2

I would have to write the for loop script for every combination of day (e.g."MONDAY, WEDNESDAY, FRIDAY") and run the getEvents search for each day.

From the log file, I can actually find which day on my calendar has the conflicting day (e.g. the third "2" is the first Friday after the start day.

If there is no significantly shorter Google Apps Script that reduces the number of getEvents searches, then I guess this script is what I need to write.

Thanks for the help.

gg-edu
  • 165
  • 1
  • 8
  • I imagine you could batch request [`FreeBusy`](https://developers.google.com/calendar/v3/reference/freebusy) information. If your goal is to check for conflicts before sending to Google Calendar, then I don't think there is any way around *you* computing when an event will recur and then querying freebusy for the relevant persons. – tehhowch Jul 22 '18 at 20:30
  • 1
    You can use the `isRecurringEvent()` method on the event to determine whether it's a recurring event. If it's a recurring event, then the same days of the week and times are in effect for the recurring events, so you should be able to compare the day of the week and the time of day. There is also a method on the Event to `getEventSeries()` – Alan Wells Jul 22 '18 at 22:26

0 Answers0