Skip to content

Commit

Permalink
Fix zero-padded numbers for Excelx
Browse files Browse the repository at this point in the history
Fixes roo-rb#139 for Excelx documents.
  • Loading branch information
stevendaniels committed Dec 18, 2015
1 parent d9667fc commit fd11800
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/roo/excelx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ def excelx_value(row, col, sheet = nil)
safe_send(sheet_for(sheet).cells[key], :cell_value)
end

# returns the internal value of an excelx cell
# Note: this is only available within the Excelx class
def formatted_value(row, col, sheet = nil)
key = normalize(row, col)
safe_send(sheet_for(sheet).cells[key], :formatted_value)
end

# returns the internal format of an excel cell
def excelx_format(row, col, sheet = nil)
key = normalize(row, col)
Expand Down
8 changes: 8 additions & 0 deletions lib/roo/excelx/cell/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def formatted_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
Expand Down Expand Up @@ -80,6 +82,12 @@ def formats
'@' => proc { |number| number }
}
end

private

def zero_padded_number?
@format[/0+/] == @format
end
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/roo/excelx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@
end
end

describe '#formatted_value' do
context 'contains zero-padded numbers' do
let(:path) { 'test/files/zero-padded-number.xlsx' }

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

describe '#excelx_format' do
let(:path) { 'test/files/style.xlsx' }

Expand Down
Binary file added test/files/zero-padded-number.xlsx
Binary file not shown.

0 comments on commit fd11800

Please sign in to comment.