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
-
#[](ref) ⇒ Elements::Cell?
Access a cell by its Excel-style reference (e.g. "A1").
-
#cell_value(ref) ⇒ Object?
Returns the raw cell value at the given Excel-style reference (e.g. "A1").
-
#cells ⇒ Array<Elements::Cell>
Returns all cells ordered by row and column index.
-
#cells_hash ⇒ Hash<String, Elements::Cell>
Returns a Hash mapping Excel cell references (e.g. "A1") to Cell objects.
-
#first_row ⇒ Elements::Row?
Returns the first row in the sheet, or nil.
-
#last_row ⇒ Elements::Row?
Returns the last row in the sheet, or nil.
-
#row_at(index) ⇒ Elements::Row?
Returns the row at the given 0-based index, or nil.
Instance Method Details
#[](ref) ⇒ Elements::Cell?
Access a cell by its Excel-style reference (e.g. "A1").
: (String | Symbol ref) -> Elements::Cell?
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
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 |
#cells ⇒ Array<Elements::Cell>
Returns all cells ordered by row and column index.
: () -> Array
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_hash ⇒ Hash<String, Elements::Cell>
Returns a Hash mapping Excel cell references (e.g. "A1") to Cell objects.
: () -> Hash[String, Elements::Cell]
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_row ⇒ Elements::Row?
Returns the first row in the sheet, or nil.
: () -> Elements::Row?
66 67 68 |
# File 'lib/xlsxrb/elements/coordinate_access.rb', line 66 def first_row rows.min_by(&:index) end |
#last_row ⇒ Elements::Row?
Returns the last row in the sheet, or nil.
: () -> Elements::Row?
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?
57 58 59 |
# File 'lib/xlsxrb/elements/coordinate_access.rb', line 57 def row_at(index) rows.find { |r| r.index == index } end |