简体   繁体   English

使用JavaScript Google Calendar API V3获取特定日期的事件

[英]Get events from a certain day with the javascript google calendar api v3

Hello my main goal is to grab all the events from a google calendar and display their name with the date (and time if available) underneath. 您好,我的主要目标是从Google日历中获取所有事件,并在其下方显示日期和时间(如果有的话)。 I am using the javascript google calendar api v3 and are running into troubles grabbing the date for each event. 我正在使用javascript Google日历api v3,在获取每个事件的日期时遇到麻烦。 Before I explain my problems further here is the current code: 在我进一步解释问题之前,这里是当前代码:

 var clientId = '200816328603.apps.googleusercontent.com'; var apiKey = 'AIzaSyD3rbV__d8u6r9u5GioBU0oVwa-53YXRqM'; var scopes = 'https://www.googleapis.com/auth/calendar'; function handleClientLoad() { gapi.client.setApiKey(apiKey); window.setTimeout(checkAuth,1); checkAuth(); } function checkAuth() { gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult); } function handleAuthResult(authResult) { var authorizeButton = document.getElementById('authorize-button'); if (authResult) { authorizeButton.style.visibility = 'hidden'; makeApiCall(); } else { authorizeButton.style.visibility = ''; authorizeButton.onclick = handleAuthClick; } } function handleAuthClick(event) { gapi.auth.authorize( {client_id: clientId, scope: scopes, immediate: false}, handleAuthResult); return false; } //document.createTextNode(resp.items[i].summary) function makeApiCall() { gapi.client.load('calendar', 'v3', function() { var request = gapi.client.calendar.events.list({ 'calendarId': 'pvhs.k12.nj.us_r6jaor04o80hpsaldf17civeio@group.calendar.google.com' }); request.execute(function(resp) { for (var i = 0; i < resp.items.length; i++) { //---------nodes for html elements var title = document.createTextNode(resp.items[i].summary); //titles are undefined so I'm using the summary as title instead //var description = document.createTextNode(resp.items[i].description); //there are no descriptions apparently var date = document.createTextNode('Start: ' + resp.items[i].start.date + ' End: ' + resp.items[i].end.date); //resp.items[i].date returns undefined //---------html elements var div = document.createElement('div'); div.className = resp.items[i].summary; var h1 = document.createElement('h1'); h1.appendChild(title); div.appendChild(h1); //loop is to filter out all the undefined if (date.textContent != 'Start: undefined End: undefined') { var p = document.createElement('p'); p.appendChild(date); div.appendChild(p); } document.body.appendChild(div); } }); }); } 

Now resp.items[i].start.date and resp.items[i].end.date will return the date occasionally but sometimes just return undefined. 现在,resp.items [i] .start.date和resp.items [i] .end.date有时会返回日期,但有时会返回未定义的日期。 How else can I grab the date of the event then? 那我还能如何获取活动日期?

My idea to get around this was to loop through every day of the month and grab all the events for that day. 我想解决这个问题的想法是循环浏览每月的每一天,并获取当天的所有事件。 However when I tried adding the timeMax and timeMin parameters with the RFC3339 timestamp for the wanted day it didn't return anything. 但是,当我尝试在所需的日期中将timeMax和timeMin参数与RFC3339时间戳添加时,它没有返回任何内容。 Could someone provide me with an example for accomplishing this? 有人可以为我提供实现此目标的示例吗?

Any help is greatly appreciated. 任何帮助是极大的赞赏。

In order to use timeMin or timeMax you need to set 'singleEvents' to true which I believe returns individual instances of reoccurring events instead of the event group. 为了使用timeMin或timeMax,您需要将'singleEvents'设置为true,我相信它会返回重复发生的事件的单个实例,而不是事件组。

var request = gapi.client.calendar.events.list({
        'calendarId': 'pvhs.k12.nj.us_r6jaor04o80hpsaldf17civeio@group.calendar.google.com',
        'singleEvents': true, /* required to use timeMin */ 
        'timeMin': '2013-02-01T11:43:22.000Z'
    });

hope that helps. 希望能有所帮助。

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

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