Module: Oxcelix::Numberhelper

Includes:
Numformats
Included in:
Cell, Sheet
Defined in:
lib/oxcelix/numformats.rb

Overview

The Numberhelper module implements methods that return the formatted value or the value converted into a Ruby type (DateTime, Numeric, etc)

Constant Summary

Constants included from Numformats

Oxcelix::Numformats::Dtmap, Oxcelix::Numformats::Formatarray

Instance Method Summary collapse

Methods included from Numformats

#add_custom_formats, #datetime, #numeric

Instance Method Details

#to_fmtString

Get the cell's value, convert it with to_ru and finally, format it based on the value's type. if it holds a number, the #Kernel::sprintf is run.

Examples:

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"

103
104
105
106
107
108
109
110
111
112
113
# 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_ruObject

Get the cell's value and excel format string and return a string, a ruby Numeric or a DateTime object accordingly

Examples:

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)>

85
86
87
88
89
90
91
92
93
94
# 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