Class: Roo::Excelx::Cell::Number

Inherits:
Base
  • Object
show all
Defined in:
lib/roo/excelx/cell/number.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#cell_type, #style

Instance Method Summary collapse

Methods inherited from Base

#empty?, #excelx_type, #excelx_value, #formula?, #hyperlink, #link?, #to_s, #type

Constructor Details

#initialize(value, formula, excelx_type, style, link, coordinate) ⇒ Number

Returns a new instance of Number.



7
8
9
10
11
12
13
14
# File 'lib/roo/excelx/cell/number.rb', line 7

def initialize(value, formula, excelx_type, style, link, coordinate)
  super
  # FIXME: change @type to number. This will break brittle tests.
  # FIXME: Excelx_type is an array, but the first value isn't used.
  @type = :float
  @format = excelx_type.last
  @value = link? ? Roo::Link.new(link, value) : create_numeric(value)
end

Instance Attribute Details

#cell_valueObject (readonly)

Returns the value of attribute cell_value.



5
6
7
# File 'lib/roo/excelx/cell/number.rb', line 5

def cell_value
  @cell_value
end

#coordinateObject (readonly)

Returns the value of attribute coordinate.



5
6
7
# File 'lib/roo/excelx/cell/number.rb', line 5

def coordinate
  @coordinate
end

#formatObject (readonly)

Returns the value of attribute format.



5
6
7
# File 'lib/roo/excelx/cell/number.rb', line 5

def format
  @format
end

#formulaObject (readonly)

Returns the value of attribute formula.



5
6
7
# File 'lib/roo/excelx/cell/number.rb', line 5

def formula
  @formula
end

Returns the value of attribute link.



5
6
7
# File 'lib/roo/excelx/cell/number.rb', line 5

def link
  @link
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/roo/excelx/cell/number.rb', line 5

def value
  @value
end

Instance Method Details

#create_numeric(number) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/roo/excelx/cell/number.rb', line 16

def create_numeric(number)
  return number if Excelx::ERROR_VALUES.include?(number)
  case @format
  when /%/
    Float(number)
  when /\.0/
    Float(number)
  else
    (number.include?('.') || (/\A[-+]?\d+E[-+]\d+\z/i =~ number)) ? Float(number) : Integer(number)
  end
end

#formatsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/roo/excelx/cell/number.rb', line 41

def formats
  # FIXME: numbers can be other colors besides red:
  # [BLACK], [BLUE], [CYAN], [GREEN], [MAGENTA], [RED], [WHITE], [YELLOW], [COLOR n]
  {
    'General' => '%.0f',
    '0' => '%.0f',
    '0.00' => '%.2f',
    '#,##0' => proc do |number|
      Kernel.format('%.0f', number).reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
    end,
    '#,##0.00' => proc do |number|
      Kernel.format('%.2f', number).reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
    end,
    '0%' =>  proc do |number|
      Kernel.format('%d%', number.to_f * 100)
    end,
    '0.00%' => proc do |number|
      Kernel.format('%.2f%', number.to_f * 100)
    end,
    '0.00E+00' => '%.2E',
    '#,##0 ;(#,##0)' => proc do |number|
      formatter = number.to_i > 0 ? '%.0f' : '(%.0f)'
      Kernel.format(formatter, number.to_f.abs).reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
    end,
    '#,##0 ;[Red](#,##0)' => proc do |number|
      formatter = number.to_i > 0 ? '%.0f' : '[Red](%.0f)'
      Kernel.format(formatter, number.to_f.abs).reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
    end,
    '#,##0.00;(#,##0.00)' => proc do |number|
      formatter = number.to_i > 0 ? '%.2f' : '(%.2f)'
      Kernel.format(formatter, number.to_f.abs).reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
    end,
    '#,##0.00;[Red](#,##0.00)' => proc do |number|
      formatter = number.to_i > 0 ? '%.2f' : '[Red](%.2f)'
      Kernel.format(formatter, number.to_f.abs).reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
    end,
    # FIXME: not quite sure what the format should look like in this case.
    '##0.0E+0' => '%.1E',
    '@' => proc { |number| number }
  }
end

#formatted_valueObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/roo/excelx/cell/number.rb', line 28

def formatted_value
  return @cell_value if Excelx::ERROR_VALUES.include?(@cell_value)

  formatter = formats[@format]
  if formatter.is_a? Proc
    formatter.call(@cell_value)
  elsif zero_padded_number?
    "%0#{@format.size}d"% @cell_value
  else
    Kernel.format(formatter, @cell_value)
  end
end