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

Fix number formatting when cells are empty #602

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions lib/roo/excelx/cell/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def initialize(value, formula, excelx_type, style, link, coordinate)

def create_numeric(number)
return number if Excelx::ERROR_VALUES.include?(number)
return nil if (number.nil? || number == "")

case @format
when /%/
Float(number)
Expand All @@ -30,6 +32,7 @@ def create_numeric(number)

def formatted_value
return @cell_value if Excelx::ERROR_VALUES.include?(@cell_value)
return '' if (@cell_value.nil? || @cell_value == "")

formatter = generate_formatter(@format)
if formatter.is_a? Proc
Expand Down
55 changes: 55 additions & 0 deletions spec/lib/roo/excelx/cell/number_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'spec_helper'

RSpec.describe Roo::Excelx::Cell::Number do

describe '#initialize' do
let(:formula) { nil }
let(:excelx_type) { [:numeric_or_formula, format] }
let(:format) { "#,##0.00" }
let(:style) { 1 }
let(:link) { nil }
let(:coordinate) { [1,1] }

let(:number) { Roo::Excelx::Cell::Number.new(value, formula, excelx_type, style, link, coordinate)}

context 'with an actual value' do
let(:value) { 0.1 }
it 'creates the object and parses the value' do
expect(number.value).to eq 0.1
end
end

context 'with an empty value' do
let(:value) { "" }
it 'creates the object with a nil value' do
expect(number.value).to eq nil
end
end
end

describe '#formatted_value' do
let(:formula) { nil }
let(:excelx_type) { [:numeric_or_formula, format] }
let(:format) { "#,##0.00" }
let(:style) { 1 }
let(:link) { nil }
let(:coordinate) { [1,1] }

let(:number) { Roo::Excelx::Cell::Number.new(value, formula, excelx_type, style, link, coordinate)}

context 'with an actual value' do
let(:value) { 0.1 }
it 'creates the object and parses the value' do
expect(number.formatted_value).to eq '0.10'
end
end

context 'with an empty value' do
let(:value) { "" }
it 'creates the object with a nil value' do
expect(number.formatted_value).to eq ''
end
end

end
end