Class: Xlsxrb::Elements::Row
- Inherits:
-
Data
- Object
- Data
- Xlsxrb::Elements::Row
- Defined in:
- lib/xlsxrb/elements/row.rb
Overview
Represents a single row in a worksheet. index is 0-based.
Instance Attribute Summary collapse
-
#cells ⇒ Object
readonly
Returns the value of attribute cells.
-
#custom_height ⇒ Object
readonly
Returns the value of attribute custom_height.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#hidden ⇒ Object
readonly
Returns the value of attribute hidden.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#outline_level ⇒ Object
readonly
Returns the value of attribute outline_level.
-
#unmapped_data ⇒ Object
readonly
Returns the value of attribute unmapped_data.
Class Method Summary collapse
Instance Method Summary collapse
-
#cell_at(column_index) ⇒ Object
Returns the cell at the given 0-based column index, or nil.
-
#initialize(index:, cells: [], height: nil, hidden: false, custom_height: false, outline_level: nil, unmapped_data: {}, errors: nil) ⇒ Row
constructor
A new instance of Row.
- #valid? ⇒ Boolean
-
#values ⇒ Object
Returns cell values as an Array (sparse columns get nil).
Constructor Details
#initialize(index:, cells: [], height: nil, hidden: false, custom_height: false, outline_level: nil, unmapped_data: {}, errors: nil) ⇒ Row
Returns a new instance of Row.
8 9 10 11 12 13 14 15 16 |
# File 'lib/xlsxrb/elements/row.rb', line 8 def initialize(index:, cells: [], height: nil, hidden: false, custom_height: false, outline_level: nil, unmapped_data: {}, errors: nil) computed_errors = errors || self.class.validate(index, cells) computed_errors = computed_errors.freeze unless computed_errors.frozen? cells = cells.freeze unless cells.frozen? super(index: index, cells: cells, height: height, hidden: hidden, custom_height: custom_height, outline_level: outline_level, unmapped_data: unmapped_data, errors: computed_errors) end |
Instance Attribute Details
#cells ⇒ Object (readonly)
Returns the value of attribute cells
7 8 9 |
# File 'lib/xlsxrb/elements/row.rb', line 7 def cells @cells end |
#custom_height ⇒ Object (readonly)
Returns the value of attribute custom_height
7 8 9 |
# File 'lib/xlsxrb/elements/row.rb', line 7 def custom_height @custom_height end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors
7 8 9 |
# File 'lib/xlsxrb/elements/row.rb', line 7 def errors @errors end |
#height ⇒ Object (readonly)
Returns the value of attribute height
7 8 9 |
# File 'lib/xlsxrb/elements/row.rb', line 7 def height @height end |
#hidden ⇒ Object (readonly)
Returns the value of attribute hidden
7 8 9 |
# File 'lib/xlsxrb/elements/row.rb', line 7 def hidden @hidden end |
#index ⇒ Object (readonly)
Returns the value of attribute index
7 8 9 |
# File 'lib/xlsxrb/elements/row.rb', line 7 def index @index end |
#outline_level ⇒ Object (readonly)
Returns the value of attribute outline_level
7 8 9 |
# File 'lib/xlsxrb/elements/row.rb', line 7 def outline_level @outline_level end |
#unmapped_data ⇒ Object (readonly)
Returns the value of attribute unmapped_data
7 8 9 |
# File 'lib/xlsxrb/elements/row.rb', line 7 def unmapped_data @unmapped_data end |
Class Method Details
.validate(index, cells) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/xlsxrb/elements/row.rb', line 37 def self.validate(index, cells) return [].freeze if index.is_a?(Integer) && index >= 0 && cells.is_a?(Array) errs = [] errs << "index must be a non-negative Integer (got #{index.inspect})" if !index.is_a?(Integer) || index.negative? errs << "cells must be an Array (got #{cells.class})" unless cells.is_a?(Array) errs end |
Instance Method Details
#cell_at(column_index) ⇒ Object
Returns the cell at the given 0-based column index, or nil.
23 24 25 |
# File 'lib/xlsxrb/elements/row.rb', line 23 def cell_at(column_index) cells.find { |c| c.column_index == column_index } end |
#valid? ⇒ Boolean
18 19 20 |
# File 'lib/xlsxrb/elements/row.rb', line 18 def valid? errors.empty? end |
#values ⇒ Object
Returns cell values as an Array (sparse columns get nil).
28 29 30 31 32 33 34 35 |
# File 'lib/xlsxrb/elements/row.rb', line 28 def values return [] if cells.empty? max_col = cells.max_by(&:column_index).column_index result = Array.new(max_col + 1) cells.each { |c| result[c.column_index] = c.value } result end |