开发者

Parsing Excel and Google Docs Spreadsheet with column headers or titles

开发者 https://www.devze.com 2023-03-07 11:27 出处:网络
I have excel or google docs spreadsheet files I need to parse. The columns may be ordered differently from file to file but there is always a 开发者_StackOverflowtitle in the first row.

I have excel or google docs spreadsheet files I need to parse. The columns may be ordered differently from file to file but there is always a 开发者_StackOverflowtitle in the first row.

I'm looking for a way to use column titles in referencing cells when reading a file with Roo or another similarly purposed gem.

Here's what I'd like to be able to do

Get the 4th for the column titled Widget Count no matter what the column position is:

thisWidgetCount = cell[4,'Widget Count'];

I realize I could just walk the columns and build a hash of title to column name but it seems like this is likely something someone has already wrapped into a lib.


You can extend Roo, or just write as a helper:

oo = Openoffice.new("simple_spreadsheet.ods")
this_widget_count = tcell(oo, 3, 'Widget Count')

def tcell(roo, line, column_title)
  column = first_column.upto(last_column).map{ |column| roo.cell(1, column) }.index( column_type ) + 1
  roo.cell(line, column)
end

And of course it is better to preload all captions in an Array, because in our case you are fetching titles every time (and that is bad idea in performance)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号