简体   繁体   English

如何使用JavaScript从Google Calendar API检索所有事件

[英]How to retrieve all events from Google Calendar API, using JavaScript

Reading Google API documentation, it was very easy to retrieve events in a calendar, but I realized that there are a few exceptions: 阅读Google API文档,可以很轻松地检索日历中的事件,但是我意识到有一些例外:

1- Recurrent events: There are a bunch of questions regarding retrieving these evens in other posts. 1-重复发生的事件:在其他帖子中,有关检索这些事件的问题很多。 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. 我在其他文章中找到的解决方案是将singleEvents设置为true,但是不幸的是,在这种情况下,全天事件都会出现问题。

2- Usually each event should have an id, which is a unique code. 2-通常每个事件应有一个ID,这是一个唯一的代码。 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. 当您检索事件的重复发生时,该ID将具有类似“ mainEventID_recurrenceTimestamp”的模式,但是当您检索日历中的事件列表时,该事件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. 但是,有时当某人更新您日历上的事件(例如更新事件的位置)时,会有两个事件的所有属性具有相同的值,唯一的区别是更新的属性(位置)和ID。 The updated event will have an id like "mainEventID_recurrenceTimestamp". 更新后的事件的ID将类似于“ mainEventID_recurrenceTimestamp”。 Two types of problems may happen in such a situation: 在这种情况下,可能会发生两种类型的问题:

a) You will retrieve and process two instances of the same event. a)您将检索并处理同一事件的两个实例。 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. b)当您想在第一个请求中找到所有重复发生的事件,并在以下请求中检索所有重复发生,并且使用诸如“ mainEventID_recurrenceTimestamp”之类的模式传递ID时,将没有响应。

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. 我花了一些时间来找出这些异常,并且编写了几行JavaScript代码,可以解决这些异常并从日历中检索所有事件,包括重复事件和全天事件。

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;
}

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 ): 我尝试在API资源管理器( https://developers.google.com/apis-explorer/#p/calendar/v3/calendar.events.list )中使用singleEvents = true检索全天事件:

GET https://www.googleapis.com/calendar/v3/calendars/primary/events?singleEvents=true&timeMax=2014-08-15T00%3A00%3A00Z&timeMin=2014-08-10T00%3A00%3A00Z 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM