Class: Exfuz::Parser

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

Constant Summary collapse

EXTENSIONS =
['.xlsx', '.xls', '.xlsm'].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
13
14
15
16
17
18
# File 'lib/exfuz/parser.rb', line 9

def initialize(path)
  raise 'not exsits file' unless File.exist?(path)

  extname = File.extname(path)
  raise 'no match extension name' unless EXTENSIONS.include?(extname)

  @absolute_path = File.absolute_path(path)
  @book = nil
  @sheet_names = []
end

Instance Attribute Details

#sheet_namesObject (readonly)

Returns the value of attribute sheet_names.



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

def sheet_names
  @sheet_names
end

Instance Method Details

#book_nameObject



25
26
27
# File 'lib/exfuz/parser.rb', line 25

def book_name
  @absolute_path
end

#each_cell_with_allObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/exfuz/parser.rb', line 29

def each_cell_with_all
  @book.sheets.each do |sheet|
    sheet_name = sheet.name
    sheet.each_with_index do |row, r_i|
      row.each_with_index do |val, c_i|
        next if val.nil?

        cell = {
          book_name: book_name,
          sheet_name: sheet_name,
          cell: to_address(r_i + 1, c_i + 1),
          value: val
        }
        yield cell
      end
    end
  end
end

#parseObject



20
21
22
23
# File 'lib/exfuz/parser.rb', line 20

def parse
  @book = Xsv.open(@absolute_path)
  @sheet_names = @book.sheets.map { |s| s.name.force_encoding(Encoding::UTF_8) }
end