Class: RubyExcel::Section

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

Overview

Superclass for Row and Column

Direct Known Subclasses

Column, Row

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) ⇒ Section

Creates a RubyExcel::Section instance

Parameters:



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

def initialize( sheet )
  @sheet = sheet
  @data = sheet.data
end

Instance Attribute Details

#dataObject (readonly)

The Data underlying the Sheet



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

def data
  @data
end

#sheetObject (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

Note:

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.

Parameters:

  • value (Object)

    the object to append



37
38
39
# File 'lib/rubyexcel/section.rb', line 37

def <<( value )
  data[ translate_address( ( col_index( idx ) == 1 ? data.cols + 1 : data.cols ) ) ] = value
end

#cell(ref) ⇒ Object

Access a cell by its index within the Section



45
46
47
# File 'lib/rubyexcel/section.rb', line 45

def cell( ref )
  Cell.new( sheet, translate_address( ref ) )
end

#deleteObject

Delete the data referenced by self



53
54
55
# File 'lib/rubyexcel/section.rb', line 53

def delete
  data.delete( self ); self
end

#eachObject

Yields each value



61
62
63
64
# File 'lib/rubyexcel/section.rb', line 61

def each
  return to_enum(:each) unless block_given?
  each_address { |addr| yield data[ addr ] }
end

#each_cellObject

Yields each cell



80
81
82
83
# File 'lib/rubyexcel/section.rb', line 80

def each_cell
  return to_enum( :each_cell ) unless block_given?
  each_address { |addr| yield Cell.new( sheet, addr ) }
end

#each_cell_without_headersObject Also known as: each_cell_wh

Yields each cell, skipping headers



89
90
91
92
# File 'lib/rubyexcel/section.rb', line 89

def each_cell_without_headers
  return to_enum( :each_cell_without_headers ) unless block_given?
  each_address( false ) { |addr| yield Cell.new( sheet, addr ) }
end

#each_without_headersObject Also known as: each_wh

Yields each value, skipping headers



70
71
72
73
# File 'lib/rubyexcel/section.rb', line 70

def each_without_headers
  return to_enum( :each_without_headers ) unless block_given?
  each_address( false ) { |addr| yield data[ addr ] }
end

#empty?Boolean

Check whether the data in self is empty

Returns:

  • (Boolean)


99
100
101
# File 'lib/rubyexcel/section.rb', line 99

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

#find {|Object| ... } ⇒ String?

Return the address of a given value

Yields:

  • (Object)

    yields each cell value to the block

Returns:

  • (String, nil)

    the address of the value or nil



110
111
112
113
# File 'lib/rubyexcel/section.rb', line 110

def find
  return to_enum( :find ) unless block_given?
  each_cell { |ce| return ce.address if yield ce.value }; nil
end

#inspectObject

View the object for debugging



119
120
121
# File 'lib/rubyexcel/section.rb', line 119

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

#last_cellRubyExcel::Cell

Return the last cell

Returns:



129
130
131
# File 'lib/rubyexcel/section.rb', line 129

def last_cell
  Cell.new( sheet, each_address.to_a.last )
end

#map!Object

Replaces each value with the result of the block



137
138
139
140
# File 'lib/rubyexcel/section.rb', line 137

def map!
  return to_enum( :map! ) unless block_given?
  each_address { |addr| data[addr] = ( yield data[addr] ) }
end

#map_without_headers!Object Also known as: map_wh!

Replaces each value with the result of the block, skipping headers



146
147
148
149
# File 'lib/rubyexcel/section.rb', line 146

def map_without_headers!
  return to_enum( :map_without_headers! ) unless block_given?
  each_address( false ) { |addr| data[addr] = ( yield data[addr] ) }
end

#read(id) ⇒ Object Also known as: []

Read a value by address

Parameters:

  • id (String, Fixnum)

    the index or reference of the required value



158
159
160
# File 'lib/rubyexcel/section.rb', line 158

def read( id )
  data[ translate_address( id ) ]
end

#summariseHash Also known as: summarize

Summarise the values of a Section into a Hash

Returns:

  • (Hash)


169
170
171
# File 'lib/rubyexcel/section.rb', line 169

def summarise
  each_wh.inject( Hash.new(0) ) { |h, v| h[v]+=1; h }
end

#to_sObject

The Section as a seperated value String



178
179
180
# File 'lib/rubyexcel/section.rb', line 178

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

Parameters:

  • id (String, Fixnum)

    the index or reference to write to

  • val (Object)

    the object to place at the address



189
190
191
192
# File 'lib/rubyexcel/section.rb', line 189

def write( id, val )
  
  data[ translate_address( id ) ] = val
end