gpt4 book ai didi

javascript - 使用 javascript google calendar api v3 获取某一天的事件

转载 作者:行者123 更新时间:2023-12-03 10:03:31 25 4
gpt4 key购买 nike

您好,我的主要目标是从谷歌日历中获取所有事件,并在下方显示其名称以及日期(和时间,如果有)。我正在使用 javascript google calendar api v3,并且在获取每个事件的日期时遇到了麻烦。在我进一步解释我的问题之前,这里是当前代码:


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

现在 resp.items[i].start.date 和 resp.items[i].end.date 将偶尔返回日期,但有时只是返回未定义。那我还能如何获取事件的日期呢?

我解决这个问题的想法是循环遍历每个月的每一天并获取当天的所有事件。但是,当我尝试为所需日期添加带有 RFC3339 时间戳的 timeMax 和 timeMin 参数时,它没有返回任何内容。有人可以为我提供一个实现这一目标的例子吗?

任何帮助是极大的赞赏。

最佳答案

为了使用 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'
});

希望有帮助。

关于javascript - 使用 javascript google calendar api v3 获取某一天的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15914553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com