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.

XMLTiedArray_WithRepeatableItems related methods collapse

XMLTiedArray_WithRepeatableItems related methods collapse

Syntactic sugar collapse

Other methods collapse

Constructor Details

#initialize(aworksheet, arowi) ⇒ Row

Returns a new instance of Row.



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

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

Instance Attribute Details

#rowiInteger (readonly)

Returns row index of the row.

Returns:

  • (Integer)

    row index of the row



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

def rowi; index end

#worksheetWorksheet (readonly)

Returns worksheet which contains the row.

Returns:

  • (Worksheet)

    worksheet which contains the row



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

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

Parameters:

  • coli (Integer ot String)

    colum index of the cell of colum letter

Returns:

  • (String or Float or Date)

    value of the cell



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

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

#[]=(coli, avalue) ⇒ Object

sets value of cell in column coli

Parameters:

  • avalue (Array)


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

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

#add_row_aboveObject Also known as: insert_row_above

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



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

def add_row_above
  parent.add_row_above(rowi)
end

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



49
50
51
52
53
54
55
# File 'lib/rspreadsheet/row.rb', line 49

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"]

Returns:

  • (Array)

    Array



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

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



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

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

#clone_above_row(target_rowi) ⇒ Object



121
122
123
# File 'lib/rspreadsheet/row.rb', line 121

def clone_above_row(target_rowi)
  parent.clone_item_before(rowi, target_rowi)
end

#next_rowObject Also known as: next



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

def next_row; relative(+1) end

#nonemptycellsObject



96
97
98
# File 'lib/rspreadsheet/row.rb', line 96

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

#nonemptycellsindexesObject



99
100
101
102
103
104
105
106
# File 'lib/rspreadsheet/row.rb', line 99

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

#prepare_subitem(coli) ⇒ Object



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

def prepare_subitem(coli); Cell.new(worksheet,rowi,coli) end

#relative(rowi_offset) ⇒ Object



117
118
119
# File 'lib/rspreadsheet/row.rb', line 117

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

#style_name=(value) ⇒ Object



92
93
94
95
# File 'lib/rspreadsheet/row.rb', line 92

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

#subnode_optionsObject



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

def subnode_options; {
  :node_name => 'table-cell', 
  :alt_node_names => ['covered-table-cell'], 
  :repeated_attribute => 'number-columns-repeated'
} end