Class: Xlsxrb::StreamSheet

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

Overview

Represents a worksheet being streamed sequentially from an XLSX file. Provides O(1) constant-memory streaming over rows and cells.

Call #load (or #to_worksheet) to convert this streaming sheet into an in-memory Elements::Worksheet supporting coordinate random access (sheet["A1"]).

Examples:

Iterate rows and cells in streaming mode (O(1) memory)

Xlsxrb.read("large_data.xlsx") do |sheet|
  puts "Processing sheet: #{sheet.name}"
  sheet.each_row do |row|
    row.each_cell do |cell|
      puts "#{cell.ref}: #{cell.value}"
    end
  end
end

Load into an in-memory Worksheet for coordinate random access

wb = Xlsxrb.read("data.xlsx")
doc_sheet = wb.sheets.first.load
puts doc_sheet["A1"].value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, sheet_xml, shared_strings, styles = nil) ⇒ StreamSheet

Initializes a streaming worksheet context.

: (String name, String sheet_xml, Array shared_strings, ?Hash[untyped, untyped]? styles) -> void

Parameters:

  • name (String)

    The sheet name.

  • sheet_xml (String)

    Raw XML content of the worksheet.

  • shared_strings (Array<String>)

    Shared strings table.

  • styles (Hash, nil) (defaults to: nil)

    Optional parsed styles hash.



43
44
45
46
47
48
# File 'lib/xlsxrb/stream_sheet.rb', line 43

def initialize(name, sheet_xml, shared_strings, styles = nil)
  @name = name
  @sheet_xml = sheet_xml
  @shared_strings = shared_strings
  @styles = styles
end

Instance Attribute Details

#nameString (readonly)

: String

Returns:

  • (String)

    The sheet name.



34
35
36
# File 'lib/xlsxrb/stream_sheet.rb', line 34

def name
  @name
end

Instance Method Details

#each {|row| ... } ⇒ void #eachEnumerator<StreamRow | Elements::Row, void> #eachvoid #eachEnumerator[StreamRow | Elements::Row, void]

Default Enumerable iteration delegates to #each_row.

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

Overloads:

Yields:

Yield Parameters:

Yield Returns:

  • (void)


103
104
105
# File 'lib/xlsxrb/stream_sheet.rb', line 103

def each(&)
  each_row(&)
end

#each_cell {|cell| ... } ⇒ void #each_cellEnumerator<Elements::Cell, void> #each_cellvoid #each_cellEnumerator[Elements::Cell, void]

Iterates over all cells across all rows continuously with O(1) memory.

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

Overloads:

  • #each_cell {|cell| ... } ⇒ void

    This method returns an undefined value.

    Yields:

    • (cell)

    Yield Parameters:

  • #each_cellEnumerator<Elements::Cell, void>

    Returns:

  • #each_cellvoid

    This method returns an undefined value.

  • #each_cellEnumerator[Elements::Cell, void]

    Returns:

Yields:

Yield Parameters:

Yield Returns:

  • (void)


82
83
84
85
86
87
88
# File 'lib/xlsxrb/stream_sheet.rb', line 82

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

  each_row do |row|
    row.each_cell(&)
  end
end

#each_row {|row| ... } ⇒ void #each_rowEnumerator<StreamRow | Elements::Row, void> #each_rowvoid #each_rowEnumerator[StreamRow | Elements::Row, void]

Iterates over rows in this streaming worksheet with O(1) memory.

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

Overloads:

Yields:

Yield Parameters:

Yield Returns:

  • (void)


63
64
65
66
67
# File 'lib/xlsxrb/stream_sheet.rb', line 63

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

  Ooxml::WorksheetParser.each_row(@sheet_xml, shared_strings: @shared_strings, &)
end

#loadElements::Worksheet Also known as: to_worksheet

Loads this sheet completely into an in-memory Elements::Worksheet, enabling coordinate random access (sheet["A1"]), row lookups (row_at), and immutable cell updates (update_cell).

: () -> Elements::Worksheet

Returns:



114
115
116
# File 'lib/xlsxrb/stream_sheet.rb', line 114

def load
  Xlsxrb.send(:build_worksheet, @name, @sheet_xml, @shared_strings, @styles)
end