rubyexcelopenxmlrubyxl

How do I create a date-formatted cell using RubyXL?


I'm using RubyXL to dynamically generate a spreadsheet file that contains some dates. The documentation is kind of sparse so I've been looking at the source, and it appears the library only has special handling for Date and DateTime when you call change_contents, so that's what I'm doing:

cell = sheet.add_cell_obj RubyXL::Cell.new(sheet, row_index, col_index)
cell.change_contents(Time.now.to_datetime)

When I create a spreadsheet this way, Excel does not format those cells as dates. I'm guessing that I need to set some other field, perhaps cell.datatype, but I'm not sure. Or maybe I'm barking up the wrong tree. Does anybody know what to do?


Solution

  • See https://github.com/weshatheleopard/rubyXL/issues/210

    c = workbook[0].add_cell(0,0)
    c.set_number_format('m/d/yy')
    c.change_contents(Date.today)