Add autofilter to worksheet
[p5sagit/Excel-Template.git] / lib / Excel / Template / Container / Worksheet.pm
1 package Excel::Template::Container::Worksheet;
2
3 use strict;
4
5 BEGIN {
6     use vars qw(@ISA);
7     @ISA = qw(Excel::Template::Container);
8
9     use Excel::Template::Container;
10 }
11
12 sub exit_scope { $_[1]->active_worksheet(undef) }
13
14 sub render {
15     my $self = shift;
16     my ($context) = @_;
17
18     my $worksheet = $context->new_worksheet($self);
19
20     my $password = $context->get( $self, 'PROTECT' );
21     if ( defined $password ) {
22         $worksheet->protect($password);
23     }
24
25     $worksheet->keep_leading_zeros(1)
26       if $context->mark('keep_leading_zeros');
27
28     if ( $context->get( $self, 'LANDSCAPE' ) && !$self->{PORTRAIT} ) {
29         $worksheet->set_landscape;
30     } elsif ( $context->get( $self, 'PORTRAIT' ) ) {
31         $worksheet->set_portrait;
32     }
33
34    
35     my $hide_gridlines = $context->get( $self, 'HIDE_GRIDLINES');
36     
37     if ( defined $hide_gridlines ) {
38         $worksheet->hide_gridlines( $hide_gridlines );
39     }
40
41     my $autofilter = $context->get( $self, "AUTOFILTER");
42     if ( defined $autofilter ) {
43         if ($autofilter =~ /^\D/) {
44             $worksheet->autofilter($autofilter);
45         }else{
46             $autofilter =~ s/ //g;
47             my ($row1, $col1, $row2, $col2) = split(',',$autofilter);
48             $worksheet->autofilter($row1, $col1, $row2, $col2);
49         }
50     }
51
52     return $self->SUPER::render($context);
53 }
54
55 1;
56 __END__
57
58 =head1 NAME
59
60 Excel::Template::Container::Worksheet - Excel::Template::Container::Worksheet
61
62 =head1 PURPOSE
63
64 To provide a new worksheet.
65
66 =head1 NODE NAME
67
68 WORKSHEET
69
70 =head1 INHERITANCE
71
72 Excel::Template::Container
73
74 =head1 ATTRIBUTES
75
76 =over 4
77
78 =item * NAME
79
80 This is the name of the worksheet to be added.
81
82 =item * PROTECT
83
84 If the attribute exists, it will mark the worksheet as being protected. Whatever
85 value is set will be used as the password.
86
87 This activates the HIDDEN and LOCKED nodes.
88
89 =item * KEEP_LEADING_ZEROS
90
91 This will change the behavior of the worksheet to preserve leading zeros.
92
93
94 =item * HIDE_GRIDLINE
95
96 his method is used to hide the gridlines on the screen and printed page. 
97 Gridlines are the lines that divide the cells on a worksheet. Screen and printed gridlines are 
98 turned on by default in an Excel worksheet. If you have defined your own cell 
99 borders you may wish to hide the default gridlines.
100
101 $worksheet->hide_gridlines();
102
103 The following values of $option are valid:
104
105     0 : Don't hide gridlines
106     1 : Hide printed gridlines only
107     2 : Hide screen and printed gridlines
108
109 If you don't supply an argument or use undef the default option is 1, i.e. only the printed gridlines are hidden.
110
111 =item * LANDSCAPE
112
113 This will set the worksheet's orientation to landscape.
114
115 =item * PORTRAIT
116
117 This will set the worksheet's orientation to portrait.
118
119 While this is the default, it's useful to override the default at times. For
120 example, in the following situation:
121
122   <workbook landscape="1">
123     <worksheet>
124       ...
125     </worksheet
126     <worksheet portrait="1">
127       ...
128     </worksheet
129     <worksheet>
130       ...
131     </worksheet
132   </workbook>
133
134 In that example, the first and third worksheets will be landscape (inheriting
135 it from the workbook node), but the second worksheet will be portrait.
136
137 =item * AUTOFILTER
138
139 With these attributes, the auto filter set for the Worksheet.
140 See L<Spreadsheet::WriteExcel>->autofilter()
141
142 Example:
143     <workbook>
144       <worksheet autofilter='A1:D11' />
145       <worksheet autofilter='0, 0, 10, 3' />
146     </workbook>
147
148 =back
149
150 =head1 CHILDREN
151
152 None
153
154 =head1 EFFECTS
155
156 None
157
158 =head1 DEPENDENCIES
159
160 None
161
162 =head1 USAGE
163
164   <worksheet name="My Taxes">
165     ... Children here
166   </worksheet>
167
168 In the above example, the children will be executed in the context of the
169 "My Taxes" worksheet.
170
171 =head1 AUTHOR
172
173 Rob Kinyon (rob.kinyon@gmail.com)
174
175 =head1 SEE ALSO
176
177 ROW, CELL, FORMULA
178
179 =cut