The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test::Excel - Interface to test and compare Excel files.

VERSION

Version 1.24

AWARD

Test::Excel has been granted the "Famous Software Award" by Download.FamousWhy.com on Wed 17 Nov 2010.

http://download.famouswhy.com/test_excel/

DESCRIPTION

This module is meant to be used for testing custom generated Excel files, it provides two functions at the moment, which is cmp_excel and compare_excel. These can be used to compare_excel 2 Excel files to see if they are visually similar. The function cmp_excel is for testing purpose where function compare_excel can be used as standalone.

RULE

The new paramter has been added to both method cmp_excel() and method compare_excel() called RULE. This is optional, however, this would allow to apply your own rule for comparison. This should be passed in as a reference to a HASH with the keys sheet, tolerance, sheet_tolerance and optionally swap_check, error_limit and message (only relevant to method cmp_excel()).

    +-----------------+---------------------------------------------------------------------+
    | Key             | Description                                                         |
    +-----------------+---------------------------------------------------------------------+
    | sheet           | "|" seperated sheet names.                                          |
    | tolerance       | Number. Apply to all NUMBERS except on 'sheet'/'spec'. e.g. 10**-12 |
    | sheet_tolerance | Number. Apply to sheets/ranges in the spec. e.g. 0.20               |
    | spec            | Path to the specification file.                                     |
    | swap_check      | Number (optional) (1 or 0). Row swapping check. Default is 0.       |
    | error_limit     | Number (optional). Limit error per sheet. Default is 0.             |
    | message         | String (optional). Only required when calling method cmp_excel().   |
    +-----------------+---------------------------------------------------------------------+

What is "Visually" Similar?

This module uses the Spreadsheet::ParseExcel module to parse Excel files, then compares the parsed data structure for differences. We ignore cetain components of the Excel file, such as embedded fonts, images, forms and annotations, and focus entirely on the layout of each Excel page instead. Future versions will likely support font and image comparisons, but not in this initial release.

METHODS

cmp_excel($got, $exp, { ...rule... })

This function will tell you whether the two Excel files are "visually" different, ignoring differences in embedded fonts/images and metadata. Both $got and $exp can be either instances of Spreadsheet::ParseExcel / file path (which is in turn passed to the Spreadsheet::ParseExcel constructor). This one is for use in TEST MODE.

    use strict; use warnings;
    use Test::More no_plan => 1;
    use Test::Excel;

    cmp_excel('foo.xls', 'bar.xls', { message => 'EXCELSs are identical.' });

    # or

    my $foo = Spreadsheet::ParseExcel::Workbook->Parse('foo.xls');
    my $bar = Spreadsheet::ParseExcel::Workbook->Parse('bar.xls');
    cmp_excel($foo, $bar, { message => 'EXCELs are identical.' });

compare_excel($got, $exp, { ...rule... })

This function will tell you whether the two Excel files are "visually" different, ignoring differences in embedded fonts/images and metadata. Both $got and $exp can be either instances of Spreadsheet::ParseExcel / file path (which is in turn passed to the Spreadsheet::ParseExcel constructor). This one is for use in STANDALONE MODE.

    use strict; use warnings;
    use Test::Excel;

    print "EXCELs are identical.\n"
        if compare_excel("foo.xls", "bar.xls");

parse()

This method parse specification file provided by the user. It expects spec file to be in a format mentioned below. Key and values are space seperated.

    sheet       Sheet1
    range       A3:B14
    range       B5:C5
    sheet       Sheet2
    range       A1:B2
    ignorerange B3:B8
    

They are grouped as sheet followed by one or more ranges.

    use strict; use warnings;
    use Test::Excel;

    my $data = Test::Excel::parse('spec-1.txt');

column_row()

This method accepts a cell address and returns column and row address as a list.

    use strict; use warnings;
    use Test::Excel;

    my $cell = 'A23';
    my ($col, $row) = Test::Excel::column_row($cell);

letter_to_number()

This method accepts a letter and returns back its equivalent number. This simply wraps around Spreadsheet::ParseExcel::Utility::col2int().

    use strict; use warnings;
    use Test::Excel;

    my $number = Test::Excel::letter_to_number('AB');

number_to_letter()

This number accepts a number and returns its equivalent letter. This simply wraps around Spreadsheet::ParseExcel::Utility::int2col().

    use strict; use warnings;
    use Test::Excel;

    my $letter = Test::Excel::number_to_letter(27);

cells_within_range()

This method accepts address range and returns all cell address within the range.

    use strict; use warnings;
    use Test::Excel;

    my $range = 'A1:B3';
    my $cells = Test::Excel::cells_within_range($range);

DEBUG

Debug mode can be turned on or off by setting package variable $DEBUG, for example,

   $Test::Excel::DEBUG = 1;

You can set it anything greater than 1 for fine grained debug information. i.e.

   $Test::Excel::DEBUG = 2;

NOTES

It should be clearly noted that this module does not claim to provide a fool-proof comparison of generated Excels. In fact there are still a number of ways in which I want to expand the existing comparison functionality. This module is actively being developed for a number of projects I am currently working on, so expect many changes to happen. If you have any suggestions/comments/questions please feel free to contact me.

CAVEATS

Testing of large Excels can take a long time, this is because, well, we are doing a lot of computation. In fact, this module test suite includes tests against several large Excels, however I am not including those in this distibution for obvious reasons.

BUGS

None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it. This is still a very early version, so it is always possible that I have just "gotten it wrong" in some places.

SEE ALSO

Spreadsheet::ParseExcel - I could not have written this without this module.

ACKNOWLEDGEMENTS

John McNamara (author of Spreadsheet::ParseExcel).
Kawai Takanori (author of Spreadsheet::ParseExcel::Utility).
Stevan Little (author of Test::PDF).

AUTHOR

Mohammad S Anwar, <mohammad.anwar@yahoo.com>

COPYRIGHT AND LICENSE

Copyright 2010-2011 by Mohammad S Anwar.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

DISCLAIMER

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.