Class: ETL::Parser::ExcelParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/etl/parser/excel_parser.rb

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Attributes inherited from Parser

#options, #source

Instance Method Summary collapse

Methods inherited from Parser

class_for_name

Constructor Details

#initialize(source, options = {}) ⇒ ExcelParser

Initialize the parser

  • source: The Source object

  • options: Parser options Hash



12
13
14
15
# File 'lib/etl/parser/excel_parser.rb', line 12

def initialize(source, options={})
  super
  configure
end

Instance Attribute Details

#ignore_blank_lineObject

Returns the value of attribute ignore_blank_line.



7
8
9
# File 'lib/etl/parser/excel_parser.rb', line 7

def ignore_blank_line
  @ignore_blank_line
end

Instance Method Details

#eachObject

Returns each row



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
51
52
53
54
55
56
# File 'lib/etl/parser/excel_parser.rb', line 18

def each
  Dir.glob(file).each do |file|
    ETL::Engine.logger.debug "parsing #{file}"
    line = 0
    lines_skipped = 0
    book = Spreadsheet.open file
    loopworksheets = []

    if worksheets.empty?
      loopworksheets = book.worksheets
    else
      worksheets.each do |index|
        loopworksheets << book.worksheet(index)
      end
    end

    loopworksheets.each do |sheet|
      sheet.each do |raw_row|
        if lines_skipped < source.skip_lines
          ETL::Engine.logger.debug "skipping line"
          lines_skipped += 1
          next
        end
        line += 1
        row = {}
        if self.ignore_blank_line and raw_row.empty?
          lines_skipped += 1
          next
        end
        validate_row(raw_row, line, file)
        raw_row.each_with_index do |value, index|
          f = fields[index]
          row[f.name] = value
        end
        yield row
      end
    end
  end
end

#fieldsObject

Get an array of defined fields



64
65
66
# File 'lib/etl/parser/excel_parser.rb', line 64

def fields
  @fields ||= []
end

#worksheetsObject

Get an array of defined worksheets



59
60
61
# File 'lib/etl/parser/excel_parser.rb', line 59

def worksheets
  @worksheets ||= []
end