The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Test::Excel - A module for testing and comparing Excel files.

VERSION

Version 1.10

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/

SYNOPSIS

  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.' });

  # or even in standalone mode:

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

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 two 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. Future versions may include other testing functions.

Definition of 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 reference to a HASH with the keys 'sheet', 'tolerance', 'sheet_tolerance' and optionally 'message'(only relevant to method cmp_excel()).

sheet: "|" seperated sheet name.

Apply sheet_tolerance to all the NUMBERS found on these sheets. Example: 'Sheet1|Sheet2'

tolerance: Number.

This would apply to all the NUMBERS found on all sheets in the excel except the one specified by the key sheet and by the title sheet in the spec file. Example: 10**-12

sheet_tolerance: Number.

These rule would be applied to all the sheets defined in the spec file by the title 'sheet' within the range specified by 'range' in the spec file and also by the key 'sheet'. Example: 0.20

spec: Path to the spec file.

This would have the path to the spec file to be used in comparing excel file.

swap_check: Number (Optional).

Set it to 1 if you want to do swap check. The default is 0. Swap check ignores if the row has been swaped around in the same sheet.

error_limit: Number (Optional).

Limit the error per sheet. Default is 0.

message: String (Optional)

Test message to be displayed. 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.

DEBUGGING

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;

METHODS

_validate_rule()

This is a local method to validate the rule definitions.

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 $expected can be either instances of Spreadsheet::ParseExcel or a file path (which is in turn passed to the Spreadsheet::ParseExcel constructor).

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 in standalone mode.

Both $got and $exp can be either instances of Spreadsheet::ParseExcel or a file path (which is in turn passed to the Spreadsheet::ParseExcel constructor).

is_swapping()

parse()

This method parse spec file provided by the user. It expects spec file to be in a format mentioned below:

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

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);

    # You should expect these values:
    # $col => 'A'
    # $row => 23

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');

    # You should expect $number to be 27.

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);

    # You should expect $letter to be 'AB'.

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);

    # $cells would have something like below:
    # [ {row => 1, col => 0},
    #   {row => 1, col => 1},
    #   {row => 2, col => 0},
    #   {row => 2, col => 1},
    #   {row => 3, col => 0},
    #   {row => 3, col => 1} ]

_log_message()

This is an internal method that dumps the message to STDOUT.

Important Disclaimer

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 Large Excels

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.

TO DO

More functions for more testing
Testing of font data
Testing of embedded image data

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 by Mohammad S Anwar.

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