Class: LiquidXlsx::CellReference

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref) ⇒ CellReference

Returns a new instance of CellReference.

Parameters:

  • ref (String)

    e.g. "A1", "$A$1", "A$1", "$A1", "AA10"

Raises:

  • (ArgumentError)


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

#colObject (readonly)

Returns the value of attribute col.



11
12
13
# File 'lib/liquid_xlsx/cell_reference.rb', line 11

def col
  @col
end

#col_absoluteObject (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

#rowObject (readonly)

Returns the value of attribute row.



11
12
13
# File 'lib/liquid_xlsx/cell_reference.rb', line 11

def row
  @row
end

#row_absoluteObject (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.

Parameters:

  • col (String)

    e.g. "A", "AA"

Returns:

  • (Integer)


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.

Parameters:

  • index (Integer)

Returns:

  • (String)


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.

Parameters:

  • row_delta (Integer)
  • col_delta (Integer) (defaults to: 0)

Returns:

  • (String)

    new cell reference



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_sObject



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