module Oxcelix::Numberhelper
The Numberhelper
module implements methods that return the formatted value or the value converted into a Ruby type (DateTime, Numeric, etc)
Public Instance Methods
to_fmt()
click to toggle source
Get the cell’s value, convert it with to_ru
and finally, format it based on the value’s type. @return [String] Value gets formatted depending on its class. If it is a DateTime, the DateTime.strftime method is used, if it holds a number, the Kernel::sprintf is run. @example Get the formatted value of a cell:
c = w.sheets[0]["B3"] # => <Oxcelix::Cell:0x00000002a5b368 @xlcoords="A3", @style="84", @type="n", @value="41155", @numformat=14> c.to_fmt # => "3/9/2012"
# File lib/oxcelix/numformats.rb, line 103 def to_fmt begin if Numformats::Formatarray[@numformat.to_i][:cls] == 'date' self.to_ru.strftime(Numformats::Formatarray[@numformat][:ostring]) rescue @value elsif Numformats::Formatarray[@numformat.to_i][:cls] == 'numeric' || Numformats::Formatarray[@numformat.to_i][:cls] == 'rational' sprintf(Numformats::Formatarray[@numformat][:ostring], self.to_ru) rescue @value else return @value end end end
to_ru()
click to toggle source
Get the cell’s value and excel format string and return a string, a ruby Numeric or a DateTime object accordingly @return [Object] A ruby object that holds and represents the value stored in the cell. Conversion is based on cell formatting. @example Get the value of a cell:
c = w.sheets[0]["B3"] # => <Oxcelix::Cell:0x00000002a5b368 @xlcoords="A3", @style="84", @type="n", @value="41155", @numformat=14> c.to_ru # => <DateTime: 2012-09-03T00:00:00+00:00 ((2456174j,0s,0n),+0s,2299161j)>
# File lib/oxcelix/numformats.rb, line 85 def to_ru if !@value.numeric? || Numformats::Formatarray[@numformat.to_i][:xl] == nil || Numformats::Formatarray[@numformat.to_i][:xl].downcase == "general" return @value end if Numformats::Formatarray[@numformat.to_i][:cls] == 'date' return DateTime.new(1899, 12, 30) + (eval @value) elsif Numformats::Formatarray[@numformat.to_i][:cls] == 'numeric' || Numformats::Formatarray[@numformat.to_i][:cls] == 'rational' return eval @value rescue @value end end