@rspreadsheet.first_row.upto(@rspreadsheet.last_row) do |y|
.. do something ..
@rspreadsheet.first_column(sheet).upto(@rspreadsheet.last_column(sheet)) do |x|
.. do something ..
end
end
と、ワークシートの使用範囲の下限.upto(上限)でループを回しているので、超格好悪いです。
なにより面倒臭いので GenericSpreadsheet にメソッドを追加します。
class GenericSpreadsheet
def to_a(sheet = nil)
sheet ||= @default_sheet
raise RangeError unless sheets.include?(sheet)
[*header_line.succ..last_row(sheet)].map |row|
[*1..last_column(sheet)].map do |col|
@cell[sheet][ [row, col] ]
end
end
end
end
本当は GenericSpreadsheet#each にしたかったのですが、@cellの構成が変態的で結局Arrayを生成するしかなさそうだったので諦めました。
HashのKeyがArrayって危険な気がするのですが……。
また、@cell[sheet][ [row, col] ] の内部表現は各パーサに任されているようなので、値の取得にはpublicメソッドの cell(row, col, sheet) を利用するべきかも知れません。 とりあえず、Excelx(xlsx)では、日付/日時のパース(@cellの中身はStringだけど、#cellの戻り値はDateないしDateTime)ぐらいしか 処理がされていない、にも関わらず判定コストが高そうなメソッドになっているので、上記のようにしています。
セコメントをする