Class: Oxcelix::Xlsheet

Inherits:
Ox::Sax
  • Object
show all
Defined in:
lib/oxcelix/sax/xlsheet.rb

Overview

The Xlsheet class is a SAX parser based on the Ox library. It parses a SpreadsheetML (AKA Office Open XML) formatted XML file and returns an array of Cell objects #cellarray and an array of merged cells #mergedcells.

Xlsheet will omit the following:

  • empty cells

  • cells containing formulas

Only non-empty cells of merged groups will be added to #cellarray. A separate array #mergedcells is reserved for merging.

Direct Known Subclasses

Cellrange, PagSheet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeXlsheet


24
25
26
27
28
29
# File 'lib/oxcelix/sax/xlsheet.rb', line 24

def initialize()
  @xmlstack    = []
  @mergedcells = []
  @cellarray   = []
  @cell        = Cell.new
end

Instance Attribute Details

#cellCell


23
# File 'lib/oxcelix/sax/xlsheet.rb', line 23

attr_accessor :xmlstack, :mergedcells, :cellarray, :cell

#cellarrayArray


23
# File 'lib/oxcelix/sax/xlsheet.rb', line 23

attr_accessor :xmlstack, :mergedcells, :cellarray, :cell

#mergedcellsArray


23
# File 'lib/oxcelix/sax/xlsheet.rb', line 23

attr_accessor :xmlstack, :mergedcells, :cellarray, :cell

#xmlstackArray


23
24
25
# File 'lib/oxcelix/sax/xlsheet.rb', line 23

def xmlstack
  @xmlstack
end

Instance Method Details

#attr(name, str) ⇒ Object

Set cell value, style, etc. This will only happen if the cell has an actual value AND the parser's state is :c. If the state is :mergeCell AND the actual attribute name is :ref the attribute will be added to the merged cells array. The attribute name is tested against the Cell object: if the cell has a method named the same way, that method is called with the str parameter.


63
64
65
66
67
68
69
70
# File 'lib/oxcelix/sax/xlsheet.rb', line 63

def attr(name, str)
  case @xmlstack.last
  when :c
    @cell.send name, str if @cell.respond_to?(name)
  when :mergeCell
    @mergedcells << str if name == :ref
  end
end

#end_element(name) ⇒ Object

Step back in the stack (#xmlstack.pop), clear actual cell information


45
46
47
48
49
50
51
52
53
# File 'lib/oxcelix/sax/xlsheet.rb', line 45

def end_element(name)
  @xmlstack.pop
  case name
  when :c
    @cell = Cell.new
  when :mergeCell
    @cell = Cell.new
  end
end

#start_element(name) ⇒ Object

Save SAX state-machine state to #xmlstack if and only if the processed element is a :c (column) or a :mergeCell (merged cell)


34
35
36
37
38
39
40
41
# File 'lib/oxcelix/sax/xlsheet.rb', line 34

def start_element(name)
  case name
  when :c
    @xmlstack << name
  when :mergeCell
    @xmlstack << name
  end
end

#text(str) ⇒ Object

Cell content is parsed here. For cells containing strings, interpolation using the sharedStrings.xml file is done in the #Sharedstrings class. The numformat attribute gets a value here based on the styles variable, to preserve the numeric formatting (thus the type) of values.


75
76
77
78
79
80
81
82
83
# File 'lib/oxcelix/sax/xlsheet.rb', line 75

def text(str)
  if @xmlstack.last == :c
    if @cell.type != "shared" && @cell.type != "e" && str.numeric?
      @cell.v str
      @cellarray << @cell
    end
    @cell = Cell.new
  end
end