Created
November 21, 2015 11:13
-
-
Save smallmake/21e5e80855e5977d73c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'icalendar' | |
require 'csv' | |
require 'optparse' | |
option={} | |
OptionParser.new do |opt| | |
opt.on('-f', '--file=VALUE', 'iCal(ics) file path') {|v| option[:ics_file] = v} | |
opt.on('-a', '--after=VALUE','after when: YYYY/mm/dd eg: -a 2015/11/22') {|v| option[:after_when] = v} | |
opt.parse!(ARGV) | |
end | |
exit unless option[:ics_file] | |
csv_path = option[:ics_file].gsub(/\.[^.]+$/, '.csv') | |
puts "Converting... #{option[:ics_file]} -> #{File.basename(csv_path)}" | |
ical_src = IO.read(option[:ics_file]) | |
cal = Icalendar.parse(ical_src, true) | |
if option[:after_when] | |
after_when = DateTime.parse(option[:after_when]) | |
if after_when | |
events = cal.events.select {|e| e.dtstart > after_when}.sort {|e1, e2| e1.dtstart <=> e2.dtstart} | |
else | |
puts "error" | |
exit | |
end | |
else | |
events = cal.events.sort {|e1, e2| e1.dtstart <=> e2.dtstart} | |
end | |
CSV.open(csv_path, "wb:cp932") do |csv| | |
csv << ['Start at','End at','Location','Summary','Description'] | |
events.each do |event| | |
start_at = event.dtstart ? event.dtstart.strftime("%Y/%m/%d %H:%M") : '' | |
end_at = event.dtend ? event.dtend.strftime("%Y/%m/%d %H:%M") : '' | |
description = event.description ? event.description.gsub(/(\r\n|\r|\n)/, "\r\n") : '' | |
row = [start_at, end_at, event.location, event.summary, description] | |
#puts row.join(',') | |
csv << row | |
end | |
end | |
puts "Completed. #{csv_path}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Converting from OSX Calender export file(ics) to CSV file(Japanese Shift-JIS CP932).
USAGE: $ ruby ics_to_csv.rb -f <ics file path> (-a <YYYY/mm/dd>)