Class: Tabulard::Adapters::Xlsx

Inherits:
Object
  • Object
show all
Includes:
Table
Defined in:
lib/tabulard/adapters/xlsx.rb

Defined Under Namespace

Classes: Cols, Rows

Constant Summary

Constants included from Table

Table::COL_CONVERTER, Table::Message

Instance Attribute Summary

Attributes included from Table

#messenger

Instance Method Summary collapse

Methods included from Table

#closed?, col2int, int2col

Constructor Details

#initialize(path, headers: nil, **opts) ⇒ Xlsx

Returns a new instance of Xlsx.



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

def initialize(path, headers: nil, **opts)
  super(**opts)

  @roo = Roo::Excelx.new(path)

  worktable = @roo.sheet_for(@roo.default_sheet)

  if worktable.first_row
    init_with_filled_table(worktable, headers: headers)
  else
    init_with_empty_table(headers: headers)
  end
end

Instance Method Details

#closeObject



67
68
69
70
71
# File 'lib/tabulard/adapters/xlsx.rb', line 67

def close
  super do
    @roo.close
  end
end

#each_header(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tabulard/adapters/xlsx.rb', line 29

def each_header(&block)
  raise_if_closed

  return to_enum(:each_header) { @cols_count } unless block
  return self if @cols_count.zero?

  if @headers
    each_custom_header(&block)
  else
    each_cell_header(&block)
  end

  self
end

#each_rowObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tabulard/adapters/xlsx.rb', line 44

def each_row
  raise_if_closed

  return to_enum(:each_row) { @rows_count } unless block_given?

  @rows_count.times do |row_index|
    row = @rows.name(row_index)

    row_value = Array.new(@cols_count) do |col_index|
      col = @cols.name(col_index)

      cell_coords = [@rows.coord(row_index), @cols.coord(col_index)]
      cell_value = @cells[cell_coords]&.value

      Cell.new(row: row, col: col, value: cell_value)
    end

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

  self
end