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, #step_index, #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

#cell(ref) ⇒ Object

Access a cell by its index within the Section



33
34
35
# File 'lib/rubyexcel/section.rb', line 33

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

#deleteObject

Delete the data referenced by self



41
42
43
# File 'lib/rubyexcel/section.rb', line 41

def delete
  data.delete( self ); self
end

#eachObject

Yields each value



49
50
51
52
# File 'lib/rubyexcel/section.rb', line 49

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

#each_cellObject

Yields each cell



68
69
70
71
# 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_without_headersObject Also known as: each_cell_wh

Yields each cell, skipping headers



77
78
79
80
# 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

#each_without_headersObject Also known as: each_wh

Yields each value, skipping headers



58
59
60
61
# 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

#empty?Boolean

Check whether the data in self is empty

Returns:

  • (Boolean)


87
88
89
# File 'lib/rubyexcel/section.rb', line 87

def empty?
  each_wh.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



98
99
100
101
# 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

#inspectObject

View the object for debugging



107
108
109
# File 'lib/rubyexcel/section.rb', line 107

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

#lastObject

Return the value of the last cell



115
116
117
# File 'lib/rubyexcel/section.rb', line 115

def last
  last_cell.value
end

#last_cellRubyExcel::Cell

Return the last cell

Returns:



125
126
127
# File 'lib/rubyexcel/section.rb', line 125

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

#map!Object

Replaces each value with the result of the block



133
134
135
136
# 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_without_headers!Object Also known as: map_wh!

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



142
143
144
145
# 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

#read(start, slice = nil) ⇒ Object Also known as: []

Read a value by address

Parameters:

  • start (String, Fixnum, ::Range)

    an index or Range of indices.

  • slice (Fixnum) (defaults to: nil)

    if the first argument is an index, how many cells to read.



155
156
157
158
159
160
161
162
163
164
165
# 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

#summariseHash Also known as: summarize

Summarise the values of a Section into a Hash

Returns:

  • (Hash)


174
175
176
# File 'lib/rubyexcel/section.rb', line 174

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

#to_sObject

The Section as a seperated value String



183
184
185
# 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) ⇒ Object Also known as: []=

Write a value by address

Parameters:

  • args (Array<String, Fixnum, ::Range, Object>)

    the address to write the data to, and the data to write.



193
194
195
196
197
198
199
200
201
202
203
204
# 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