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.row(5)

Mostly you will this object to access values of cells in the row

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

or directly row Cell objects

@row.cell(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



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

def initialize(aworksheet,arowi)
  initialize_xml_tied_array
  initialize_xml_tied_item(aworksheet,arowi)
end

Instance Attribute Details

#rowiInteger (readonly)



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

def rowi; index end

#worksheetWorksheet (readonly)



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

def worksheet; parent 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


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

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

#[]=(coli, avalue) ⇒ Object

sets value of cell in column coli



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

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

#add_row_aboveObject

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



101
102
103
# File 'lib/rspreadsheet/row.rb', line 101

def add_row_above
  parent.add_row_above(rowi)
end

#cells(*params) ⇒ Object Also known as: cell



41
42
43
44
45
46
47
# File 'lib/rspreadsheet/row.rb', line 41

def cells(*params)
  if params.length == 1
    subitems(Tools.convert_column_name_to_index(params[0]))
  else
    subitems(*params)
  end
end

#cellvaluesArray

return array of cell values

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


78
79
80
# File 'lib/rspreadsheet/row.rb', line 78

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 |


68
69
70
71
# File 'lib/rspreadsheet/row.rb', line 68

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

#next_rowObject Also known as: next



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

def next_row; relative(+1) end

#nonemptycellsObject



88
89
90
# File 'lib/rspreadsheet/row.rb', line 88

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

#nonemptycellsindexesObject



91
92
93
94
95
96
97
98
# File 'lib/rspreadsheet/row.rb', line 91

def nonemptycellsindexes
  myxmlnode = xmlnode
  if myxmlnode.nil?
    []
  else
    worksheet.find_nonempty_subnode_indexes(myxmlnode, subitem_xml_options)
  end
end

#relative(rowi_offset) ⇒ Object



108
109
110
# File 'lib/rspreadsheet/row.rb', line 108

def relative(rowi_offset)
  worksheet.row(self.rowi+rowi_offset)
end

#style_name=(value) ⇒ Object



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

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