lib/axlsx/workbook/worksheet/row.rb in axlsx-2.0.1 vs lib/axlsx/workbook/worksheet/row.rb in axlsx-2.1.0.pre
- old
+ new
@@ -1,14 +1,14 @@
# encoding: UTF-8
module Axlsx
# A Row is a single row in a worksheet.
# @note The recommended way to manage rows and cells is to use Worksheet#add_row
# @see Worksheet#add_row
- class Row
-
+ class Row < SimpleTypedList
include SerializedAttributes
include Accessors
+
# No support is provided for the following attributes
# spans
# thickTop
# thickBottom
@@ -26,15 +26,14 @@
# @option options [Array, Integer] style
# @option options [Float] height the row's height (in points)
# @see Row#array_to_cells
# @see Cell
def initialize(worksheet, values=[], options={})
- @ht = nil
self.worksheet = worksheet
- @cells = SimpleTypedList.new Cell
- @worksheet.rows << self
- self.height = options.delete(:height) if options[:height]
+ super(Cell, nil, values.size)
+ self.height = options.delete(:height)
+ worksheet.rows << self
array_to_cells(values, options)
end
# A list of serializable attributes.
serializable_attributes :hidden, :outline_level, :collapsed, :custom_format, :s, :ph, :custom_height, :ht
@@ -44,18 +43,14 @@
# The worksheet this row belongs to
# @return [Worksheet]
attr_reader :worksheet
- # The cells this row holds
- # @return [SimpleTypedList]
- attr_reader :cells
-
# Row height measured in point size. There is no margin padding on row height.
# @return [Float]
def height
- @ht
+ defined?(@ht) ? @ht : nil
end
# Outlining level of the row, when outlining is on
# @return [Integer]
attr_reader :outline_level
@@ -75,67 +70,66 @@
# @see Row#outline
def outline_level=(v)
Axlsx.validate_unsigned_numeric(v)
@outline_level = v
end
+
alias :outlineLevel= :outline_level=
# The index of this row in the worksheet
# @return [Integer]
- def index
+ def row_index
worksheet.rows.index(self)
end
# Serializes the row
# @param [Integer] r_index The row index, 0 based.
# @param [String] str The string this rows xml will be appended to.
# @return [String]
def to_xml_string(r_index, str = '')
- str << '<row '
- serialized_attributes(str, { :r => r_index + 1 })
- str << '>'
- @cells.each_with_index { |cell, c_index| cell.to_xml_string(r_index, c_index, str) }
- str << '</row>'
+ serialized_tag('row', str, :r => r_index + 1) do
+ tmp = '' # time / memory tradeoff, lots of calls to rubyzip costs more
+ # time..
+ each_with_index { |cell, c_index| cell.to_xml_string(r_index, c_index, tmp) }
+ str << tmp
+ end
end
- # Adds a single sell to the row based on the data provided and updates the worksheet's autofit data.
+ # Adds a single cell to the row based on the data provided and updates the worksheet's autofit data.
# @return [Cell]
- def add_cell(value="", options={})
+ def add_cell(value = '', options = {})
c = Cell.new(self, value, options)
- worksheet.send(:update_column_info, self.cells, [])
+ self << c
+ worksheet.send(:update_column_info, self, [])
c
end
# sets the style for every cell in this row
def style=(style)
- cells.each_with_index do | cell, index |
- s = style.is_a?(Array) ? style[index] : style
- cell.style = s
+ each_with_index do | cell, index |
+ cell.style = style.is_a?(Array) ? style[index] : style
end
end
- # returns the cells in this row as an array
- # This lets us transpose the rows into columns
- # @return [Array]
- def to_ary
- @cells.to_ary
- end
-
# @see height
def height=(v)
- Axlsx::validate_unsigned_numeric(v)
unless v.nil?
- @ht = v
+ Axlsx::validate_unsigned_numeric(v)
@custom_height = true
+ @ht = v
end
- @ht
end
+
+ # return cells
+ def cells
+ self
+ end
private
# assigns the owning worksheet for this row
- def worksheet=(v) DataTypeValidator.validate "Row.worksheet", Worksheet, v; @worksheet=v; end
+ def worksheet=(v) DataTypeValidator.validate :row_worksheet, Worksheet, v; @worksheet=v; end
# Converts values, types, and style options into cells and associates them with this row.
# A new cell is created for each item in the values array.
# If value option is defined and is a symbol it is applied to all the cells created.
# If the value option is an array, cell types are applied by index for each cell
@@ -143,29 +137,17 @@
# If the style option is an array, style is applied by index for each cell.
# @option options [Array] values
# @option options [Array, Symbol] types
# @option options [Array, Integer] style
def array_to_cells(values, options={})
- values = values
- DataTypeValidator.validate 'Row.array_to_cells', Array, values
+ DataTypeValidator.validate :array_to_cells, Array, values
types, style, formula_values = options.delete(:types), options.delete(:style), options.delete(:formula_values)
values.each_with_index do |value, index|
+ options[:style] = style.is_a?(Array) ? style[index] : style if style
+ options[:type] = types.is_a?(Array) ? types[index] : types if types
+ options[:formula_value] = formula_values[index] if formula_values.is_a?(Array)
- #WTF IS THIS PAP?
- cell_style = style.is_a?(Array) ? style[index] : style
- options[:style] = cell_style if cell_style
-
- cell_type = types.is_a?(Array)? types[index] : types
- options[:type] = cell_type if cell_type
-
- formula_value = formula_values[index] if formula_values.is_a?(Array)
- options[:formula_value] = formula_value if formula_value
-
- Cell.new(self, value, options)
-
- options.delete(:style)
- options.delete(:type)
- options.delete(:formula_value)
+ self[index] = Cell.new(self, value, options)
end
end
end
end