Add autofilter to worksheet
Robert Bohne [Thu, 17 Jun 2010 21:39:17 +0000 (23:39 +0200)]
Changes
lib/Excel/Template.pm
lib/Excel/Template/Container/Worksheet.pm
t/029_worksheet_autofilter.t [new file with mode: 0644]
t/Spreadsheet/WriteExcel/Worksheet.pm

diff --git a/Changes b/Changes
index fb1b0d3..c8ed818 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,7 @@
 Revision history for Perl distribution Excel::Template
 
 X.XX
+    - Add autofilter to worksheet
     - Swichting to Module::Install
 
 0.31 Mo Dec 17 13:41:00 2007
index 0bc2562..0769f89 100644 (file)
@@ -464,6 +464,8 @@ There is a mailing list at http://groups.google.com/group/ExcelTemplate or excel
 
 =item * Swichting to Module::Install
 
+=item * Add autofilter to worksheet
+
 =back
 
 =head2 Robert Graff
index 9a789d8..3b6f26c 100644 (file)
@@ -38,6 +38,17 @@ sub render {
         $worksheet->hide_gridlines( $hide_gridlines );
     }
 
+    my $autofilter = $context->get( $self, "AUTOFILTER");
+    if ( defined $autofilter ) {
+        if ($autofilter =~ /^\D/) {
+            $worksheet->autofilter($autofilter);
+        }else{
+            $autofilter =~ s/ //g;
+            my ($row1, $col1, $row2, $col2) = split(',',$autofilter);
+            $worksheet->autofilter($row1, $col1, $row2, $col2);
+        }
+    }
+
     return $self->SUPER::render($context);
 }
 
@@ -123,6 +134,17 @@ example, in the following situation:
 In that example, the first and third worksheets will be landscape (inheriting
 it from the workbook node), but the second worksheet will be portrait.
 
+=item * AUTOFILTER
+
+With these attributes, the auto filter set for the Worksheet.
+See L<Spreadsheet::WriteExcel>->autofilter()
+
+Example:
+    <workbook>
+      <worksheet autofilter='A1:D11' />
+      <worksheet autofilter='0, 0, 10, 3' />
+    </workbook>
+
 =back
 
 =head1 CHILDREN
diff --git a/t/029_worksheet_autofilter.t b/t/029_worksheet_autofilter.t
new file mode 100644 (file)
index 0000000..19c4a4c
--- /dev/null
@@ -0,0 +1,38 @@
+use strict;
+
+use Test::More tests => 4;
+
+use lib 't';
+use mock;
+use Data::Dumper;
+mock::reset;
+
+my $CLASS = 'Excel::Template';
+use_ok( $CLASS );
+
+my $object = $CLASS->new(
+    filename => \*DATA,
+);
+isa_ok( $object, $CLASS );
+
+ok( $object->write_file( 'filename' ), 'Something returned' );
+
+my @calls = mock::get_calls;
+# warn Dumper \@calls;
+is( join( $/, @calls, '' ), <<__END_EXPECTED__, 'Calls match up' );
+Spreadsheet::WriteExcel::new( 'filename' )
+Spreadsheet::WriteExcel::add_format( '' )
+Spreadsheet::WriteExcel::add_worksheet( '' )
+Spreadsheet::WriteExcel::Worksheet::new( '' )
+Spreadsheet::WriteExcel::Worksheet::autofilter( 'A1:D11' )
+Spreadsheet::WriteExcel::add_worksheet( '' )
+Spreadsheet::WriteExcel::Worksheet::new( '' )
+Spreadsheet::WriteExcel::Worksheet::autofilter( '0', '0', '10', '3' )
+Spreadsheet::WriteExcel::close( '' )
+__END_EXPECTED__
+
+__DATA__
+<workbook>
+  <worksheet autofilter='A1:D11' />
+  <worksheet autofilter='0, 0, 10, 3' />
+</workbook>
index 00985da..0436735 100644 (file)
@@ -19,7 +19,7 @@ sub new {
 my @funcs = qw(
     write_string write_number write_blank write_url write_formula write_date_time write
     set_row set_column keep_leading_zeros insert_bitmap freeze_panes
-    set_landscape set_portrait merge_range hide_gridlines
+    set_landscape set_portrait merge_range hide_gridlines autofilter
 );
 
 foreach my $func ( @funcs ) {