#!/usr/bin/perl -w ############################################################################### # # A test for Spreadsheet::WriteExcelXML. # # Test autofilters. # # reverse('©'), April 2005, John McNamara, jmcnamara@cpan.org # use strict; use Spreadsheet::WriteExcelXML; use Test::More tests => 13; ############################################################################## # # Create a new Excel XML file with different formats on each page. # my $test_file = "temp_test_file.xml"; my $workbook = Spreadsheet::WriteExcelXML->new($test_file); my $worksheet = $workbook->add_worksheet(); my $bold = $workbook->add_format(bold => 1); $worksheet->set_column('C:F', 12); $worksheet->set_row(2, 20, $bold); my @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', ], ['East', 'Pear', '7000', 'October', ], ['North', 'Pear', '9000', 'August', ], ['West', 'Orange', '1000', 'December', ], ['West', 'Grape', '1000', 'November', ], ['South', 'Pear', '10000', 'April', ], ]; $worksheet->write('C3', \@data); $worksheet->autofilter('C3:F13'); $worksheet->filter_column('C', 'x eq East'); $worksheet->filter_column('E', 'x > 1000 and x < 9000'); $workbook->close(); ############################################################################## # # Re-open and reread the Excel file. # open 'XML', $test_file or die "Couldn't open $test_file: $!\n"; my @swex_data = extract_data(*XML); close XML; unlink $test_file; ############################################################################## # # Read the data from the Excel file in the __DATA__ section # my @test_data = extract_data(*DATA); ############################################################################## # # Pad the SWEX and test data if necessary. # push @swex_data, ('') x (@test_data -@swex_data); push @test_data, ('') x (@swex_data -@test_data); ############################################################################## # # Run the tests # for my $i (0 .. @test_data -1) { is($swex_data[$i], $test_data[$i], " \t" . $test_data[$i]); } ############################################################################## # # Extract autofilter elements from a given filehandle. # sub extract_data { my $fh = $_[0]; my $in_opt = 0; my $setup = ''; my @options; while (<$fh>) { s/^\s+([<| ])/$1/; s/\s+$//; push @options, $_ if /^ Region Item Volume Month East Apple 9000 July East Apple 5000 July South Orange 9000 September North Apple 2000 November West Apple 9000 November East Pear 7000 October North Pear 9000 August West Orange 1000 December West Grape 1000 November South Pear 10000 April
3 15 1 False False