Google Calendar API: get list of free slots on specified day

后端 未结 1 1351
野性不改
野性不改 2021-02-11 06:33

I need to get list of free slots in my google calendar. Now I am just getting list of events. I am using google calendar npm.

google_calendar.events.list(calObj.         


        
相关标签:
1条回答
  • 2021-02-11 07:29

    You can get free slots from google calendar in two steps. Using npm google-calendar

    1st you have to get all free/busy times in your 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);
    }
    
    0 讨论(0)
提交回复
热议问题