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

Support formatting excel currencies #557

Merged
merged 9 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Support formatting excel currencies
  • Loading branch information
Brad Midgley committed Mar 11, 2021
commit f982d13c807374563815edc23c9f2df9f5f0f4b4
10 changes: 10 additions & 0 deletions lib/roo/excelx/cell/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def generate_formatter(format)
when '##0.0E+0' then '%.1E'
when "_-* #,##0.00\\ _€_-;\\-* #,##0.00\\ _€_-;_-* \"-\"??\\ _€_-;_-@_-" then number_format('%.2f', '-%.2f')
Copy link
Author

@bmidgley bmidgley Mar 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expression is similar but it isn't defended with a spec so I can't tell if I could handle it too. It also appears to discard the €--we expect to have it in the formatted value.

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
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/roo/excelx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,22 @@
expect(subject.formatted_value(4, 1)).to eq '05010'
end
end

context 'contains US currency' do
let(:path) { 'test/files/currency-us.xlsx' }

it 'returns a zero-padded number' do
expect(subject.formatted_value(4, 1)).to eq '$20.51'
end
end

context 'contains other currency' do
let(:path) { 'test/files/currency-euro.xlsx' }

it 'returns a zero-padded number' do
expect(subject.formatted_value(4, 1)).to eq '€20.51'
end
end
end

describe '#row' do
Expand Down
Binary file added test/files/currency-euro.xlsx
Binary file not shown.
Binary file added test/files/currency-us.xlsx
Binary file not shown.