class RubyExcel::Range

A Range of Cells

Public Class Methods

new( sheet, addr ) click to toggle source
Calls superclass method RubyExcel::Element::new
# File lib/rubyexcel/element.rb, line 162
def initialize( sheet, addr )
  fail ArgumentError, "Invalid Range address: #{ addr }" unless addr =~ /\A[A-Z]{1,3}\d+:[A-Z]{1,3}\d+\z|\A[A-Z]{1,3}:[A-Z]{1,3}\z|\A\d+:\d+\z/i
  super
end

Public Instance Methods

to_s() click to toggle source

The data at address as a TSV String

# File lib/rubyexcel/element.rb, line 218
def to_s
  value.map { |ar| ar.map { |v| v.to_s.gsub(/\t|\n|\r/,' ') }.join "\t" }.join($/)
end
value() click to toggle source

Return the value at this Range’s address

@return [Array<Object>] the Array of Objects within the data, referenced by the address

# File lib/rubyexcel/element.rb, line 173
def value
  expand( address ).map { |ar| ar.map { |addr| data[ addr ] } }
end
value=( val ) click to toggle source

Set the value at this Range’s address

@param [Object, Array<Object>] val the Object or Array of Objects to write into the data

# File lib/rubyexcel/element.rb, line 183
def value=( val )

  addresses = expand( address )
  
  # 2D Array of Values
  if multi_array?( val ) && addresses.length > 1
  
    # Check the dimensions
    val_rows, val_cols, range_rows, range_cols = val.length, val.max_by(&:length).length, addresses.length, addresses.max_by(&:length).length
    val_rows == range_rows && val_cols == range_cols or fail ArgumentError, "Dimension mismatch! Value - rows: #{val_rows}, columns: #{ val_cols }. Range - rows: #{ range_rows }, columns: #{ range_cols }"
    
    # Write the values in order
    addresses.each_with_index { |row,idx| row.each_with_index { |el,i| data[el] = val[idx][i] } }
    
  # Array of Values
  elsif val.is_a?( Array )
    
    # Write the values in order
    addresses.flatten.each_with_index { |addr, i| data[addr] = val[i] }
    
  # Single Value
  else
  
    # Write the same value to every cell in the Range
    addresses.each { |ar| ar.each { |addr| data[ addr ] = val } }
    
  end

  val
end