导入带有完整日历的iCal(ics)



要用fullcalendar加载.ics文件,需要做什么?不幸的是,我不能使用php或.net。

我设法做到了。没有我想象的那么难。我使用ical.js作为ics解析器。解析后,我得到一个json对象,它包含了ics中的所有信息。然后遍历它并根据FullCalendar事件对象的定义构造事件对象。

使用FullCalendar v4更新并重新记录事件

由于v4更改了初始化代码,而下面的代码不包括重复事件,因此这里有一个可以使用v4版本的代码:

$.get(calendarUrl).then(function (data) {
     // parse the ics data
     var jcalData = ICAL.parse(data.trim());
     var comp = new ICAL.Component(jcalData);
     var eventComps = comp.getAllSubcomponents("vevent");
     // map them to FullCalendar events
     var events = $.map(eventComps, function (item) {
        if (item.getFirstPropertyValue("class") == "PRIVATE") {
            return null;
        }
        else {
            var toreturn = {
                "title": item.getFirstPropertyValue("summary"),
                "location": item.getFirstPropertyValue("location"),
            };
            var rrule=item.getFirstPropertyValue("rrule");
            if(rrule!= null){ //event recurs
                toreturn.rrule={};
                if(rrule.freq) toreturn.rrule.freq=rrule.freq;
                if(rrule.parts.BYDAY) toreturn.rrule.byweekday=rrule.parts.BYDAY;
                if(rrule.until) toreturn.rrule.until=rrule.until.toString();
                if(rrule.until) toreturn.rrule.until=rrule.until.toString();
                if(rrule.interval) toreturn.rrule.interval=rrule.interval;
                var dtstart=item.getFirstPropertyValue("dtstart").toString();
                var dtend=item.getFirstPropertyValue("dtend").toString();
                toreturn.rrule.dtstart=dtstart;
                //count duration ms
                var startdate=new Date(dtstart);
                var enddate=new Date(dtend);
                toreturn.duration = enddate - startdate;
            }else{
                toreturn.start=item.getFirstPropertyValue("dtstart").toString();
                toreturn.end=item.getFirstPropertyValue("dtend").toString();
            }
            return toreturn;
        }
     });
     var calendarEl = document.getElementById('calendar');
     var calendar = new FullCalendar.Calendar(calendarEl, {
              plugins: [ 'interaction','dayGrid','rrule' ],
              defaultView: 'dayGridWeek',
              displayEventEnd: true,
              header: {
                  left: 'prev,next',
                  center: 'title',
                  right: 'dayGridDay,dayGridWeek,dayGridMonth'
              },
              events: events,
              eventRender: function (info) {
                // console.log(info.event);
                // append location
                if (info.event.extendedProps.location != null && info.event.extendedProps.location != "") {
                    info.el.append(info.event.extendedProps.location );
                }
              }
     });
     calendar.render();
});

原始答案代码(v3及更低版本):

$.get(calendarUrl).then(function (data) {
// parse the ics data
var jcalData = ICAL.parse(data.trim());
var comp = new ICAL.Component(jcalData);
var eventComps = comp.getAllSubcomponents("vevent");
// console.log(JSON.stringify(eventComps));
// map them to FullCalendar events
var events = $.map(eventComps, function (item) {
    if (item.getFirstPropertyValue("class") == "PRIVATE") {
        return null;
    }
    else {
        return {
            "title": item.getFirstPropertyValue("summary") + ";",
            "start": item.getFirstPropertyValue("dtstart").toJSDate(),
            "end": item.getFirstPropertyValue("dtend").toJSDate(),
            "location": item.getFirstPropertyValue("location")
        };
    }
});
// refresh the control
calendarCtrl.fullCalendar('destroy');
calendarCtrl.fullCalendar({
    events: events,
    timeFormat: "H:mm",
    displayEventEnd: true,
    eventRender: function (event, element) {
        // console.log(element);
        // append location
        if (event.location != null && event.location != "") {
            element.append("<span>" + event.location + "</span>");
        }
    },
    header: {
        left: 'title',
        center: '',
        right: 'today,month,basicWeek,listDay prev,next'
    }
});
});

您需要的是编写自己的fullcalendar扩展(类似于fullcalendary提供的gcal.js),您可以称之为ical.js

你应该知道,编写一个完整的ical解析器可能会非常耗费精力,所以你可能想考虑在后端使用谷歌日历,除非你有令人信服的理由。

如果你正在开发自己的fullcalendar扩展,你可能想看看现有的jquery ical解析器(这里-免责声明:我从未尝试过这个插件)

自V 5.5.0起,支持iCalendar作为事件源。

您可以在此处查看文档:https://fullcalendar.io/docs/icalendar

示例:

import dayGridPlugin from '@fullcalendar/daygrid'
import iCalendarPlugin from '@fullcalendar/icalendar'
var calendar = new Calendar(calendarEl, {
  plugins: [dayGridPlugin, iCalendarPlugin],
  events: {
    url: 'https://mywebsite/icalendar-feed.ics',
    format: 'ics'
  }
})
calendar.render()

您可以将其导入谷歌日历,然后将谷歌日历导入FullCalendar。

如果你有一个wordpress网站,有一个应用程序。http://wordpress.org/extend/plugins/amr-ical-events-list/

如果你没有wordpress网站,请提供更多信息,以便人们能够就你的情况提供更充分的建议-有一些专门的icalendar脚本-我已经有一段时间没有看了,所以不能保证任何例如:http://phpicalendar.net/

最新更新