class RubyExcel::Section

Superclass for Row and Column

Attributes

data[R]

The Data underlying the Sheet

parent[R]

The Sheet parent of the Section

sheet[R]

The Sheet parent of the Section

Public Class Methods

new( sheet ) click to toggle source

Creates a RubyExcel::Section instance

@param [RubyExcel::Sheet] sheet the parent Sheet

# File lib/rubyexcel/section.rb, line 24
def initialize( sheet )
  @sheet = sheet
  @data = sheet.data
end

Public Instance Methods

[]( start, slice=nil )
Alias for: read
[]=( *args )
Alias for: write
cell( ref ) click to toggle source

Access a cell by its index within the Section

# File lib/rubyexcel/section.rb, line 33
def cell( ref )
  Cell.new( sheet, translate_address( ref ) )
end
delete() click to toggle source

Delete the data referenced by self

# File lib/rubyexcel/section.rb, line 41
def delete
  data.delete( self ); self
end
each() { |data| ... } click to toggle source

Yields each value

# File lib/rubyexcel/section.rb, line 49
def each
  return to_enum(:each) unless block_given?
  each_address { |addr| yield data[ addr ] }
end
each_cell() { |cell| ... } click to toggle source

Yields each cell

# File lib/rubyexcel/section.rb, line 68
def each_cell
  return to_enum( :each_cell ) unless block_given?
  each_address { |addr| yield Cell.new( sheet, addr ) }
end
each_cell_wh()
each_cell_without_headers() { |cell| ... } click to toggle source

Yields each cell, skipping headers

# File lib/rubyexcel/section.rb, line 77
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
Also aliased as: each_cell_wh
each_wh()
each_without_headers() { |data| ... } click to toggle source

Yields each value, skipping headers

# File lib/rubyexcel/section.rb, line 58
def each_without_headers
  return to_enum( :each_without_headers ) unless block_given?
  each_address( false ) { |addr| yield data[ addr ] }
end
Also aliased as: each_wh
empty?() click to toggle source

Check whether the data in self is empty

# File lib/rubyexcel/section.rb, line 87
def empty?
  each_wh.all? { |val| val.to_s.empty? }
end
find() { |value| ... } click to toggle source

Return the address of a given value

@yield [Object] yields each cell value to the block @return [String, nil] the address of the value or nil

# File lib/rubyexcel/section.rb, line 98
def find
  return to_enum( :find ) unless block_given?
  each_cell { |ce| return ce.address if yield ce.value }; nil
end
inspect() click to toggle source

View the object for debugging

# File lib/rubyexcel/section.rb, line 107
def inspect
  "#{ self.class }:0x#{ '%x' % (object_id << 1) }: #{ idx }"
end
last() click to toggle source

Return the value of the last cell

# File lib/rubyexcel/section.rb, line 115
def last
  last_cell.value
end
last_cell() click to toggle source

Return the last cell

@return [RubyExcel::Cell]

# File lib/rubyexcel/section.rb, line 125
def last_cell
  Cell.new( sheet, each_address.to_a.last )
end
map!() { |data )| ... } click to toggle source

Replaces each value with the result of the block

# File lib/rubyexcel/section.rb, line 133
def map!
  return to_enum( :map! ) unless block_given?
  each_address { |addr| data[addr] = ( yield data[addr] ) }
end
map_wh!()
map_without_headers!() { |data )| ... } click to toggle source

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

# File lib/rubyexcel/section.rb, line 142
def map_without_headers!
  return to_enum( :map_without_headers! ) unless block_given?
  each_address( false ) { |addr| data[addr] = ( yield data[addr] ) }
end
Also aliased as: map_wh!
read( start, slice=nil ) click to toggle source

Read a value by address

@param [String, Fixnum, ::Range] start an index or Range of indices. @param [Fixnum] slice if the first argument is an index, how many cells to read.

# File lib/rubyexcel/section.rb, line 155
def read( start, slice=nil )
  if slice
    ( start..( step_index( start, slice ) ) ).map { |n| data[ translate_address( n ) ] }
  else
    if start.is_a?( ::Range ) # Standard Ruby Range
      start.map { |n| data[ translate_address( n ) ] }
    else # Single value
      data[ translate_address( start ) ]
    end
  end
end
Also aliased as: []
summarise() click to toggle source

Summarise the values of a Section into a Hash

@return [Hash]

# File lib/rubyexcel/section.rb, line 174
def summarise
  each_wh.inject( Hash.new(0) ) { |h, v| h[v]+=1; h }
end
Also aliased as: summarize
summarize()
Alias for: summarise
to_s() click to toggle source

The Section as a seperated value String

# File lib/rubyexcel/section.rb, line 183
def to_s
  to_a.map { |v| v.to_s.gsub(/\t|\n|\r/,' ') }.join ( self.is_a?( Row ) ? "\t" : "\n" )
end
write( *args ) click to toggle source

Write a value by address

@param [Array<String, Fixnum, ::Range, Object>] args the address to write the data to, and the data to write.

# File lib/rubyexcel/section.rb, line 193
def write( *args )
  val = args.pop
  if args.length == 1
    if args[0].is_a?( ::Range ) # Standard Ruby Range
      sheet.range( to_range_address( translate_address( args[0].first ), translate_address( args[0].last ) ) ).value = val
    else # Single value
      data[ translate_address( args[0] ) ] = val
    end
  else # Slice
    sheet.range( to_range_address( translate_address( args[0] ), translate_address( step_index( args[0], args[1] ) ) ) ).value = val
  end
end
Also aliased as: []=