Skip to content

Commit 348868b

Browse files
author
Hugh McGowan
committed
add font support to 2003xml parsing
1 parent 4d64ee7 commit 348868b

File tree

3 files changed

+33
-51
lines changed

3 files changed

+33
-51
lines changed

lib/roo/excel2003xml.rb

+29-46
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ def initialize(filename, packed=nil, file_warning=:error)
4040
@last_row = Hash.new
4141
@first_column = Hash.new
4242
@last_column = Hash.new
43+
@header_line = 1
4344
@style = Hash.new
4445
@style_defaults = Hash.new { |h,k| h[k] = [] }
4546
@style_definitions = Hash.new
46-
@header_line = 1
47+
read_styles
4748
end
4849

4950
# Returns the content of a spreadsheet-cell.
@@ -87,11 +88,11 @@ class Font
8788
attr_accessor :bold, :italic, :underline
8889

8990
def bold?
90-
@bold == 'bold'
91+
@bold == '1'
9192
end
9293

9394
def italic?
94-
@italic == 'italic'
95+
@italic == '1'
9596
end
9697

9798
def underline?
@@ -241,42 +242,32 @@ def read_cells(sheet=nil)
241242
sheet_found = true
242243
row = 1
243244
col = 1
245+
column_attributes = {}
246+
idx = 0
247+
ws.find('.//ss:Column').each do |c|
248+
column_attributes[(idx += 1).to_s] = c.attributes['StyleID']
249+
end
244250
ws.find('.//ss:Row').each do |r|
245251
skip_to_row = r.attributes['Index'].to_i
246252
row = skip_to_row if skip_to_row > 0
253+
style_name = r.attributes['StyleID'] if r.attributes['StyleID']
247254
r.each do |c|
248255
next unless c.name == 'Cell'
249256
skip_to_col = c.attributes['Index'].to_i
250257
col = skip_to_col if skip_to_col > 0
258+
if c.attributes['StyleID']
259+
style_name = c.attributes['StyleID']
260+
elsif
261+
style_name ||= column_attributes[c.attributes['Index']]
262+
end
251263
c.each_element do |cell|
252264
formula = nil
253-
style_name = cell.attributes['StyleID']
254265
if cell.name == 'Data'
255266
formula = cell.attributes['Formula']
256267
vt = cell.attributes['Type'].downcase.to_sym
257268
v = cell.content
258269
str_v = v
259270
case vt
260-
# when :string
261-
# str_v = ''
262-
# # insert \n if there is more than one paragraph
263-
# para_count = 0
264-
# cell.each_element do |str|
265-
# if str.name == 'p'
266-
# v = str.content
267-
# str_v += "\n" if para_count > 0
268-
# para_count += 1
269-
# if str.children.size > 1
270-
# str_v += children_to_string(str.children)
271-
# else
272-
# str.children.each do |child|
273-
# str_v += child.content #.text
274-
# end
275-
# end
276-
# str_v.gsub!(/'/,"'") # special case not supported by unescapeHTML
277-
# str_v = CGI.unescapeHTML(str_v)
278-
# end # == 'p'
279-
# end
280271
when :number
281272
v = v.to_f
282273
vt = :float
@@ -290,17 +281,9 @@ def read_cells(sheet=nil)
290281
end
291282
when :boolean
292283
v = cell.attributes['boolean-value']
293-
else
294-
# raise "unknown type #{vt}"
295284
end
296-
# puts vt
297-
# puts v
298-
# puts str_v
299-
# puts row
300-
# puts col
301-
# puts '---'
302285
end
303-
set_cell_values(sheet,col,row,0,v,vt.to_sym,formula,cell,str_v,style_name)
286+
set_cell_values(sheet,col,row,0,v,vt,formula,cell,str_v,style_name)
304287
end
305288
col += 1
306289
end
@@ -314,19 +297,19 @@ def read_cells(sheet=nil)
314297
@cells_read[sheet] = true
315298
end
316299

317-
def read_styles(style_elements)
318-
@style_definitions['Default'] = Openoffice::Font.new
319-
style_elements.each do |style|
320-
next unless style.name == 'style'
321-
style_name = style.attributes['name']
322-
style.each do |properties|
323-
font = Openoffice::Font.new
324-
font.bold = properties.attributes['font-weight']
325-
font.italic = properties.attributes['font-style']
326-
font.underline = properties.attributes['text-underline-style']
327-
@style_definitions[style_name] = font
328-
end
329-
end
300+
def read_styles
301+
@doc.find("ss:Styles").each do |styles|
302+
styles.find('.//ss:Style').each do |style|
303+
style_id = style.attributes['ID']
304+
@style_definitions[style_id] = Excel2003XML::Font.new
305+
font = style.find_first('.//ss:Font')
306+
if font
307+
@style_definitions[style_id].bold = font.attributes['Bold']
308+
@style_definitions[style_id].italic = font.attributes['Italic']
309+
@style_definitions[style_id].underline = font.attributes['Underline']
310+
end
311+
end
312+
end
330313
end
331314

332315
# Checks if the default_sheet exists. If not an RangeError exception is

lib/roo/excelx.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,12 @@ def read_cells(sheet=nil)
391391
raise RangeError unless self.sheets.include? sheet
392392
n = self.sheets.index(sheet)
393393
@sheet_doc[n].find("//*[local-name()='c']").each do |c|
394-
s_attribute = c.attributes.to_h['s'].to_i # should be here
394+
s_attribute = c.attributes.to_h['s'].to_i
395395
if (c.attributes.to_h['t'] == 's')
396396
tmp_type = :shared
397397
elsif (c.attributes.to_h['t'] == 'b')
398398
tmp_type = :boolean
399399
else
400-
# s_attribute = c.attributes.to_h['s'].to_i # was here
401400
format = attribute2format(s_attribute)
402401
tmp_type = format2type(format)
403402
end

test/test_roo.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1673,7 +1673,7 @@ def test_cell_multiline
16731673
end
16741674

16751675
def test_cell_styles
1676-
with_each_spreadsheet(:name=>'style', :format=>[:openoffice, :excel, :excelx]) do |oo|
1676+
with_each_spreadsheet(:name=>'style', :format=>[:openoffice, :excel, :excelx, :excel2003xml]) do |oo|
16771677
# bold
16781678
assert_equal true, oo.font(1,1).bold?
16791679
assert_equal false, oo.font(1,1).italic?
@@ -1716,8 +1716,8 @@ def test_cell_styles
17161716

17171717
# bolded col
17181718
assert_equal true, oo.font(9,2).bold?
1719-
assert_equal false, oo.font(9,2).italic?
1720-
assert_equal false, oo.font(9,2).underline?
1719+
assert_equal false, oo.font(9,2).italic?
1720+
assert_equal false, oo.font(9,2).underline?
17211721

17221722
# bolded row, italic col
17231723
assert_equal true, oo.font(10,3).bold?

0 commit comments

Comments
 (0)