标签:javascript google-api google-calendar-api
您好我的主要目标是从谷歌日历中获取所有活动,并在下方显示其日期(如果可用的话)的名称.我正在使用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会偶尔返回日期,但有时只返回undefined.那我怎么能抓住活动的日期呢?
我想解决这个问题的方法是在每个月的每一天进行循环并抓住当天的所有事件.但是,当我尝试使用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,google-api,google-calendar-api 来源: https://codeday.me/bug/20190901/1780212.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。