Skip to content

Commit

Permalink
workaround for spreadsheet date issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugh McGowan committed Jul 24, 2009
1 parent 4336fc7 commit 9bc89bf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 30 deletions.
84 changes: 55 additions & 29 deletions lib/roo/excel.rb
Expand Up @@ -12,42 +12,68 @@
module Spreadsheet
module Excel
class Row < Spreadsheet::Row
def _datetime data # :nodoc:
return data if data.is_a?(DateTime)
base = @worksheet.date_base
date = base + data.to_f
hour = (data % 1) * 24
min = (hour % 1) * 60
sec = ((min % 1) * 60).round
min = min.floor
hour = hour.floor
if sec > 59
sec = 0
min += 1
end
if min > 59
min = 0
hour += 1
end
if hour > 23
hour = 0
date += 1
end
if LEAP_ERROR > base
date -= 1
end
DateTime.new(date.year, date.month, date.day, hour, min, sec)
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
# 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
# leave the Formula value blank
# 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
set_cell worksheet, row, column, xf, formula
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
2 changes: 1 addition & 1 deletion lib/roo/version.rb
Expand Up @@ -2,7 +2,7 @@ module Roo #:nodoc:
module VERSION #:nodoc:
MAJOR = 1
MINOR = 3
TINY = 8
TINY = 9

STRING = [MAJOR, MINOR, TINY].join('.')
end
Expand Down

0 comments on commit 9bc89bf

Please sign in to comment.