Class: Axlsx::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/axlsx/workbook/worksheet/row.rb

Overview

Note:

The recommended way to manage rows and cells is to use Worksheet#add_row

A Row is a single row in a worksheet.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worksheet, values = [], options = {}) ⇒ Row

Creates a new row. New Cell objects are created based on the values, types and style options. A new cell is created for each item in the values array. style and types options are applied as follows:

If the types option is defined and is a symbol it is applied to all the cells created.
If the types option is an array, cell types are applied by index for each cell
If the types option is not set, the cell will automatically determine its type.
If the style option is defined and is an Integer, it is applied to all cells created.
If the style option is an array, style is applied by index for each cell.
If the style option is not defined, the default style (0) is applied to each cell.

Parameters:

  • worksheet (Worksheet)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • values (Array)
  • types (Array, Symbol)
  • style (Array, Integer)
  • height (Float)

    the row’s height (in points)

See Also:

  • #array_to_cells
  • Cell


47
48
49
50
51
52
53
54
# File 'lib/axlsx/workbook/worksheet/row.rb', line 47

def initialize(worksheet, values=[], options={})
  @height = nil
  self.worksheet = worksheet
  @cells = SimpleTypedList.new Cell
  @worksheet.rows << self
  self.height = options.delete(:height) if options[:height]
  array_to_cells(values, options)
end

Instance Attribute Details

#cellsSimpleTypedList (readonly)

The cells this row holds

Returns:

  • (SimpleTypedList)


14
15
16
# File 'lib/axlsx/workbook/worksheet/row.rb', line 14

def cells
  @cells
end

#heightFloat

The height of this row in points, if set explicitly.

Returns:

  • (Float)


18
19
20
# File 'lib/axlsx/workbook/worksheet/row.rb', line 18

def height
  @height
end

#worksheetWorksheet

The worksheet this row belongs to

Returns:



10
11
12
# File 'lib/axlsx/workbook/worksheet/row.rb', line 10

def worksheet
  @worksheet
end

Instance Method Details

#add_cell(value = "", options = {}) ⇒ Cell

Adds a singel sell to the row based on the data provided and updates the worksheet’s autofit data.

Returns:



73
74
75
76
77
# File 'lib/axlsx/workbook/worksheet/row.rb', line 73

def add_cell(value="", options={})
  c = Cell.new(self, value, options)
  update_auto_fit_data
  c
end

#custom_height?Boolean

true if the row height has been manually set

Returns:

  • (Boolean)

See Also:



100
101
102
# File 'lib/axlsx/workbook/worksheet/row.rb', line 100

def custom_height?
  @height != nil
end

#indexInteger

The index of this row in the worksheet

Returns:

  • (Integer)


58
59
60
# File 'lib/axlsx/workbook/worksheet/row.rb', line 58

def index
  worksheet.rows.index(self)
end

#style=(style) ⇒ Object

sets the style for every cell in this row



80
81
82
83
84
85
# File 'lib/axlsx/workbook/worksheet/row.rb', line 80

def style=(style)
  cells.each_with_index do | cell, index |
    s = style.is_a?(Array) ? style[index] : style
    cell.style = s
  end
end

#to_aryArray

returns the cells in this row as an array This lets us transpose the rows into columns

Returns:

  • (Array)


90
91
92
# File 'lib/axlsx/workbook/worksheet/row.rb', line 90

def to_ary
  @cells.to_ary
end

#to_xml(xml) ⇒ String

Serializes the row

Parameters:

  • xml (Nokogiri::XML::Builder)

    The document builder instance this objects xml will be added to.

Returns:

  • (String)


65
66
67
68
69
# File 'lib/axlsx/workbook/worksheet/row.rb', line 65

def to_xml(xml)
  attrs = {:r => index+1}
  attrs.merge!(:customHeight => 1, :ht => height) if custom_height?
  xml.row(attrs) { |ixml| @cells.each { |cell| cell.to_xml(ixml) } }
end