Class: Roo::Excelx::Cell::Number
- Defined in:
- lib/roo/excelx/cell/number.rb
Instance Attribute Summary collapse
-
#cell_value ⇒ Object
readonly
Returns the value of attribute cell_value.
-
#coordinate ⇒ Object
readonly
Returns the value of attribute coordinate.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#formula ⇒ Object
readonly
Returns the value of attribute formula.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Attributes inherited from Base
Instance Method Summary collapse
- #create_numeric(number) ⇒ Object
- #formatted_value ⇒ Object
- #generate_formatter(format) ⇒ Object
-
#initialize(value, formula, excelx_type, style, link, coordinate) ⇒ Number
constructor
A new instance of Number.
Methods inherited from Base
#empty?, #excelx_type, #excelx_value, #formula?, #hyperlink, #link, #link?, #presence, #to_s, #type
Methods included from Helpers::DefaultAttrReader
Constructor Details
#initialize(value, formula, excelx_type, style, link, coordinate) ⇒ Number
Returns a new instance of Number.
12 13 14 15 16 17 |
# File 'lib/roo/excelx/cell/number.rb', line 12 def initialize(value, formula, excelx_type, style, link, coordinate) super # FIXME: Excelx_type is an array, but the first value isn't used. @format = excelx_type.last @value = link ? Roo::Link.new(link, value) : create_numeric(value) end |
Instance Attribute Details
#cell_value ⇒ Object (readonly)
Returns the value of attribute cell_value.
7 8 9 |
# File 'lib/roo/excelx/cell/number.rb', line 7 def cell_value @cell_value end |
#coordinate ⇒ Object (readonly)
Returns the value of attribute coordinate.
7 8 9 |
# File 'lib/roo/excelx/cell/number.rb', line 7 def coordinate @coordinate end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
7 8 9 |
# File 'lib/roo/excelx/cell/number.rb', line 7 def format @format end |
#formula ⇒ Object (readonly)
Returns the value of attribute formula.
7 8 9 |
# File 'lib/roo/excelx/cell/number.rb', line 7 def formula @formula end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
7 8 9 |
# File 'lib/roo/excelx/cell/number.rb', line 7 def value @value end |
Instance Method Details
#create_numeric(number) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/roo/excelx/cell/number.rb', line 19 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, 10) end end |
#formatted_value ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/roo/excelx/cell/number.rb', line 31 def formatted_value return @cell_value if Excelx::ERROR_VALUES.include?(@cell_value) formatter = generate_formatter(@format) if formatter.is_a? Proc formatter.call(@cell_value) else Kernel.format(formatter, @cell_value) end end |
#generate_formatter(format) ⇒ Object
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 82 |
# File 'lib/roo/excelx/cell/number.rb', line 42 def generate_formatter(format) # FIXME: numbers can be other colors besides red: # [BLACK], [BLUE], [CYAN], [GREEN], [MAGENTA], [RED], [WHITE], [YELLOW], [COLOR n] case format when /^General$/i then '%.0f' when '0' then '%.0f' when /^(0+)$/ then "%0#{$1.size}d" when /^0\.(0+)$/ then "%.#{$1.size}f" when '#,##0' then number_format('%.0f') when /^#,##0.(0+)$/ then number_format("%.#{$1.size}f") when '0%' proc do |number| Kernel.format('%.0f%%', number.to_f * 100) end when '0.00%' proc do |number| Kernel.format('%.2f%%', number.to_f * 100) end when '0.00E+00' then '%.2E' when '#,##0 ;(#,##0)' then number_format('%.0f', '(%.0f)') when '#,##0 ;[Red](#,##0)' then number_format('%.0f', '[Red](%.0f)') when '#,##0.00;(#,##0.00)' then number_format('%.2f', '(%.2f)') when '#,##0.00;[Red](#,##0.00)' then number_format('%.2f', '[Red](%.2f)') # FIXME: not quite sure what the format should look like in this case. when '##0.0E+0' then '%.1E' when "_-* #,##0.00\\ _€_-;\\-* #,##0.00\\ _€_-;_-* \"-\"??\\ _€_-;_-@_-" then number_format('%.2f', '-%.2f') when '@' then proc { |number| number } when /^(?:_\()?"([^"]*)"(?:\* )?([^_]+)/ proc do |number| formatted_number = generate_formatter($2).call(number) "#{$1}#{formatted_number}" end when /^_[- \(]\[\$([^-]*)[^#@]+([^_]+)/ proc do |number| formatted_number = generate_formatter($2).call(number) "#{$1}#{formatted_number}" end else raise "Unknown format: #{format.inspect}" end end |