Class: RubyExcel::Element

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Address
Defined in:
lib/rubyexcel/element.rb

Overview

A Range or Cell in a Sheet

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Address

#address_to_col_index, #address_to_indices, #col_index, #col_letter, #column_id, #expand, #indices_to_address, #multi_array?, #offset, #row_id, #to_range_address

Constructor Details

#initialize(sheet, addr) ⇒ Element

Creates a RubyExcel::Element instance

Parameters:

  • sheet (RubyExcel::Sheet)

    the parent Sheet

  • addr (String)

    the address to reference



34
35
36
37
38
39
40
41
# File 'lib/rubyexcel/element.rb', line 34

def initialize( sheet, addr )
  fail ArgumentError, "Invalid range: #{ addr }" unless addr =~ /\A[A-Z]{1,3}\d+:[A-Z]{1,3}\d+\z|\A[A-Z]{1,3}\d+\z|\A[A-Z]{1,3}:[A-Z]{1,3}\z|\A\d+:\d+\z/
  @sheet = sheet
  @data = sheet.data
  @address = addr
  @column = column_id( addr )
  @row = row_id( addr )
end

Instance Attribute Details

#addressObject (readonly)

The address



16
17
18
# File 'lib/rubyexcel/element.rb', line 16

def address
  @address
end

#columnObject (readonly)

The first Column id in the address



22
23
24
# File 'lib/rubyexcel/element.rb', line 22

def column
  @column
end

#dataObject (readonly)

The Data underlying the Sheet



19
20
21
# File 'lib/rubyexcel/element.rb', line 19

def data
  @data
end

#rowObject (readonly)

The first Row id in the address



25
26
27
# File 'lib/rubyexcel/element.rb', line 25

def row
  @row
end

#sheetObject (readonly) Also known as: parent

The parent Sheet



12
13
14
# File 'lib/rubyexcel/element.rb', line 12

def sheet
  @sheet
end

Instance Method Details

#deleteObject

Delete the data referenced by self.address



47
48
49
# File 'lib/rubyexcel/element.rb', line 47

def delete
  data.delete( self ); self
end

#eachObject

Yields each value in the data referenced by the address



55
56
57
58
# File 'lib/rubyexcel/element.rb', line 55

def each
  return to_enum( :each ) unless block_given?
  expand( address ).flatten.each { |addr| yield data[ addr ] }
end

#each_cellObject

Yields each Element referenced by the address



64
65
66
67
# File 'lib/rubyexcel/element.rb', line 64

def each_cell
  return to_enum( :each_cell ) unless block_given?
  expand( address ).flatten.each { |addr| yield Element.new( sheet, addr ) }
end

#empty?Boolean

Checks whether the data referenced by the address is empty

Returns:

  • (Boolean)


73
74
75
# File 'lib/rubyexcel/element.rb', line 73

def empty?
  all? { |v| v.to_s.empty? }
end

#first_cellRubyExcel::Element

Return the first cell in the Range

Returns:



83
84
85
# File 'lib/rubyexcel/element.rb', line 83

def first_cell
  Element.new( sheet, expand( address ).flatten.first )
end

#inspectObject

View the object for debugging



91
92
93
# File 'lib/rubyexcel/element.rb', line 91

def inspect
  "#{ self.class }:0x#{ '%x' % ( object_id << 1 ) }: #{ address }"
end

#last_cellRubyExcel::Element

Return the last cell in the Range

Returns:



101
102
103
# File 'lib/rubyexcel/element.rb', line 101

def last_cell
  Element.new( sheet, expand( address ).flatten.last )
end

#map!Object

Replaces each value with the result of the block



109
110
111
112
# File 'lib/rubyexcel/element.rb', line 109

def map!
  return to_enum( :map! ) unless block_given?
  expand( address ).flatten.each { |addr| data[ addr ] = yield data[ addr ] }
end

#to_sObject

The data at address as a TSV String



172
173
174
# File 'lib/rubyexcel/element.rb', line 172

def to_s
  value.is_a?( Array ) ? value.map { |ar| ar.join "\t" }.join($/) : value.to_s
end

#valueObject+

Return the value at this Element’s address

Returns:

  • (Object, Array<Object>)

    the Object or Array of Objects within the data, referenced by the address



120
121
122
# File 'lib/rubyexcel/element.rb', line 120

def value
  address.include?( ':' ) ? expand( address ).map { |ar| ar.map { |addr| data[ addr ] } } : data[ address ]
end

#value=(val) ⇒ Object

Set the value at this Element’s address

Parameters:

  • val (Object, Array<Object>)

    the Object or Array of Objects to write into the data



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rubyexcel/element.rb', line 130

def value=( val )

  #Range
  if address.include? ':'
  
    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 )
      
      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
  
  #Cell
  else
    data[ address ] = val
  end
  
  self
end