Module: Xlsxrb::Elements::CoordinateAccess

Defined in:
lib/xlsxrb/elements/coordinate_access.rb,
sig/generated/xlsxrb/elements/coordinate_access.rbs

Overview

Mixin providing coordinate-based and random-access cell/row lookups for in-memory worksheet structures.

Expects the including class to provide a #rows method returning an Array<Elements::Row>.

Instance Method Summary collapse

Instance Method Details

#[](ref) ⇒ Elements::Cell?

Access a cell by its Excel-style reference (e.g. "A1").

: (String | Symbol ref) -> Elements::Cell?

Examples:

sheet["A1"] #=> #<Elements::Cell value="Hello">

Parameters:

  • ref (String, Symbol)

    Cell reference (e.g. "A1" or :A1).

Returns:



47
48
49
# File 'lib/xlsxrb/elements/coordinate_access.rb', line 47

def [](ref)
  cells_hash[ref.to_s.upcase]
end

#cell_value(ref) ⇒ Object?

Returns the raw cell value at the given Excel-style reference (e.g. "A1").

: (String ref) -> untyped

Examples:

sheet.cell_value("A1") #=> "Sales Report"

Parameters:

  • ref (String)

    Cell reference (e.g. "A1").

Returns:

  • (Object, nil)


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/xlsxrb/elements/coordinate_access.rb', line 88

def cell_value(ref)
  parsed = Cell.parse_ref(ref)
  return nil unless parsed

  row_idx, col_idx = parsed
  row = row_at(row_idx)
  return nil unless row

  cell = row.cell_at(col_idx)
  cell&.value
end

#cellsArray<Elements::Cell>

Returns all cells ordered by row and column index.

: () -> Array

Returns:



34
35
36
# File 'lib/xlsxrb/elements/coordinate_access.rb', line 34

def cells
  cells_hash.values.sort_by { |c| [c.row_index, c.column_index] }
end

#cells_hashHash<String, Elements::Cell>

Returns a Hash mapping Excel cell references (e.g. "A1") to Cell objects.

: () -> Hash[String, Elements::Cell]

Returns:



18
19
20
21
22
23
24
25
26
27
# File 'lib/xlsxrb/elements/coordinate_access.rb', line 18

def cells_hash
  h = {}
  rows.each do |r|
    r.cells.each do |c|
      ref = "#{Cell.column_letter(c.column_index)}#{c.row_index + 1}"
      h[ref] = c
    end
  end
  h
end

#first_rowElements::Row?

Returns the first row in the sheet, or nil.

: () -> Elements::Row?

Returns:



66
67
68
# File 'lib/xlsxrb/elements/coordinate_access.rb', line 66

def first_row
  rows.min_by(&:index)
end

#last_rowElements::Row?

Returns the last row in the sheet, or nil.

: () -> Elements::Row?

Returns:



75
76
77
# File 'lib/xlsxrb/elements/coordinate_access.rb', line 75

def last_row
  rows.max_by(&:index)
end

#row_at(index) ⇒ Elements::Row?

Returns the row at the given 0-based index, or nil.

: (Integer index) -> Elements::Row?

Parameters:

  • index (Integer)

    0-based row index.

Returns:



57
58
59
# File 'lib/xlsxrb/elements/coordinate_access.rb', line 57

def row_at(index)
  rows.find { |r| r.index == index }
end