Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for some formats #387

Merged
merged 4 commits into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions lib/roo/excelx/cell/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,43 @@ def create_numeric(number)
def formatted_value
return @cell_value if Excelx::ERROR_VALUES.include?(@cell_value)

formatter = formats[@format]
formatter = generate_formatter(@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

def formats
def generate_formatter(format)
# 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.000000' => '%.6f',
'#,##0' => number_format('%.0f'),
'#,##0.00' => number_format('%.2f'),
'0%' => proc do |number|
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.00' then number_format('%.2f')
when '0%'
proc do |number|
Kernel.format('%d%', number.to_f * 100)
end,
'0.00%' => proc do |number|
end
when '0.00%'
proc do |number|
Kernel.format('%.2f%', number.to_f * 100)
end,
'0.00E+00' => '%.2E',
'#,##0 ;(#,##0)' => number_format('%.0f', '(%.0f)'),
'#,##0 ;[Red](#,##0)' => number_format('%.0f', '[Red](%.0f)'),
'#,##0.00;(#,##0.00)' => number_format('%.2f', '(%.2f)'),
'#,##0.00;[Red](#,##0.00)' => number_format('%.2f', '[Red](%.2f)'),
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.
'##0.0E+0' => '%.1E',
'@' => proc { |number| number }
}
when '##0.0E+0' then '%.1E'
when '@' then proc { |number| number }
else
raise "Unknown format: #{format.inspect}"
end
end

private
Expand All @@ -77,10 +79,6 @@ def number_format(formatter, negative_formatter = nil)
Kernel.format(formatter, number).reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end
end

def zero_padded_number?
@format[/0+/] == @format
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/excelx/cell/test_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ def test_numbers_with_cell_errors
def test_formats
[
['General', '1042'],
['GENERAL', '1042'],
['0', '1042'],
['000000', '001042'],
['0.00', '1042.00'],
['0.0000', '1042.0000'],
['0.000000000', '1042.000000000'],
['#,##0', '1,042'],
['#,##0.00', '1,042.00'],
['0%', '104200%'],
Expand Down