1

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.

benzado
  • 82,288
  • 22
  • 110
  • 138
Jeff Tratner
  • 16,270
  • 4
  • 47
  • 67
  • I forked RubyXL and attempted to get this working; the code is a mess. I since discovered **Axlsx**, a much better gem. – benzado Nov 08 '12 at 19:55
  • @benzado I agree that `Axslx` is a better alternative for *generation*--it looked really nice. I was looking for something to *read* .xlsx files (e.g. to allow for uploaded user input, etc) and I didn't see where Axlsx would allow you to do that. – Jeff Tratner Nov 08 '12 at 22:59
  • Oops, sorry about that. I was experiencing the same trouble simply trying to generate a file with a date in it (per your example code), hence my perspective. Good luck to you. – benzado Nov 08 '12 at 23:20
  • 1
    @benzado it's useful to mention that alternative for internet searchers, etc! :) – Jeff Tratner Nov 08 '12 at 23:30

1 Answers1

0

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 27 '23 at 19:29