Class: RubyXL::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyXL/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Parser

:data_only allows only the sheet data to be parsed, so as to speed up parsing. However, using this option will result in date-formatted cells being interpreted as numbers.



14
15
16
# File 'lib/rubyXL/parser.rb', line 14

def initialize(opts = {})
  @opts = opts
end

Class Method Details

.parse(file_path, opts = {}) ⇒ Object



8
9
10
# File 'lib/rubyXL/parser.rb', line 8

def self.parse(file_path, opts = {})
  self.new(opts).parse(file_path)
end

Instance Method Details

#parse(xl_file_path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubyXL/parser.rb', line 18

def parse(xl_file_path)
  root = RubyXL::WorkbookRoot.parse_file(xl_file_path, @opts)

  wb = root.workbook

  if wb.stylesheet then
    #fills out count information for each font, fill, and border
    wb.cell_xfs.each { |style|

      id = style.font_id
      wb.fonts[id].count += 1 #unless id.nil?

      id = style.fill_id
      wb.fills[id].count += 1 #unless id.nil?

      id = style.border_id
      wb.borders[id].count += 1 #unless id.nil?
    }
  end

  wb.sheets.each_with_index { |sheet, i|
    sheet_obj = wb.relationship_container.related_files[sheet.r_id]

    wb.worksheets[i] = sheet_obj # Must be done first so the sheet becomes aware of its number
    sheet_obj.workbook = wb

    sheet_obj.sheet_name = sheet.name
    sheet_obj.sheet_id = sheet.sheet_id
    sheet_obj.state = sheet.state
  }

  wb
end