仕事上必要な工夫など、備忘録として残します。
[Ruby][iCal] iCal_to_CSVのEXE版なのですが・・・
ical_to_csv xxxx.ics とするだけです。 xxxx.ics はiCalファイル名です。 xxxx.CSV に変換されます。
[Ruby][iCal]iCalファイルをCSV形式に変換するコード
iCal(エンコードShift-JIS)をCSVに変換するRubyコードを作成
#! ruby -Ks
# coding:windows-31j
#! /usr/bin/env ruby
require 'rubygems'
require 'csv'
require 'nkf'
require 'ri_cal'
require 'kconv'
require 'vr/vrdialog'
#OCRA でコンパイルする時に内容チェックさせない。
unless (defined?(Ocra))
#入力ファイル名を引数で受け取る。引数がなければ中止。
inf=ARGV[0]
if inf.nil? then
SWin::Application.messageBox("処理対象のファイルをドラッグして起動するか、
バッチファイルで
ical_csv 対象ファイル名
で起動してください。
終了します。" ,"エラー",0)
exit
else
SWin::Application.messageBox("iCSファイルをCSVに変換します。" ,"処理開始",0)
end
f = open(inf, "r")
str = f.read
f.close
#入力ファイルがShift-JISではないかもしれないので強制変換して.tmpに出力
ott=File.dirname(inf)+"/"+File.basename(inf,".*")+".tmp"
ot= open(ott,"w")
str_sjis=NKF.nkf('-s -Lw',str)
ot.print str_sjis
ot.close
#.tmpから入力して.csvに出力すると宣言
inf=File.dirname(inf)+"/"+File.basename(inf,".*")+".tmp"
otf=File.dirname(inf)+"/"+File.basename(inf,".*")+".csv"
ics = open(inf)
cal = RiCal.parse(ics)
# calendarという文字列があればカレンダーと判断 <<<< 必要に応じて修正
calortask=cal.first.prodid.downcase.index(" calendar")
CSV.open(otf, 'w') do |writer|
unless calortask.nil? then
#カレンダーと判断した時のヘッダ
writer << ["Subject","Start Date","Start Time","End Date","End Time"," Meeting Organizer"," Description", "Location","Categories","Attendee","Status","Duration"]
else
#タスクと判断した時のヘッダ
writer << ["Subject","Start Date","Due Date","Status","Date Completed","% Complete","Categories","Notes","Priority"]
end
end
unless calortask.nil? then
cal.first.events.each do |event|
p1= event.summary
x=event.dtstart
unless x.nil? then
p2= x.strftime("%Y/%m/%d")
p3= x.strftime("%H:%M")
else
p2= ""
p3=""
end
x=event.dtend
unless x.nil? then
p4= x.strftime("%Y/%m/%d")
p5= x.strftime("%H:%M")
else
p4= ""
p5=""
end
x=event.organizer
unless x.nil? then
p6= x
else
p6= ""
end
x= event.description
unless x.nil? then
p7= x.gsub(/(
|
|
)/, '
')
else
p7= ""
end
x=event.location
unless x.nil? then
p8= x
else
p8= ""
end
x=event.categories
unless x.nil? then
p9= x
else
p9= ""
end
x=event.attendee
unless x.nil? then
p10= x
else
p10= ""
end
x=event.status
unless x.nil? then
p11= x
else
p11= ""
end
x=event.duration
unless x.nil? then
p12= x
else
p12= ""
end
CSV.open(otf, 'a') do |csv|
unless p1.nil? then
row = [p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12]
csv << row
end
end
end
# cal.first.events.each do に対応
else
#calortask=nil なのでtodoと判断した時の処理
cal.first.todos.each do |todos|
x=todos.summary
unless x.nil? then
p1= x
else
p1= ""
end
x=todos.dtstart
unless x.nil? then
p2= x.strftime("%Y/%m/%d %H:%M")
else
p2= ""
end
x=todos.due
unless x.nil? then
p3= x.strftime("%Y/%m/%d %H:%M")
else
p3= ""
end
x=todos.status
unless x.nil? then
p4= x
else
p4= "Not Started"
end
x=todos.completed
unless x.nil? then
p5= x.strftime("%Y/%m/%d %H:%M")
else
p5= ""
end
x=todos.percent_complete
unless x.nil? then
p6= x
else
p6= ""
end
x=todos.categories
unless x.nil? then
p7= x
else
p7= ""
end
x= todos.description
unless x.nil? then
p8= x.gsub(/(
|
|
)/, '
')
else
p8= ""
end
x=todos.priority
unless x.nil? then
p9= x
else
p9= "Normal"
end
CSV.open(otf, 'a') do |csv|
unless p1.nil? then
row = [p1,p2,p3,p4,p5,p6,p7,p8,p9]
csv << row
end
end
end # cal.first.todos.each do に対応
end #calortask=nil なのでtodoと判断した時の処理を閉じる
SWin::Application.messageBox(otf+"に出力しました" ,"完了",0)
ics.close
a=File.delete(*inf)
end
Remember The Milk の iCal の PRODID PRODID:-//Remember The Milk//rtm.Service.iCalendar.Export 3.0//EN Google Calendar の iCal の PRODID PRODID:-//Google Inc//Google Calendar 70.9054//ENPRODIDでカレンダーかタスクかの分岐設定をしていますので、ご自分の使いたいiCalのPRODIDに合わせてください。私の場合RTMとGoogleの区別だけつけば良く、Googleカレンダーでは、「 Calendar」となっているので、スペース+Calendarを含んでいればGoogleカレンダーとしています。
iCal_to_CSV.rb xxxxx.ics のように引数でインプットファイルを指定します。 これにより xxxxx.CSV が作成されます。 ただし、RUBY.EXE に PATH が通っていなければエラーになります。
[Ruby] Shift-JIS/UTF-8(BOM)/UTF-8(NBOM)を相互変換する
#! ruby -Ks
# coding:windows-31j
#! /usr/bin/env ruby
require 'nkf'
inf=ARGV[0]
linesep=ARGV[1]
f = open(inf, "r")
str = f.read
f.close
unless NKF.guess(str)==NKF::UTF8 || NKF.guess(str)==NKF::SJIS then
puts "Only for UTF-8 or Shift-JIS"
exit
end
if linesep==nil then
linesep="-Lu"
end
# -Lw:Windows(CRLF) -Lu:UNIX(LF) -Lm:Mac(CR)
print "
Option of Line Separator : "+linesep+"
"
end
doc_nbom=NKF.nkf('-w80 '+linesep,str)
doc_sjis=NKF.nkf('-s '+linesep,str)
NBOMf=File.dirname(inf)+"/"+File.basename(inf,".*")+"_NBOM"+File.extname(inf)
BOMf=File.dirname(inf)+"/"+File.basename(inf,".*")+"_BOM"+File.extname(inf)
SJISf=File.dirname(inf)+"/"+File.basename(inf,".*")+"_SJIS"+File.extname(inf)
ot = open(NBOMf,"w")
ot.print doc_nbom
print "
Created UTF-8 without BOM(NBOM) Formated File
"
print " "+NBOMf+"
"
ot.close
ot2 = open(BOMf,"w")
ot2.print("")
ot2.print doc_nbom
print "
Created UTF-8 with BOM Formated File
"
print " "+BOMf+"
"
ot2.close
ot3= open(SJISf,"w")
ot3.print doc_sjis
print "
Created Shift-JIS Formated File
"
print " "+SJISf+"
"
ot3.close
sjis_utf.rb xxxxx.xxx [-Lw/-Lu/-Lm] 第1引数でインプットファイルを指定します。 第2引数は -Lw:Windows(CRLF) -Lu:UNIX(LF) -Lm:Mac(CR) (省略すると-Lu) これにより xxxxx_BOM.xxx,xxxxx_NBOM.xxx,xxxxx_SJIS.xxx が作成されます。 ただし、RUBY.EXE に PATH が通っていなければエラーになります。
プロフィール
カレンダー
ブログ内検索
カテゴリー
最新記事
忍者カウンター
P R