Skip to content

Commit

Permalink
Add workaround for strange formula parse error in excel
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugh McGowan committed Jul 18, 2009
1 parent b8cdaab commit 52cbe6a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
42 changes: 34 additions & 8 deletions lib/roo/excel.rb
Expand Up @@ -12,16 +12,42 @@
module Spreadsheet
module Excel
class Row < Spreadsheet::Row
def _date data # :nodoc:
return data if data.is_a?(Date)
date = @worksheet.date_base + data.to_i
if LEAP_ERROR > @worksheet.date_base
date -= 1
end
date
end
public :_date
public :_datetime
end
# patch for ruby-spreadsheet parsing formulas
class Reader
def read_formula worksheet, addr, work
row, column, xf, rtype, rval, rcheck, opts = work.unpack 'v3CxCx3v2'
formula = Formula.new
formula.shared = (opts & 0x08) > 0
formula.data = work[20..-1]
if rcheck != 0xffff || rtype > 3
value, = work.unpack 'x6E'
unless value
# on architectures where sizeof(double) > 8
value, = work.unpack 'x6e'
end
formula.value = value
elsif rtype == 0
pos, op, len, work = get_next_chunk
if op == :string
formula.value = client read_string(work, 2), @workbook.encoding
else
# This seems to work but I don't know why :). It at least
# seems to correct the case we saw but doubtful it's the right fix
formula.value = client read_string(work[10..-1], 2), @workbook.encoding
end
elsif rtype == 1
formula.value = rval > 0
elsif rtype == 2
formula.value = Error.new rval
else
# leave the Formula value blank
end
set_cell worksheet, row, column, xf, formula
end
end
end
end

Expand Down
Binary file added test/formula_parse_error.xls
Binary file not shown.
15 changes: 12 additions & 3 deletions test/test_roo.rb
Expand Up @@ -130,7 +130,7 @@ class TestRoo < Test::Unit::TestCase

OPENOFFICE = true # do Openoffice-Spreadsheet Tests?
EXCEL = true # do Excel Tests?
GOOGLE = true # do Google-Spreadsheet Tests?
GOOGLE = false # do Google-Spreadsheet Tests?
EXCELX = true # do Excel-X Tests? (.xlsx-files)

ONLINE = true
Expand Down Expand Up @@ -1756,18 +1756,27 @@ def test_col_whitespace
end
end


def test_ruby_spreadsheet_formula_bug
with_each_spreadsheet(:name=>'formula_parse_error', :format=>:excel) do |oo|
assert_equal '5026', oo.cell(2,3)
assert_equal '5026', oo.cell(3,3)
end
end


# Excel has two base date formats one from 1900 and the other from 1904.
# There's a MS bug that 1900 base dates include an extra day due to erroneously
# including 1900 as a leap yar.
def test_base_dates_in_excel
with_each_spreadsheet(:name=>'1900_base', :format=>:excel) do |oo|
assert_equal Date.new(2009,06,15), oo.cell(1,1)
assert_equal Date.new(Time.now.year,Time.now.month,Time.now.day), oo.cell(2,1) #formula for TODAY()
assert_equal Date.new(2009,06,28), oo.cell(2,1) #formula for TODAY(), last calculated on 06.28
assert_equal :date, oo.celltype(1,1)
end
with_each_spreadsheet(:name=>'1904_base', :format=>:excel) do |oo|
assert_equal Date.new(2009,06,15), oo.cell(1,1)
assert_equal Date.new(Time.now.year,Time.now.month,Time.now.day), oo.cell(2,1) #formula for TODAY()
assert_equal Date.new(2009,06,28), oo.cell(2,1) #formula for TODAY(), last calculated on 06.28
assert_equal :date, oo.celltype(1,1)
end
end
Expand Down

0 comments on commit 52cbe6a

Please sign in to comment.