Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of .xlsx files exported from Numbers #114

Merged
merged 1 commit into from
Mar 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/roo/excelx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,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 @@ -2315,4 +2315,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