Class: Xlsxrb::Elements::Row
- Inherits:
-
Data
- Object
- Data
- Xlsxrb::Elements::Row
- Includes:
- Enumerable
- 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
- #[](col_index) ⇒ Object
-
#cell_at(column_index) ⇒ Object
Returns the cell at the given 0-based column index, or nil.
- #each ⇒ Object
- #each_cell ⇒ Object
-
#initialize(index:, cells: [], height: nil, hidden: false, custom_height: false, outline_level: nil, unmapped_data: {}, errors: nil) ⇒ Row
constructor
A new instance of Row.
- #to_a ⇒ Object
- #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.
12 13 14 15 16 17 18 19 20 |
# File 'lib/xlsxrb/elements/row.rb', line 12 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
9 10 11 |
# File 'lib/xlsxrb/elements/row.rb', line 9 def cells @cells end |
#custom_height ⇒ Object (readonly)
Returns the value of attribute custom_height
9 10 11 |
# File 'lib/xlsxrb/elements/row.rb', line 9 def custom_height @custom_height end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors
9 10 11 |
# File 'lib/xlsxrb/elements/row.rb', line 9 def errors @errors end |
#height ⇒ Object (readonly)
Returns the value of attribute height
9 10 11 |
# File 'lib/xlsxrb/elements/row.rb', line 9 def height @height end |
#hidden ⇒ Object (readonly)
Returns the value of attribute hidden
9 10 11 |
# File 'lib/xlsxrb/elements/row.rb', line 9 def hidden @hidden end |
#index ⇒ Object (readonly)
Returns the value of attribute index
9 10 11 |
# File 'lib/xlsxrb/elements/row.rb', line 9 def index @index end |
#outline_level ⇒ Object (readonly)
Returns the value of attribute outline_level
9 10 11 |
# File 'lib/xlsxrb/elements/row.rb', line 9 def outline_level @outline_level end |
#unmapped_data ⇒ Object (readonly)
Returns the value of attribute unmapped_data
9 10 11 |
# File 'lib/xlsxrb/elements/row.rb', line 9 def unmapped_data @unmapped_data end |
Class Method Details
.validate(index, cells) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/xlsxrb/elements/row.rb', line 68 def self.validate(index, cells) return [].freeze if index.is_a?(Integer) && index >= 0 && index < 1_048_576 && cells.is_a?(Array) errs = [] if !index.is_a?(Integer) || index.negative? errs << "index must be a non-negative Integer (got #{index.inspect})" elsif index >= 1_048_576 errs << "index must be < 1048576 (got #{index}, max row is 1048576)" end errs << "cells must be an Array (got #{cells.class})" unless cells.is_a?(Array) errs end |
Instance Method Details
#[](col_index) ⇒ Object
22 23 24 |
# File 'lib/xlsxrb/elements/row.rb', line 22 def [](col_index) cells[col_index] end |
#cell_at(column_index) ⇒ Object
Returns the cell at the given 0-based column index, or nil.
54 55 56 |
# File 'lib/xlsxrb/elements/row.rb', line 54 def cell_at(column_index) cells.find { |c| c.column_index == column_index } end |
#each ⇒ Object
26 27 28 29 30 |
# File 'lib/xlsxrb/elements/row.rb', line 26 def each(&) return to_enum(:each) unless block_given? cells.each(&) end |
#each_cell ⇒ Object
32 33 34 35 36 |
# File 'lib/xlsxrb/elements/row.rb', line 32 def each_cell(&) return to_enum(:each_cell) unless block_given? cells.each(&) end |
#to_a ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/xlsxrb/elements/row.rb', line 38 def to_a return [] if cells.empty? max_col = cells.map(&:column_index).max arr = Array.new(max_col + 1) cells.each do |cell| arr[cell.column_index] = cell.value end arr end |
#valid? ⇒ Boolean
49 50 51 |
# File 'lib/xlsxrb/elements/row.rb', line 49 def valid? errors.empty? end |
#values ⇒ Object
Returns cell values as an Array (sparse columns get nil).
59 60 61 62 63 64 65 66 |
# File 'lib/xlsxrb/elements/row.rb', line 59 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 |