Class: RubyExcel::Element

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

Overview

A Range or Cell in a Sheet

Direct Known Subclasses

Cell, Range

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, addr) ⇒ Element

Creates a RubyExcel::Element instance

Parameters:

  • sheet (RubyExcel::Sheet)

    the parent Sheet

  • addr (String)

    the address to reference



34
35
36
37
38
39
40
# File 'lib/rubyexcel/element.rb', line 34

def initialize( sheet, addr )
  @sheet = sheet
  @data = sheet.data
  @address = addr
  @column = column_id( addr )
  @row = row_id( addr )
end

Instance Attribute Details

#addressObject (readonly)

The address



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

def address
  @address
end

#columnObject (readonly)

The first Column id in the address



22
23
24
# File 'lib/rubyexcel/element.rb', line 22

def column
  @column
end

#dataObject (readonly)

The Data underlying the Sheet



19
20
21
# File 'lib/rubyexcel/element.rb', line 19

def data
  @data
end

#rowObject (readonly)

The first Row id in the address



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

def row
  @row
end

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

#deleteObject

Delete the data referenced by self.address



46
47
48
# File 'lib/rubyexcel/element.rb', line 46

def delete
  data.delete( self ); self
end

#eachObject

Yields each value in the data referenced by the address



54
55
56
57
# File 'lib/rubyexcel/element.rb', line 54

def each
  return to_enum( :each ) unless block_given?
  expand( address ).flatten.each { |addr| yield data[ addr ] }
end

#each_cellObject

Yields each Element referenced by the address



63
64
65
66
# File 'lib/rubyexcel/element.rb', line 63

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

#empty?Boolean

Checks whether the data referenced by the address is empty

Returns:

  • (Boolean)


72
73
74
# File 'lib/rubyexcel/element.rb', line 72

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

#first_cellRubyExcel::Cell

Return the first cell in the Range

Returns:



82
83
84
# File 'lib/rubyexcel/element.rb', line 82

def first_cell
  Cell.new( sheet, expand( address ).flatten.first )
end

#inspectObject

View the object for debugging



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

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

#last_cellRubyExcel::Cell

Return the last cell in the Range

Returns:



100
101
102
# File 'lib/rubyexcel/element.rb', line 100

def last_cell
  Cell.new( sheet, expand( address ).flatten.last )
end

#map!Object

Replaces each value with the result of the block



108
109
110
111
# File 'lib/rubyexcel/element.rb', line 108

def map!
  return to_enum( :map! ) unless block_given?
  expand( address ).flatten.each { |addr| data[ addr ] = yield data[ addr ] }
end