Created
January 25, 2013 18:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)}" |
read_flawed_spreadsheet.rb
results in:
in `
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:
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
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