Class: RubyExcel::Element
- Inherits:
-
Object
- Object
- RubyExcel::Element
- Includes:
- Enumerable, Address
- Defined in:
- lib/rubyexcel/element.rb
Overview
A Range or Cell in a Sheet
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
The address.
-
#column ⇒ Object
readonly
The first Column id in the address.
-
#data ⇒ Object
readonly
The Data underlying the Sheet.
-
#row ⇒ Object
readonly
The first Row id in the address.
-
#sheet ⇒ Object
(also: #parent)
readonly
The parent Sheet.
Instance Method Summary collapse
-
#delete ⇒ Object
Delete the data referenced by self.address.
-
#each ⇒ Object
Yields each value in the data referenced by the address.
-
#each_cell ⇒ Object
Yields each Element referenced by the address.
-
#empty? ⇒ Boolean
Checks whether the data referenced by the address is empty.
-
#first_cell ⇒ RubyExcel::Element
Return the first cell in the Range.
-
#initialize(sheet, addr) ⇒ Element
constructor
Creates a RubyExcel::Element instance.
-
#inspect ⇒ Object
View the object for debugging.
-
#last_cell ⇒ RubyExcel::Element
Return the last cell in the Range.
-
#map! ⇒ Object
Replaces each value with the result of the block.
-
#to_s ⇒ Object
The data at address as a TSV String.
-
#value ⇒ Object+
Return the value at this Element’s address.
-
#value=(val) ⇒ Object
Set the value at this Element’s address.
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
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
#address ⇒ Object (readonly)
The address
16 17 18 |
# File 'lib/rubyexcel/element.rb', line 16 def address @address end |
#column ⇒ Object (readonly)
The first Column id in the address
22 23 24 |
# File 'lib/rubyexcel/element.rb', line 22 def column @column end |
#data ⇒ Object (readonly)
The Data underlying the Sheet
19 20 21 |
# File 'lib/rubyexcel/element.rb', line 19 def data @data end |
#row ⇒ Object (readonly)
The first Row id in the address
25 26 27 |
# File 'lib/rubyexcel/element.rb', line 25 def row @row end |
#sheet ⇒ Object (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
#delete ⇒ Object
Delete the data referenced by self.address
47 48 49 |
# File 'lib/rubyexcel/element.rb', line 47 def delete data.delete( self ); self end |
#each ⇒ Object
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? ( address ).flatten.each { |addr| yield data[ addr ] } end |
#each_cell ⇒ Object
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? ( address ).flatten.each { |addr| yield Element.new( sheet, addr ) } end |
#empty? ⇒ Boolean
Checks whether the data referenced by the address is empty
73 74 75 |
# File 'lib/rubyexcel/element.rb', line 73 def empty? all? { |v| v.to_s.empty? } end |
#first_cell ⇒ RubyExcel::Element
Return the first cell in the Range
83 84 85 |
# File 'lib/rubyexcel/element.rb', line 83 def first_cell Element.new( sheet, ( address ).flatten.first ) end |
#inspect ⇒ Object
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_cell ⇒ RubyExcel::Element
Return the last cell in the Range
101 102 103 |
# File 'lib/rubyexcel/element.rb', line 101 def last_cell Element.new( sheet, ( 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? ( address ).flatten.each { |addr| data[ addr ] = yield data[ addr ] } end |
#to_s ⇒ Object
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 |
#value ⇒ Object+
Return the value at this Element’s address
120 121 122 |
# File 'lib/rubyexcel/element.rb', line 120 def value address.include?( ':' ) ? ( address ).map { |ar| ar.map { |addr| data[ addr ] } } : data[ address ] end |
#value=(val) ⇒ Object
Set the value at this Element’s address
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 = ( 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 |