writeexcel 0.1.0 → 0.3.0

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