Skip to content

Commit

Permalink
Merge pull request #114 from sidonath/fix-numbers-xlsx-parsing
Browse files Browse the repository at this point in the history
Fix parsing of .xlsx files exported from Numbers
  • Loading branch information
Empact committed Mar 27, 2014
2 parents 53aab98 + 2b7df1a commit 1ef91fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/roo/excelx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,23 @@ def process_zipfile(tmpdir, zipfilename, zip, path='')
"#{tmpdir}/roo_sharedStrings.xml"
elsif entry_name.end_with?('styles.xml')
"#{tmpdir}/roo_styles.xml"
elsif entry_name =~ /sheet([0-9]+).xml$/
elsif entry_name =~ /sheet([0-9]+)?.xml$/
nr = $1
@sheet_files[nr.to_i-1] = "#{tmpdir}/roo_sheet#{nr}"
path = "#{tmpdir}/roo_sheet#{nr.to_i}"

# Numbers 3.1 exports first sheet without sheet number. Such sheets
# are always added to the beginning of the array which, naturally,
# causes other sheets to be pushed to the next index which could
# lead to sheet references getting overwritten, so we need to
# handle that case specifically.
if nr
sheet_files_index = nr.to_i - 1
sheet_files_index += 1 if @sheet_files[sheet_files_index]
@sheet_files[sheet_files_index] = path
else
@sheet_files.unshift path
path
end
elsif entry_name =~ /comments([0-9]+).xml$/
nr = $1
@comments_files[nr.to_i-1] = "#{tmpdir}/roo_comments#{nr}"
Expand Down
Binary file added test/files/numbers-export.xlsx
Binary file not shown.
15 changes: 15 additions & 0 deletions test/test_roo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2318,4 +2318,19 @@ def test_bug_numbered_sheet_names
end
end

def test_parsing_xslx_from_numbers
return unless EXCELX
xlsx = Roo::Excelx.new(File.join(TESTDIR, "numbers-export.xlsx"))

xlsx.default_sheet = xlsx.sheets.first
assert_equal 'Sheet 1', xlsx.cell('a',1)

# Another buggy behavior of Numbers 3.1: if a warkbook has more than a
# single sheet, all sheets except the first one will have an extra row and
# column added to the beginning. That's why we assert against cell B2 and
# not A1
xlsx.default_sheet = xlsx.sheets.last
assert_equal 'Sheet 2', xlsx.cell('b',2)
end

end # class

0 comments on commit 1ef91fa

Please sign in to comment.