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:

Constant Summary collapse

SERIALIZABLE_ATTRIBUTES =
Note:

height(ht) and customHeight are manages separately for now. Have a look at Row#height

A list of serilizable attributes.

[:hidden, :outlineLevel, :collapsed, :s, :customFormat, :ph]

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


75
76
77
78
79
80
81
82
# File 'lib/axlsx/workbook/worksheet/row.rb', line 75

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)


24
25
26
# File 'lib/axlsx/workbook/worksheet/row.rb', line 24

def cells
  @cells
end

#collapsedBoolean

Flag indicating if the outlining of row.

Returns:

  • (Boolean)


32
33
34
# File 'lib/axlsx/workbook/worksheet/row.rb', line 32

def collapsed
  @collapsed
end

#customFormatBoolean (readonly)

indicates that a style has been applied directly to the row via Row#s

Returns:

  • (Boolean)


48
49
50
# File 'lib/axlsx/workbook/worksheet/row.rb', line 48

def customFormat
  @customFormat
end

#heightFloat

Row height measured in point size. There is no margin padding on row height.

Returns:

  • (Float)


28
29
30
# File 'lib/axlsx/workbook/worksheet/row.rb', line 28

def height
  @height
end

#hiddenBoolean

Flag indicating if the the row is hidden.

Returns:

  • (Boolean)


36
37
38
# File 'lib/axlsx/workbook/worksheet/row.rb', line 36

def hidden
  @hidden
end

#outlineLevelInteger

Outlining level of the row, when outlining is on

Returns:

  • (Integer)


40
41
42
# File 'lib/axlsx/workbook/worksheet/row.rb', line 40

def outlineLevel
  @outlineLevel
end

#phBoolean

indicates if the row should show phonetic

Returns:

  • (Boolean)


52
53
54
# File 'lib/axlsx/workbook/worksheet/row.rb', line 52

def ph
  @ph
end

#sInteger

The style applied ot the row. This affects the entire row.

Returns:

  • (Integer)


44
45
46
# File 'lib/axlsx/workbook/worksheet/row.rb', line 44

def s
  @s
end

#worksheetWorksheet

The worksheet this row belongs to

Returns:



20
21
22
# File 'lib/axlsx/workbook/worksheet/row.rb', line 20

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:



135
136
137
138
139
# File 'lib/axlsx/workbook/worksheet/row.rb', line 135

def add_cell(value="", options={})
  c = Cell.new(self, value, options)
  worksheet.send(:update_column_info, self.cells, [])
  c
end

#custom_height?Boolean

true if the row height has been manually set

Returns:

  • (Boolean)

See Also:



162
163
164
# File 'lib/axlsx/workbook/worksheet/row.rb', line 162

def custom_height?
  @height != nil
end

#indexInteger

The index of this row in the worksheet

Returns:

  • (Integer)


110
111
112
# File 'lib/axlsx/workbook/worksheet/row.rb', line 110

def index
  worksheet.rows.index(self)
end

#style=(style) ⇒ Object

sets the style for every cell in this row



142
143
144
145
146
147
# File 'lib/axlsx/workbook/worksheet/row.rb', line 142

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)


152
153
154
# File 'lib/axlsx/workbook/worksheet/row.rb', line 152

def to_ary
  @cells.to_ary
end

#to_xml_string(r_index, str = '') ⇒ String

Serializes the row

Parameters:

  • r_index (Integer)

    The row index, 0 based.

  • str (String) (defaults to: '')

    The string this rows xml will be appended to.

Returns:

  • (String)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/axlsx/workbook/worksheet/row.rb', line 118

def to_xml_string(r_index, str = '')
  str << '<row r="' << (r_index + 1 ).to_s << '" '
  instance_values.select { |key, value| SERIALIZABLE_ATTRIBUTES.include? key.to_sym }.each do |key, value|
    str << key << '="' << value.to_s << '" '
  end
  if custom_height?
    str << 'customHeight="1" ht="' << height.to_s << '">'
  else
    str << '>'
  end
  @cells.each_with_index { |cell, c_index| cell.to_xml_string(r_index, c_index, str) }
  str << '</row>'
  str
end