roo-rb / roo

Roo provides an interface to spreadsheets of several sorts.
MIT License
2.78k stars 503 forks source link

Some ODS do not load (float cell issue) #528

Open krzykos opened 4 years ago

krzykos commented 4 years ago

Issue

LibreOffice generates following XML for float cells:

<table:table-cell office:value-type="float" office:value="11010" calcext:value-type="float">
<text:p>11010</text:p>
</table:table-cell>

Some other generators (like https://github.com/westonganger/rodf) do not put redundant children elements, so the outcome is as follows:

<table:table-cell office:value-type="float" office:value="1">
</table:table-cell>

Attempt to read such ODS throws at open_office.rb line 424 (or so)

Fix

Replace:

@cell[sheet][key] = (table_cell.attributes['value'].to_s.include?(".") || table_cell.children.first.text.include?(".")) ? v.to_f : v.to_i

With:

@cell[sheet][key] = (table_cell.attributes['value'].to_s.include?(".") || (table_cell.children.first && table_cell.children.first.text.include?("."))) ? v.to_f : v.to_i

The former version does not comply to the best possible coding style. The variable v comes from the caller and in this case is set to the stringified value attribute, also the caller does not check for the presence of the child element. The original code tries to access the first child element even if it's not present (resulting in exception)

Even if the problematic ODS is not valid, this patch will improve the code quality and so increase Roo compatibility.

System configuration

Roo version: 2.7.1 (still present in 2.8.2) Ruby version: ruby 2.4.5p335 (2018-10-18 revision 65137) [x64-mingw32]