writeexcel 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +26 -31
- data/examples/a_simple.rb +42 -42
- data/examples/{autofilters.rb → autofilter.rb} +264 -266
- data/examples/bigfile.rb +29 -0
- data/examples/chart_area.rb +120 -0
- data/examples/chart_bar.rb +119 -0
- data/examples/chart_column.rb +119 -0
- data/examples/chart_line.rb +119 -0
- data/examples/chart_pie.rb +107 -0
- data/examples/chart_scatter.rb +120 -0
- data/examples/chart_stock.rb +147 -0
- data/examples/copyformat.rb +51 -51
- data/examples/data_validate.rb +278 -278
- data/examples/date_time.rb +86 -86
- data/examples/defined_name.rb +31 -0
- data/examples/demo.rb +120 -118
- data/examples/diag_border.rb +35 -35
- data/examples/formats.rb +489 -489
- data/examples/header.rb +136 -136
- data/examples/hidden.rb +28 -28
- data/examples/hyperlink.rb +42 -42
- data/examples/images.rb +52 -52
- data/examples/merge1.rb +39 -39
- data/examples/merge2.rb +44 -44
- data/examples/merge3.rb +65 -65
- data/examples/merge4.rb +82 -82
- data/examples/merge5.rb +79 -79
- data/examples/properties.rb +33 -0
- data/examples/properties_jp.rb +32 -0
- data/examples/protection.rb +46 -46
- data/examples/regions.rb +52 -52
- data/examples/repeat.rb +42 -42
- data/examples/stats.rb +75 -75
- data/examples/stocks.rb +80 -80
- data/examples/tab_colors.rb +30 -30
- data/examples/write_arrays.rb +82 -0
- data/lib/writeexcel.rb +1134 -18
- data/lib/writeexcel/biffwriter.rb +273 -260
- data/lib/writeexcel/chart.rb +2306 -217
- data/lib/writeexcel/charts/area.rb +152 -0
- data/lib/writeexcel/charts/bar.rb +177 -0
- data/lib/writeexcel/charts/column.rb +156 -0
- data/lib/writeexcel/charts/external.rb +61 -0
- data/lib/writeexcel/charts/line.rb +152 -0
- data/lib/writeexcel/charts/pie.rb +169 -0
- data/lib/writeexcel/charts/scatter.rb +192 -0
- data/lib/writeexcel/charts/stock.rb +211 -0
- data/lib/writeexcel/excelformulaparser.rb +208 -195
- data/lib/writeexcel/format.rb +1697 -1108
- data/lib/writeexcel/formula.rb +1050 -986
- data/lib/writeexcel/olewriter.rb +322 -322
- data/lib/writeexcel/properties.rb +251 -250
- data/lib/writeexcel/storage_lite.rb +968 -0
- data/lib/writeexcel/workbook.rb +3294 -2630
- data/lib/writeexcel/worksheet.rb +9012 -6377
- data/test/excelfile/Chart1.xls +0 -0
- data/test/excelfile/Chart2.xls +0 -0
- data/test/excelfile/Chart3.xls +0 -0
- data/test/excelfile/Chart4.xls +0 -0
- data/test/excelfile/Chart5.xls +0 -0
- data/test/perl_output/Chart1.xls.data +0 -0
- data/test/perl_output/Chart2.xls.data +0 -0
- data/test/perl_output/Chart3.xls.data +0 -0
- data/test/perl_output/Chart4.xls.data +0 -0
- data/test/perl_output/Chart5.xls.data +0 -0
- data/test/perl_output/a_simple.xls +0 -0
- data/test/perl_output/autofilter.xls +0 -0
- data/test/perl_output/chart_area.xls +0 -0
- data/test/perl_output/chart_bar.xls +0 -0
- data/test/perl_output/chart_column.xls +0 -0
- data/test/perl_output/chart_line.xls +0 -0
- data/test/perl_output/data_validate.xls +0 -0
- data/test/perl_output/date_time.xls +0 -0
- data/test/perl_output/demo.xls +0 -0
- data/test/perl_output/demo101.bin +0 -0
- data/test/perl_output/demo201.bin +0 -0
- data/test/perl_output/demo301.bin +0 -0
- data/test/perl_output/demo401.bin +0 -0
- data/test/perl_output/demo501.bin +0 -0
- data/test/perl_output/diag_border.xls +0 -0
- data/test/perl_output/headers.xls +0 -0
- data/test/perl_output/hyperlink.xls +0 -0
- data/test/perl_output/images.xls +0 -0
- data/test/perl_output/merge1.xls +0 -0
- data/test/perl_output/merge2.xls +0 -0
- data/test/perl_output/merge3.xls +0 -0
- data/test/perl_output/merge4.xls +0 -0
- data/test/perl_output/merge5.xls +0 -0
- data/test/perl_output/protection.xls +0 -0
- data/test/perl_output/regions.xls +0 -0
- data/test/perl_output/stats.xls +0 -0
- data/test/perl_output/stocks.xls +0 -0
- data/test/perl_output/tab_colors.xls +0 -0
- data/test/perl_output/unicode_cyrillic.xls +0 -0
- data/test/perl_output/workbook1.xls +0 -0
- data/test/perl_output/workbook2.xls +0 -0
- data/test/tc_all.rb +32 -31
- data/test/tc_biff.rb +104 -104
- data/test/tc_chart.rb +22 -22
- data/test/tc_example_match.rb +1944 -1280
- data/test/tc_format.rb +1254 -1267
- data/test/tc_formula.rb +63 -63
- data/test/tc_ole.rb +110 -110
- data/test/tc_storage_lite.rb +149 -0
- data/test/tc_workbook.rb +140 -115
- data/test/tc_worksheet.rb +115 -115
- data/test/test_00_IEEE_double.rb +14 -14
- data/test/test_01_add_worksheet.rb +12 -12
- data/test/test_02_merge_formats.rb +58 -58
- data/test/test_04_dimensions.rb +397 -397
- data/test/test_05_rows.rb +182 -182
- data/test/test_06_extsst.rb +80 -80
- data/test/test_11_date_time.rb +484 -484
- data/test/test_12_date_only.rb +506 -506
- data/test/test_13_date_seconds.rb +486 -486
- data/test/test_21_escher.rb +642 -629
- data/test/test_22_mso_drawing_group.rb +750 -739
- data/test/test_23_note.rb +78 -78
- data/test/test_24_txo.rb +80 -80
- data/test/test_25_position_object.rb +82 -0
- data/test/test_26_autofilter.rb +327 -327
- data/test/test_27_autofilter.rb +144 -144
- data/test/test_28_autofilter.rb +174 -174
- data/test/test_29_process_jpg.rb +681 -131
- data/test/test_30_validation_dval.rb +82 -82
- data/test/test_31_validation_dv_strings.rb +131 -131
- data/test/test_32_validation_dv_formula.rb +211 -211
- data/test/test_40_property_types.rb +191 -191
- data/test/test_41_properties.rb +238 -238
- data/test/test_42_set_properties.rb +442 -419
- data/test/test_50_name_stored.rb +305 -0
- data/test/test_51_name_print_area.rb +363 -0
- data/test/test_52_name_print_titles.rb +460 -0
- data/test/test_53_autofilter.rb +209 -0
- data/test/test_60_chart_generic.rb +576 -0
- data/test/test_61_chart_subclasses.rb +97 -0
- data/test/test_62_chart_formats.rb +270 -0
- data/test/test_63_chart_area_formats.rb +647 -0
- data/test/test_chartex.rb +35 -0
- data/test/ts_all.rb +46 -34
- data/writeexcel.gemspec +18 -0
- data/writeexcel.rdoc +583 -0
- metadata +162 -108
data/README
CHANGED
@@ -2,20 +2,23 @@
|
|
2
2
|
|
3
3
|
Write to a cross-platform Excel binary file.
|
4
4
|
|
5
|
-
==
|
5
|
+
== Recent Changes
|
6
|
+
|
7
|
+
0.3.0
|
8
|
+
* Chart support(Area, Bar, Column, Line, Pie, Scatter, Stock).
|
9
|
+
* Document added. See rdoc in your installed gem directory.
|
10
|
+
* Change Class name from 'Spreadsheet::WriteExcel' to 'WriteExcel'.
|
11
|
+
* many bug fix.
|
6
12
|
|
7
|
-
|
8
|
-
http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel-2.25/
|
13
|
+
== Description
|
9
14
|
|
10
|
-
|
11
|
-
|
12
|
-
* output is file only. not to a memory.
|
13
|
-
* file property of Excel(such as author, time, etc.) is not supported.
|
15
|
+
This library is converted from Spreadsheet::WriteExcel module of Perl.
|
16
|
+
http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel-2.37/
|
14
17
|
|
15
|
-
|
16
|
-
|
18
|
+
Some examples written in perl was successfully converted to Ruby. But
|
19
|
+
this library written in Ruby has many bugs, I think.
|
17
20
|
|
18
|
-
|
21
|
+
Original description is below:
|
19
22
|
|
20
23
|
The Spreadsheet::WriteExcel module can be used to create a cross-
|
21
24
|
platform Excel binary file. Multiple worksheets can be added to a
|
@@ -23,25 +26,19 @@
|
|
23
26
|
formulas, hyperlinks and images can be written to the cells.
|
24
27
|
|
25
28
|
TThe Excel file produced by this module is compatible with 97,
|
26
|
-
2000, 2002 and
|
29
|
+
2000, 2002, 2003 and 2007.
|
27
30
|
|
28
31
|
The module will work on the majority of Windows, UNIX and
|
29
32
|
Macintosh platforms. Generated files are also compatible with the
|
30
33
|
spreadsheet applications Gnumeric and OpenOffice.org.
|
31
34
|
|
32
|
-
This module cannot be used to read an Excel file.
|
33
|
-
Spreadsheet::ParseExcel or look at the main documentation for some
|
34
|
-
suggestions. This module cannot be uses to write to an existing
|
35
|
-
Excel file.
|
36
|
-
|
35
|
+
This module cannot be used to read an Excel file.
|
37
36
|
|
38
37
|
== Usage
|
39
38
|
|
40
|
-
|
41
|
-
reference of methods, see original document.
|
42
|
-
http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel-2.25/lib/Spreadsheet/WriteExcel.pm
|
39
|
+
See rdoc's WriteExcel class document.
|
43
40
|
|
44
|
-
|
41
|
+
Example Code:
|
45
42
|
|
46
43
|
require 'writeexcel'
|
47
44
|
|
@@ -71,7 +68,7 @@
|
|
71
68
|
|
72
69
|
== Difference with Perl module
|
73
70
|
|
74
|
-
*
|
71
|
+
* WriteExcel.new()
|
75
72
|
* accept default format parameter such as new('foo.xls', :font => 'Roman', :size => 12)
|
76
73
|
* Worksheet.write(row, col, token, format)
|
77
74
|
* if token.kind_of?(Numeric) then call write_number, if token.kind_of?(String) then not call write_number().
|
@@ -81,23 +78,21 @@
|
|
81
78
|
|
82
79
|
== Caution
|
83
80
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
81
|
+
This library can handle multibyte character written in utf8. I tested in Japanese,
|
82
|
+
and it looks work well. But, in this version, utf8 or not is recognized
|
83
|
+
by matching non-ascii characters because ruby 1.8 can not recognize character code
|
84
|
+
correctly.
|
88
85
|
|
89
86
|
== Dependencies
|
90
87
|
|
91
|
-
|
92
|
-
* ruby-ole (you can install via rubygem)
|
88
|
+
* ruby 1.8
|
93
89
|
|
94
90
|
== Author
|
95
91
|
|
96
|
-
|
97
|
-
|
98
|
-
Convert to ruby by Hideo Nakamura (cxn03651@msj.biglobe.ne.jp)
|
92
|
+
Original was written in Perl by John McNamara (jmcnamara@cpan.org).
|
99
93
|
|
94
|
+
Convert to ruby by Hideo Nakamura (cxn03651@msj.biglobe.ne.jp)
|
100
95
|
|
101
96
|
== License
|
102
97
|
|
103
|
-
|
98
|
+
same as Ruby.
|
data/examples/a_simple.rb
CHANGED
@@ -1,42 +1,42 @@
|
|
1
|
-
#!/usr/bin/ruby -w
|
2
|
-
#
|
3
|
-
# Example of how to use the WriteExcel module to write text and numbers
|
4
|
-
# to an Excel binary file.
|
5
|
-
#
|
6
|
-
# reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
|
7
|
-
#
|
8
|
-
# original written in Perl by John McNamara
|
9
|
-
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
10
|
-
#
|
11
|
-
require 'rubygems'
|
12
|
-
require 'writeexcel'
|
13
|
-
|
14
|
-
# Create a new workbook called simple.xls and add a worksheet
|
15
|
-
workbook =
|
16
|
-
worksheet = workbook.add_worksheet
|
17
|
-
|
18
|
-
# The general syntax is write(row, column, token). Note that row and
|
19
|
-
# column are zero indexed
|
20
|
-
#
|
21
|
-
|
22
|
-
# Write some text
|
23
|
-
worksheet.write(0, 0, "Hi Excel!")
|
24
|
-
|
25
|
-
|
26
|
-
# Write some numbers
|
27
|
-
worksheet.write(2, 0, 3) # Writes 3
|
28
|
-
worksheet.write(3, 0, 3.00000) # Writes 3
|
29
|
-
worksheet.write(4, 0, 3.00001) # Writes 3.00001
|
30
|
-
worksheet.write(5, 0, 3.14159) # TeX revision no.?
|
31
|
-
|
32
|
-
|
33
|
-
# Write some formulas
|
34
|
-
worksheet.write(7, 0, '=A3 + A6')
|
35
|
-
worksheet.write(8, 0, '=IF(A5>3,"Yes", "No")')
|
36
|
-
|
37
|
-
|
38
|
-
# Write a hyperlink
|
39
|
-
worksheet.write(10, 0, 'http://www.perl.com/')
|
40
|
-
|
41
|
-
# File save
|
42
|
-
workbook.close
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
#
|
3
|
+
# Example of how to use the WriteExcel module to write text and numbers
|
4
|
+
# to an Excel binary file.
|
5
|
+
#
|
6
|
+
# reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
|
7
|
+
#
|
8
|
+
# original written in Perl by John McNamara
|
9
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
10
|
+
#
|
11
|
+
require 'rubygems'
|
12
|
+
require 'writeexcel'
|
13
|
+
|
14
|
+
# Create a new workbook called simple.xls and add a worksheet
|
15
|
+
workbook = WriteExcel.new('a_simple.xls');
|
16
|
+
worksheet = workbook.add_worksheet
|
17
|
+
|
18
|
+
# The general syntax is write(row, column, token). Note that row and
|
19
|
+
# column are zero indexed
|
20
|
+
#
|
21
|
+
|
22
|
+
# Write some text
|
23
|
+
worksheet.write(0, 0, "Hi Excel!")
|
24
|
+
|
25
|
+
|
26
|
+
# Write some numbers
|
27
|
+
worksheet.write(2, 0, 3) # Writes 3
|
28
|
+
worksheet.write(3, 0, 3.00000) # Writes 3
|
29
|
+
worksheet.write(4, 0, 3.00001) # Writes 3.00001
|
30
|
+
worksheet.write(5, 0, 3.14159) # TeX revision no.?
|
31
|
+
|
32
|
+
|
33
|
+
# Write some formulas
|
34
|
+
worksheet.write(7, 0, '=A3 + A6')
|
35
|
+
worksheet.write(8, 0, '=IF(A5>3,"Yes", "No")')
|
36
|
+
|
37
|
+
|
38
|
+
# Write a hyperlink
|
39
|
+
worksheet.write(10, 0, 'http://www.perl.com/')
|
40
|
+
|
41
|
+
# File save
|
42
|
+
workbook.close
|
@@ -1,266 +1,264 @@
|
|
1
|
-
#!/usr/bin/ruby -w
|
2
|
-
|
3
|
-
#######################################################################
|
4
|
-
#
|
5
|
-
# Example of how to create autofilters with
|
6
|
-
#
|
7
|
-
# reverse('©'), September 2007, John McNamara, jmcnamara@cpan.org
|
8
|
-
#
|
9
|
-
# original written in Perl by John McNamara
|
10
|
-
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
11
|
-
#
|
12
|
-
require '
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
['East', 'Apple',
|
18
|
-
['
|
19
|
-
['
|
20
|
-
['
|
21
|
-
['
|
22
|
-
['
|
23
|
-
['
|
24
|
-
['West', '
|
25
|
-
['
|
26
|
-
['
|
27
|
-
['
|
28
|
-
['
|
29
|
-
['
|
30
|
-
['
|
31
|
-
['
|
32
|
-
['
|
33
|
-
['
|
34
|
-
['
|
35
|
-
['
|
36
|
-
['
|
37
|
-
['
|
38
|
-
['
|
39
|
-
['North', '
|
40
|
-
['North', '
|
41
|
-
['
|
42
|
-
['
|
43
|
-
['
|
44
|
-
['
|
45
|
-
['
|
46
|
-
['
|
47
|
-
['East', 'Orange',
|
48
|
-
['
|
49
|
-
['
|
50
|
-
['
|
51
|
-
['
|
52
|
-
['
|
53
|
-
['
|
54
|
-
['
|
55
|
-
['
|
56
|
-
['
|
57
|
-
['
|
58
|
-
['
|
59
|
-
['
|
60
|
-
['
|
61
|
-
['
|
62
|
-
['
|
63
|
-
['
|
64
|
-
['
|
65
|
-
['
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
#
|
72
|
-
#
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
worksheet.
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
#
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
#
|
110
|
-
#
|
111
|
-
|
112
|
-
#
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
#
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
#
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
region
|
129
|
-
|
130
|
-
|
131
|
-
#
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
#
|
144
|
-
#
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
worksheet3.
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
#
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
region
|
159
|
-
|
160
|
-
|
161
|
-
#
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
#
|
174
|
-
#
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
worksheet4.
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
#
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
volume
|
191
|
-
|
192
|
-
|
193
|
-
#
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
#
|
206
|
-
#
|
207
|
-
|
208
|
-
#
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
#
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
region
|
223
|
-
|
224
|
-
|
225
|
-
#
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
#
|
238
|
-
#
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
#
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
region
|
252
|
-
|
253
|
-
|
254
|
-
#
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
|
3
|
+
#######################################################################
|
4
|
+
#
|
5
|
+
# Example of how to create autofilters with WriteExcel.
|
6
|
+
#
|
7
|
+
# reverse('©'), September 2007, John McNamara, jmcnamara@cpan.org
|
8
|
+
#
|
9
|
+
# original written in Perl by John McNamara
|
10
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
11
|
+
#
|
12
|
+
require 'writeexcel'
|
13
|
+
|
14
|
+
def get_data
|
15
|
+
[
|
16
|
+
['East', 'Apple', 9000, 'July'],
|
17
|
+
['East', 'Apple', 5000, 'July'],
|
18
|
+
['South', 'Orange', 9000, 'September'],
|
19
|
+
['North', 'Apple', 2000, 'November'],
|
20
|
+
['West', 'Apple', 9000, 'November'],
|
21
|
+
['South', 'Pear', 7000, 'October'],
|
22
|
+
['North', 'Pear', 9000, 'August'],
|
23
|
+
['West', 'Orange', 1000, 'December'],
|
24
|
+
['West', 'Grape', 1000, 'November'],
|
25
|
+
['South', 'Pear', 10000, 'April'],
|
26
|
+
['West', 'Grape', 6000, 'January'],
|
27
|
+
['South', 'Orange', 3000, 'May'],
|
28
|
+
['North', 'Apple', 3000, 'December'],
|
29
|
+
['South', 'Apple', 7000, 'February'],
|
30
|
+
['West', 'Grape', 1000, 'December'],
|
31
|
+
['East', 'Grape', 8000, 'February'],
|
32
|
+
['South', 'Grape', 10000, 'June'],
|
33
|
+
['West', 'Pear', 7000, 'December'],
|
34
|
+
['South', 'Apple', 2000, 'October'],
|
35
|
+
['East', 'Grape', 7000, 'December'],
|
36
|
+
['North', 'Grape', 6000, 'April'],
|
37
|
+
['East', 'Pear', 8000, 'February'],
|
38
|
+
['North', 'Apple', 7000, 'August'],
|
39
|
+
['North', 'Orange', 7000, 'July'],
|
40
|
+
['North', 'Apple', 6000, 'June'],
|
41
|
+
['South', 'Grape', 8000, 'September'],
|
42
|
+
['West', 'Apple', 3000, 'October'],
|
43
|
+
['South', 'Orange', 10000, 'November'],
|
44
|
+
['West', 'Grape', 4000, 'July'],
|
45
|
+
['North', 'Orange', 5000, 'August'],
|
46
|
+
['East', 'Orange', 1000, 'November'],
|
47
|
+
['East', 'Orange', 4000, 'October'],
|
48
|
+
['North', 'Grape', 5000, 'August'],
|
49
|
+
['East', 'Apple', 1000, 'December'],
|
50
|
+
['South', 'Apple', 10000, 'March'],
|
51
|
+
['East', 'Grape', 7000, 'October'],
|
52
|
+
['West', 'Grape', 1000, 'September'],
|
53
|
+
['East', 'Grape', 10000, 'October'],
|
54
|
+
['South', 'Orange', 8000, 'March'],
|
55
|
+
['North', 'Apple', 4000, 'July'],
|
56
|
+
['South', 'Orange', 5000, 'July'],
|
57
|
+
['West', 'Apple', 4000, 'June'],
|
58
|
+
['East', 'Apple', 5000, 'April'],
|
59
|
+
['North', 'Pear', 3000, 'August'],
|
60
|
+
['East', 'Grape', 9000, 'November'],
|
61
|
+
['North', 'Orange', 8000, 'October'],
|
62
|
+
['East', 'Apple', 10000, 'June'],
|
63
|
+
['South', 'Pear', 1000, 'December'],
|
64
|
+
['North', 'Grape', 10000, 'July'],
|
65
|
+
['East', 'Grape', 6000, 'February'],
|
66
|
+
]
|
67
|
+
end
|
68
|
+
|
69
|
+
#######################################################################
|
70
|
+
#
|
71
|
+
# Main
|
72
|
+
#
|
73
|
+
|
74
|
+
xlsfile = 'autofilter.xls'
|
75
|
+
|
76
|
+
workbook = WriteExcel.new(xlsfile)
|
77
|
+
|
78
|
+
worksheet1 = workbook.add_worksheet
|
79
|
+
worksheet2 = workbook.add_worksheet
|
80
|
+
worksheet3 = workbook.add_worksheet
|
81
|
+
worksheet4 = workbook.add_worksheet
|
82
|
+
worksheet5 = workbook.add_worksheet
|
83
|
+
worksheet6 = workbook.add_worksheet
|
84
|
+
|
85
|
+
bold = workbook.add_format(:bold => 1)
|
86
|
+
|
87
|
+
# Extract the data embedded at the end of this file.
|
88
|
+
headings = %w(Region Item Volume Month)
|
89
|
+
data = get_data
|
90
|
+
|
91
|
+
# Set up several sheets with the same data.
|
92
|
+
workbook.sheets.each do |worksheet|
|
93
|
+
worksheet.set_column('A:D', 12)
|
94
|
+
worksheet.set_row(0, 20, bold)
|
95
|
+
worksheet.write('A1', headings)
|
96
|
+
end
|
97
|
+
|
98
|
+
###############################################################################
|
99
|
+
#
|
100
|
+
# Example 1. Autofilter without conditions.
|
101
|
+
#
|
102
|
+
|
103
|
+
worksheet1.autofilter('A1:D51')
|
104
|
+
worksheet1.write('A2', [data])
|
105
|
+
|
106
|
+
###############################################################################
|
107
|
+
#
|
108
|
+
#
|
109
|
+
# Example 2. Autofilter with a filter condition in the first column.
|
110
|
+
#
|
111
|
+
|
112
|
+
# The range in this example is the same as above but in row-column notation.
|
113
|
+
worksheet2.autofilter(0, 0, 50, 3)
|
114
|
+
|
115
|
+
# The placeholder "Region" in the filter is ignored and can be any string
|
116
|
+
# that adds clarity to the expression.
|
117
|
+
#
|
118
|
+
worksheet2.filter_column(0, 'Region eq East')
|
119
|
+
|
120
|
+
#
|
121
|
+
# Hide the rows that don't match the filter criteria.
|
122
|
+
#
|
123
|
+
row = 1
|
124
|
+
|
125
|
+
data.each do |row_data|
|
126
|
+
region = row_data[0]
|
127
|
+
|
128
|
+
if region == 'East'
|
129
|
+
# Row is visible.
|
130
|
+
else
|
131
|
+
# Hide row.
|
132
|
+
worksheet2.set_row(row, nil, nil, 1)
|
133
|
+
end
|
134
|
+
|
135
|
+
worksheet2.write(row, 0, row_data)
|
136
|
+
row += 1
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
###############################################################################
|
141
|
+
#
|
142
|
+
#
|
143
|
+
# Example 3. Autofilter with a dual filter condition in one of the columns.
|
144
|
+
#
|
145
|
+
|
146
|
+
worksheet3.autofilter('A1:D51')
|
147
|
+
|
148
|
+
worksheet3.filter_column('A', 'x eq East or x eq South')
|
149
|
+
|
150
|
+
#
|
151
|
+
# Hide the rows that don't match the filter criteria.
|
152
|
+
#
|
153
|
+
row = 1
|
154
|
+
|
155
|
+
data.each do |row_data|
|
156
|
+
region = row_data[0]
|
157
|
+
|
158
|
+
if region == 'East' || region == 'South'
|
159
|
+
# Row is visible.
|
160
|
+
else
|
161
|
+
# Hide row.
|
162
|
+
worksheet3.set_row(row, nil, nil, 1)
|
163
|
+
end
|
164
|
+
|
165
|
+
worksheet3.write(row, 0, row_data)
|
166
|
+
row += 1
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
###############################################################################
|
171
|
+
#
|
172
|
+
#
|
173
|
+
# Example 4. Autofilter with filter conditions in two columns.
|
174
|
+
#
|
175
|
+
|
176
|
+
worksheet4.autofilter('A1:D51')
|
177
|
+
|
178
|
+
worksheet4.filter_column('A', 'x eq East')
|
179
|
+
worksheet4.filter_column('C', 'x > 3000 and x < 8000' )
|
180
|
+
|
181
|
+
#
|
182
|
+
# Hide the rows that don't match the filter criteria.
|
183
|
+
#
|
184
|
+
row = 1
|
185
|
+
|
186
|
+
data.each do |row_data|
|
187
|
+
region = row_data[0]
|
188
|
+
volume = row_data[2]
|
189
|
+
|
190
|
+
if region == 'East' && volume > 3000 && volume < 8000
|
191
|
+
# Row is visible.
|
192
|
+
else
|
193
|
+
# Hide row.
|
194
|
+
worksheet4.set_row(row, nil, nil, 1)
|
195
|
+
end
|
196
|
+
|
197
|
+
worksheet4.write(row, 0, row_data)
|
198
|
+
row += 1
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
###############################################################################
|
203
|
+
#
|
204
|
+
#
|
205
|
+
# Example 5. Autofilter with filter for blanks.
|
206
|
+
#
|
207
|
+
|
208
|
+
# Create a blank cell in our test data.
|
209
|
+
data[5][0] = ''
|
210
|
+
|
211
|
+
worksheet5.autofilter('A1:D51')
|
212
|
+
worksheet5.filter_column('A', 'x == Blanks')
|
213
|
+
|
214
|
+
#
|
215
|
+
# Hide the rows that don't match the filter criteria.
|
216
|
+
#
|
217
|
+
row = 1
|
218
|
+
|
219
|
+
data.each do |row_data|
|
220
|
+
region = row_data[0]
|
221
|
+
|
222
|
+
if region == ''
|
223
|
+
# Row is visible.
|
224
|
+
else
|
225
|
+
# Hide row.
|
226
|
+
worksheet5.set_row(row, nil, nil, 1)
|
227
|
+
end
|
228
|
+
|
229
|
+
worksheet5.write(row, 0, row_data)
|
230
|
+
row += 1
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
###############################################################################
|
235
|
+
#
|
236
|
+
#
|
237
|
+
# Example 6. Autofilter with filter for non-blanks.
|
238
|
+
#
|
239
|
+
|
240
|
+
worksheet6.autofilter('A1:D51')
|
241
|
+
worksheet6.filter_column('A', 'x == NonBlanks')
|
242
|
+
|
243
|
+
#
|
244
|
+
# Hide the rows that don't match the filter criteria.
|
245
|
+
#
|
246
|
+
row = 1
|
247
|
+
|
248
|
+
data.each do |row_data|
|
249
|
+
region = row_data[0]
|
250
|
+
|
251
|
+
if region != ''
|
252
|
+
# Row is visible.
|
253
|
+
else
|
254
|
+
# Hide row.
|
255
|
+
worksheet6.set_row(row, nil, nil, 1)
|
256
|
+
end
|
257
|
+
|
258
|
+
worksheet6.write(row, 0, row_data)
|
259
|
+
row += 1
|
260
|
+
end
|
261
|
+
|
262
|
+
workbook.close
|
263
|
+
|
264
|
+
|