Sha256: ba5f964b8b1cae96eaf55b27c4fca1f2cb19ef7d0ed8caedd2ebe4918506d8c2

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true
module Saxlsx
  class SheetCollectionParser < Ox::Sax

    CurrentSheet = Struct.new :index, :name

    def self.parse(file_system, workbook, &block)
      SaxParser.parse(
        self.new(file_system, workbook, &block),
        file_system.workbook
      )
    end

    def initialize(file_system, workbook, &block)
      @file_system = file_system
      @workbook = workbook
      @block = block
      @index = -1
      @workbook_pr = false
    end

    def start_element(name)
      case name
      when :sheet
        @current_sheet = CurrentSheet.new(@index += 1)
      when :workbookPr
        @workbook_pr = true
      end
    end

    def end_element(name)
      case name
      when :sheet
        @block.call Sheet.new(
          @current_sheet.name,
          @current_sheet.index,
          @file_system,
          @workbook
        )
        @current_sheet = nil
      when :workbookPr
        @workbook_pr = false
      end
    end

    def attr(name, value)
      if @current_sheet
        if name == :name
          @current_sheet.name = value
        end
      elsif @workbook_pr
        if name == :date1904 && value =~ /true|1/i
          @workbook.date1904 = true
        end
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
saxlsx-1.11.0 lib/saxlsx/sheet_collection_parser.rb
saxlsx-1.10.0 lib/saxlsx/sheet_collection_parser.rb
saxlsx-1.9.0 lib/saxlsx/sheet_collection_parser.rb
saxlsx-1.8.0 lib/saxlsx/sheet_collection_parser.rb
saxlsx-1.7.1 lib/saxlsx/sheet_collection_parser.rb
saxlsx-1.7.0 lib/saxlsx/sheet_collection_parser.rb