Class: Xlsxrb::Elements::Row

Inherits:
Data
  • Object
show all
Defined in:
lib/xlsxrb/elements/row.rb

Overview

Represents a single row in a worksheet. index is 0-based.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#cellsObject (readonly)

Returns the value of attribute cells

Returns:

  • (Object)

    the current value of cells



7
8
9
# File 'lib/xlsxrb/elements/row.rb', line 7

def cells
  @cells
end

#custom_heightObject (readonly)

Returns the value of attribute custom_height

Returns:

  • (Object)

    the current value of custom_height



7
8
9
# File 'lib/xlsxrb/elements/row.rb', line 7

def custom_height
  @custom_height
end

#errorsObject (readonly)

Returns the value of attribute errors

Returns:

  • (Object)

    the current value of errors



7
8
9
# File 'lib/xlsxrb/elements/row.rb', line 7

def errors
  @errors
end

#heightObject (readonly)

Returns the value of attribute height

Returns:

  • (Object)

    the current value of height



7
8
9
# File 'lib/xlsxrb/elements/row.rb', line 7

def height
  @height
end

#hiddenObject (readonly)

Returns the value of attribute hidden

Returns:

  • (Object)

    the current value of hidden



7
8
9
# File 'lib/xlsxrb/elements/row.rb', line 7

def hidden
  @hidden
end

#indexObject (readonly)

Returns the value of attribute index

Returns:

  • (Object)

    the current value of index



7
8
9
# File 'lib/xlsxrb/elements/row.rb', line 7

def index
  @index
end

#outline_levelObject (readonly)

Returns the value of attribute outline_level

Returns:

  • (Object)

    the current value of outline_level



7
8
9
# File 'lib/xlsxrb/elements/row.rb', line 7

def outline_level
  @outline_level
end

#unmapped_dataObject (readonly)

Returns the value of attribute unmapped_data

Returns:

  • (Object)

    the current value of 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

Returns:

  • (Boolean)


18
19
20
# File 'lib/xlsxrb/elements/row.rb', line 18

def valid?
  errors.empty?
end

#valuesObject

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