oxcelix 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,28 +1,28 @@
1
- module Oxcelix
2
- # The Comments class is a parser which builds an array of comments
3
- class Comments < ::Ox::Sax
4
- # @!attribute [rw] commarray
5
- # @return [Array] the array of all comments of a given sheet
6
- # @!attribute [rw] comment
7
- # @return [Hash] a hash representing a comment
8
- attr_accessor :commarray, :comment
9
- def initialize
10
- @commarray=[]
11
- @comment={}
12
- end
13
-
14
- # Push Cell comment hash (comment + reference) to @commarray
15
- def text(str)
16
- @comment[:comment]=str.gsub('&#10;', '')
17
- @commarray << @comment
18
- @comment = Hash.new
19
- end
20
-
21
- # Returns reference
22
- def attr(name, str)
23
- if name == :ref
24
- @comment[:ref]=str
25
- end
26
- end
27
- end
28
- end
1
+ module Oxcelix
2
+ # The Comments class is a parser which builds an array of comments
3
+ class Comments < ::Ox::Sax
4
+ # @!attribute [rw] commarray
5
+ # @return [Array] the array of all comments of a given sheet
6
+ # @!attribute [rw] comment
7
+ # @return [Hash] a hash representing a comment
8
+ attr_accessor :commarray, :comment
9
+ def initialize
10
+ @commarray=[]
11
+ @comment={}
12
+ end
13
+
14
+ # Push Cell comment hash (comment + reference) to @commarray
15
+ def text(str)
16
+ @comment[:comment]=str.gsub('&#10;', '')
17
+ @commarray << @comment
18
+ @comment = Hash.new
19
+ end
20
+
21
+ # Returns reference
22
+ def attr(name, str)
23
+ if name == :ref
24
+ @comment[:ref]=str
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,17 +1,17 @@
1
- module Oxcelix
2
- # Ox based SAX parser which pushes shared strings (taken from the sharedString.xml file) to an array
3
- # These strings will replace the references in the cells (interpolation).
4
- class Sharedstrings < ::Ox::Sax
5
- # @!attribute [rw] stringarray
6
- # @return [Array] the array of all the strings found in sharedStrings.xml
7
- attr_accessor :stringarray
8
- def initialize
9
- @stringarray=[]
10
- end
11
-
12
- # Push the comment string into @stringarray
13
- def text(str)
14
- @stringarray << str
15
- end
16
- end
17
- end
1
+ module Oxcelix
2
+ # Ox based SAX parser which pushes shared strings (taken from the sharedString.xml file) to an array
3
+ # These strings will replace the references in the cells (interpolation).
4
+ class Sharedstrings < ::Ox::Sax
5
+ # @!attribute [rw] stringarray
6
+ # @return [Array] the array of all the strings found in sharedStrings.xml
7
+ attr_accessor :stringarray
8
+ def initialize
9
+ @stringarray=[]
10
+ end
11
+
12
+ # Push the comment string into @stringarray
13
+ def text(str)
14
+ @stringarray << str
15
+ end
16
+ end
17
+ end
@@ -1,49 +1,49 @@
1
- require 'ox'
2
- module Oxcelix
3
-
4
- # Ox based SAX parser which pushes the number formats (taken from the styles.xml file) to an array
5
- # The reference taken from the cell's 's' attribute points to an element of the
6
- # style array, which in turn points to a number format (numFmt) that can be
7
- # either built-in (@formats) or defined in the styles.xml itself.
8
- class Styles < ::Ox::Sax
9
- attr_accessor :styleary, :xmlstack, :temparray
10
- def initialize
11
- @temparray=[]
12
- @styleary=[]
13
- @xmlstack = []
14
- @numform={}
15
- end
16
-
17
- def nf key, value
18
- @numform[key]=value
19
- if @numform.size == 2
20
- @temparray << @numform
21
- @numform = {}
22
- end
23
- end
24
-
25
- def numFmtId str
26
- if @xmlstack[-2] == :cellXfs
27
- @styleary << str
28
- elsif @xmlstack[-2] == :numFmts
29
- nf :numFmtId, str
30
- end
31
- end
32
-
33
- def formatCode str
34
- nf :formatCode, str
35
- end
36
-
37
- def start_element(name)
38
- @xmlstack << name
39
- end
40
-
41
- def end_element(name)
42
- @xmlstack.pop
43
- end
44
-
45
- def attr(name, str)
46
- self.send name, str if self.respond_to?(name)
47
- end
48
- end
1
+ require 'ox'
2
+ module Oxcelix
3
+
4
+ # Ox based SAX parser which pushes the number formats (taken from the styles.xml file) to an array
5
+ # The reference taken from the cell's 's' attribute points to an element of the
6
+ # style array, which in turn points to a number format (numFmt) that can be
7
+ # either built-in (@formats) or defined in the styles.xml itself.
8
+ class Styles < ::Ox::Sax
9
+ attr_accessor :styleary, :xmlstack, :temparray
10
+ def initialize
11
+ @temparray=[]
12
+ @styleary=[]
13
+ @xmlstack = []
14
+ @numform={}
15
+ end
16
+
17
+ def nf key, value
18
+ @numform[key]=value
19
+ if @numform.size == 2
20
+ @temparray << @numform
21
+ @numform = {}
22
+ end
23
+ end
24
+
25
+ def numFmtId str
26
+ if @xmlstack[-2] == :cellXfs
27
+ @styleary << str
28
+ elsif @xmlstack[-2] == :numFmts
29
+ nf :numFmtId, str
30
+ end
31
+ end
32
+
33
+ def formatCode str
34
+ nf :formatCode, str
35
+ end
36
+
37
+ def start_element(name)
38
+ @xmlstack << name
39
+ end
40
+
41
+ def end_element(name)
42
+ @xmlstack.pop
43
+ end
44
+
45
+ def attr(name, str)
46
+ self.send name, str if self.respond_to?(name)
47
+ end
48
+ end
49
49
  end
