Skip to content

Instantly share code, notes, and snippets.

@BiggerNoise
Created January 25, 2013 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BiggerNoise/4636781 to your computer and use it in GitHub Desktop.
Save BiggerNoise/4636781 to your computer and use it in GitHub Desktop.
Shows using the spreadsheet gem to read a file that is flawed. Worksheets and dimensions are correct, but cell data cannot be read. File that causes the issue can be found at: http://dl.dropbox.com/u/83477489/sample.xls
#!/usr/bin/env ruby
wb = Spreadsheet.open('sample.xls')
puts "Opened Spreadsheet, #{wb.sheet_count} sheets encountered"
(0..wb.sheet_count-1).each do |sheet_number|
ws = wb.worksheet(sheet_number)
puts "\tWorksheet #{ws.name} has #{ws.row_count} rows and #{ws.column_count} columns}; and Dimensions: #{ws.dimensions}"
end
puts "Opened Spreadsheet, #{wb.sheet_count} sheets encountered"
ws = wb.worksheet(0)
puts "\tWorksheet '#{ws.name}' has #{ws.row_count} rows and #{ws.column_count} columns}; and Dimensions: #{ws.dimensions}"
row_index = 5
row = ws.row row_index
puts "\tWorksheet '#{ws.name}' row #{row_index}: #{row.to_s}"
puts "\tWorksheet '#{ws.name}' cell(#{row_index}, 0): #{ws.cell(row_index, 0)}"
@zdavatz
Copy link

zdavatz commented Jan 26, 2013

Does the solution of Javix work for you?

require 'spreadsheet'

book = Spreadsheet.open('sample.xls')
puts "file found" if book
puts "Sheets found: #{book.worksheets.size}"
book.worksheets.each do |sheet|
puts "Got sheet: #{sheet.name}"
end

sheet1 = book.worksheets[0]
puts "Got the first sheet: #{sheet1.name}"
puts "cell 1: #{sheet1[2,1]}"

I can't figure out out why the standard way to read rows values like that:

sheet1.rows each { | row| puts row }

also see: https://groups.google.com/d/msg/rubyspreadsheet/vkHOulBnjMc/r792XlGEWLsJ

@zdavatz
Copy link

zdavatz commented Jan 26, 2013

read_flawed_spreadsheet.rb
results in:
in `

': uninitialized constant Spreadsheet (NameError)

Where as the solution of Javix results in:

file found
Sheets found: 7
Got sheet: Summary Statistics
Got sheet: Patient Sample
Got sheet: Local Weights
Got sheet: Age.Gender Dist
Got sheet: Total Cost Risk Dist
Got sheet: Hospitalization Risk Dist
Got sheet: Build Options
Got the first sheet: Summary Statistics
cell 1:

@BiggerNoise
Copy link
Author

Yes, that's the same thing that I have been getting. You can read the sheets and their titles, but you cannot see any of the data.

So, no, this is still not working for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment