简体   繁体   English

使用 Google Calendar API v3 获取重复事件的下一个实例

[英]Get the next instance of recurring events with the Google Calendar API v3

I am trying to get the events in my calendar over a date range (single or recurring events alike).我试图在我的日历中获取某个日期范围内的事件(单一事件或重复事件等)。 Now I will not necessarily know the exact datetime for the next instance of a recurring event as .现在我不一定知道重复事件的下一个实例的确切日期时间为 . For recurring events I want the date of the next occurrence rather than the date of the first instance as I am getting with the request below.对于重复发生的事件,我想要下一次发生的日期而不是第一次发生的日期,因为我收到了下面的请求。 How do I do this?我该怎么做呢?

https://www.googleapis.com/calendar/v3/calendars/ {calendarid}/events?fields=items(id,summary,start)&key={APIkey}&timeMax={enddate}&timeMin={startdate} https://www.googleapis.com/calendar/v3/calendars/ {calendarid}/events?fields=items(id,summary,start)&key={APIkey}&timeMax={enddate}&timeMin={startdate}

Add in the URL parameter singleEvents=True .添加 URL 参数singleEvents=True This will expand all recurring events into their individual items.这会将所有重复发生的事件扩展到各自的项目中。 There isn't any way to specifically get just the second event in a series if you don't already know when it is, but using that parameter will let you parse out all instances and get the one you want.如果您还不知道它是什么时候,则没有任何方法可以专门获取系列中的第二个事件,但是使用该参数可以让您解析所有实例并获得您想要的实例。

Source: https://developers.google.com/google-apps/calendar/v3/reference/events/list来源: https : //developers.google.com/google-apps/calendar/v3/reference/events/list

long now = System.currentTimeMillis();

Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(eventsUriBuilder, Long.MIN_VALUE);
ContentUris.appendId(eventsUriBuilder, Long.MAX_VALUE);
String[] projection = new String[]{CalendarContract.Instances.CALENDAR_ID, CalendarContract.Instances.TITLE,
        CalendarContract.Instances.DESCRIPTION, CalendarContract.Instances.BEGIN,
        CalendarContract.Instances.END, CalendarContract.Instances.EVENT_LOCATION,
        CalendarContract.Instances.EVENT_ID};

Uri eventsUri = eventsUriBuilder.build();
Cursor instance_cursor = getActivity().getContentResolver().query(
        eventsUri, projection, CalendarContract.Instances.BEGIN + " >= " + now + " and " + CalendarContract.Instances.BEGIN
                + " <= " + (now + 2592000000L) + " and " + CalendarContract.Instances.VISIBLE + " = 1",
        null, CalendarContract.Instances.DTSTART + " ASC");

There is now direct way of doing that but you can hack together a method to accomplish that.现在有直接的方法可以做到这一点,但您可以将一种方法组合在一起来实现。

The idea is that you can specify time boundaries and limit the number of records and that's enough to do the job.这个想法是您可以指定时间边界并限制记录数量,这足以完成这项工作。

So setting the timeMin to now (or right after your reference event end date), timeMax to a big enough number, like a year from timeMin and maxResults = 1 will get the next instance if it exists.因此,将timeMin设置为现在(或在您的参考事件结束日期之后),将timeMax设置为足够大的数字,例如从 timeMin 和maxResults = 1 算起的一年,如果它存在,将获得下一个实例。

Here is how this can be done in nodejs:以下是在 nodejs 中如何做到这一点:

    let startDate = startTime ? new Date(startTime) : new Date()
    let endDate = endTime ? new Date(endTime) : new Date(startDate.getTime() + 12 * 31 * 24 * 60 * 60 * 1000) // 1 year ahead
    let startDateISO = startDate.toISOString()
    let endDateISO = endDate.toISOString()

    let result = await  calendar.events.instances({
        calendarId: "primary",
        eventId: masterEventId,
        maxResults: 1,
        timeMin: startDateISO,
        timeMax: endDateISO,
        orderBy: "startTime",
        timeZone: "UTC"
    })
    let nextInstance = res.data.items[0]

Similarly you can get previous instance if you order results in descending order and reverse the time boundaries (set timeMin to a year back and timeMax before the start of the reference event).同样,如果您按降序对结果进行排序并反转时间边界(将timeMin设置为一年前,将timeMax设置为参考事件开始之前),则您可以获得前一个实例。

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

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