简体   繁体   English

如何在Perl中将多个日历(iCal格式)合并为一个?

[英]How do I merge several calendar (iCal format) into one in Perl?

I have several non-overlapping ical files that I want to merge into one ical file. 我有几个非重叠的ical文件,我想合并为一个ical文件。
What is the preferred way to do this? 首选的方法是什么?

After reading correct (self)-answer from @knarf and Format iCalendar files at support.google.com, I succesfully used this: 从@knarf读取正确的(自我)答案并在support.google.com上格式化iCalendar文件后,我成功使用了此方法:

mergeIcal() {
    sed -e '$d' $1
    sed -e '1,/VEVENT/{/VEVENT/p;d}' $2
}

This take whole first argument file but not last line (footer) and concatenate second argument file without header. 这将占用整个第一个参数文件,而不是最后一行(页脚),并连接没有标题的第二个参数文件

Event duplication will be automatically dropped when loaded by most common Calendar Agent . 由大多数常见的Calendar Agent加载时,事件重复项将被自动删除。

There are a couple of options (ordered IMHO least useful to most useful for your task): 有两个选项(按顺序排列的恕我直言,对您的任务最无用,而对您的任务最有用):

  • If you prefer to use the "standard" DateTime set of modules for datetime manipulation, you can parse in and print calendars using DateTime::Format::ICal . 如果您更喜欢使用“标准” DateTime模块集进行日期时间操作,则可以使用DateTime::Format::ICal解析并打印日历。 However, that module parses iCal formatted data but not files. 但是,该模块解析的是iCal格式的数据,而不是文件。

  • For purpose of parsing/combining multiple files, you can use iCal::Parser . 为了解析/合并多个文件,可以使用iCal::Parser From SYNOPSIS (see line #3 from the botom): 从提要(请参阅底部的第3行):

     use iCal::Parser; my $parser=iCal::Parser->new(); my $hash=$parser->parse($file); $parser->parse($another_file); my $combined=$parser->calendar; 

    Or 要么

     use iCal::Parser; my $combined=iCal::Parser->new->parse(@files); #### BINGO!!!!! 

    However, this module doesn't print the resulting merged data back into a file , at least that I know of. 但是,此模块不会将生成的合并数据打印回到文件中 ,至少我知道。

  • Data::ICal set of modules has both parsing and printing, with main module Data::ICal capable of generating a file. Data::ICal模块集具有解析和打印功能,主模块Data :: ICal能够生成文件。

    I wasn't able to find out how to merge the different calendars officially, but it should be fairly trivially to do by looping over second calendar object's entries and properties (one's an arrayref one is a hash) and using add_entry() and add_property() of the first calendar object to merge. 我无法找到如何正式合并不同日历的方法,但是通过遍历第二个日历对象的entriesproperties (一个arrayref就是一个哈希)并使用add_entry()add_property()合并的第一个日历对象。

  • I think you should also be able to convert ical::Parser merged data into Data::ICal , since the former produces lists of Text::vFile::asData objects (with dates replaced by DateTime objects); 我认为您还应该能够将ical::Parser合并的数据转换为Data::ICal ,因为前者会生成Text::vFile::asData对象的列表(日期替换为DateTime对象); and the latter has parse_object() method to parse Text::vFile::asData objects. 后者具有parse_object()方法来解析Text::vFile::asData对象。

I ended up concatenate every file without their header/footer and adding header/footer manually. 我最终将每个没有头文件/页脚的文件连接起来,然后手动添加头文件/页脚。
(I can't accept my own answer whithin 2 days) (我无法在2天内接受自己的回答)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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