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.
- data/.yardopts +1 -1
- data/CHANGES +5 -0
- data/LICENSE +20 -20
- data/README.md +70 -70
- data/README.rdoc +61 -61
- data/lib/oxcelix.rb +47 -47
- data/lib/oxcelix/cell.rb +22 -22
- data/lib/oxcelix/cellhelper.rb +48 -48
- data/lib/oxcelix/nf.rb +172 -172
- data/lib/oxcelix/numformats.rb +114 -114
- data/lib/oxcelix/sax/comments.rb +28 -28
- data/lib/oxcelix/sax/sharedstrings.rb +17 -17
- data/lib/oxcelix/sax/styles.rb +48 -48
- data/lib/oxcelix/sax/xlsheet.rb +78 -78
- data/lib/oxcelix/sheet.rb +75 -75
- data/lib/oxcelix/workbook.rb +256 -252
- data/oxcelix.gemspec +26 -26
- data/spec/cell_spec.rb +13 -13
- data/spec/fixnum_spec.rb +11 -11
- data/spec/matrix_spec.rb +11 -11
- data/spec/oxcelix_spec.rb +36 -36
- data/spec/spec_helper.rb +3 -3
- data/spec/string_spec.rb +19 -19
- metadata +9 -10
data/lib/oxcelix/cellhelper.rb
CHANGED
@@ -1,49 +1,49 @@
|
|
1
|
-
module Oxcelix
|
2
|
-
|
3
|
-
# The Cellvalues module provides methods for setting cell values. They are named after the relevant XML entitiesd and
|
4
|
-
# called directly by the Xlsheet SAX parser.
|
5
|
-
module Cellvalues
|
6
|
-
# Set the excel cell name (eg: 'A2')
|
7
|
-
# @param [String] val Excel cell address name
|
8
|
-
def r(val); @xlcoords = val; end;
|
9
|
-
# Set cell type
|
10
|
-
def t(val); @type = val; end;
|
11
|
-
# Set cell value
|
12
|
-
def v(val); @value = val; end;
|
13
|
-
# Set cell style (number format and style)
|
14
|
-
def s(val); @style = val; end;
|
15
|
-
end
|
16
|
-
|
17
|
-
# The Cellhelper module defines some methods useful to manipulate Cell objects
|
18
|
-
module Cellhelper
|
19
|
-
# When called without parameters, returns the x coordinate of the calling cell object based on the value of #@xlcoords
|
20
|
-
# If a parameter is given, #x will return the x coordinate corresponding to the parameter
|
21
|
-
# @example find x coordinate (column number) of a cell
|
22
|
-
# c = Cell.new
|
23
|
-
# c.xlcoords = ('B3')
|
24
|
-
# c.x #=> 1
|
25
|
-
# @param [String] coord Optional parameter used when method is not called from a Cell object
|
26
|
-
# @return [Integer] x coordinate
|
27
|
-
def x(coord=nil)
|
28
|
-
if coord.nil?
|
29
|
-
coord = @xlcoords
|
30
|
-
end
|
31
|
-
('A'..(coord.scan(/\p{Alpha}+|\p{Digit}+/u)[0])).to_a.length-1
|
32
|
-
end
|
33
|
-
|
34
|
-
# When called without parameters, returns the y coordinate of the calling cell object based on the value of #@xlcoords
|
35
|
-
# If a parameter is given, #y will return the y coordinate corresponding to the parameter
|
36
|
-
# @example find y coordinate (row number) of a cell
|
37
|
-
# c = Cell.new
|
38
|
-
# c.xlcoords = ('B3')
|
39
|
-
# c.y #=> 2
|
40
|
-
# @param [String] coord Optional parameter used when method is not called from a Cell object
|
41
|
-
# @return [Integer] x coordinate
|
42
|
-
def y(coord=nil)
|
43
|
-
if coord.nil?
|
44
|
-
coord = @xlcoords
|
45
|
-
end
|
46
|
-
coord.scan(/\p{Alpha}+|\p{Digit}+/u)[1].to_i-1
|
47
|
-
end
|
48
|
-
end
|
1
|
+
module Oxcelix
|
2
|
+
|
3
|
+
# The Cellvalues module provides methods for setting cell values. They are named after the relevant XML entitiesd and
|
4
|
+
# called directly by the Xlsheet SAX parser.
|
5
|
+
module Cellvalues
|
6
|
+
# Set the excel cell name (eg: 'A2')
|
7
|
+
# @param [String] val Excel cell address name
|
8
|
+
def r(val); @xlcoords = val; end;
|
9
|
+
# Set cell type
|
10
|
+
def t(val); @type = val; end;
|
11
|
+
# Set cell value
|
12
|
+
def v(val); @value = val; end;
|
13
|
+
# Set cell style (number format and style)
|
14
|
+
def s(val); @style = val; end;
|
15
|
+
end
|
16
|
+
|
17
|
+
# The Cellhelper module defines some methods useful to manipulate Cell objects
|
18
|
+
module Cellhelper
|
19
|
+
# When called without parameters, returns the x coordinate of the calling cell object based on the value of #@xlcoords
|
20
|
+
# If a parameter is given, #x will return the x coordinate corresponding to the parameter
|
21
|
+
# @example find x coordinate (column number) of a cell
|
22
|
+
# c = Cell.new
|
23
|
+
# c.xlcoords = ('B3')
|
24
|
+
# c.x #=> 1
|
25
|
+
# @param [String] coord Optional parameter used when method is not called from a Cell object
|
26
|
+
# @return [Integer] x coordinate
|
27
|
+
def x(coord=nil)
|
28
|
+
if coord.nil?
|
29
|
+
coord = @xlcoords
|
30
|
+
end
|
31
|
+
('A'..(coord.scan(/\p{Alpha}+|\p{Digit}+/u)[0])).to_a.length-1
|
32
|
+
end
|
33
|
+
|
34
|
+
# When called without parameters, returns the y coordinate of the calling cell object based on the value of #@xlcoords
|
35
|
+
# If a parameter is given, #y will return the y coordinate corresponding to the parameter
|
36
|
+
# @example find y coordinate (row number) of a cell
|
37
|
+
# c = Cell.new
|
38
|
+
# c.xlcoords = ('B3')
|
39
|
+
# c.y #=> 2
|
40
|
+
# @param [String] coord Optional parameter used when method is not called from a Cell object
|
41
|
+
# @return [Integer] x coordinate
|
42
|
+
def y(coord=nil)
|
43
|
+
if coord.nil?
|
44
|
+
coord = @xlcoords
|
45
|
+
end
|
46
|
+
coord.scan(/\p{Alpha}+|\p{Digit}+/u)[1].to_i-1
|
47
|
+
end
|
48
|
+
end
|
49
49
|
end
|
data/lib/oxcelix/nf.rb
CHANGED
@@ -1,172 +1,172 @@
|
|
1
|
-
module Oxcelix
|
2
|
-
module Numformats
|
3
|
-
# Formatarray is the array of default format strings in Excel. Nil values should apparently contain CJK date format strings,
|
4
|
-
#feel free to add/document those according to existing standards.
|
5
|
-
Formatarray = [
|
6
|
-
{:id => '0', :xl => 'General', :ostring => nil, :cls => 'string'},
|
7
|
-
{:id => '1', :xl => '0', :ostring => '%1d', :cls => 'numeric'},
|
8
|
-
{:id => '2', :xl => '0.00', :ostring => '%1.2f', :cls => 'numeric'},
|
9
|
-
{:id => '3', :xl => '#,##0', :ostring => '%#4d', :cls => 'numeric'},
|
10
|
-
{:id => '4', :xl => '#,##0.00', :ostring => '%#4.2f', :cls => 'numeric'},
|
11
|
-
{:id => '5', :xl => '', :ostring => nil, :cls => 'string'},
|
12
|
-
{:id => '6', :xl => '', :ostring => nil, :cls => 'string'},
|
13
|
-
{:id => '7', :xl => '', :ostring => nil, :cls => 'string'},
|
14
|
-
{:id => '8', :xl => '', :ostring => nil, :cls => 'string'},
|
15
|
-
{:id => '9', :xl => '0%', :ostring => '%1d%', :cls => 'numeric'},
|
16
|
-
{:id => '10', :xl => '0.00%', :ostring => '%1.2f%', :cls => 'numeric'},
|
17
|
-
{:id => '11', :xl => '0.00E+00', :ostring => '%1.2fE+', :cls => 'numeric'},
|
18
|
-
{:id => '12', :xl => '# ?/?', :ostring => '%#1d', :cls => 'rational'},
|
19
|
-
{:id => '13', :xl => '# ??/??', :ostring => '%#1d', :cls => 'rational'},
|
20
|
-
{:id => '14', :xl => 'd/m/yyyy', :ostring => '%-d/%-m/%Y', :cls => 'date'},
|
21
|
-
{:id => '15', :xl => 'd-mmm-yy', :ostring => '%-d-%b-%y', :cls => 'date'},
|
22
|
-
{:id => '16', :xl => 'd-mmm', :ostring => '%-d-%b', :cls => 'date'},
|
23
|
-
{:id => '17', :xl => 'mmm-yy', :ostring => '%b-%y', :cls => 'date'},
|
24
|
-
{:id => '18', :xl => 'h:mm tt', :ostring => '%-k:%M tt', :cls => 'date'},
|
25
|
-
{:id => '19', :xl => 'h:mm:ss tt', :ostring => '%-k:%M:%-S tt', :cls => 'date'},
|
26
|
-
{:id => '20', :xl => 'H:mm', :ostring => '%-k:%M', :cls => 'date'},
|
27
|
-
{:id => '21', :xl => 'H:mm:ss', :ostring => '%-k:%M:%-S', :cls => 'date'},
|
28
|
-
{:id => '22', :xl => 'm/d/yyyy H:mm', :ostring => '%-m/%-d/%Y %-k:%M', :cls => 'date'},
|
29
|
-
{:id => '23', :xl => '', :ostring => nil, :cls => 'string'},
|
30
|
-
{:id => '24', :xl => '', :ostring => nil, :cls => 'string'},
|
31
|
-
{:id => '25', :xl => '', :ostring => nil, :cls => 'string'},
|
32
|
-
{:id => '26', :xl => '', :ostring => nil, :cls => 'string'},
|
33
|
-
{:id => '27', :xl => '', :ostring => nil, :cls => 'string'},
|
34
|
-
{:id => '28', :xl => '', :ostring => nil, :cls => 'string'},
|
35
|
-
{:id => '29', :xl => '', :ostring => nil, :cls => 'string'},
|
36
|
-
{:id => '30', :xl => '', :ostring => nil, :cls => 'string'},
|
37
|
-
{:id => '31', :xl => '', :ostring => nil, :cls => 'string'},
|
38
|
-
{:id => '32', :xl => '', :ostring => nil, :cls => 'string'},
|
39
|
-
{:id => '33', :xl => '', :ostring => nil, :cls => 'string'},
|
40
|
-
{:id => '34', :xl => '', :ostring => nil, :cls => 'string'},
|
41
|
-
{:id => '35', :xl => '', :ostring => nil, :cls => 'string'},
|
42
|
-
{:id => '36', :xl => '', :ostring => nil, :cls => 'string'},
|
43
|
-
{:id => '37', :xl => '#,##0 ;(#,##0)', :ostring => '%#4d', :cls => 'numeric'},
|
44
|
-
{:id => '38', :xl => '#,##0 ;[Red](#,##0)', :ostring => '%#4d', :cls => 'numeric'},
|
45
|
-
{:id => '39', :xl => '#,##0.00;(#,##0.00)', :ostring => '%#4.2f', :cls => 'numeric'},
|
46
|
-
{:id => '40', :xl => '#,##0.00;[Red](#,##0.00)', :ostring => '%#4.2f', :cls => 'numeric'},
|
47
|
-
{:id => '41', :xl => '', :ostring => nil, :cls => 'string'},
|
48
|
-
{:id => '42', :xl => '', :ostring => nil, :cls => 'string'},
|
49
|
-
{:id => '43', :xl => '', :ostring => nil, :cls => 'string'},
|
50
|
-
{:id => '44', :xl => '', :ostring => nil, :cls => 'string'},
|
51
|
-
{:id => '45', :xl => 'mm:ss', :ostring => '%M:%-S', :cls => 'date'},
|
52
|
-
{:id => '46', :xl => '[h]:mm:ss', :ostring => '%-k:%M:%-S', :cls => 'date'},
|
53
|
-
{:id => '47', :xl => 'mmss.0', :ostring => '%M%-S.%1n', :cls => 'date'},
|
54
|
-
{:id => '48', :xl => '##0.0E+0', :ostring => '%#3.1E', :cls => 'numeric'},
|
55
|
-
{:id => '49', :xl => '@,', :ostring => '@%d', :cls => 'numeric'},
|
56
|
-
{:id => '50', :xl => '', :ostring => nil, :cls => 'string'},
|
57
|
-
{:id => '51', :xl => '', :ostring => nil, :cls => 'string'},
|
58
|
-
{:id => '52', :xl => '', :ostring => nil, :cls => 'string'},
|
59
|
-
{:id => '53', :xl => '', :ostring => nil, :cls => 'string'},
|
60
|
-
{:id => '54', :xl => '', :ostring => nil, :cls => 'string'},
|
61
|
-
{:id => '55', :xl => '', :ostring => nil, :cls => 'string'},
|
62
|
-
{:id => '56', :xl => '', :ostring => nil, :cls => 'string'},
|
63
|
-
{:id => '57', :xl => '', :ostring => nil, :cls => 'string'},
|
64
|
-
{:id => '58', :xl => '', :ostring => nil, :cls => 'string'},
|
65
|
-
{:id => '59', :xl => '', :ostring => nil, :cls => 'string'},
|
66
|
-
{:id => '60', :xl => '', :ostring => nil, :cls => 'string'},
|
67
|
-
{:id => '61', :xl => '', :ostring => nil, :cls => 'string'},
|
68
|
-
{:id => '62', :xl => '', :ostring => nil, :cls => 'string'},
|
69
|
-
{:id => '63', :xl => '', :ostring => nil, :cls => 'string'},
|
70
|
-
{:id => '64', :xl => '', :ostring => nil, :cls => 'string'},
|
71
|
-
{:id => '65', :xl => '', :ostring => nil, :cls => 'string'},
|
72
|
-
{:id => '66', :xl => '', :ostring => nil, :cls => 'string'},
|
73
|
-
{:id => '67', :xl => '', :ostring => nil, :cls => 'string'},
|
74
|
-
{:id => '68', :xl => '', :ostring => nil, :cls => 'string'},
|
75
|
-
{:id => '69', :xl => '', :ostring => nil, :cls => 'string'},
|
76
|
-
{:id => '70', :xl => '', :ostring => nil, :cls => 'string'},
|
77
|
-
{:id => '71', :xl => '', :ostring => nil, :cls => 'string'},
|
78
|
-
{:id => '72', :xl => '', :ostring => nil, :cls => 'string'},
|
79
|
-
{:id => '73', :xl => '', :ostring => nil, :cls => 'string'},
|
80
|
-
{:id => '74', :xl => '', :ostring => nil, :cls => 'string'},
|
81
|
-
{:id => '75', :xl => '', :ostring => nil, :cls => 'string'},
|
82
|
-
{:id => '76', :xl => '', :ostring => nil, :cls => 'string'},
|
83
|
-
{:id => '77', :xl => '', :ostring => nil, :cls => 'string'},
|
84
|
-
{:id => '78', :xl => '', :ostring => nil, :cls => 'string'},
|
85
|
-
{:id => '79', :xl => '', :ostring => nil, :cls => 'string'},
|
86
|
-
{:id => '80', :xl => '', :ostring => nil, :cls => 'string'},
|
87
|
-
{:id => '81', :xl => '', :ostring => nil, :cls => 'string'},
|
88
|
-
{:id => '82', :xl => '', :ostring => nil, :cls => 'string'},
|
89
|
-
{:id => '83', :xl => '', :ostring => nil, :cls => 'string'},
|
90
|
-
{:id => '84', :xl => '', :ostring => nil, :cls => 'string'},
|
91
|
-
{:id => '85', :xl => '', :ostring => nil, :cls => 'string'},
|
92
|
-
{:id => '86', :xl => '', :ostring => nil, :cls => 'string'},
|
93
|
-
{:id => '87', :xl => '', :ostring => nil, :cls => 'string'},
|
94
|
-
{:id => '88', :xl => '', :ostring => nil, :cls => 'string'},
|
95
|
-
{:id => '89', :xl => '', :ostring => nil, :cls => 'string'},
|
96
|
-
{:id => '90', :xl => '', :ostring => nil, :cls => 'string'},
|
97
|
-
{:id => '91', :xl => '', :ostring => nil, :cls => 'string'},
|
98
|
-
{:id => '92', :xl => '', :ostring => nil, :cls => 'string'},
|
99
|
-
{:id => '93', :xl => '', :ostring => nil, :cls => 'string'},
|
100
|
-
{:id => '94', :xl => '', :ostring => nil, :cls => 'string'},
|
101
|
-
{:id => '95', :xl => '', :ostring => nil, :cls => 'string'},
|
102
|
-
{:id => '96', :xl => '', :ostring => nil, :cls => 'string'},
|
103
|
-
{:id => '97', :xl => '', :ostring => nil, :cls => 'string'},
|
104
|
-
{:id => '98', :xl => '', :ostring => nil, :cls => 'string'},
|
105
|
-
{:id => '99', :xl => '', :ostring => nil, :cls => 'string'},
|
106
|
-
{:id => '100', :xl => '', :ostring => nil, :cls => 'string'},
|
107
|
-
{:id => '101', :xl => '', :ostring => nil, :cls => 'string'},
|
108
|
-
{:id => '102', :xl => '', :ostring => nil, :cls => 'string'},
|
109
|
-
{:id => '103', :xl => '', :ostring => nil, :cls => 'string'},
|
110
|
-
{:id => '104', :xl => '', :ostring => nil, :cls => 'string'},
|
111
|
-
{:id => '105', :xl => '', :ostring => nil, :cls => 'string'},
|
112
|
-
{:id => '106', :xl => '', :ostring => nil, :cls => 'string'},
|
113
|
-
{:id => '107', :xl => '', :ostring => nil, :cls => 'string'},
|
114
|
-
{:id => '108', :xl => '', :ostring => nil, :cls => 'string'},
|
115
|
-
{:id => '109', :xl => '', :ostring => nil, :cls => 'string'},
|
116
|
-
{:id => '110', :xl => '', :ostring => nil, :cls => 'string'},
|
117
|
-
{:id => '111', :xl => '', :ostring => nil, :cls => 'string'},
|
118
|
-
{:id => '112', :xl => '', :ostring => nil, :cls => 'string'},
|
119
|
-
{:id => '113', :xl => '', :ostring => nil, :cls => 'string'},
|
120
|
-
{:id => '114', :xl => '', :ostring => nil, :cls => 'string'},
|
121
|
-
{:id => '115', :xl => '', :ostring => nil, :cls => 'string'},
|
122
|
-
{:id => '116', :xl => '', :ostring => nil, :cls => 'string'},
|
123
|
-
{:id => '117', :xl => '', :ostring => nil, :cls => 'string'},
|
124
|
-
{:id => '118', :xl => '', :ostring => nil, :cls => 'string'},
|
125
|
-
{:id => '119', :xl => '', :ostring => nil, :cls => 'string'},
|
126
|
-
{:id => '120', :xl => '', :ostring => nil, :cls => 'string'},
|
127
|
-
{:id => '121', :xl => '', :ostring => nil, :cls => 'string'},
|
128
|
-
{:id => '122', :xl => '', :ostring => nil, :cls => 'string'},
|
129
|
-
{:id => '123', :xl => '', :ostring => nil, :cls => 'string'},
|
130
|
-
{:id => '124', :xl => '', :ostring => nil, :cls => 'string'},
|
131
|
-
{:id => '125', :xl => '', :ostring => nil, :cls => 'string'},
|
132
|
-
{:id => '126', :xl => '', :ostring => nil, :cls => 'string'},
|
133
|
-
{:id => '127', :xl => '', :ostring => nil, :cls => 'string'},
|
134
|
-
{:id => '128', :xl => '', :ostring => nil, :cls => 'string'},
|
135
|
-
{:id => '129', :xl => '', :ostring => nil, :cls => 'string'},
|
136
|
-
{:id => '130', :xl => '', :ostring => nil, :cls => 'string'},
|
137
|
-
{:id => '131', :xl => '', :ostring => nil, :cls => 'string'},
|
138
|
-
{:id => '132', :xl => '', :ostring => nil, :cls => 'string'},
|
139
|
-
{:id => '133', :xl => '', :ostring => nil, :cls => 'string'},
|
140
|
-
{:id => '134', :xl => '', :ostring => nil, :cls => 'string'},
|
141
|
-
{:id => '135', :xl => '', :ostring => nil, :cls => 'string'},
|
142
|
-
{:id => '136', :xl => '', :ostring => nil, :cls => 'string'},
|
143
|
-
{:id => '137', :xl => '', :ostring => nil, :cls => 'string'},
|
144
|
-
{:id => '138', :xl => '', :ostring => nil, :cls => 'string'},
|
145
|
-
{:id => '139', :xl => '', :ostring => nil, :cls => 'string'},
|
146
|
-
{:id => '140', :xl => '', :ostring => nil, :cls => 'string'},
|
147
|
-
{:id => '141', :xl => '', :ostring => nil, :cls => 'string'},
|
148
|
-
{:id => '142', :xl => '', :ostring => nil, :cls => 'string'},
|
149
|
-
{:id => '143', :xl => '', :ostring => nil, :cls => 'string'},
|
150
|
-
{:id => '144', :xl => '', :ostring => nil, :cls => 'string'},
|
151
|
-
{:id => '145', :xl => '', :ostring => nil, :cls => 'string'},
|
152
|
-
{:id => '146', :xl => '', :ostring => nil, :cls => 'string'},
|
153
|
-
{:id => '147', :xl => '', :ostring => nil, :cls => 'string'},
|
154
|
-
{:id => '148', :xl => '', :ostring => nil, :cls => 'string'},
|
155
|
-
{:id => '149', :xl => '', :ostring => nil, :cls => 'string'},
|
156
|
-
{:id => '150', :xl => '', :ostring => nil, :cls => 'string'},
|
157
|
-
{:id => '151', :xl => '', :ostring => nil, :cls => 'string'},
|
158
|
-
{:id => '152', :xl => '', :ostring => nil, :cls => 'string'},
|
159
|
-
{:id => '153', :xl => '', :ostring => nil, :cls => 'string'},
|
160
|
-
{:id => '154', :xl => '', :ostring => nil, :cls => 'string'},
|
161
|
-
{:id => '155', :xl => '', :ostring => nil, :cls => 'string'},
|
162
|
-
{:id => '156', :xl => '', :ostring => nil, :cls => 'string'},
|
163
|
-
{:id => '157', :xl => '', :ostring => nil, :cls => 'string'},
|
164
|
-
{:id => '158', :xl => '', :ostring => nil, :cls => 'string'},
|
165
|
-
{:id => '159', :xl => '', :ostring => nil, :cls => 'string'},
|
166
|
-
{:id => '160', :xl => '', :ostring => nil, :cls => 'string'},
|
167
|
-
{:id => '161', :xl => '', :ostring => nil, :cls => 'string'},
|
168
|
-
{:id => '162', :xl => '', :ostring => nil, :cls => 'string'},
|
169
|
-
{:id => '163', :xl => '', :ostring => nil, :cls => 'string'},
|
170
|
-
]
|
171
|
-
end
|
172
|
-
end
|
1
|
+
module Oxcelix
|
2
|
+
module Numformats
|
3
|
+
# Formatarray is the array of default format strings in Excel. Nil values should apparently contain CJK date format strings,
|
4
|
+
#feel free to add/document those according to existing standards.
|
5
|
+
Formatarray = [
|
6
|
+
{:id => '0', :xl => 'General', :ostring => nil, :cls => 'string'},
|
7
|
+
{:id => '1', :xl => '0', :ostring => '%1d', :cls => 'numeric'},
|
8
|
+
{:id => '2', :xl => '0.00', :ostring => '%1.2f', :cls => 'numeric'},
|
9
|
+
{:id => '3', :xl => '#,##0', :ostring => '%#4d', :cls => 'numeric'},
|
10
|
+
{:id => '4', :xl => '#,##0.00', :ostring => '%#4.2f', :cls => 'numeric'},
|
11
|
+
{:id => '5', :xl => '', :ostring => nil, :cls => 'string'},
|
12
|
+
{:id => '6', :xl => '', :ostring => nil, :cls => 'string'},
|
13
|
+
{:id => '7', :xl => '', :ostring => nil, :cls => 'string'},
|
14
|
+
{:id => '8', :xl => '', :ostring => nil, :cls => 'string'},
|
15
|
+
{:id => '9', :xl => '0%', :ostring => '%1d%', :cls => 'numeric'},
|
16
|
+
{:id => '10', :xl => '0.00%', :ostring => '%1.2f%', :cls => 'numeric'},
|
17
|
+
{:id => '11', :xl => '0.00E+00', :ostring => '%1.2fE+', :cls => 'numeric'},
|
18
|
+
{:id => '12', :xl => '# ?/?', :ostring => '%#1d', :cls => 'rational'},
|
19
|
+
{:id => '13', :xl => '# ??/??', :ostring => '%#1d', :cls => 'rational'},
|
20
|
+
{:id => '14', :xl => 'd/m/yyyy', :ostring => '%-d/%-m/%Y', :cls => 'date'},
|
21
|
+
{:id => '15', :xl => 'd-mmm-yy', :ostring => '%-d-%b-%y', :cls => 'date'},
|
22
|
+
{:id => '16', :xl => 'd-mmm', :ostring => '%-d-%b', :cls => 'date'},
|
23
|
+
{:id => '17', :xl => 'mmm-yy', :ostring => '%b-%y', :cls => 'date'},
|
24
|
+
{:id => '18', :xl => 'h:mm tt', :ostring => '%-k:%M tt', :cls => 'date'},
|
25
|
+
{:id => '19', :xl => 'h:mm:ss tt', :ostring => '%-k:%M:%-S tt', :cls => 'date'},
|
26
|
+
{:id => '20', :xl => 'H:mm', :ostring => '%-k:%M', :cls => 'date'},
|
27
|
+
{:id => '21', :xl => 'H:mm:ss', :ostring => '%-k:%M:%-S', :cls => 'date'},
|
28
|
+
{:id => '22', :xl => 'm/d/yyyy H:mm', :ostring => '%-m/%-d/%Y %-k:%M', :cls => 'date'},
|
29
|
+
{:id => '23', :xl => '', :ostring => nil, :cls => 'string'},
|
30
|
+
{:id => '24', :xl => '', :ostring => nil, :cls => 'string'},
|
31
|
+
{:id => '25', :xl => '', :ostring => nil, :cls => 'string'},
|
32
|
+
{:id => '26', :xl => '', :ostring => nil, :cls => 'string'},
|
33
|
+
{:id => '27', :xl => '', :ostring => nil, :cls => 'string'},
|
34
|
+
{:id => '28', :xl => '', :ostring => nil, :cls => 'string'},
|
35
|
+
{:id => '29', :xl => '', :ostring => nil, :cls => 'string'},
|
36
|
+
{:id => '30', :xl => '', :ostring => nil, :cls => 'string'},
|
37
|
+
{:id => '31', :xl => '', :ostring => nil, :cls => 'string'},
|
38
|
+
{:id => '32', :xl => '', :ostring => nil, :cls => 'string'},
|
39
|
+
{:id => '33', :xl => '', :ostring => nil, :cls => 'string'},
|
40
|
+
{:id => '34', :xl => '', :ostring => nil, :cls => 'string'},
|
41
|
+
{:id => '35', :xl => '', :ostring => nil, :cls => 'string'},
|
42
|
+
{:id => '36', :xl => '', :ostring => nil, :cls => 'string'},
|
43
|
+
{:id => '37', :xl => '#,##0 ;(#,##0)', :ostring => '%#4d', :cls => 'numeric'},
|
44
|
+
{:id => '38', :xl => '#,##0 ;[Red](#,##0)', :ostring => '%#4d', :cls => 'numeric'},
|
45
|
+
{:id => '39', :xl => '#,##0.00;(#,##0.00)', :ostring => '%#4.2f', :cls => 'numeric'},
|
46
|
+
{:id => '40', :xl => '#,##0.00;[Red](#,##0.00)', :ostring => '%#4.2f', :cls => 'numeric'},
|
47
|
+
{:id => '41', :xl => '', :ostring => nil, :cls => 'string'},
|
48
|
+
{:id => '42', :xl => '', :ostring => nil, :cls => 'string'},
|
49
|
+
{:id => '43', :xl => '', :ostring => nil, :cls => 'string'},
|
50
|
+
{:id => '44', :xl => '', :ostring => nil, :cls => 'string'},
|
51
|
+
{:id => '45', :xl => 'mm:ss', :ostring => '%M:%-S', :cls => 'date'},
|
52
|
+
{:id => '46', :xl => '[h]:mm:ss', :ostring => '%-k:%M:%-S', :cls => 'date'},
|
53
|
+
{:id => '47', :xl => 'mmss.0', :ostring => '%M%-S.%1n', :cls => 'date'},
|
54
|
+
{:id => '48', :xl => '##0.0E+0', :ostring => '%#3.1E', :cls => 'numeric'},
|
55
|
+
{:id => '49', :xl => '@,', :ostring => '@%d', :cls => 'numeric'},
|
56
|
+
{:id => '50', :xl => '', :ostring => nil, :cls => 'string'},
|
57
|
+
{:id => '51', :xl => '', :ostring => nil, :cls => 'string'},
|
58
|
+
{:id => '52', :xl => '', :ostring => nil, :cls => 'string'},
|
59
|
+
{:id => '53', :xl => '', :ostring => nil, :cls => 'string'},
|
60
|
+
{:id => '54', :xl => '', :ostring => nil, :cls => 'string'},
|
61
|
+
{:id => '55', :xl => '', :ostring => nil, :cls => 'string'},
|
62
|
+
{:id => '56', :xl => '', :ostring => nil, :cls => 'string'},
|
63
|
+
{:id => '57', :xl => '', :ostring => nil, :cls => 'string'},
|
64
|
+
{:id => '58', :xl => '', :ostring => nil, :cls => 'string'},
|
65
|
+
{:id => '59', :xl => '', :ostring => nil, :cls => 'string'},
|
66
|
+
{:id => '60', :xl => '', :ostring => nil, :cls => 'string'},
|
67
|
+
{:id => '61', :xl => '', :ostring => nil, :cls => 'string'},
|
68
|
+
{:id => '62', :xl => '', :ostring => nil, :cls => 'string'},
|
69
|
+
{:id => '63', :xl => '', :ostring => nil, :cls => 'string'},
|
70
|
+
{:id => '64', :xl => '', :ostring => nil, :cls => 'string'},
|
71
|
+
{:id => '65', :xl => '', :ostring => nil, :cls => 'string'},
|
72
|
+
{:id => '66', :xl => '', :ostring => nil, :cls => 'string'},
|
73
|
+
{:id => '67', :xl => '', :ostring => nil, :cls => 'string'},
|
74
|
+
{:id => '68', :xl => '', :ostring => nil, :cls => 'string'},
|
75
|
+
{:id => '69', :xl => '', :ostring => nil, :cls => 'string'},
|
76
|
+
{:id => '70', :xl => '', :ostring => nil, :cls => 'string'},
|
77
|
+
{:id => '71', :xl => '', :ostring => nil, :cls => 'string'},
|
78
|
+
{:id => '72', :xl => '', :ostring => nil, :cls => 'string'},
|
79
|
+
{:id => '73', :xl => '', :ostring => nil, :cls => 'string'},
|
80
|
+
{:id => '74', :xl => '', :ostring => nil, :cls => 'string'},
|
81
|
+
{:id => '75', :xl => '', :ostring => nil, :cls => 'string'},
|
82
|
+
{:id => '76', :xl => '', :ostring => nil, :cls => 'string'},
|
83
|
+
{:id => '77', :xl => '', :ostring => nil, :cls => 'string'},
|
84
|
+
{:id => '78', :xl => '', :ostring => nil, :cls => 'string'},
|
85
|
+
{:id => '79', :xl => '', :ostring => nil, :cls => 'string'},
|
86
|
+
{:id => '80', :xl => '', :ostring => nil, :cls => 'string'},
|
87
|
+
{:id => '81', :xl => '', :ostring => nil, :cls => 'string'},
|
88
|
+
{:id => '82', :xl => '', :ostring => nil, :cls => 'string'},
|
89
|
+
{:id => '83', :xl => '', :ostring => nil, :cls => 'string'},
|
90
|
+
{:id => '84', :xl => '', :ostring => nil, :cls => 'string'},
|
91
|
+
{:id => '85', :xl => '', :ostring => nil, :cls => 'string'},
|
92
|
+
{:id => '86', :xl => '', :ostring => nil, :cls => 'string'},
|
93
|
+
{:id => '87', :xl => '', :ostring => nil, :cls => 'string'},
|
94
|
+
{:id => '88', :xl => '', :ostring => nil, :cls => 'string'},
|
95
|
+
{:id => '89', :xl => '', :ostring => nil, :cls => 'string'},
|
96
|
+
{:id => '90', :xl => '', :ostring => nil, :cls => 'string'},
|
97
|
+
{:id => '91', :xl => '', :ostring => nil, :cls => 'string'},
|
98
|
+
{:id => '92', :xl => '', :ostring => nil, :cls => 'string'},
|
99
|
+
{:id => '93', :xl => '', :ostring => nil, :cls => 'string'},
|
100
|
+
{:id => '94', :xl => '', :ostring => nil, :cls => 'string'},
|
101
|
+
{:id => '95', :xl => '', :ostring => nil, :cls => 'string'},
|
102
|
+
{:id => '96', :xl => '', :ostring => nil, :cls => 'string'},
|
103
|
+
{:id => '97', :xl => '', :ostring => nil, :cls => 'string'},
|
104
|
+
{:id => '98', :xl => '', :ostring => nil, :cls => 'string'},
|
105
|
+
{:id => '99', :xl => '', :ostring => nil, :cls => 'string'},
|
106
|
+
{:id => '100', :xl => '', :ostring => nil, :cls => 'string'},
|
107
|
+
{:id => '101', :xl => '', :ostring => nil, :cls => 'string'},
|
108
|
+
{:id => '102', :xl => '', :ostring => nil, :cls => 'string'},
|
109
|
+
{:id => '103', :xl => '', :ostring => nil, :cls => 'string'},
|
110
|
+
{:id => '104', :xl => '', :ostring => nil, :cls => 'string'},
|
111
|
+
{:id => '105', :xl => '', :ostring => nil, :cls => 'string'},
|
112
|
+
{:id => '106', :xl => '', :ostring => nil, :cls => 'string'},
|
113
|
+
{:id => '107', :xl => '', :ostring => nil, :cls => 'string'},
|
114
|
+
{:id => '108', :xl => '', :ostring => nil, :cls => 'string'},
|
115
|
+
{:id => '109', :xl => '', :ostring => nil, :cls => 'string'},
|
116
|
+
{:id => '110', :xl => '', :ostring => nil, :cls => 'string'},
|
117
|
+
{:id => '111', :xl => '', :ostring => nil, :cls => 'string'},
|
118
|
+
{:id => '112', :xl => '', :ostring => nil, :cls => 'string'},
|
119
|
+
{:id => '113', :xl => '', :ostring => nil, :cls => 'string'},
|
120
|
+
{:id => '114', :xl => '', :ostring => nil, :cls => 'string'},
|
121
|
+
{:id => '115', :xl => '', :ostring => nil, :cls => 'string'},
|
122
|
+
{:id => '116', :xl => '', :ostring => nil, :cls => 'string'},
|
123
|
+
{:id => '117', :xl => '', :ostring => nil, :cls => 'string'},
|
124
|
+
{:id => '118', :xl => '', :ostring => nil, :cls => 'string'},
|
125
|
+
{:id => '119', :xl => '', :ostring => nil, :cls => 'string'},
|
126
|
+
{:id => '120', :xl => '', :ostring => nil, :cls => 'string'},
|
127
|
+
{:id => '121', :xl => '', :ostring => nil, :cls => 'string'},
|
128
|
+
{:id => '122', :xl => '', :ostring => nil, :cls => 'string'},
|
129
|
+
{:id => '123', :xl => '', :ostring => nil, :cls => 'string'},
|
130
|
+
{:id => '124', :xl => '', :ostring => nil, :cls => 'string'},
|
131
|
+
{:id => '125', :xl => '', :ostring => nil, :cls => 'string'},
|
132
|
+
{:id => '126', :xl => '', :ostring => nil, :cls => 'string'},
|
133
|
+
{:id => '127', :xl => '', :ostring => nil, :cls => 'string'},
|
134
|
+
{:id => '128', :xl => '', :ostring => nil, :cls => 'string'},
|
135
|
+
{:id => '129', :xl => '', :ostring => nil, :cls => 'string'},
|
136
|
+
{:id => '130', :xl => '', :ostring => nil, :cls => 'string'},
|
137
|
+
{:id => '131', :xl => '', :ostring => nil, :cls => 'string'},
|
138
|
+
{:id => '132', :xl => '', :ostring => nil, :cls => 'string'},
|
139
|
+
{:id => '133', :xl => '', :ostring => nil, :cls => 'string'},
|
140
|
+
{:id => '134', :xl => '', :ostring => nil, :cls => 'string'},
|
141
|
+
{:id => '135', :xl => '', :ostring => nil, :cls => 'string'},
|
142
|
+
{:id => '136', :xl => '', :ostring => nil, :cls => 'string'},
|
143
|
+
{:id => '137', :xl => '', :ostring => nil, :cls => 'string'},
|
144
|
+
{:id => '138', :xl => '', :ostring => nil, :cls => 'string'},
|
145
|
+
{:id => '139', :xl => '', :ostring => nil, :cls => 'string'},
|
146
|
+
{:id => '140', :xl => '', :ostring => nil, :cls => 'string'},
|
147
|
+
{:id => '141', :xl => '', :ostring => nil, :cls => 'string'},
|
148
|
+
{:id => '142', :xl => '', :ostring => nil, :cls => 'string'},
|
149
|
+
{:id => '143', :xl => '', :ostring => nil, :cls => 'string'},
|
150
|
+
{:id => '144', :xl => '', :ostring => nil, :cls => 'string'},
|
151
|
+
{:id => '145', :xl => '', :ostring => nil, :cls => 'string'},
|
152
|
+
{:id => '146', :xl => '', :ostring => nil, :cls => 'string'},
|
153
|
+
{:id => '147', :xl => '', :ostring => nil, :cls => 'string'},
|
154
|
+
{:id => '148', :xl => '', :ostring => nil, :cls => 'string'},
|
155
|
+
{:id => '149', :xl => '', :ostring => nil, :cls => 'string'},
|
156
|
+
{:id => '150', :xl => '', :ostring => nil, :cls => 'string'},
|
157
|
+
{:id => '151', :xl => '', :ostring => nil, :cls => 'string'},
|
158
|
+
{:id => '152', :xl => '', :ostring => nil, :cls => 'string'},
|
159
|
+
{:id => '153', :xl => '', :ostring => nil, :cls => 'string'},
|
160
|
+
{:id => '154', :xl => '', :ostring => nil, :cls => 'string'},
|
161
|
+
{:id => '155', :xl => '', :ostring => nil, :cls => 'string'},
|
162
|
+
{:id => '156', :xl => '', :ostring => nil, :cls => 'string'},
|
163
|
+
{:id => '157', :xl => '', :ostring => nil, :cls => 'string'},
|
164
|
+
{:id => '158', :xl => '', :ostring => nil, :cls => 'string'},
|
165
|
+
{:id => '159', :xl => '', :ostring => nil, :cls => 'string'},
|
166
|
+
{:id => '160', :xl => '', :ostring => nil, :cls => 'string'},
|
167
|
+
{:id => '161', :xl => '', :ostring => nil, :cls => 'string'},
|
168
|
+
{:id => '162', :xl => '', :ostring => nil, :cls => 'string'},
|
169
|
+
{:id => '163', :xl => '', :ostring => nil, :cls => 'string'},
|
170
|
+
]
|
171
|
+
end
|
172
|
+
end
|
data/lib/oxcelix/numformats.rb
CHANGED
@@ -1,114 +1,114 @@
|
|
1
|
-
module Oxcelix
|
2
|
-
# The Numformats module provides helper methods that either return the Cell object's raw @value as a ruby value
|
3
|
-
# (e.g. Numeric, DateTime, String) or formats it according to the excel _numformat_ string (#Cell.numformat).
|
4
|
-
module Numformats
|
5
|
-
Dtmap = {'hh'=>'%H', 'ii'=>'%M', 'i'=>'%-M', 'H'=>'%-k', 'h'=>'%-k',\
|
6
|
-
'ss'=>'%-S', 's'=>'%S', 'mmmmm'=>'%b', 'mmmm'=>'%B', 'mmm'=>'%b', 'mm'=>'%m', \
|
7
|
-
'm'=>'%-m', 'dddd'=>'%A', 'ddd'=>'%a', 'dd'=>'%d', 'd'=>'%-d', 'yyyy'=>'%Y', \
|
8
|
-
'yy'=>'%y', 'AM/PM'=>'%p', 'A/P'=>'%p', '.0'=>'', 'ss'=>'%-S', 's'=>'%S'}
|
9
|
-
|
10
|
-
# Convert the temporary format array (the collection of non-default number formatting strings defined in the excel sheet in use)
|
11
|
-
# to a series of hashes containing an id, an excel format string, a converted format string and an object class the format is
|
12
|
-
# interpreted on.
|
13
|
-
def add_custom_formats fmtary
|
14
|
-
fmtary.each do |x|
|
15
|
-
if x[:formatCode] =~ /[#0%\?]/
|
16
|
-
ostring = numeric x[:formatCode]
|
17
|
-
if x[:formatCode] =~ /\//
|
18
|
-
cls = 'rational'
|
19
|
-
else
|
20
|
-
cls = 'numeric'
|
21
|
-
end
|
22
|
-
elsif x[:formatCode].downcase =~ /[dmysh]/
|
23
|
-
ostring = datetime x[:formatCode]
|
24
|
-
cls = 'date'
|
25
|
-
elsif x[:formatCode].downcase == "general"
|
26
|
-
ostring = nil
|
27
|
-
cls = 'string'
|
28
|
-
end
|
29
|
-
Formatarray << {:id => x[:numFmtId].to_s, :xl => x[:formatCode].to_s, :ostring => ostring, :cls => cls}
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
# Convert the excel-style number format to a ruby #Kernel::Format string and return that String.
|
34
|
-
# The conversion is internally done by regexp'ing 7 groups: prefix, decimals, separator, floats, exponential (E+)
|
35
|
-
# and postfix. Rational numbers ar not handled yet.
|
36
|
-
# @param [String] val an Excel number format string.
|
37
|
-
# @return [String] a rubyish Kernel::Format string.
|
38
|
-
def numeric val
|
39
|
-
ostring = "%"
|
40
|
-
strippedfmt = val.gsub(/\?/, '0').gsub(',','')
|
41
|
-
prefix, decimals, sep, floats, expo, postfix=/(^[^\#0e].?)?([\#0]*)?(\.)?([\#0]*)?(e.?)?(.?[^\#0e]$)?/i.match(strippedfmt).captures
|
42
|
-
ostring.prepend prefix.to_s
|
43
|
-
if !decimals.nil? && decimals.size != 0
|
44
|
-
if (eval decimals) == nil
|
45
|
-
ostring += "##{decimals.size}"
|
46
|
-
elsif (eval decimals) == 0
|
47
|
-
ostring += decimals.size.to_s
|
48
|
-
end
|
49
|
-
else
|
50
|
-
ostring += decimals
|
51
|
-
end
|
52
|
-
ostring += sep.to_s
|
53
|
-
if !floats.nil? && floats.size != 0 # expo!!!
|
54
|
-
ostring += ((floats.size.to_s) +"f")
|
55
|
-
end
|
56
|
-
if sep.nil? && floats.nil? || floats.size == 0
|
57
|
-
ostring += "d"
|
58
|
-
end
|
59
|
-
ostring += (expo.to_s + postfix.to_s) #postfix '+' ?
|
60
|
-
return ostring
|
61
|
-
end
|
62
|
-
|
63
|
-
# Convert excel-style date formats into ruby DateTime strftime format strings
|
64
|
-
# @param [String] val an Excel number format string.
|
65
|
-
# @return [String] a DateTime::strftime format string.
|
66
|
-
def datetime formatcode
|
67
|
-
deminutified = formatcode.downcase.gsub(/(?<hrs>H|h)(?<div>.)m/, '\k<hrs>\k<div>i')
|
68
|
-
.gsub(/im/, 'ii')
|
69
|
-
.gsub(/m(?<div>.)(?<secs>s)/, 'i\k<div>\k<secs>')
|
70
|
-
.gsub(/mi/, 'ii')
|
71
|
-
return deminutified.gsub(/[yMmDdHhSsi]*/, Dtmap)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
# The Numberhelper module implements methods that return the formatted value or the value converted into a Ruby type (DateTime, Numeric, etc)
|
76
|
-
module Numberhelper
|
77
|
-
include Numformats
|
78
|
-
# Get the cell's value and excel format string and return a string, a ruby Numeric or a DateTime object accordingly
|
79
|
-
# @return [Object] A ruby object that holds and represents the value stored in the cell. Conversion is based on cell formatting.
|
80
|
-
# @example Get the value of a cell:
|
81
|
-
# c = w.sheets[0]["B3"] # => <Oxcelix::Cell:0x00000002a5b368 @xlcoords="A3", @style="84", @type="n", @value="41155", @numformat=14>
|
82
|
-
# c.to_ru # => <DateTime: 2012-09-03T00:00:00+00:00 ((2456174j,0s,0n),+0s,2299161j)>
|
83
|
-
#
|
84
|
-
def to_ru
|
85
|
-
if !@value.numeric? || Numformats::Formatarray[@numformat.to_i][:xl] == nil || Numformats::Formatarray[@numformat.to_i][:xl].downcase == "general"
|
86
|
-
return @value
|
87
|
-
end
|
88
|
-
if Numformats::Formatarray[@numformat.to_i][:cls] == 'date'
|
89
|
-
return DateTime.new(1899, 12, 30) + (eval @value)
|
90
|
-
else Numformats::Formatarray[@numformat.to_i][:cls] == 'numeric' || Numformats::Formatarray[@numformat.to_i][:cls] == 'rational'
|
91
|
-
return eval @value rescue @value
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
# Get the cell's value, convert it with to_ru and finally, format it based on the value's type.
|
96
|
-
# @return [String] Value gets formatted depending on its class. If it is a DateTime, the #DateTime.strftime method is used,
|
97
|
-
# if it holds a number, the #Kernel::sprintf is run.
|
98
|
-
# @example Get the formatted value of a cell:
|
99
|
-
# c = w.sheets[0]["B3"] # => <Oxcelix::Cell:0x00000002a5b368 @xlcoords="A3", @style="84", @type="n", @value="41155", @numformat=14>
|
100
|
-
# c.to_fmt # => "3/9/2012"
|
101
|
-
#
|
102
|
-
def to_fmt
|
103
|
-
begin
|
104
|
-
if Numformats::Formatarray[@numformat][:cls] == 'date'
|
105
|
-
self.to_ru.strftime(Numformats::Formatarray[@numformat][:ostring]) rescue @value
|
106
|
-
elsif Numformats::Formatarray[@numformat.to_i][:cls] == 'numeric' || Numformats::Formatarray[@numformat.to_i][:cls] == 'rational'
|
107
|
-
sprintf(Numformats::Formatarray[@numformat][:ostring], self.to_ru) rescue @value
|
108
|
-
else
|
109
|
-
return @value
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
1
|
+
module Oxcelix
|
2
|
+
# The Numformats module provides helper methods that either return the Cell object's raw @value as a ruby value
|
3
|
+
# (e.g. Numeric, DateTime, String) or formats it according to the excel _numformat_ string (#Cell.numformat).
|
4
|
+
module Numformats
|
5
|
+
Dtmap = {'hh'=>'%H', 'ii'=>'%M', 'i'=>'%-M', 'H'=>'%-k', 'h'=>'%-k',\
|
6
|
+
'ss'=>'%-S', 's'=>'%S', 'mmmmm'=>'%b', 'mmmm'=>'%B', 'mmm'=>'%b', 'mm'=>'%m', \
|
7
|
+
'm'=>'%-m', 'dddd'=>'%A', 'ddd'=>'%a', 'dd'=>'%d', 'd'=>'%-d', 'yyyy'=>'%Y', \
|
8
|
+
'yy'=>'%y', 'AM/PM'=>'%p', 'A/P'=>'%p', '.0'=>'', 'ss'=>'%-S', 's'=>'%S'}
|
9
|
+
|
10
|
+
# Convert the temporary format array (the collection of non-default number formatting strings defined in the excel sheet in use)
|
11
|
+
# to a series of hashes containing an id, an excel format string, a converted format string and an object class the format is
|
12
|
+
# interpreted on.
|
13
|
+
def add_custom_formats fmtary
|
14
|
+
fmtary.each do |x|
|
15
|
+
if x[:formatCode] =~ /[#0%\?]/
|
16
|
+
ostring = numeric x[:formatCode]
|
17
|
+
if x[:formatCode] =~ /\//
|
18
|
+
cls = 'rational'
|
19
|
+
else
|
20
|
+
cls = 'numeric'
|
21
|
+
end
|
22
|
+
elsif x[:formatCode].downcase =~ /[dmysh]/
|
23
|
+
ostring = datetime x[:formatCode]
|
24
|
+
cls = 'date'
|
25
|
+
elsif x[:formatCode].downcase == "general"
|
26
|
+
ostring = nil
|
27
|
+
cls = 'string'
|
28
|
+
end
|
29
|
+
Formatarray << {:id => x[:numFmtId].to_s, :xl => x[:formatCode].to_s, :ostring => ostring, :cls => cls}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Convert the excel-style number format to a ruby #Kernel::Format string and return that String.
|
34
|
+
# The conversion is internally done by regexp'ing 7 groups: prefix, decimals, separator, floats, exponential (E+)
|
35
|
+
# and postfix. Rational numbers ar not handled yet.
|
36
|
+
# @param [String] val an Excel number format string.
|
37
|
+
# @return [String] a rubyish Kernel::Format string.
|
38
|
+
def numeric val
|
39
|
+
ostring = "%"
|
40
|
+
strippedfmt = val.gsub(/\?/, '0').gsub(',','')
|
41
|
+
prefix, decimals, sep, floats, expo, postfix=/(^[^\#0e].?)?([\#0]*)?(\.)?([\#0]*)?(e.?)?(.?[^\#0e]$)?/i.match(strippedfmt).captures
|
42
|
+
ostring.prepend prefix.to_s
|
43
|
+
if !decimals.nil? && decimals.size != 0
|
44
|
+
if (eval decimals) == nil
|
45
|
+
ostring += "##{decimals.size}"
|
46
|
+
elsif (eval decimals) == 0
|
47
|
+
ostring += decimals.size.to_s
|
48
|
+
end
|
49
|
+
else
|
50
|
+
ostring += decimals
|
51
|
+
end
|
52
|
+
ostring += sep.to_s
|
53
|
+
if !floats.nil? && floats.size != 0 # expo!!!
|
54
|
+
ostring += ((floats.size.to_s) +"f")
|
55
|
+
end
|
56
|
+
if sep.nil? && floats.nil? || floats.size == 0
|
57
|
+
ostring += "d"
|
58
|
+
end
|
59
|
+
ostring += (expo.to_s + postfix.to_s) #postfix '+' ?
|
60
|
+
return ostring
|
61
|
+
end
|
62
|
+
|
63
|
+
# Convert excel-style date formats into ruby DateTime strftime format strings
|
64
|
+
# @param [String] val an Excel number format string.
|
65
|
+
# @return [String] a DateTime::strftime format string.
|
66
|
+
def datetime formatcode
|
67
|
+
deminutified = formatcode.downcase.gsub(/(?<hrs>H|h)(?<div>.)m/, '\k<hrs>\k<div>i')
|
68
|
+
.gsub(/im/, 'ii')
|
69
|
+
.gsub(/m(?<div>.)(?<secs>s)/, 'i\k<div>\k<secs>')
|
70
|
+
.gsub(/mi/, 'ii')
|
71
|
+
return deminutified.gsub(/[yMmDdHhSsi]*/, Dtmap)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# The Numberhelper module implements methods that return the formatted value or the value converted into a Ruby type (DateTime, Numeric, etc)
|
76
|
+
module Numberhelper
|
77
|
+
include Numformats
|
78
|
+
# Get the cell's value and excel format string and return a string, a ruby Numeric or a DateTime object accordingly
|
79
|
+
# @return [Object] A ruby object that holds and represents the value stored in the cell. Conversion is based on cell formatting.
|
80
|
+
# @example Get the value of a cell:
|
81
|
+
# c = w.sheets[0]["B3"] # => <Oxcelix::Cell:0x00000002a5b368 @xlcoords="A3", @style="84", @type="n", @value="41155", @numformat=14>
|
82
|
+
# c.to_ru # => <DateTime: 2012-09-03T00:00:00+00:00 ((2456174j,0s,0n),+0s,2299161j)>
|
83
|
+
#
|
84
|
+
def to_ru
|
85
|
+
if !@value.numeric? || Numformats::Formatarray[@numformat.to_i][:xl] == nil || Numformats::Formatarray[@numformat.to_i][:xl].downcase == "general"
|
86
|
+
return @value
|
87
|
+
end
|
88
|
+
if Numformats::Formatarray[@numformat.to_i][:cls] == 'date'
|
89
|
+
return DateTime.new(1899, 12, 30) + (eval @value)
|
90
|
+
else Numformats::Formatarray[@numformat.to_i][:cls] == 'numeric' || Numformats::Formatarray[@numformat.to_i][:cls] == 'rational'
|
91
|
+
return eval @value rescue @value
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Get the cell's value, convert it with to_ru and finally, format it based on the value's type.
|
96
|
+
# @return [String] Value gets formatted depending on its class. If it is a DateTime, the #DateTime.strftime method is used,
|
97
|
+
# if it holds a number, the #Kernel::sprintf is run.
|
98
|
+
# @example Get the formatted value of a cell:
|
99
|
+
# c = w.sheets[0]["B3"] # => <Oxcelix::Cell:0x00000002a5b368 @xlcoords="A3", @style="84", @type="n", @value="41155", @numformat=14>
|
100
|
+
# c.to_fmt # => "3/9/2012"
|
101
|
+
#
|
102
|
+
def to_fmt
|
103
|
+
begin
|
104
|
+
if Numformats::Formatarray[@numformat][:cls] == 'date'
|
105
|
+
self.to_ru.strftime(Numformats::Formatarray[@numformat][:ostring]) rescue @value
|
106
|
+
elsif Numformats::Formatarray[@numformat.to_i][:cls] == 'numeric' || Numformats::Formatarray[@numformat.to_i][:cls] == 'rational'
|
107
|
+
sprintf(Numformats::Formatarray[@numformat][:ostring], self.to_ru) rescue @value
|
108
|
+
else
|
109
|
+
return @value
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|