Skip to content

Commit

Permalink
Use our old Excelx::FORMATS hash to spec out the new implementation. …
Browse files Browse the repository at this point in the history
…Support some (mildly dubious) exceptions for backwards compatibility.
  • Loading branch information
Empact committed Mar 14, 2013
1 parent e9c9b43 commit d97b57a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 47 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ gemspec
# additional testing libs
gem 'webmock'
gem 'shoulda'
gem 'rspec'
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ GEM
loquacious (~> 1.9.1)
rake (>= 0.8.7)
crack (0.3.1)
diff-lcs (1.2.1)
little-plugger (1.1.3)
loquacious (1.9.1)
nokogiri (1.5.6)
rake (0.9.2.2)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.1)
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.0)
ruby-ole (1.2.11.6)
rubyzip (0.9.9)
shoulda (3.0.1)
Expand All @@ -39,5 +48,6 @@ PLATFORMS
DEPENDENCIES
bones (>= 3.8.0)
roo!
rspec
shoulda
webmock
110 changes: 63 additions & 47 deletions lib/roo/excelx.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'fileutils'
require 'date'
require 'rubygems'
require 'nokogiri'

unless ''.respond_to?(:end_with?)
Expand All @@ -12,36 +11,67 @@ def end_with?(str)
end

class Roo::Excelx < Roo::GenericSpreadsheet
STANDARD_FORMATS = {
0 => 'General',
1 => '0',
2 => '0.00',
3 => '#,##0',
4 => '#,##0.00',
9 => '0%',
10 => '0.00%',
11 => '0.00E+00',
12 => '# ?/?',
13 => '# ??/??',
14 => 'mm-dd-yy',
15 => 'd-mmm-yy',
16 => 'd-mmm',
17 => 'mmm-yy',
18 => 'h:mm AM/PM',
19 => 'h:mm:ss AM/PM',
20 => 'h:mm',
21 => 'h:mm:ss',
22 => 'm/d/yy h:mm',
37 => '#,##0 ;(#,##0)',
38 => '#,##0 ;[Red](#,##0)',
39 => '#,##0.00;(#,##0.00)',
40 => '#,##0.00;[Red](#,##0.00)',
45 => 'mm:ss',
46 => '[h]:mm:ss',
47 => 'mmss.0',
48 => '##0.0E+0',
49 => '@',
}
module Format
EXCEPTIONAL_FORMATS = {
'h:mm am/pm' => :date,
'h:mm:ss am/pm' => :date,
'm/d/yy h:mm' => :date,
'#,##0 ;[red](#,##0)' => :float,
'#,##0.00;[red](#,##0.00)' => :float
}

STANDARD_FORMATS = {
0 => 'General',
1 => '0',
2 => '0.00',
3 => '#,##0',
4 => '#,##0.00',
9 => '0%',
10 => '0.00%',
11 => '0.00E+00',
12 => '# ?/?',
13 => '# ??/??',
14 => 'mm-dd-yy',
15 => 'd-mmm-yy',
16 => 'd-mmm',
17 => 'mmm-yy',
18 => 'h:mm AM/PM',
19 => 'h:mm:ss AM/PM',
20 => 'h:mm',
21 => 'h:mm:ss',
22 => 'm/d/yy h:mm',
37 => '#,##0 ;(#,##0)',
38 => '#,##0 ;[Red](#,##0)',
39 => '#,##0.00;(#,##0.00)',
40 => '#,##0.00;[Red](#,##0.00)',
45 => 'mm:ss',
46 => '[h]:mm:ss',
47 => 'mmss.0',
48 => '##0.0E+0',
49 => '@',
}

def to_type(format)
format = format.to_s.downcase
if type = EXCEPTIONAL_FORMATS[format]
type
elsif format.include?('d') || format.include?('y')
if format.include?('h') || format.include?('s')
:datetime
else
:date
end
elsif format.include?('h') || format.include?('s')
:time
elsif format.include?('%')
:percentage
else
:float
end
end

module_function :to_type
end

# initialization and opening of a spreadsheet file
# values for packed: :zip
Expand Down Expand Up @@ -362,20 +392,6 @@ def set_cell_values(sheet,x,y,i,v,value_type,formula,
@s_attribute[sheet][key] = s_attribute
end

def format2type(format)
format = format.to_s.downcase
case format
when /d|y/
case format
when /h|s/ then :datetime
else :date
end
when /h|s/ then :time
when /%/ then :percentage
else :float
end
end

# read all cells in the selected sheet
def read_cells(sheet=nil)
sheet ||= @default_sheet
Expand All @@ -401,7 +417,7 @@ def read_cells(sheet=nil)
# 2011-09-15 END
else
format = attribute2format(s_attribute)
format2type(format)
Format.to_type(format)
end
formula = nil
c.children.each do |cell|
Expand Down Expand Up @@ -627,7 +643,7 @@ def read_styles(doc)
# convert internal excelx attribute to a format
def attribute2format(s)
id = @cellXfs[s.to_i]
@numFmts[id] || STANDARD_FORMATS[id.to_i]
@numFmts[id] || Format::STANDARD_FORMATS[id.to_i]
end

end # class
48 changes: 48 additions & 0 deletions spec/lib/roo/excelx/format_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'spec_helper'

describe Roo::Excelx::Format do
describe '#to_type' do
FORMATS = {
'General' => :float,
'0' => :float,
'0.00' => :float,
'#,##0' => :float,
'#,##0.00' => :float,
'0%' => :percentage,
'0.00%' => :percentage,
'0.00E+00' => :float,
'# ?/?' => :float, #??? TODO:
'# ??/??' => :float, #??? TODO:
'mm-dd-yy' => :date,
'd-mmm-yy' => :date,
'd-mmm' => :date,
'mmm-yy' => :date,
'h:mm AM/PM' => :date,
'h:mm:ss AM/PM' => :date,
'h:mm' => :time,
'h:mm:ss' => :time,
'm/d/yy h:mm' => :date,
'#,##0 ;(#,##0)' => :float,
'#,##0 ;[Red](#,##0)' => :float,
'#,##0.00;(#,##0.00)' => :float,
'#,##0.00;[Red](#,##0.00)' => :float,
'mm:ss' => :time,
'[h]:mm:ss' => :time,
'mmss.0' => :time,
'##0.0E+0' => :float,
'@' => :float,
#-- zusaetzliche Formate, die nicht standardmaessig definiert sind:
"yyyy\\-mm\\-dd" => :date,
'dd/mm/yy' => :date,
'hh:mm:ss' => :time,
"dd/mm/yy\\ hh:mm" => :datetime,
'dd/mmm/yy' => :date, # 2011-05-21
'yyyy-mm-dd' => :date, # 2011-09-16
'yyyy-mm-dd;@' => :date
}.each do |format, type|
it "translates #{format} to #{type}" do
Roo::Excelx::Format.to_type(format).should == type
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require File.expand_path("../../lib/roo", __FILE__)

0 comments on commit d97b57a

Please sign in to comment.