Class: Rspreadsheet::Row

Inherits:
XMLTiedItem
  • Object
show all
Defined in:
lib/rspreadsheet/row.rb

Overview

Represents a row in a spreadsheet which has coordinates, contains value, formula and can be formated. You can get this object like this (suppose that @worksheet contains Worksheet object)

@row = @worksheet.rows(5)

Mostly you will this object to access row cells values

@row[2]           # identical to @worksheet[5,2] or @row.cells(2).value

or directly row Cell objects

@row.cells(2)     # => identical to @worksheet.rows(5).cells(2)

You can use it to manipulate rows

@row.add_row_above   # adds empty row above
@row.delete          # deletes row 

and shifts all other rows down/up appropriatelly.

Instance Attribute Summary collapse

Syntactic sugar collapse

Other methods collapse

Instance Method Summary collapse

Constructor Details

#initialize(aworksheet, arowi) ⇒ Row

Returns a new instance of Row.



36
37
38
39
40
# File 'lib/rspreadsheet/row.rb', line 36

def initialize(aworksheet,arowi)
  @worksheet = aworksheet
  @rowi = arowi
  @itemcache = Hash.new  #TODO: move to module XMLTiedArray
end

Instance Attribute Details

#rowiInteger (readonly)

Returns row index of the row.

Returns:

  • (Integer)

    row index of the row



34
35
36
# File 'lib/rspreadsheet/row.rb', line 34

def rowi
  @rowi
end

#worksheetWorksheet (readonly)

Returns worksheet which contains the row.

Returns:

  • (Worksheet)

    worksheet which contains the row



31
32
33
# File 'lib/rspreadsheet/row.rb', line 31

def worksheet
  @worksheet
end

Instance Method Details

#[](coli) ⇒ String or Float or Date

returns value of the cell at column coli.

@row = @worksheet.rows(5)     # with cells containing names of months
@row[1]                       # => "January" 
@row.cells(2).value           # => "February"  
@row[1].class                 # => String

Parameters:

  • coli (Integer)

    colum index of the cell

Returns:

  • (String or Float or Date)

    value of the cell



55
# File 'lib/rspreadsheet/row.rb', line 55

def [](coli); cells(coli).value end

#[]=(coli, avalue) ⇒ Object

sets value of cell in column coli

Parameters:



58
# File 'lib/rspreadsheet/row.rb', line 58

def []=(coli,avalue); cells(coli).value=avalue end

#add_row_aboveObject

Inserts row above itself (and shifts itself and all following rows down)



98
99
100
# File 'lib/rspreadsheet/row.rb', line 98

def add_row_above
  parent.add_row_above(rowi)
end

#cells(*params) ⇒ Object



45
# File 'lib/rspreadsheet/row.rb', line 45

def cells(*params); subitems(*params) end

#cellvaluesArray

return array of cell values

@worksheet[3,3] = "text"
@worksheet[3,1] = 123
@worksheet.rows(3).cellvalues            # => [123, nil, "text"]

Returns:



75
76
77
# File 'lib/rspreadsheet/row.rb', line 75

def cellvalues
  cells.collect{|c| c.value}
end

#cellvalues=(avalue) ⇒ Object

sets values of cells of row to values from avalue. Attention: it deletes the rest of row

@row = @worksheet.rows(5)     
@row.values = ["January", "Feb", nil, 4] # =>  | January | Feb |  | 4 |
@row[2] = "foo")                         # =>  | January | foo |  | 4 |

Parameters:

  • avalue (Array)

    array with values



65
66
67
68
# File 'lib/rspreadsheet/row.rb', line 65

def cellvalues=(avalue)
  self.truncate
  avalue.each_with_index{ |val,i| self[i+1] = val }
end

#nonemptycellsObject



85
86
87
# File 'lib/rspreadsheet/row.rb', line 85

def nonemptycells
  nonemptycellsindexes.collect{ |index| subitem(index) }
end

#nonemptycellsindexesObject



88
89
90
91
92
93
94
95
# File 'lib/rspreadsheet/row.rb', line 88

def nonemptycellsindexes
  myxmlnode = xmlnode
  if myxmlnode.nil?
    []
  else
    @worksheet.find_nonempty_subnode_indexes(myxmlnode, {:xml_items_node_name => 'table-cell', :xml_repeated_attribute => 'number-columns-repeated'})
  end
end

#style_name=(value) ⇒ Object



81
82
83
84
# File 'lib/rspreadsheet/row.rb', line 81

def style_name=(value); 
  detach_if_needed
  Tools.set_ns_attribute(xmlnode,'table','style-name',value)
end

#xmlnodeObject



42
# File 'lib/rspreadsheet/row.rb', line 42

def xmlnode; parent.find_my_subnode_respect_repeated(index, xml_options)  end