谷歌日历API:获取指定日期的空闲时间列表

4

我需要获取我的谷歌日历中的免费时间槽列表。现在我只能获取事件列表。我正在使用google calendar npm。

google_calendar.events.list(calObj.name,{
    timeMin: "2018-03-02T08:00:00.000Z",
    timeMax: "2018-03-02T16:00:00.000Z",
    singleEvents: true,
    orderBy: "startTime"
}, function (err, eventList) {
    // handle to get output like
    // freeSlots -> [{
    //     "startDate": "2018-03-02T08:00:00.000Z",
    //     "endDate": "2018-03-02T09:00:00.000Z"
    // },{
    //     "startDate": "2018-03-02T07:00:00.000Z",
    //     "endDate": "2018-03-02T08:00:00.000Z"
    // }]

    // if at this day are events between 10:00 and 16:00 (so calendar is busy)
})

据我所知,没有API端点可以获取特定日期的空闲时间列表。一个解决方法是获取当天的事件列表,然后从一天的开始到结束进行排列。计算事件之间的时间差。这样你就可以得到那一天的空闲时间。希望这能帮到你。 - Mr.Rebot
首先,您需要定义“时间段”。日历没有固定的“时间段”。事件可以任意长短,并且可以在任何您喜欢的时间开始/结束,因此您的问题并没有真正意义上的答案。 - ADyson
1个回答

5
你可以通过两个步骤从谷歌日历中获取免费时间。使用npm google-calendar
第一步,你需要获取你的日历中所有的空闲/忙碌时间。
var startDate = new Date(),
    endDate = new Date();

var rootStart = startDate,
    rootEnd = endDate;

gcal(<accessToken>).freebusy.query({
    "items":[{
        "id": calObj.name
    }],
    "timeMin": startDate.toISOString(),
    "timeMax": endDate.toISOString(),
    "timeZone": "GMT+0100"
},{
    fields: "calendars,groups,kind,timeMax,timeMin", 
    alt:"json"
}, function(err, data) {
    if(err) return console.log(err)

    // then calculate free slots
    return slotsFromEvents(startDate, data.calendars[<calName>].busy)
})

var interval = 2, // how big single slot should be (in this case 2 hrs) 
freeSlots = []; 

function slotsFromEvents(date,events) {
    events.forEach(function (event, index) { //calculate free from busy times
        if (index == 0 && startDate < event.start) {
            freeSlots.push({startDate: startDate, endDate: event.start});
        }
        else if (index == 0) {
            startDate = event.end;
        }
        else if (events[index - 1].end < event.start) {
            freeSlots.push({startDate: events[index - 1].end, endDate: event.start});
        }

        if (events.length == (index + 1) && event.end < endDate) {
            freeSlots.push({startDate: event.end, endDate: endDate});
        }
    });


    if (events.length == 0) {
        freeSlots.push({startDate: startDate, endDate: endDate});
    }

    var temp = {}, hourSlots = [];
    freeSlots.forEach(function(free, index) {
        var freeHours = new Date(free.endDate).getHours() - new Date(free.startDate).getHours(), freeStart = new Date(free.startDate), freeEnd = new Date(free.endDate);
        while(freeStart.getHours()+freeHours+interval>=0) { // 11 + 4 + 2 >= 0
            if(freeHours>=interval) {
                temp.e = new Date(free.startDate);
                temp.e.setHours(temp.e.getHours()+freeHours);
                temp.s = new Date(free.startDate);
                temp.s.setHours(temp.s.getHours()+freeHours-interval);
                if(temp.s.getHours() >= rootStart.getHours() && temp.e.getHours() <= rootEnd.getHours()) {
                    hourSlots.push({calName: calObj.name, startDate:temp.s, endDate:temp.e});
                    temp = {};
                }
            }
            freeHours--;
        }
    })

    // callBack(freeSlots, hourSlots);
}

你好。这个可以轻松转换成一种可与其他人共享的日历形式(类似于“我的空闲时间安排”)吗? - Vicent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接