2013-04-09 67 views
2

您好,我的主要目標是從谷歌日曆中抓取所有事件,並在下方顯示其日期(以及時間,如果可用)的名稱。我正在使用JavaScript的谷歌日曆api v3,並遇到麻煩搶劫每個事件的日期。在我進一步的解釋我的問題是當前的代碼:從javascript日曆api v3獲取活動來自某一天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': '[email protected]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參數爲所需的一天它不返回任何東西。有人能爲我提供一個實現這個目標的例子嗎?

任何幫助,非常感謝。

回答

4

爲了使用timeMin或timeMax,您需要將'singleEvents'設置爲true,我相信這會返回重現事件的單個實例而不是事件組。

var request = gapi.client.calendar.events.list({ 
     'calendarId': '[email protected]com', 
     'singleEvents': true, /* required to use timeMin */ 
     'timeMin': '2013-02-01T11:43:22.000Z' 
    }); 

希望有幫助。