Class: Sheetah::Backends::Xlsx

Inherits:
Object
  • Object
show all
Includes:
Sheet
Defined in:
lib/sheetah/backends/xlsx.rb

Constant Summary

Constants included from Sheet

Sheet::COL_CONVERTER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sheet

col2int, int2col

Constructor Details

#initialize(path:) ⇒ Xlsx

Returns a new instance of Xlsx.

Raises:



29
30
31
32
33
34
35
36
# File 'lib/sheetah/backends/xlsx.rb', line 29

def initialize(path:)
  raise Error if path.nil?

  @roo = Roo::Excelx.new(path)
  @is_empty = worksheet.first_row.nil?
  @headers = detect_headers
  @cols_count = @headers.size
end

Class Method Details

.register(registry = Backends.registry) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sheetah/backends/xlsx.rb', line 16

def self.register(registry = Backends.registry)
  registry.set(self) do |args, opts|
    next false unless args.empty?

    case opts
    in { path: /\.xlsx$/i, **nil }
      true
    else
      false
    end
  end
end

Instance Method Details

#closeObject



75
76
77
78
79
# File 'lib/sheetah/backends/xlsx.rb', line 75

def close
  @roo.close

  nil
end

#each_headerObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sheetah/backends/xlsx.rb', line 38

def each_header
  return to_enum(:each_header) { @cols_count } unless block_given?

  @headers.each_with_index do |header, col_idx|
    col = Sheet.int2col(col_idx + 1)

    yield Header.new(col: col, value: header)
  end

  self
end

#each_rowObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sheetah/backends/xlsx.rb', line 50

def each_row
  return to_enum(:each_row) unless block_given?

  return if @is_empty

  first_row = 2
  last_row = worksheet.last_row
  row = 0

  first_row.upto(last_row) do |cursor|
    raw = worksheet.row(cursor)
    row += 1

    value = Array.new(@cols_count) do |col_idx|
      col = Sheet.int2col(col_idx + 1)

      Cell.new(row: row, col: col, value: raw[col_idx])
    end

    yield Row.new(row: row, value: value)
  end

  self
end