Class: RubyExcel::Section
- Inherits:
-
Object
- Object
- RubyExcel::Section
- Includes:
- Enumerable, Address
- Defined in:
- lib/rubyexcel/section.rb
Overview
Superclass for Row and Column
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The Data underlying the Sheet.
-
#sheet ⇒ Object
(also: #parent)
readonly
The Sheet parent of the Section.
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Append a value to the Section.
-
#cell(ref) ⇒ Object
Access a cell by its index within the Section.
-
#delete ⇒ Object
Delete the data referenced by self.
-
#each ⇒ Object
Yields each value.
-
#each_cell ⇒ Object
Yields each cell.
-
#each_cell_without_headers ⇒ Object
(also: #each_cell_wh)
Yields each cell, skipping headers.
-
#each_without_headers ⇒ Object
(also: #each_wh)
Yields each value, skipping headers.
-
#empty? ⇒ Boolean
Check whether the data in self is empty.
-
#find {|Object| ... } ⇒ String?
Return the address of a given value.
-
#initialize(sheet) ⇒ Section
constructor
Creates a RubyExcel::Section instance.
-
#inspect ⇒ Object
View the object for debugging.
-
#last_cell ⇒ RubyExcel::Element
Return the last cell.
-
#map! ⇒ Object
Replaces each value with the result of the block.
-
#map_without_headers! ⇒ Object
Replaces each value with the result of the block, skipping headers.
-
#read(id) ⇒ Object
(also: #[])
Read a value by address.
-
#summarise ⇒ Hash
(also: #summarize)
Summarise the values of a Section into a Hash.
-
#to_s ⇒ Object
The Section as a seperated value String.
-
#write(id, val) ⇒ Object
(also: #[]=)
Write a value by 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) ⇒ Section
Creates a RubyExcel::Section instance
24 25 26 27 |
# File 'lib/rubyexcel/section.rb', line 24 def initialize( sheet ) @sheet = sheet @data = sheet.data end |
Instance Attribute Details
#data ⇒ Object (readonly)
The Data underlying the Sheet
16 17 18 |
# File 'lib/rubyexcel/section.rb', line 16 def data @data end |
#sheet ⇒ Object (readonly) Also known as: parent
The Sheet parent of the Section
12 13 14 |
# File 'lib/rubyexcel/section.rb', line 12 def sheet @sheet end |
Instance Method Details
#<<(value) ⇒ Object
This only adds an extra cell if it is the first Row / Column. This prevents a loop through Rows or Columns from extending diagonally away from the main data.
Append a value to the Section.
37 38 39 40 41 42 43 |
# File 'lib/rubyexcel/section.rb', line 37 def <<( value ) case self when Row ; lastone = ( idx == 1 ? data.cols + 1 : data.cols ) else ; lastone = ( idx == 'A' ? data.rows + 1 : data.rows ) end data[ translate_address( lastone ) ] = value end |
#cell(ref) ⇒ Object
Access a cell by its index within the Section
49 50 51 |
# File 'lib/rubyexcel/section.rb', line 49 def cell( ref ) Element.new( sheet, translate_address( ref ) ) end |
#delete ⇒ Object
Delete the data referenced by self
57 58 59 |
# File 'lib/rubyexcel/section.rb', line 57 def delete data.delete( self ); self end |
#each ⇒ Object
Yields each value
65 66 67 68 |
# File 'lib/rubyexcel/section.rb', line 65 def each return to_enum(:each) unless block_given? each_address { |addr| yield data[ addr ] } end |
#each_cell ⇒ Object
Yields each cell
84 85 86 87 |
# File 'lib/rubyexcel/section.rb', line 84 def each_cell return to_enum( :each_cell ) unless block_given? each_address { |addr| yield Element.new( sheet, addr ) } end |
#each_cell_without_headers ⇒ Object Also known as: each_cell_wh
Yields each cell, skipping headers
93 94 95 96 |
# File 'lib/rubyexcel/section.rb', line 93 def each_cell_without_headers return to_enum( :each_cell_without_headers ) unless block_given? each_address_without_headers { |addr| yield Element.new( sheet, addr ) } end |
#each_without_headers ⇒ Object Also known as: each_wh
Yields each value, skipping headers
74 75 76 77 |
# File 'lib/rubyexcel/section.rb', line 74 def each_without_headers return to_enum( :each_without_headers ) unless block_given? each_address_without_headers { |addr| yield data[ addr ] } end |
#empty? ⇒ Boolean
Check whether the data in self is empty
103 104 105 |
# File 'lib/rubyexcel/section.rb', line 103 def empty? all? { |val| val.to_s.empty? } end |
#find {|Object| ... } ⇒ String?
Return the address of a given value
114 115 116 117 |
# File 'lib/rubyexcel/section.rb', line 114 def find return to_enum( :find ) unless block_given? each_cell { |ce| return ce.address if yield ce.value }; nil end |
#inspect ⇒ Object
View the object for debugging
123 124 125 |
# File 'lib/rubyexcel/section.rb', line 123 def inspect "#{ self.class }:0x#{ '%x' % (object_id << 1) }: #{ idx }" end |
#last_cell ⇒ RubyExcel::Element
Return the last cell
133 134 135 |
# File 'lib/rubyexcel/section.rb', line 133 def last_cell Element.new( sheet, each_address.to_a.last ) end |
#map! ⇒ Object
Replaces each value with the result of the block
141 142 143 144 |
# File 'lib/rubyexcel/section.rb', line 141 def map! return to_enum( :map! ) unless block_given? each_address { |addr| data[addr] = ( yield data[addr] ) } end |
#map_without_headers! ⇒ Object
Replaces each value with the result of the block, skipping headers
150 151 152 153 |
# File 'lib/rubyexcel/section.rb', line 150 def map_without_headers! return to_enum( :map_without_headers! ) unless block_given? each_address_without_headers { |addr| data[addr] = ( yield data[addr] ) } end |
#read(id) ⇒ Object Also known as: []
Read a value by address
161 162 163 |
# File 'lib/rubyexcel/section.rb', line 161 def read( id ) data[ translate_address( id ) ] end |
#summarise ⇒ Hash Also known as: summarize
Summarise the values of a Section into a Hash
172 173 174 |
# File 'lib/rubyexcel/section.rb', line 172 def summarise each_wh.inject( Hash.new(0) ) { |h, v| h[v]+=1; h } end |
#to_s ⇒ Object
The Section as a seperated value String
181 182 183 |
# File 'lib/rubyexcel/section.rb', line 181 def to_s to_a.join ( self.is_a?( Row ) ? "\t" : "\n" ) end |
#write(id, val) ⇒ Object Also known as: []=
Write a value by address
192 193 194 |
# File 'lib/rubyexcel/section.rb', line 192 def write( id, val ) data[ translate_address( id ) ] = val end |