Reading cell display data using Roo gem

I am developing a 508 html table by analyzing excel files. I want to read the data of the cells that are displayed (and not the float values, as roo splashes out). When there are formulas, roo gem does not just read data after applying the format. Like here, I get ex float values: "90.909090909090907", but the number displayed on excel is "91". Its fine if I just get the string values ​​of the displayed excel data.

Thanks in advance.

+4
source share
4 answers

A similar problem. As a complement to the Magesh answer , if you don’t know which cells will be integers, you can check:

s.cell(i, j).to_i if s.cell(i,j).is_a?(Float) && s.cell(i,j).denominator == 1

Obviously, you may have false positives, but for the most part it will turn cells that should be integers into integers and save those that should be floating points.

+2
source

I had the same problem when I first tried to use the roo gem. You can fix this using .to_i,

 row = spreadsheet.row(1) row[0].to_i 

Here .to_i will change the number from a float representation to a normal integer, for example 91 instead of 91.0 or 91.03030, or something else that could be

+1
source

Check out the Roo::Base.cell_to_csv (private) method for an idea of ​​how to handle spreadsheet data types.

0
source

To get the displayed value (formatted value), use this method

 xlsx.formatted_value(row,col) 

http://www.rubydoc.info/gems/roo/frames

0
source

All Articles