0

Reading Google API documentation, it was very easy to retrieve events in a calendar, but I realized that there are a few exceptions:

1- Recurrent events: There are a bunch of questions regarding retrieving these evens in other posts. The solution that I found in other posts was to set singleEvents to true, but unfortunately in this case there will be problems with all-day events.

2- Usually each event should have an id, which is a unique code. When you retrieve recurrences of an event the id will have a pattern like "mainEventID_recurrenceTimestamp", but when you retrieve the list of events in a calendar, there should be no underscore in the event id. However sometimes when someone updates an event on your calendar, for example updating the location of the event, there will be two events with the same value for all the properties, and the only difference will be the updated property (location) and ids. The updated event will have an id like "mainEventID_recurrenceTimestamp". Two types of problems may happen in such a situation:

a) You will retrieve and process two instances of the same event. b) When you want to find all of the recurrent events in your first request, and retrieve all the recurrences in the following requests, and you pass the id with a pattern like "mainEventID_recurrenceTimestamp", there will be no response.

I spent some time to figure out these exceptions, and I wrote a couple of lines of JavaScript code which can solve these exceptions and retrieve all events from a calendar including recurring and all-day events.

I put my code here, so if anyone else encountered similar exceptions, they will be able to use this code. Also I will appreciate it if you improve this code, so others can benefit from it:

function retrieveAllEvents(yourcalendarId, yourKey) {
    var events;
    var waitForResponses = [];
    var recievingData = false;

    $.get( "https://www.googleapis.com/calendar/v3/calendars/" + yourcalendarId + "/events/?", 
        { (Optional parameters like maxResults), key: yourKey } )
    .done(function( data ) {
        events = data["items"];

        for (i = 0; i < events.length; i++) {
            if (events[i]['status'] != "confirmed") {
                events.splice(i, 1);
                i--;
            }
            for (j = 0; j < events.length; j++) {
                if (i != j && events[i]['id'].indexOf(events[j]['id']) > -1) {
                    events.splice(i, 1);
                    i--;
                    break;
                }
            }
        }

        var thereisRecurrentEvent = false;
        for (i = 0; i < events.length; i++) {
            if ("recurrence" in events[i]) {
                waitForResponses.push(events[i]['id']);
                thereisRecurrentEvent = true;
                events.splice(i, 1);
                i--;
                recieveRecurrentEvent();
            }
        }
        if (!thereisRecurrentEvent) {
            AllEventsReceived(events);
        }
    });

    function retrieveRecurrentEvent() {
        if (!recievingData) {
            recievingData = true;
            $.get( "https://www.googleapis.com/calendar/v3/calendars/" + yourcalendarId + "/events/" + waitForResponses[0] + "/instances?", 
                { (Optional parameters like maxResults), key: yourKey } )
            .done(function( data ) {
                events = events.concat(data["items"]);

                for (j = 0; j < events.length; j++) {
                    if (events[j]['status'] != "confirmed") {
                        events.splice(j, 1);
                        j--;
                    }
                }

                waitForResponses.splice(0, 1);
                if (waitForResponses.length == 0) {
                    AllEventsReceived(events);
                }
                else {
                    recievingData = false;
                    retrieveRecurrentEvent();
                }
            });
        }
    }

    return events;
}
1man
  • 5,216
  • 7
  • 42
  • 56
  • Can you please mention what is the problem with singleEvents=true and all day events? This code does so many calls and work that will be eating quota and does not even handle pagination correctly. – luc Aug 12 '14 at 20:02
  • This is what happened: I used singleEvents=true and all day events did not show up. Would you please try it yourself and inform us about the result? Also I will really appreciate it if you revise my code and make it more efficient, so everybody can benefit from it. Thank you. – 1man Aug 12 '14 at 20:12
  • Yup, I'm sorry if I was a little harsh. Thanks for sharing your code, please check my answer. – luc Aug 13 '14 at 06:28

1 Answers1

0

I tried retrieving all day events with singleEvents = true in the API explorer (https://developers.google.com/apis-explorer/#p/calendar/v3/calendar.events.list):

GET https://www.googleapis.com/calendar/v3/calendars/primary/events?singleEvents=true&timeMax=2014-08-15T00%3A00%3A00Z&timeMin=2014-08-10T00%3A00%3A00Z

and correctly got back an all-day event with:

   "start": {
    "date": "2014-08-08"
   },
   "end": {
    "date": "2014-08-11"
   },

My proposal would be instead of your code to do:

var pageToken;
var events = [];
var query = "https://www.googleapis.com/calendar/v3/calendars/" + yourcalendarId + "/events/?";
do {
    $.get( query, { pageToken: pageToken, singleEvents:true, key: yourKey } )
            .done(function( data ) {
        events = events.concat(data["items"]);
        pageToken = data["nextPageToken"];
    });
}
while (pageToken);
luc
  • 3,642
  • 1
  • 18
  • 21