Class: Xlsxrb::StreamRow

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsxrb/stream_row.rb,
sig/generated/xlsxrb/stream_row.rbs

Overview

Streaming row implementation that parses cells on-demand / lazily. Provides O(1) memory consumption even for rows with tens of thousands of columns.

Examples:

Streaming cells one-by-one (O(1) memory)

row.each_cell do |cell|
  puts "#{cell.ref}: #{cell.value}"
end

Random access or array conversion (cached on-demand)

cell = row[0]
values = row.to_a

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index:, xml_bytes:, from:, to:, shared_strings:, height: nil, hidden: false, custom_height: false, outline_level: nil) ⇒ StreamRow

: (index: Integer, xml_bytes: String, from: Integer, to: Integer, shared_strings: Array, ?height: Float | Integer | nil, ?hidden: bool, ?custom_height: bool, ?outline_level: Integer | nil) -> void

Parameters:

  • index (Integer)

    0-based row index.

  • xml_bytes (String)

    Raw ASCII-8BIT XML bytes.

  • from (Integer)

    Byte offset where cells start.

  • to (Integer)

    Byte offset where cells end.

  • shared_strings (Array<String>)

    Shared strings table.

  • height (Float, Integer, nil) (defaults to: nil)

    Row height in points.

  • hidden (Boolean) (defaults to: false)

    Whether the row is hidden.

  • custom_height (Boolean) (defaults to: false)

    Whether custom height is set.

  • outline_level (Integer, nil) (defaults to: nil)

    Grouping/outline level.

  • index: (Integer)
  • xml_bytes: (String)
  • from: (Integer)
  • to: (Integer)
  • shared_strings: (Array[String])
  • height: (Float, Integer, nil) (defaults to: nil)
  • hidden: (Boolean) (defaults to: false)
  • custom_height: (Boolean) (defaults to: false)
  • outline_level: (Integer, nil) (defaults to: nil)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/xlsxrb/stream_row.rb', line 34

def initialize(index:, xml_bytes:, from:, to:, shared_strings:, height: nil, hidden: false,
               custom_height: false, outline_level: nil)
  @index = index
  @xml = xml_bytes
  @from = from
  @to = to
  @shared_strings = shared_strings
  @height = height
  @hidden = hidden
  @custom_height = custom_height
  @outline_level = outline_level
  @cells_cache = nil
end

Instance Attribute Details

#custom_heightObject (readonly)

Returns:

  • (Object)


22
23
24
# File 'lib/xlsxrb/stream_row.rb', line 22

def custom_height
  @custom_height
end

#heightObject (readonly)

Returns:

  • (Object)


22
23
24
# File 'lib/xlsxrb/stream_row.rb', line 22

def height
  @height
end

#hiddenObject (readonly)

Returns:

  • (Object)


22
23
24
# File 'lib/xlsxrb/stream_row.rb', line 22

def hidden
  @hidden
end

#indexObject (readonly)

Returns:

  • (Object)


22
23
24
# File 'lib/xlsxrb/stream_row.rb', line 22

def index
  @index
end

#outline_levelObject (readonly)

Returns:

  • (Object)


22
23
24
# File 'lib/xlsxrb/stream_row.rb', line 22

def outline_level
  @outline_level
end

Instance Method Details

#[](col_index) ⇒ Elements::Cell, ...

Access a cell by 0-based column index, or access row attributes via Symbol.

: (Integer | Symbol col_index) -> untyped

Parameters:

  • col_index (Integer, Symbol)

    Column index or attribute symbol.

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/xlsxrb/stream_row.rb', line 93

def [](col_index)
  case col_index
  when Symbol
    case col_index
    when :cells then cells
    when :index then index
    when :height then height
    when :hidden then hidden
    when :custom_height then custom_height
    when :outline_level then outline_level
    when :attrs then { height: height, hidden: hidden, custom_height: custom_height, outline_level: outline_level }
    end
  else
    cells[col_index]
  end
end

#cell_at(col_index) ⇒ Elements::Cell?

Access a cell by 0-based column index.

: (Integer col_index) -> Elements::Cell?

Parameters:

  • col_index (Integer)

    0-based column index.

Returns:



116
117
118
# File 'lib/xlsxrb/stream_row.rb', line 116

def cell_at(col_index)
  cells.find { |c| c.column_index == col_index }
end

#cellsArray<Elements::Cell>

Returns all cells as an Array. Cached on first access.

: () -> Array

Returns:



83
84
85
# File 'lib/xlsxrb/stream_row.rb', line 83

def cells
  @cells ||= each_cell.to_a.freeze
end

#each {|cell| ... } ⇒ Enumerator, void

Iterate over cells in this streaming row.

: () { (Elements::Cell) -> void } -> void : | () -> Enumerator[Elements::Cell, void]

Yields:

  • (cell)

Yield Parameters:

Returns:

  • (Enumerator, void)


74
75
76
# File 'lib/xlsxrb/stream_row.rb', line 74

def each(&)
  each_cell(&)
end

#each_cell {|cell| ... } ⇒ Enumerator, void

Iterate over cells in this streaming row one by one.

: () { (Elements::Cell) -> void } -> void : | () -> Enumerator[Elements::Cell, void]

Yields:

  • (cell)

Yield Parameters:

Returns:

  • (Enumerator, void)


56
57
58
59
60
61
62
63
64
# File 'lib/xlsxrb/stream_row.rb', line 56

def each_cell(&)
  return enum_for(:each_cell) unless block_given?

  if @cells
    @cells.each(&)
  else
    Ooxml::WorksheetParser.fast_scan_cells_direct(@xml, @from, @to, @shared_strings, { row: @index }, &)
  end
end

#errorsArray<String>

Validation errors for compatibility with Elements::Row.

: () -> Array

Returns:

  • (Array<String>)


168
169
170
# File 'lib/xlsxrb/stream_row.rb', line 168

def errors
  Elements::EMPTY_ERRORS
end

#inspectString

Human-readable representation.

: () -> String

Returns:

  • (String)


177
178
179
# File 'lib/xlsxrb/stream_row.rb', line 177

def inspect
  "#<#{self.class.name} index=#{index} height=#{height.inspect} hidden=#{hidden}>"
end

#to_aArray<Object>

Convert row cells to an Array of raw values (sparse columns get nil).

: () -> Array

Returns:

  • (Array<Object>)


125
126
127
128
129
130
131
132
133
134
# File 'lib/xlsxrb/stream_row.rb', line 125

def to_a
  return [] if cells.empty?

  max_col = cells.map(&:column_index).max || 0
  arr = Array.new(max_col + 1)
  cells.each do |cell|
    arr[cell.column_index] = cell.value
  end
  arr
end

#unmapped_dataHash

Unmapped metadata for compatibility with Elements::Row.

: () -> Hash[untyped, untyped]

Returns:

  • (Hash)


159
160
161
# File 'lib/xlsxrb/stream_row.rb', line 159

def unmapped_data
  Elements::EMPTY_HASH
end

#valid?Boolean

Returns whether the row is valid according to OOXML specifications.

: () -> bool

Returns:

  • (Boolean)


150
151
152
# File 'lib/xlsxrb/stream_row.rb', line 150

def valid?
  true
end

#valuesArray<Object>

Returns cell values as an Array.

: () -> Array

Returns:

  • (Array<Object>)


141
142
143
# File 'lib/xlsxrb/stream_row.rb', line 141

def values
  to_a
end