class RubyXL::Address

Constants

COLUMN_REF_FORMAT
ROW_REF_FORMAT

Attributes

worksheet[W]

@param [RubyXL::Worksheet] worksheet @return [RubyXL::Worksheet]

Public Class Methods

column_ind2ref(ind) click to toggle source

@param [Integer] ind @return [String]

# File lib/rubyXL/address.rb, line 26
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) click to toggle source

@param [String, Symbol] ref @return [Integer]

# File lib/rubyXL/address.rb, line 49
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
new(worksheet, ref: nil, row: nil, column: nil) click to toggle source

@param [RubyXL::Worksheet] worksheet @param [String, Symbol] ref @param [Integer, String, Symbol] row @param [Integer, String, Symbol] column

# File lib/rubyXL/address.rb, line 83
def initialize(worksheet, ref: nil, row: nil, column: nil)
  @worksheet = worksheet

  row, column = RubyXL::Reference.ref2ind(ref) if ref
  self.row    = row
  self.column = column
end
row_ind2ref(ind) click to toggle source

@param [Integer] ind @return [String]

# File lib/rubyXL/address.rb, line 18
def row_ind2ref(ind)
  validate_index(:row, ind)

  (ind + 1).to_s.freeze
end
row_ref2ind(ref) click to toggle source

@param [String, Symbol] ref @return [Integer]

# File lib/rubyXL/address.rb, line 40
def row_ref2ind(ref)
  message = "invalid row #{ref.inspect}"
  raise ArgumentError, message unless ROW_REF_FORMAT =~ ref

  ref.to_s.to_i - 1
end

Private Class Methods

normalize(row_or_column, value) click to toggle source
# File lib/rubyXL/address.rb, line 58
def normalize(row_or_column, value)
  case value
  when String, Symbol
    value = public_send("#{row_or_column}_ref2ind", value)
  else
    validate_index(row_or_column, value)
  end
  value
end
validate_index(row_or_column, index) click to toggle source
# File lib/rubyXL/address.rb, line 68
def validate_index(row_or_column, index)
  message = "invalid #{row_or_column} #{index.inspect}"
  raise TypeError,     message unless index.is_a?(Integer)
  raise ArgumentError, message unless index >= 0
end

Public Instance Methods

cell() click to toggle source

@return [RubyXL::Cell, nil]

# File lib/rubyXL/address.rb, line 196
def cell
  (row = @worksheet[@row]) && row[@column]
end
column(column = nil) click to toggle source

@param [Integer, String, Symbol, nil] column @return [Integer, RubyXL::Address]

# File lib/rubyXL/address.rb, line 113
def column(column = nil)
  if column.nil?
    @column
  else
    self.class.new(@worksheet, row: @row, column: column)
  end
end
column=(column) click to toggle source

@param [Integer, String, Symbol] column @return [Integer, String, Symbol]

# File lib/rubyXL/address.rb, line 129
def column=(column)
  @column = self.class.__send__(:normalize, :column, column)
end
down(amount = 1) click to toggle source

@param [Integer] amount @return [RubyXL::Address]

# File lib/rubyXL/address.rb, line 151
def down(amount = 1)
  row(@row + amount)
end
down!(amount = 1) click to toggle source

@param [Integer] amount @return [self]

# File lib/rubyXL/address.rb, line 176
def down!(amount = 1)
  self.row += amount
  self
end
inspect() click to toggle source

@return [String]

# File lib/rubyXL/address.rb, line 139
def inspect
  format('#<%s %s!%s>', self.class.name, @worksheet.sheet_name, ref)
end
left(amount = 1) click to toggle source

@param [Integer] amount @return [RubyXL::Address]

# File lib/rubyXL/address.rb, line 157
def left(amount = 1)
  column(@column - amount)
end
left!(amount = 1) click to toggle source

@param [Integer] amount @return [self]

# File lib/rubyXL/address.rb, line 183
def left!(amount = 1)
  self.column -= amount
  self
end
ref() click to toggle source

@return [String]

# File lib/rubyXL/address.rb, line 134
def ref
  RubyXL::Reference.ind2ref(@row, @column)
end
right(amount = 1) click to toggle source

@param [Integer] amount @return [RubyXL::Address]

# File lib/rubyXL/address.rb, line 163
def right(amount = 1)
  column(@column + amount)
end
right!(amount = 1) click to toggle source

@param [Integer] amount @return [self]

# File lib/rubyXL/address.rb, line 190
def right!(amount = 1)
  self.column += amount
  self
end
row(row = nil) click to toggle source

@param [Integer, String, Symbol, nil] row @return [Integer, RubyXL::Address]

# File lib/rubyXL/address.rb, line 103
def row(row = nil)
  if row.nil?
    @row
  else
    self.class.new(@worksheet, row: row, column: @column)
  end
end
row=(row) click to toggle source

@param [Integer, String, Symbol] row @return [Integer, String, Symbol]

# File lib/rubyXL/address.rb, line 123
def row=(row)
  @row = self.class.__send__(:normalize, :row, row)
end
up(amount = 1) click to toggle source

@param [Integer] amount @return [RubyXL::Address]

# File lib/rubyXL/address.rb, line 145
def up(amount = 1)
  row(@row - amount)
end
up!(amount = 1) click to toggle source

@param [Integer] amount @return [self]

# File lib/rubyXL/address.rb, line 169
def up!(amount = 1)
  self.row -= amount
  self
end
value() click to toggle source

@return [Object]

# File lib/rubyXL/address.rb, line 201
def value
  cell && cell.value
end
value=(value) click to toggle source

@param [Object] value @return [Object]

# File lib/rubyXL/address.rb, line 207
def value=(value)
  @worksheet.add_cell(@row, @column) unless cell
  cell.change_contents(value)
end
worksheet(worksheet = nil) click to toggle source

@param [RubyXL::Worksheet, nil] worksheet @return [RubyXL::Workbook, RubyXL::Address]

# File lib/rubyXL/address.rb, line 93
def worksheet(worksheet = nil)
  if worksheet.nil?
    @worksheet
  else
    self.class.new(worksheet, row: @row, column: @column)
  end
end