简体   繁体   中英

How can I merge 2 google calendars to display events in order?

I'm trying to replace the clunky google calendar agenda view in my googlesite with a display that I create myself doing via google script.

I can get the event detail on the page ok, but I need to show multiple calendars - how do I ensure that the items go in correct order on the page, and are not grouped by calendar?

I could either:

create a temp calendar each time I run the script, merging multiple calendars (note - I can't see any create calendar option)

or dump info to a spreadsheet, and do a sort from there

or somehow loop by time, not calendar.

I think the spreadsheet one is the best option - any other thoughts?

Is there a simple way to copy in the iCal file to a spreadsheet automatically?

George

If PHP and the Zend Gdata library are an option, you can grab the calendar list feed and then loop through each calendar's events and put them into an array. Afterwards, you can sort them by start time with this:

     function cmp( $a, $b )
     {
        if(  $a->when[0]->startTime ==  $b->when[0]->startTime ){ return 0 ; }
        return ($a->when[0]->startTime < $b->when[0]->startTime) ? -1 : 1;
     }

     uasort($allEvents,'cmp');

I've dealt with this a lot. Especially when sending guest list emails for events on more than one of my calendars. The function below will take an array of calendars and a start and end time and turn it into a single array of unique CalendarEvent objects.

You can run a version of the code here . You'll also find that it is commented there as well.

The sort piece is what you seem most interested in. It is contained in the byStart function. You can find more information about array sorting at the MDN Array.Sort Reference . byStart will work for any array of CalendarEvent .

Due to the listy nature of managing calendars, the iterative functions available in Javascript make this light work. MDN has more info on Iteration Methods

Here it is:

function mergeCalendarEvents(calendars, startTime, endTime) {

  var params = { start:startTime, end:endTime, uniqueIds:[] };

  return calendars.map(toUniqueEvents_, params)
                  .reduce(toSingleArray_)
                  .sort(byStart_);
}

function toCalendars_(id) { return CalendarApp.getCalendarById(id); }

function toUniqueEvents_ (calendar) {
  return calendar.getEvents(this.start, this.end)
                 .filter(onlyUniqueEvents_, this.uniqueIds);
}

function onlyUniqueEvents_(event) {
  var eventId = event.getId();
  var uniqueEvent = this.indexOf(eventId) < 0;
  if(uniqueEvent) this.push(eventId);
  return uniqueEvent;
}

function toSingleArray_(a, b) { return a.concat(b) }

function byStart_(a, b) {
  return a.getStartTime().getTime() - b.getStartTime().getTime();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM