Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add font support to 2003xml parsing

  • Loading branch information...
commit 348868bc72992c3dfebdf707c0466b17e4f3c9de 1 parent 4d64ee7
@hmcgowan authored
Showing with 33 additions and 51 deletions.
  1. +29 −46 lib/roo/excel2003xml.rb
  2. +1 −2  lib/roo/excelx.rb
  3. +3 −3 test/test_roo.rb
View
75 lib/roo/excel2003xml.rb
@@ -40,10 +40,11 @@ def initialize(filename, packed=nil, file_warning=:error)
@last_row = Hash.new
@first_column = Hash.new
@last_column = Hash.new
+ @header_line = 1
@style = Hash.new
@style_defaults = Hash.new { |h,k| h[k] = [] }
@style_definitions = Hash.new
- @header_line = 1
+ read_styles
end
# Returns the content of a spreadsheet-cell.
@@ -87,11 +88,11 @@ class Font
attr_accessor :bold, :italic, :underline
def bold?
- @bold == 'bold'
+ @bold == '1'
end
def italic?
- @italic == 'italic'
+ @italic == '1'
end
def underline?
@@ -241,42 +242,32 @@ def read_cells(sheet=nil)
sheet_found = true
row = 1
col = 1
+ column_attributes = {}
+ idx = 0
+ ws.find('.//ss:Column').each do |c|
+ column_attributes[(idx += 1).to_s] = c.attributes['StyleID']
+ end
ws.find('.//ss:Row').each do |r|
skip_to_row = r.attributes['Index'].to_i
row = skip_to_row if skip_to_row > 0
+ style_name = r.attributes['StyleID'] if r.attributes['StyleID']
r.each do |c|
next unless c.name == 'Cell'
skip_to_col = c.attributes['Index'].to_i
col = skip_to_col if skip_to_col > 0
+ if c.attributes['StyleID']
+ style_name = c.attributes['StyleID']
+ elsif
+ style_name ||= column_attributes[c.attributes['Index']]
+ end
c.each_element do |cell|
formula = nil
- style_name = cell.attributes['StyleID']
if cell.name == 'Data'
formula = cell.attributes['Formula']
vt = cell.attributes['Type'].downcase.to_sym
v = cell.content
str_v = v
case vt
- # when :string
- # str_v = ''
- # # insert \n if there is more than one paragraph
- # para_count = 0
- # cell.each_element do |str|
- # if str.name == 'p'
- # v = str.content
- # str_v += "\n" if para_count > 0
- # para_count += 1
- # if str.children.size > 1
- # str_v += children_to_string(str.children)
- # else
- # str.children.each do |child|
- # str_v += child.content #.text
- # end
- # end
- # str_v.gsub!(/'/,"'") # special case not supported by unescapeHTML
- # str_v = CGI.unescapeHTML(str_v)
- # end # == 'p'
- # end
when :number
v = v.to_f
vt = :float
@@ -290,17 +281,9 @@ def read_cells(sheet=nil)
end
when :boolean
v = cell.attributes['boolean-value']
- else
- # raise "unknown type #{vt}"
end
- # puts vt
- # puts v
- # puts str_v
- # puts row
- # puts col
- # puts '---'
end
- set_cell_values(sheet,col,row,0,v,vt.to_sym,formula,cell,str_v,style_name)
+ set_cell_values(sheet,col,row,0,v,vt,formula,cell,str_v,style_name)
end
col += 1
end
@@ -314,19 +297,19 @@ def read_cells(sheet=nil)
@cells_read[sheet] = true
end
- def read_styles(style_elements)
- @style_definitions['Default'] = Openoffice::Font.new
- style_elements.each do |style|
- next unless style.name == 'style'
- style_name = style.attributes['name']
- style.each do |properties|
- font = Openoffice::Font.new
- font.bold = properties.attributes['font-weight']
- font.italic = properties.attributes['font-style']
- font.underline = properties.attributes['text-underline-style']
- @style_definitions[style_name] = font
- end
- end
+ def read_styles
+ @doc.find("ss:Styles").each do |styles|
+ styles.find('.//ss:Style').each do |style|
+ style_id = style.attributes['ID']
+ @style_definitions[style_id] = Excel2003XML::Font.new
+ font = style.find_first('.//ss:Font')
+ if font
+ @style_definitions[style_id].bold = font.attributes['Bold']
+ @style_definitions[style_id].italic = font.attributes['Italic']
+ @style_definitions[style_id].underline = font.attributes['Underline']
+ end
+ end
+ end
end
# Checks if the default_sheet exists. If not an RangeError exception is
View
3  lib/roo/excelx.rb
@@ -391,13 +391,12 @@ def read_cells(sheet=nil)
raise RangeError unless self.sheets.include? sheet
n = self.sheets.index(sheet)
@sheet_doc[n].find("//*[local-name()='c']").each do |c|
- s_attribute = c.attributes.to_h['s'].to_i # should be here
+ s_attribute = c.attributes.to_h['s'].to_i
if (c.attributes.to_h['t'] == 's')
tmp_type = :shared
elsif (c.attributes.to_h['t'] == 'b')
tmp_type = :boolean
else
- # s_attribute = c.attributes.to_h['s'].to_i # was here
format = attribute2format(s_attribute)
tmp_type = format2type(format)
end
View
6 test/test_roo.rb
@@ -1673,7 +1673,7 @@ def test_cell_multiline
end
def test_cell_styles
- with_each_spreadsheet(:name=>'style', :format=>[:openoffice, :excel, :excelx]) do |oo|
+ with_each_spreadsheet(:name=>'style', :format=>[:openoffice, :excel, :excelx, :excel2003xml]) do |oo|
# bold
assert_equal true, oo.font(1,1).bold?
assert_equal false, oo.font(1,1).italic?
@@ -1716,8 +1716,8 @@ def test_cell_styles
# bolded col
assert_equal true, oo.font(9,2).bold?
- assert_equal false, oo.font(9,2).italic?
- assert_equal false, oo.font(9,2).underline?
+ assert_equal false, oo.font(9,2).italic?
+ assert_equal false, oo.font(9,2).underline?
# bolded row, italic col
assert_equal true, oo.font(10,3).bold?
Please sign in to comment.
Something went wrong with that request. Please try again.