Module: RubyXL::Addressing

Defined in:
lib/rubyXL/addressing.rb,
lib/rubyXL/addressing/version.rb

Constant Summary collapse

ROW_REF_FORMAT =
/\A[1-9]\d*\z/
COLUMN_REF_FORMAT =
/\A[A-Z]+\z/
VERSION =
'0.2.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.column_ind2ref(ind) ⇒ String

Parameters:

  • ind (Integer)

Returns:

  • (String)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubyXL/addressing.rb', line 24

def column_ind2ref(ind)
  validate_index('column', ind)

  ref = ''.dup
  loop do
    ref.prepend((ind % 26 + 65).chr)
    ind = ind / 26 - 1
    break if ind < 0
  end
  ref.freeze
end

.column_ref2ind(ref) ⇒ Integer

Parameters:

  • ref (String, Symbol)

Returns:

  • (Integer)

Raises:

  • (ArgumentError)


47
48
49
50
51
52
# File 'lib/rubyXL/addressing.rb', line 47

def column_ref2ind(ref)
  message = "invalid column #{ref.inspect}"
  raise ArgumentError, message unless COLUMN_REF_FORMAT =~ ref

  ref.to_s.each_byte.reduce(0) { |a, e| a * 26 + (e - 64) } - 1
end

.row_ind2ref(ind) ⇒ String

Parameters:

  • ind (Integer)

Returns:

  • (String)


16
17
18
19
20
# File 'lib/rubyXL/addressing.rb', line 16

def row_ind2ref(ind)
  validate_index('row', ind)

  (ind + 1).to_s.freeze
end

.row_ref2ind(ref) ⇒ Integer

Parameters:

  • ref (String, Symbol)

Returns:

  • (Integer)

Raises:

  • (ArgumentError)


38
39
40
41
42
43
# File 'lib/rubyXL/addressing.rb', line 38

def row_ref2ind(ref)
  message = "invalid row #{ref.inspect}"
  raise ArgumentError, message unless ROW_REF_FORMAT =~ ref

  ref.to_s.to_i - 1
end

Instance Method Details

#addr(ref_or_row, column = nil) ⇒ RubyXL::Address

Parameters:

  • ref_or_row (String, Symbol, Integer)

    is ref ‘’A1’‘ or row index `0` or row label `’1’‘

  • column (String, Symbol, Integer) (defaults to: nil)

    is column index ‘0` or column label `’A’‘

Returns:



79
80
81
82
83
84
85
# File 'lib/rubyXL/addressing.rb', line 79

def addr(ref_or_row, column = nil)
  if column.nil?
    Address.new(self, ref: ref_or_row)
  else
    Address.new(self, row: ref_or_row, column: column)
  end
end