/
autofilter.pl
270 lines (213 loc) · 6.55 KB
/
autofilter.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/perl -w
###############################################################################
#
# An example of how to create autofilters with Spreadsheet::WriteExcel.
#
# An autofilter is a way of adding drop down lists to the headers of a 2D range
# of worksheet data. This is turn allow users to filter the data based on
# simple criteria so that some data is shown and some is hidden.
#
# reverse('©'), September 2007, John McNamara, jmcnamara@cpan.org
#
use strict;
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new('autofilter.xls');
die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
my $worksheet1 = $workbook->add_worksheet();
my $worksheet2 = $workbook->add_worksheet();
my $worksheet3 = $workbook->add_worksheet();
my $worksheet4 = $workbook->add_worksheet();
my $worksheet5 = $workbook->add_worksheet();
my $worksheet6 = $workbook->add_worksheet();
my $bold = $workbook->add_format(bold => 1);
# Extract the data embedded at the end of this file.
my @headings = split ' ', <DATA>;
my @data;
push @data, [split] while <DATA>;
# Set up several sheets with the same data.
for my $worksheet ($workbook->sheets()) {
$worksheet->set_column('A:D', 12);
$worksheet->set_row(0, 20, $bold);
$worksheet->write('A1', \@headings);
}
###############################################################################
#
# Example 1. Autofilter without conditions.
#
$worksheet1->autofilter('A1:D51');
$worksheet1->write('A2', [[@data]]);
###############################################################################
#
#
# Example 2. Autofilter with a filter condition in the first column.
#
# The range in this example is the same as above but in row-column notation.
$worksheet2->autofilter(0, 0, 50, 3);
# The placeholder "Region" in the filter is ignored and can be any string
# that adds clarity to the expression.
#
$worksheet2->filter_column(0, 'Region eq East');
#
# Hide the rows that don't match the filter criteria.
#
my $row = 1;
for my $row_data (@data) {
my $region = $row_data->[0];
if ($region eq 'East') {
# Row is visible.
}
else {
# Hide row.
$worksheet2->set_row($row, undef, undef, 1);
}
$worksheet2->write($row++, 0, $row_data);
}
###############################################################################
#
#
# Example 3. Autofilter with a dual filter condition in one of the columns.
#
$worksheet3->autofilter('A1:D51');
$worksheet3->filter_column('A', 'x eq East or x eq South');
#
# Hide the rows that don't match the filter criteria.
#
$row = 1;
for my $row_data (@data) {
my $region = $row_data->[0];
if ($region eq 'East' or $region eq 'South') {
# Row is visible.
}
else {
# Hide row.
$worksheet3->set_row($row, undef, undef, 1);
}
$worksheet3->write($row++, 0, $row_data);
}
###############################################################################
#
#
# Example 4. Autofilter with filter conditions in two columns.
#
$worksheet4->autofilter('A1:D51');
$worksheet4->filter_column('A', 'x eq East');
$worksheet4->filter_column('C', 'x > 3000 and x < 8000' );
#
# Hide the rows that don't match the filter criteria.
#
$row = 1;
for my $row_data (@data) {
my $region = $row_data->[0];
my $volume = $row_data->[2];
if ($region eq 'East' and
$volume > 3000 and $volume < 8000
)
{
# Row is visible.
}
else {
# Hide row.
$worksheet4->set_row($row, undef, undef, 1);
}
$worksheet4->write($row++, 0, $row_data);
}
###############################################################################
#
#
# Example 5. Autofilter with filter for blanks.
#
# Create a blank cell in our test data.
$data[5]->[0] = '';
$worksheet5->autofilter('A1:D51');
$worksheet5->filter_column('A', 'x == Blanks');
#
# Hide the rows that don't match the filter criteria.
#
$row = 1;
for my $row_data (@data) {
my $region = $row_data->[0];
if ($region eq '')
{
# Row is visible.
}
else {
# Hide row.
$worksheet5->set_row($row, undef, undef, 1);
}
$worksheet5->write($row++, 0, $row_data);
}
###############################################################################
#
#
# Example 6. Autofilter with filter for non-blanks.
#
$worksheet6->autofilter('A1:D51');
$worksheet6->filter_column('A', 'x == NonBlanks');
#
# Hide the rows that don't match the filter criteria.
#
$row = 1;
for my $row_data (@data) {
my $region = $row_data->[0];
if ($region ne '')
{
# Row is visible.
}
else {
# Hide row.
$worksheet6->set_row($row, undef, undef, 1);
}
$worksheet6->write($row++, 0, $row_data);
}
__DATA__
Region Item Volume Month
East Apple 9000 July
East Apple 5000 July
South Orange 9000 September
North Apple 2000 November
West Apple 9000 November
South Pear 7000 October
North Pear 9000 August
West Orange 1000 December
West Grape 1000 November
South Pear 10000 April
West Grape 6000 January
South Orange 3000 May
North Apple 3000 December
South Apple 7000 February
West Grape 1000 December
East Grape 8000 February
South Grape 10000 June
West Pear 7000 December
South Apple 2000 October
East Grape 7000 December
North Grape 6000 April
East Pear 8000 February
North Apple 7000 August
North Orange 7000 July
North Apple 6000 June
South Grape 8000 September
West Apple 3000 October
South Orange 10000 November
West Grape 4000 July
North Orange 5000 August
East Orange 1000 November
East Orange 4000 October
North Grape 5000 August
East Apple 1000 December
South Apple 10000 March
East Grape 7000 October
West Grape 1000 September
East Grape 10000 October
South Orange 8000 March
North Apple 4000 July
South Orange 5000 July
West Apple 4000 June
East Apple 5000 April
North Pear 3000 August
East Grape 9000 November
North Orange 8000 October
East Apple 10000 June
South Pear 1000 December
North Grape 10000 July
East Grape 6000 February