@@ -1,78 +1,78 @@
1
-
2
- module Oxcelix
3
- ##
4
- # The Xlsheet class is a SAX parser based on the Ox library. It parses a
5
- # SpreadsheetML (AKA Office Open XML) formatted XML file and returns an array
6
- # of Cell objects {#cellarray} and an array of merged cells {#mergedcells}.
7
- #
8
- # Xlsheet will omit the following:
9
- # * empty cells
10
- # * cells containing formulas
11
- #
12
- # Only non-empty cells of merged groups will be added to {#cellarray}. A separate array
13
- # {#mergedcells} is reserved for merging.
14
- class Xlsheet < ::Ox::Sax
15
- # @!attribute [rw] xmlstack
16
- # @return [Array] Stores the state machine's actual state
17
- # @!attribute [rw] mergedcells
18
- # @return [Array] the array of merged cells
19
- # @!attribute [rw] cellarray
20
- # @return [Array] the array of non-empty (meaningful) cells of the current sheet
21
- # @!attribute [rw] cell
22
- # @return [Cell] the cell currently being processed.
23
- attr_accessor :xmlstack, :mergedcells, :cellarray, :cell
24
- def initialize()
25
- @xmlstack = []
26
- @mergedcells = []
27
- @cellarray = []
28
- @cell = Cell.new
29
- end
30
-
31
- # Save SAX state-machine state to {#xmlstack} if and only if the processed
32
- # element is a :c (column) or a :mergeCell (merged cell)
33
- # @param [String] name Start element
34
- def start_element(name)
35
- if name == :c || name == :mergeCell
36
- @xmlstack << name
37
- end
38
- end
39
-
40
- # Step back in the stack ({#xmlstack}.pop), clear actual cell information
41
- # @param [String] name Element ends
42
- def end_element(name)
43
- @xmlstack.pop
44
- if name == :c || name == :mergeCell
45
- @cell=Cell.new
46
- end
47
- end
48
-
49
- # Set cell value, style, etc. This will only happen if the cell has an
50
- # actual value AND the parser's state is :c.
51
- # If the state is :mergeCell AND the actual attribute name is :ref the
52
- # attribute will be added to the merged cells array.
53
- # The attribute name is tested against the Cell object: if the cell
54
- # has a method named the same way, that method is called with the str parameter.
55
- # @param [String] name of the attribute.
56
- # @param [String] str Content of the attribute
57
- def attr(name, str)
58
- if @xmlstack.last == :c
59
- @cell.send name, str if @cell.respond_to?(name)
60
- elsif xmlstack.last == :mergeCell && name == :ref
61
- @mergedcells << str
62
- end
63
- end
64
-
65
- # Cell content is parsed here. For cells containing strings, interpolation using the
66
- # sharedStrings.xml file is done in the #Sharedstrings class.
67
- # The numformat attribute gets a value here based on the styles variable, to preserve the numeric formatting (thus the type) of values.
68
- def text(str)
69
- if @xmlstack.last == :c
70
- if @cell.type != "shared" && @cell.type != "e" && str.numeric?
71
- @cell.v str
72
- @cellarray << @cell
73
- end
74
- cell=Cell.new
75
- end
76
- end
77
- end
78
- end
1
+
2
+ module Oxcelix
3
+ ##
4
+ # The Xlsheet class is a SAX parser based on the Ox library. It parses a
5
+ # SpreadsheetML (AKA Office Open XML) formatted XML file and returns an array
6
+ # of Cell objects {#cellarray} and an array of merged cells {#mergedcells}.
7
+ #
8
+ # Xlsheet will omit the following:
9
+ # * empty cells
10
+ # * cells containing formulas
11
+ #
12
+ # Only non-empty cells of merged groups will be added to {#cellarray}. A separate array
13
+ # {#mergedcells} is reserved for merging.
14
+ class Xlsheet < ::Ox::Sax
15
+ # @!attribute [rw] xmlstack
16
+ # @return [Array] Stores the state machine's actual state
17
+ # @!attribute [rw] mergedcells
18
+ # @return [Array] the array of merged cells
19
+ # @!attribute [rw] cellarray
20
+ # @return [Array] the array of non-empty (meaningful) cells of the current sheet
21
+ # @!attribute [rw] cell
22
+ # @return [Cell] the cell currently being processed.
23
+ attr_accessor :xmlstack, :mergedcells, :cellarray, :cell
24
+ def initialize()
25
+ @xmlstack = []
26
+ @mergedcells = []
27
+ @cellarray = []
28
+ @cell = Cell.new
29
+ end
30
+
31
+ # Save SAX state-machine state to {#xmlstack} if and only if the processed
32
+ # element is a :c (column) or a :mergeCell (merged cell)
33
+ # @param [String] name Start element
34
+ def start_element(name)
35
+ if name == :c || name == :mergeCell
36
+ @xmlstack << name
37
+ end
38
+ end
39
+
40
+ # Step back in the stack ({#xmlstack}.pop), clear actual cell information
41
+ # @param [String] name Element ends
42
+ def end_element(name)
43
+ @xmlstack.pop
44
+ if name == :c || name == :mergeCell
45
+ @cell=Cell.new
46
+ end
47
+ end
48
+
49
+ # Set cell value, style, etc. This will only happen if the cell has an
50
+ # actual value AND the parser's state is :c.
51
+ # If the state is :mergeCell AND the actual attribute name is :ref the
52
+ # attribute will be added to the merged cells array.
53
+ # The attribute name is tested against the Cell object: if the cell
54
+ # has a method named the same way, that method is called with the str parameter.
55
+ # @param [String] name of the attribute.
56
+ # @param [String] str Content of the attribute
57
+ def attr(name, str)
58
+ if @xmlstack.last == :c
59
+ @cell.send name, str if @cell.respond_to?(name)
60
+ elsif xmlstack.last == :mergeCell && name == :ref
61
+ @mergedcells << str
62
+ end
63
+ end
64
+
65
+ # Cell content is parsed here. For cells containing strings, interpolation using the
66
+ # sharedStrings.xml file is done in the #Sharedstrings class.
67
+ # The numformat attribute gets a value here based on the styles variable, to preserve the numeric formatting (thus the type) of values.
68
+ def text(str)
69
+ if @xmlstack.last == :c
70
+ if @cell.type != "shared" && @cell.type != "e" && str.numeric?
71
+ @cell.v str
72
+ @cellarray << @cell
73
+ end
74
+ @cell=Cell.new
75
+ end
76
+ end
77
+ end
78
+ end
@@ -1,75 +1,75 @@
1
-
2
- module Oxcelix
3
- # The Sheet class represents an excel sheet.
4
- class Sheet < Matrix
5
- include Cellhelper
6
- include Numberhelper
7
- # @!attribute [rw] name
8
- # @return [String] Sheet name
9
- # @!attribute [rw] sheetId
10
- # @return [String] returns the sheetId SheetML internal attribute
11
- # @!attribute [rw] relationId
12
- # @return [String] Internal reference key. relationID is used internally by Excel 2010 to e.g. build up the relationship between worksheets and comments
13
- attr_accessor :name, :sheetId, :relationId
14
-
15
- # The [] method overrides the standard Matrix::[]. It will now accept Excel-style cell coordinates.
16
- # @param [String] i
17
- # @return [Cell] the object denoted with the Excel column-row name.
18
- # @example Select a cell in a sheet
19
- # w = Oxcelix::Workbook.new('Example.xlsx')
20
- # w.sheets[0][3,1] #=> #<Oxcelix::Cell:0x00000001e00fa0 @xlcoords="B4", @style="0", @type="n", @value="3">
21
- # w.sheets[0]['B4'] #=> #<Oxcelix::Cell:0x00000001e00fa0 @xlcoords="B4", @style="0", @type="n", @value="3">
22
- def [](i, *j)
23
- if i.is_a? String
24
- super(y(i),x(i))
25
- else
26
- super(i,j[0])
27
- end
28
- end
29
-
30
- #The to_m method returns a simple Matrix object filled with the raw values of the original Sheet object.
31
- # @return [Matrix] a collection of string values (the former #Cell::value)
32
- def to_m(*attrs)
33
- m=Matrix.build(self.row_size, self.column_size){nil}
34
- self.each_with_index do |x, row, col|
35
- if attrs.size == 0 || attrs.nil?
36
- m[row, col] = x.value
37
- end
38
- end
39
- return m
40
- end
41
-
42
- # The to_ru method returns a Matrix of "rubified" values. It basically builds a new Matrix
43
- # and puts the result of the #Cell::to_ru method of every cell of the original sheet in
44
- # the corresponding Matrix cell.
45
- # @return [Matrix] a collection of ruby objects (#Integers, #Floats, #DateTimes, #Rationals, #Strings)
46
- def to_ru
47
- m=Matrix.build(self.row_size, self.column_size){nil}
48
- self.each_with_index do |x, row, col|
49
- if x.nil? || x.value.nil?
50
- m[row, col] = nil
51
- else
52
- m[row, col] = x.to_ru
53
- end
54
- end
55
- return m
56
- end
57
-
58
- # The to_fmt method returns a Matrix of "formatted" values. It basically builds a new Matrix
59
- # and puts the result of the #Cell::to_fmt method of every cell of the original sheet in
60
- # the corresponding Matrix cell. The #Cell::to_fmt will pass the original values to to_ru, and then
61
- # depending on the value, will run strftime on DateTime objects and sprintf on numeric types.
62
- # @return [Matrix] a collection of Strings
63
- def to_fmt
64
- m=Matrix.build(self.row_size, self.column_size){nil}
65
- self.each_with_index do |x, row, col|
66
- if x.nil? || x.value.nil?
67
- m[row, col] = nil
68
- else
69
- m[row, col] = x.to_fmt
70
- end
71
- end
72
- return m
73
- end
74
- end
75
- end
1
+
2
+ module Oxcelix
3
+ # The Sheet class represents an excel sheet.
4
+ class Sheet < Matrix
5
+ include Cellhelper
6
+ include Numberhelper
7
+ # @!attribute [rw] name
8
+ # @return [String] Sheet name
9
+ # @!attribute [rw] sheetId
10
+ # @return [String] returns the sheetId SheetML internal attribute
11
+ # @!attribute [rw] relationId
12
+ # @return [String] Internal reference key. relationID is used internally by Excel 2010 to e.g. build up the relationship between worksheets and comments
13
+ attr_accessor :name, :sheetId, :relationId
14
+
15
+ # The [] method overrides the standard Matrix::[]. It will now accept Excel-style cell coordinates.
16
+ # @param [String] i
17
+ # @return [Cell] the object denoted with the Excel column-row name.
18
+ # @example Select a cell in a sheet
19
+ # w = Oxcelix::Workbook.new('Example.xlsx')
20
+ # w.sheets[0][3,1] #=> #<Oxcelix::Cell:0x00000001e00fa0 @xlcoords="B4", @style="0", @type="n", @value="3">
21
+ # w.sheets[0]['B4'] #=> #<Oxcelix::Cell:0x00000001e00fa0 @xlcoords="B4", @style="0", @type="n", @value="3">
22
+ def [](i, *j)
23
+ if i.is_a? String
24
+ super(y(i),x(i))
25
+ else
26
+ super(i,j[0])
27
+ end
28
+ end
29
+
30
+ #The to_m method returns a simple Matrix object filled with the raw values of the original Sheet object.
31
+ # @return [Matrix] a collection of string values (the former #Cell::value)
32
+ def to_m(*attrs)
33
+ m=Matrix.build(self.row_size, self.column_size){nil}
34
+ self.each_with_index do |x, row, col|
35
+ if attrs.size == 0 || attrs.nil?
36
+ m[row, col] = x.value
37
+ end
38
+ end
39
+ return m
40
+ end
41
+
42
+ # The to_ru method returns a Matrix of "rubified" values. It basically builds a new Matrix
43
+ # and puts the result of the #Cell::to_ru method of every cell of the original sheet in
44
+ # the corresponding Matrix cell.
45
+ # @return [Matrix] a collection of ruby objects (#Integers, #Floats, #DateTimes, #Rationals, #Strings)
46
+ def to_ru
47
+ m=Matrix.build(self.row_size, self.column_size){nil}
48
+ self.each_with_index do |x, row, col|
49
+ if x.nil? || x.value.nil?
50
+ m[row, col] = nil
51
+ else
52
+ m[row, col] = x.to_ru
53
+ end
54
+ end
55
+ return m
56
+ end
57
+
58
+ # The to_fmt method returns a Matrix of "formatted" values. It basically builds a new Matrix
59
+ # and puts the result of the #Cell::to_fmt method of every cell of the original sheet in
60
+ # the corresponding Matrix cell. The #Cell::to_fmt will pass the original values to to_ru, and then
61
+ # depending on the value, will run strftime on DateTime objects and sprintf on numeric types.
62
+ # @return [Matrix] a collection of Strings
63
+ def to_fmt
64
+ m=Matrix.build(self.row_size, self.column_size){nil}
65
+ self.each_with_index do |x, row, col|
66
+ if x.nil? || x.value.nil?
67
+ m[row, col] = nil
68
+ else
69
+ m[row, col] = x.to_fmt
70
+ end
71
+ end
72
+ return m
73
+ end
74
+ end
75
+ end