Skip to content

Commit

Permalink
Merge pull request #8 from mak-it/auto-format-config-and-rational-sup…
Browse files Browse the repository at this point in the history
…port

Make auto formatting/convert configurable and support rationals
  • Loading branch information
ebeigarts authored Apr 24, 2017
2 parents e52ad9e + f6cee2d commit cafdaf2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
14 changes: 9 additions & 5 deletions lib/saxlsx/rows_collection_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class RowsCollectionParser < Ox::Sax
9 => :percentage, # 0%
10 => :percentage, # 0.00%
11 => :bignum, # 0.00E+00
12 => :unsupported, # # ?/?
13 => :unsupported, # # ??/??
12 => :rational, # # ?/?
13 => :rational, # # ??/??
14 => :date, # mm-dd-yy
15 => :date, # d-mmm-yy
16 => :date, # d-mmm
Expand All @@ -42,6 +42,7 @@ def self.parse(index, data, workbook, &block)

def initialize(workbook, &block)
@base_date = workbook.base_date
@auto_format = workbook.auto_format
@shared_strings = workbook.shared_strings
@number_formats = workbook.number_formats
@block = block
Expand Down Expand Up @@ -109,17 +110,20 @@ def value_of(text)
date = @base_date + Rational((Float(text) * SECONDS_IN_DAY).round, SECONDS_IN_DAY)
DateTime.new(date.year, date.month, date.day, date.hour, date.minute, date.second)
when :fixnum
Integer(text)
Integer(text, 10)
when :float, :percentage
Float(text)
when :rational
Rational(text)
when :bignum
Float(text) # raises ArgumentError if text is not a number
BigDecimal(text) # doesn't raise ArgumentError
else
if @current_type == 'n'
Float(text)
elsif text =~ /\A-?\d+(\.\d+(?:e[+-]\d+)?)?\Z/i # Auto convert numbers
$1 ? Float(text) : Integer(text)
elsif @auto_format && text =~ /\A-?\d+(\.\d+(?:e[+-]\d+)?)?\Z/i
# Auto convert numbers
$1 ? Float(text) : Integer(text, 10)
else
CGI.unescapeHTML(text)
end
Expand Down
8 changes: 5 additions & 3 deletions lib/saxlsx/workbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ class Workbook
DATE_SYSTEM_1904 = DateTime.new(1904, 1, 1)

attr_accessor :date1904
attr_reader :auto_format

def self.open(filename)
def self.open(*args)
begin
workbook = self.new(filename)
workbook = self.new(*args)
yield workbook
ensure
workbook.close
end
end

def initialize(filename)
def initialize(filename, auto_format: true)
@file_system = FileSystem.new filename
@auto_format = auto_format
end

def close
Expand Down
Binary file modified spec/data/SpecNumberFormat.xlsx
Binary file not shown.
31 changes: 30 additions & 1 deletion spec/sheet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@
end
end

context 'with number formats' do
context 'with number formats and auto format' do
let(:filename) { "#{File.dirname(__FILE__)}/data/SpecNumberFormat.xlsx" }

[ ["General", "Test"],
["General", 123],
["General", 123.5],
["Fixnum", 123],
["Currency", 123.0],
["Date", DateTime.new(1970, 1, 1)],
Expand All @@ -145,4 +147,31 @@
end
end
end

context 'with number formats and without auto format' do
let(:filename) { "#{File.dirname(__FILE__)}/data/SpecNumberFormat.xlsx" }

[ ["General", "Test"],
["General", "0123"],
["General", "0123.50"],
["Fixnum", 123],
["Currency", 123.0],
["Date", DateTime.new(1970, 1, 1)],
["Time", DateTime.new(2015, 2, 13, 12, 40, 5)],
["Percentage", 0.9999],
["Fraction", 0.5],
["Scientific", BigDecimal.new('3.4028236692093801E+38')],
["Custom", 123.0],
].each.with_index do |row, i|
name, value = row

it "should typecast #{name}" do
Workbook.open filename, auto_format: false do |w|
w.sheets[0].tap do |s|
expect(s.rows[i+1]).to eq([name, value, "Test"])
end
end
end
end
end
end

0 comments on commit cafdaf2

Please sign in to comment.