【问题标题】:How to import ICS file with multiple events into multiple calendars?如何将具有多个事件的 ICS 文件导入多个日历?
【发布时间】:2019-04-21 22:17:12
【问题描述】:

生成ICS文件后,当我导入Outlook/Google时,发现Add by URL时只有一个事件被导入。即使使用Import 选项,它也会给出消息1 out of 9 events imported

在检查了多个链接、iCalendar 规范之后,我很清楚这是非常稀少的文档。另外,到目前为止,没有人能够回答这类问题。有“类似”的问题,尽管没有一个对我和其他许多人有用!

代码

PHP

function generateICS()
{
require_once(__DIR__."/libs/icalendar/zapcallib.php");
$iCalObj = new ZCiCal();
$ZDateHelper = new ZDateHelper();
$handle = fopen(__DIR__."\\generated\\eventlist.txt", "r");
if($handle){
    while(($line = fgets($handle)) !== false){
        $eventarr = json_decode($line, true);
        $eventObj = new ZCiCalNode("VEVENT", $iCalObj->curnode);
        // add title
        $eventObj->addNode(new ZCiCalDataNode("SUMMARY:" . $eventarr["EventName"]));
        if(isset($eventarr['EventDuration'])){
            // create timestamp
            $start = date_timestamp_get(date_create_from_format("y-M-d H:i", $eventarr['EventStart']));
            // add start date
            $eventObj->addNode(new ZCiCalDataNode("DTSTART:" . $ZDateHelper->fromUnixDateTimetoiCal($start)));
            // add duration
            $eventObj->addNode(new ZCiCalDataNode("DURATION:" . $eventarr['EventDuration']));
        }
        else{
            // create timestamp
            $start = date_timestamp_get(date_create_from_format("y-M-d H:i", $eventarr['EventStart']));
            $end = date_timestamp_get(date_create_from_format("y-M-d H:i", $eventarr['EventEnd']));

            // add start date
            $eventObj->addNode(new ZCiCalDataNode("DTSTART:" . $ZDateHelper->fromUnixDateTimetoiCal($start)));
            // add end date
            $eventObj->addNode(new ZCiCalDataNode("DTEND:" . $ZDateHelper->fromUnixDateTimetoiCal($end)));
        }
        // UID is a required item in VEVENT, create unique string for this event
        // Adding your domain to the end is a good way of creating uniqueness
        $uid = "event".time()."@company.org";
        $eventObj->addNode(new ZCiCalDataNode("UID:" . $uid));
        $eventObj->addNode(new ZCiCalDataNode("Description:" . ZCiCal::formatContent($eventarr['EventDesc'])));
        $eventObj->addNode(new ZCiCalDataNode("METHOD:" . ZCiCal::formatContent("PUBLISH")));
        $eventObj->addNode(new ZCiCalDataNode("X-WR-CALNAME:" . ZCiCal::formatContent("OurCalendar")));
        $eventObj->addNode(new ZCiCalDataNode("X-WR-TIMEZONE:" . ZCiCal::formatContent("(GMT-05:00) Eastern Time (US & Canada)")));

        $eventObj->addNode(new ZCiCalDataNode("SEQUENCE:" . ZCiCal::formatContent("0")));
        $eventObj->addNode(new ZCiCalDataNode("CLASS:" . ZCiCal::formatContent("PUBLIC")));
        $eventObj->addNode(new ZCiCalDataNode("DTSTAMP:".$ZDateHelper->fromUnixDateTimetoiCal(time())));
        $eventObj->addNode(new ZCiCalDataNode("LAST-MODIFIED:".$ZDateHelper->fromUnixDateTimetoiCal(time())));
    }
    fclose($handle);
}
else{
    echo "<div class='alert alert-danger'>Error reading events file!</div>";
}
$ics = $iCalObj->export();
echo $ics;
file_put_contents(__DIR__."\\generated\\company.ics", $ics);
}

Javascript

function genBtns() {
var _html = "";
var subIcal = "<a class='m-1 btn bg-lightdark text-dark' href='" + encodeURI("webcal://outwebsite.net/generated/ourcal.ics") + "'>Subscribe to iCalendar</a>";
var subGcal = "<a class='m-1 btn bg-lightdark text-dark' href='http://www.google.com/calendar/render?cid=" + encodeURI("webcal://ourwebsite.net/generated/ourcal.ics") + "'>Subscribe with Google Calendar</a>";
var subMcal = "<a class='m-1 btn bg-lightdark text-dark' href='http://calendar.live.com/calendar/calendar.asp?rru=addsubscription&url=" + encodeURI("https://ourwebsite.net/generated/ourcal.ics") + "&name=" + encodeURI("OurCalendar") + "'>Subscribe with Microsoft Calendar</a>";
_html = subIcal + "<br/>" + subGcal + "<br/>" + subMcal;
return _html;
}

其中,第一个按钮直接链接到文件本身,第二个按钮有效并链接到google,最后一个按钮无效,但链接到outlook。

目标

