pythonicrubyist / creek

Ruby library for parsing large Excel files.
http://rubygems.org/gems/creek
MIT License
388 stars 109 forks source link

All numbers are read as Float #100

Open amhofny opened 3 years ago

amhofny commented 3 years ago

When reading an excel file that is exported from other libraries, for example caxlsx https://github.com/caxlsx/caxlsx The library generates Excel with floats and integers represented with t="n", for example from the "sheet1.xml"

<c r="Z2" s="0" t="n"><v>1.0</v></c>
<c r="G2" s="0" t="n"><v>114</v></c>

Both of these numbers are read by Creek as: 1.0 , 114.0, which is causing problems when the system expects to see an integer value instead of float, for example when using that number to load Models from the Database

I have found in this line https://github.com/pythonicrubyist/creek/blob/master/lib/creek/styles/converter.rb#L47 That Creek always read the value as float.

My suggestion is to replace

value.to_f

with

if value.to_i.to_s == value.to_s
     value.to_i
 else
     value.to_f
 end

I don't know if this is the right solution or not, but by applying this as a patch in the system solved our problem.

bmd08a1 commented 10 months ago

I think we can check for the style :fixnum or :bignum to decide whether we should use to_i or convert_bignum. Something like this:

when `n`
  case style
  when :fixnum
    value.to_i
  when :bignum
    convert_bignum(value)
  else
    value.to_f
  end