rubyXL with dates - spec works, actual use doesn't

952 Views Asked by At

Using rubyXL to handle xlsx files with dates. The spec for date handling passes, but when I try to replicate it in my own script, it doesn't work. Instead, it leaves the dates as floats/numbers (which is how dates are stored in excel). I downloaded the rubyXL source from Github and ran the spec. Everything related to dates passed. However, when I run the following outside of a spec file, it exhibits the wrong behavior.

require 'rubyXL'

workbook = RubyXL::Workbook.new
worksheet = RubyXL::Worksheet.new(workbook)
workbook.worksheets << worksheet

worksheet.add_cell(0, 0, "0:0")
cell = worksheet[0][0]

date = Date.parse("January 20, 2011")
cell.change_contents(date)

puts cell.is_date? # false -- spec says it should be true
puts cell.value == date # false -- spec says it should be true
puts date # 2011-01-20
puts cell.value # 40563/1 <-- should be a date object

Compare this to the rubyXL spec test (cell starts out exactly the same as I've put it above):

it 'should return the value of a date' do
      date = Date.parse('January 1, 2011')
      @cell.change_contents(date)
      @cell.should_receive(:is_date?).any_number_of_times.and_return(true)
      @cell.value.should == date
    end

I even added some puts statements to check that it's actually working -- it is (outputs 2011-01-01-like things). I also tried deleting every other file in the spec folder except for cell_spec.rb, just to see if there was a setting being set that I was missing. Still didn't change anything.

At this point, I'm baffled but I'm sure I must be missing something simple. I just don't get why I can't replicate the spec behavior in regular code.

1

There are 1 best solutions below

1
On

The value is actually a epoc, try using Time.at(cell.value)