diff --git a/lib/roo/excelx.rb b/lib/roo/excelx.rb index 5c01d997..164ca2ae 100644 --- a/lib/roo/excelx.rb +++ b/lib/roo/excelx.rb @@ -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) diff --git a/lib/roo/excelx/cell/number.rb b/lib/roo/excelx/cell/number.rb index 20d1d2b2..de4eea86 100644 --- a/lib/roo/excelx/cell/number.rb +++ b/lib/roo/excelx/cell/number.rb @@ -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 @@ -80,6 +82,12 @@ def formats '@' => proc { |number| number } } end + + private + + def zero_padded_number? + @format[/0+/] == @format + end end end end diff --git a/spec/lib/roo/excelx_spec.rb b/spec/lib/roo/excelx_spec.rb index 52ab33d6..cc244579 100644 --- a/spec/lib/roo/excelx_spec.rb +++ b/spec/lib/roo/excelx_spec.rb @@ -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' } diff --git a/test/files/zero-padded-number.xlsx b/test/files/zero-padded-number.xlsx new file mode 100644 index 00000000..5307aad1 Binary files /dev/null and b/test/files/zero-padded-number.xlsx differ