Class: Xlsxrb::Elements::Row

Inherits:
Data
  • Object
show all
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

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.



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

#cellsObject (readonly)

Returns the value of attribute cells

Returns:

  • (Object)

    the current value of cells



9
10
11
# File 'lib/xlsxrb/elements/row.rb', line 9

def cells
  @cells
end

#custom_heightObject (readonly)

Returns the value of attribute custom_height

Returns:

  • (Object)

    the current value of custom_height



9
10
11
# File 'lib/xlsxrb/elements/row.rb', line 9

def custom_height
  @custom_height
end

#errorsObject (readonly)

Returns the value of attribute errors

Returns:

  • (Object)

    the current value of errors



9
10
11
# File 'lib/xlsxrb/elements/row.rb', line 9

def errors
  @errors
end

#heightObject (readonly)

Returns the value of attribute height

Returns:

  • (Object)

    the current value of height



9
10
11
# File 'lib/xlsxrb/elements/row.rb', line 9

def height
  @height
end

#hiddenObject (readonly)

Returns the value of attribute hidden

Returns:

  • (Object)

    the current value of hidden



9
10
11
# File 'lib/xlsxrb/elements/row.rb', line 9

def hidden
  @hidden
end

#indexObject (readonly)

Returns the value of attribute index

Returns:

  • (Object)

    the current value of index



9
10
11
# File 'lib/xlsxrb/elements/row.rb', line 9

def index
  @index
end

#outline_levelObject (readonly)

Returns the value of attribute outline_level

Returns:

  • (Object)

    the current value of outline_level



9
10
11
# File 'lib/xlsxrb/elements/row.rb', line 9

def outline_level
  @outline_level
end

#unmapped_dataObject (readonly)

Returns the value of attribute unmapped_data

Returns:

  • (Object)

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

#eachObject



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_cellObject



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_aObject



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

Returns:

  • (Boolean)


49
50
51
# File 'lib/xlsxrb/elements/row.rb', line 49

def valid?
  errors.empty?
end

#valuesObject

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