@@ -40,10 +40,11 @@ def initialize(filename, packed=nil, file_warning=:error)
40
40
@last_row = Hash . new
41
41
@first_column = Hash . new
42
42
@last_column = Hash . new
43
+ @header_line = 1
43
44
@style = Hash . new
44
45
@style_defaults = Hash . new { |h , k | h [ k ] = [ ] }
45
46
@style_definitions = Hash . new
46
- @header_line = 1
47
+ read_styles
47
48
end
48
49
49
50
# Returns the content of a spreadsheet-cell.
@@ -87,11 +88,11 @@ class Font
87
88
attr_accessor :bold , :italic , :underline
88
89
89
90
def bold?
90
- @bold == 'bold '
91
+ @bold == '1 '
91
92
end
92
93
93
94
def italic?
94
- @italic == 'italic '
95
+ @italic == '1 '
95
96
end
96
97
97
98
def underline?
@@ -241,42 +242,32 @@ def read_cells(sheet=nil)
241
242
sheet_found = true
242
243
row = 1
243
244
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
244
250
ws . find ( './/ss:Row' ) . each do |r |
245
251
skip_to_row = r . attributes [ 'Index' ] . to_i
246
252
row = skip_to_row if skip_to_row > 0
253
+ style_name = r . attributes [ 'StyleID' ] if r . attributes [ 'StyleID' ]
247
254
r . each do |c |
248
255
next unless c . name == 'Cell'
249
256
skip_to_col = c . attributes [ 'Index' ] . to_i
250
257
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
251
263
c . each_element do |cell |
252
264
formula = nil
253
- style_name = cell . attributes [ 'StyleID' ]
254
265
if cell . name == 'Data'
255
266
formula = cell . attributes [ 'Formula' ]
256
267
vt = cell . attributes [ 'Type' ] . downcase . to_sym
257
268
v = cell . content
258
269
str_v = v
259
270
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
280
271
when :number
281
272
v = v . to_f
282
273
vt = :float
@@ -290,17 +281,9 @@ def read_cells(sheet=nil)
290
281
end
291
282
when :boolean
292
283
v = cell . attributes [ 'boolean-value' ]
293
- else
294
- # raise "unknown type #{vt}"
295
284
end
296
- # puts vt
297
- # puts v
298
- # puts str_v
299
- # puts row
300
- # puts col
301
- # puts '---'
302
285
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 )
304
287
end
305
288
col += 1
306
289
end
@@ -314,19 +297,19 @@ def read_cells(sheet=nil)
314
297
@cells_read [ sheet ] = true
315
298
end
316
299
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
330
313
end
331
314
332
315
# Checks if the default_sheet exists. If not an RangeError exception is
0 commit comments