Class: LiquidXlsx::CellReference
- Inherits:
-
Object
- Object
- LiquidXlsx::CellReference
- Defined in:
- lib/liquid_xlsx/cell_reference.rb
Overview
Parses and manipulates Excel cell references (A1 notation).
Constant Summary collapse
- PATTERN =
/\A (?<col_absolute>\$)?(?<col>[A-Z]+) (?<row_absolute>\$)?(?<row>\d+) \z/x
Instance Attribute Summary collapse
-
#col ⇒ Object
readonly
Returns the value of attribute col.
-
#col_absolute ⇒ Object
readonly
Returns the value of attribute col_absolute.
-
#row ⇒ Object
readonly
Returns the value of attribute row.
-
#row_absolute ⇒ Object
readonly
Returns the value of attribute row_absolute.
Class Method Summary collapse
-
.col_to_index(col) ⇒ Integer
Convert column letters to zero-based index.
-
.index_to_col(index) ⇒ String
Convert zero-based index to column letters.
Instance Method Summary collapse
-
#initialize(ref) ⇒ CellReference
constructor
A new instance of CellReference.
-
#shift(row_delta, col_delta = 0) ⇒ String
Shift the reference by given row and column deltas.
- #to_s ⇒ Object
Constructor Details
#initialize(ref) ⇒ CellReference
Returns a new instance of CellReference.
14 15 16 17 18 19 20 21 22 |
# File 'lib/liquid_xlsx/cell_reference.rb', line 14 def initialize(ref) match = PATTERN.match(ref) raise ArgumentError, "Invalid cell reference: #{ref}" unless match @col = match[:col] @row = match[:row].to_i @col_absolute = !match[:col_absolute].nil? @row_absolute = !match[:row_absolute].nil? end |
Instance Attribute Details
#col ⇒ Object (readonly)
Returns the value of attribute col.
11 12 13 |
# File 'lib/liquid_xlsx/cell_reference.rb', line 11 def col @col end |
#col_absolute ⇒ Object (readonly)
Returns the value of attribute col_absolute.
11 12 13 |
# File 'lib/liquid_xlsx/cell_reference.rb', line 11 def col_absolute @col_absolute end |
#row ⇒ Object (readonly)
Returns the value of attribute row.
11 12 13 |
# File 'lib/liquid_xlsx/cell_reference.rb', line 11 def row @row end |
#row_absolute ⇒ Object (readonly)
Returns the value of attribute row_absolute.
11 12 13 |
# File 'lib/liquid_xlsx/cell_reference.rb', line 11 def row_absolute @row_absolute end |
Class Method Details
.col_to_index(col) ⇒ Integer
Convert column letters to zero-based index.
47 48 49 50 51 52 53 |
# File 'lib/liquid_xlsx/cell_reference.rb', line 47 def self.col_to_index(col) result = 0 col.each_char do |c| result = (result * 26) + (c.ord - "A".ord + 1) end result - 1 end |
.index_to_col(index) ⇒ String
Convert zero-based index to column letters.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/liquid_xlsx/cell_reference.rb', line 58 def self.index_to_col(index) result = "" n = index + 1 while n > 0 n -= 1 result = ((n % 26) + "A".ord).chr + result n /= 26 end result end |
Instance Method Details
#shift(row_delta, col_delta = 0) ⇒ String
Shift the reference by given row and column deltas.
28 29 30 31 32 33 34 35 36 |
# File 'lib/liquid_xlsx/cell_reference.rb', line 28 def shift(row_delta, col_delta = 0) new_row = @row_absolute ? @row : @row + row_delta new_col = @col_absolute ? @col : column_add(@col, col_delta) col_prefix = @col_absolute ? "$" : "" row_prefix = @row_absolute ? "$" : "" "#{col_prefix}#{new_col}#{row_prefix}#{new_row}" end |
#to_s ⇒ Object
38 39 40 41 42 |
# File 'lib/liquid_xlsx/cell_reference.rb', line 38 def to_s col_prefix = @col_absolute ? "$" : "" row_prefix = @row_absolute ? "$" : "" "#{col_prefix}#{@col}#{row_prefix}#{@row}" end |