Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time cells not properly shown #19

Open
westonganger opened this issue Jan 29, 2018 · 7 comments
Open

Time cells not properly shown #19

westonganger opened this issue Jan 29, 2018 · 7 comments

Comments

@westonganger
Copy link
Owner

Time cells are not being properly shown in v1.0.0

Fix in progress on develop branch.

@mmagn
Copy link

mmagn commented Jan 30, 2018

I just tested the develop fix (commit f38bca8) with this simple rake task :

end of Rakefile :

[...]

task :example do
  require 'rodf'

  RODF::Spreadsheet.file("example.ods") do
    table 'table1' do
      row do
        cell Time.now
      end
    end
  end
end

The content.xml produced in example.ods contains this tag : <table:table-cell office:value-type="time" office:time-value="20180130T150458"></table:table-cell>.
But when I open the .ods file with LibreOffice, the value of the cell is 0.

After some investigations I achieved to correctly display Date & Time values with this modification on cell.rb :

[...]

unless empty?(@value)
  @url = opts[:url]

  if !@type
    if @value.is_a?(Numeric)
      @type = :float
    elsif @value.respond_to?(:strftime)
      @type = :date
    else
      @type = :string
      @value = @value.to_s
    end
  end

  case @value.class.name
  when 'Date'
    @value = @value.strftime("%Y-%m-%d") if @value.respond_to?(:strftime)
  when 'Time'
    @value = @value.strftime("%Y-%m-%dT%T") if @value.respond_to?(:strftime)
  end
end

[...]

If you want I can prepare a pull request with this modification & some specs in the following week.

(edit) disclaimer : the discussion started here westonganger/spreadsheet_architect#14

@westonganger
Copy link
Owner Author

Unfortunately that solution doesn't seem correct. However I fixed the time format and added a fix for DateTime objects.

Please test again with the latest commit on develop branch

@westonganger
Copy link
Owner Author

We're you able to test the latest commit?

@mmagn
Copy link

mmagn commented Feb 8, 2018

Sorry for the delay.
Unfortunately, the cell value is 00:00:00 (The cell is formatted to only display the time without the date), and when I change to a datetime format the cell value is 30/12/1899 00:00:00 (ruby DateTime value was 2017-11-22 14:30:52 UTC)

@westonganger
Copy link
Owner Author

I see axlsx has the following code for serializing dates & times. Maybe I can take some hints from that approach.

https://github.com/randym/axlsx/blob/d136835f3a3a54831ad2c7c793a51d3a57273892/lib/axlsx/workbook/worksheet/date_time_converter.rb

@westonganger
Copy link
Owner Author

westonganger commented Jun 20, 2018

I was unsuccessful in my attempts to resolve this issue. If anyone wants to tackle this I would be happy to accept/collaborate on a PR.

The relevant method is RODF::Cell.initialize. Check the commit history on this method for some clues about what didnt work.

Until this is resolved, I am recommending that all dates/times be passed as strings instead.

@anewb
Copy link

anewb commented Jul 25, 2022

A workaround might be to manually calculate to fraction of the day like this:
t = Time.parse('10:30')
day_fraction = (t.sec + t.min*60 + t.hour*3600).to_f/(24*3600),
set the cell value to day_fraction and afterwards manually change the number format of the cell to time. If changing the number format of the cell could be done with this library, the workaround would not need any manual steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants