#! ruby -Ks # coding:windows-31j #! /usr/bin/env ruby # GCalExcel.rb # Coded in Ruby 1.9.3 require 'rubygems' require 'vr/vrdialog' require 'spreadsheet' require 'nkf' #引数チェック(この if then ブロック使わないなら vr/vrdialog Gem はrequire不要) inf=ARGV[0] if inf.nil? then SWin::Application.messageBox("処理対象のファイルをドラッグして起動するか、\n\nバッチファイルで\n GCalExcel.rb 対象ファイル名 \nで起動してください。\n\n終了します。" ,"エラー",0) exit else SWin::Application.messageBox("GCalExcelファイルをGoogleカレンダー対応CSVに変換します。" ,"処理開始",0) end #引数で渡されたファイルから入力して.csvに出力すると宣言 book = Spreadsheet.open(inf,"rb") otf=File.dirname(inf)+"/"+File.basename(inf,".*")+".csv" o=File.open(otf,"w") #引数で渡されたファイルから入力して.csvに出力すると宣言 book.worksheets.each do |ws| #ヘッダチェック:Location(GCal2EccelのWhere)格納列はどこか? for i in 0..10 if ws[0,i]=="Where" then LocationCol=i end end #ヘッダ出力 record = [] record << ["Subject","Start Date","Start Time","End Day","End Time","Description","Location","All Day Event"] o.puts NKF.nkf('-w80 -Lu',record.join(",")) #一行ずつ処理 ws.each do |row| record = [] allDayEvent=TRUE unless row[3][0,5]=="24:00" then allDayEvent=FALSE end unless row[0]=="Title" && row[1]=="Start" then unless row[0].nil? then row[0]=row[0].gsub(",","") end unless row[4].nil? then row[4]=row[4].gsub(",","") end unless row[LocationCol].nil? then row[LocationCol]=row[LocationCol].gsub(",","") end record << [row[0],row[1][0,10],row[1][11,5],row[2][0,10],row[2][11,5],row[4],row[LocationCol],allDayEvent] #日付時刻のデータの正当性チェックをしていないので必要ならチェック手順を追加して下さい o.puts NKF.nkf('-w80 -Lu',record.join(",").gsub(/\n/,":|:")) end end end #使用ファイルのクローズ処理 o.close book.io.close #-------------------------------------------------- #---- Additional Setting for Spreadsheet Gem ----# #-------------------------------------------------- module Spreadsheet class Workbook attr_accessor :io end end