Class: ETL::Parser::ExcelParser

Inherits:
Parser 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

#validate_rowsObject

Returns the value of attribute validate_rows



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

def validate_rows
  @validate_rows
end

#worksheet_columnObject

Returns the value of attribute worksheet_column



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

def worksheet_column
  @worksheet_column
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
57
58
59
60
61
62
63
64
65
66
# 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 = Roo::Spreadsheet.open file
    loopworksheets = []

    if worksheets.empty?
      loopworksheets = book.sheets
    else
      worksheets.each do |index|
        loopworksheets << book.sheet(index)
      end
    end
    
    sheet_index = -1

    book.each_with_pagename do |name, sheet|
      sheet_index += 1
      # puts "Sheet: #{name}"
      # puts worksheets.inspect
      if !worksheets.empty? && !worksheets.include?(sheet_index)
        # puts "No!!! #{sheet_index.inspect}"
        next
      end
      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) if self.validate_rows
        raw_row.each_with_index do |value, index|
          f = fields[index]
          row[f.name] = value
        end
        row[worksheet_column] = name if worksheet_column
        yield row
      end
    end
  end
end

#fieldsObject

Get an array of defined fields



74
75
76
# File 'lib/etl/parser/excel_parser.rb', line 74

def fields
  @fields ||= []
end

#worksheetsObject

Get an array of defined worksheets



69
70
71
# File 'lib/etl/parser/excel_parser.rb', line 69

def worksheets
  @worksheets ||= []
end