我们的目标是从我们的银行网站后端读取用户的日历,然后解析事件,并使它们在多个流行的日历中可用,以便用户可以无缝地同步他的付款日期和其他此类事件。

注意:这样的日历不是 iCal 格式,但为了模拟这一点,我编写了 php 脚本并假设事件列表以这种格式到达文本文件中。

{"EventName":"Car loan Payment","EventDesc":"","EventStart":"18-Nov-22 09:00","EventDuration":"PT24H"}
{"EventName":"Bernie's Anniversary ","EventDesc":"A small gathering to celebrate Bernie's 10 successful years at the company!","EventStart":"18-Nov-17 16:00","EventEnd":"18-Nov-17 18:00"}
{"EventName":"Snow day","EventDesc":"Snow all day!","EventStart":"18-Nov-15 05:00","EventDuration":"PT24H"}
.
.
.

生成的ICS(iCalendar)文件如下图所示:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//ZContent.net//ZapCalLib 1.0//EN
CALSCALE:GREGORIAN
METHOD:PUBLISH
BEGIN:VEVENT
SUMMARY:Car loan Payment
DTSTART:20181122T090000
DURATION:PT24H
UID:event1542634898@Company.org
Description:
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Bernie's Anniversary 
DTSTART:20181117T160000
DTEND:20181117T180000
UID:event1542634898@Company.org
Description:A small gathering to celebrate Bernie's 10 successful years at 
the company!
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Snow day
DTSTART:20181115T050000
DURATION:PT24H
UID:event1542634898@Company.org
Description:Snow all day!
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Gary's Bday
DTSTART:20181127T170000
DTEND:20181127T190000
UID:event1542634898@Company.org
Description:A small celebration for Gary's Birthday 
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Avengers Movie night
DTSTART:20181115T180000
DURATION:PT24H
UID:event1542634898@Company.org
Description:Watch avengers movie for free tonight!
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Electric Bill Payment
DTSTART:20181118T070000
DURATION:PT24H
UID:event1542634898@Company.org
Description:Please make a payment of $50 towards this month's bill!
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Gas Bill Payment
DTSTART:20181118T080000
DURATION:PT24H
UID:event1542634898@Company.org
Description:Please make a payment of $150 towards the current gas bill!
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Snow day
DTSTART:20181128T060000
DURATION:PT24H
UID:event1542634898@Company.org
Description:A winter storm warning is in effect and DG will remain closed f
or the day
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
BEGIN:VEVENT
SUMMARY:Thanksgiving Lunch
DTSTART:20181123T120000
DTEND:20181123T140000
UID:event1542634898@Company.org
Description:Please gather for free food friday\, where you can enjoy turkey
  and salad!
METHOD:PUBLISH
X-WR-CALNAME:CompanyCalendar
X-WR-TIMEZONE:(GMT-05:00) Eastern Time (US & Canada)
SEQUENCE:0
CLASS:PUBLIC
DTSTAMP:20181119T134138
LAST-MODIFIED:20181119T134138
END:VEVENT
END:VCALENDAR

挑战

  1. 全天事件未正确解析。
  2. 无法正常导入其他日历。
  3. 文档太宽泛,示例太少。
  4. 库对此的支持非常有限。
  5. Windows 10 日历中的相同 ics 文件在通过单击打开并显示所有事件时工作正常(所有事件都已加载)。但是,时间仍未结束,如之前多篇帖子所述,全天活动将于第二天凌晨 12 点结束,无论PT24H 持续时间如何。
  6. 像 Stanza.co 这样的流行网站创建自己的日历,然后允许用户从所有其他日历订阅它。这不是开源的。

我研究的内容与您在使用任何搜索栏时都能找到的内容完全相同。我在多个网站上围绕整个主题访问了至少 100 篇或更多文章。这是我第二次尝试得到答案。

【问题讨论】:

    标签: php icalendar


    【解决方案1】:

    有错误。通过各种 ics 验证器放置您的 ics 文件。我尽可能多地做,因为他们不都说同样的话。 Google 不会告诉您出了什么问题,只会拒绝无效事件。将一个验证器(https://icalendar.org/validator.html)与我得到的你的 ics 内容一起使用:

    问题!发现5个错误错误

    UID value is not unique, duplicate found near line # 6Reference: RFC 5545 3.8.4.7. Unique Identifier
    UID value is not unique, duplicate found near line # 20Reference: RFC 5545 3.8.4.7. Unique Identifier
    Missing DTSTAMP property near line # 35Reference: RFC 5545 3.6.1. Event Component
    Missing UID property near line # 35Reference: RFC 5545 3.6.1. Event Component
    Missing DTSTART property in VEVENT near line # 35Reference: RFC 5545 3.6.1. Event Component
    

    【讨论】:

    • 谢谢,这是合理的,会的。我已经停止了这方面的开发,并开始查看 Outlook 和谷歌日历 API 本身。 Outlook MSGraph API 似乎很容易使用。我还不知道这是否适合我的项目。
    猜你喜欢
    • 1970-01-01
    • 2019-01-21
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    相关资源
    最近更新 更多