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

NAME

Spreadsheet::HTML - Just another HTML table generator.

SYNOPSIS

    use Spreadsheet::HTML;

    $data = [ [qw(a1 a2 a3)], [qw(b1 b2 b3)], [qw(c1 c2 c3)] ];

    $generator = Spreadsheet::HTML->new( data => $data, indent => "\t" );
    print $generator->portrait;
    print $generator->landscape( encodes => '<>' );

    # load from files (first table found)
    $generator = Spreadsheet::HTML->new( file => 'data.xls', cache => 1 );
    print $generator->generate( preserve => 1 );

Procedural interface:

    use Spreadsheet::HTML qw( portrait landscape );

    print portrait( $data, td => sub { sprintf "%02d", shift } );
    print landscape( $data, tr => { class => [qw(odd even)] } );

DESCRIPTION

Generate HTML tables with ease (HTML4, XHTML and HTML5). Can generate landscape and other rotated views, Handsontable tables, HTML calendars, checkerboard patterns, games such as sudoku, banners and mazes, and can create animations of cell values and backgrounds via jQuery. Can rewrap existing tables from Excel, HTML, JSON, CSV and YAML files.

THIS MODULE IS AN ALPHA RELEASE! Although we are very close to BETA.

CLI TOOL

A command line interface tool is available for you to quickly generate tables without writing a script: mktable

  $ mktable landscape --param file=data.xls --param preserve=1 > out.html

If you have HTML::Display installed, you can direct the output to your default browser:

  $ mktable sudoku --display

  $ mktable landscape --param data=[[a..d],[1..4],[5..8]] --display

  $ mktable conway --param data=[1..300] --param wrap=20 --param matrix=1 --display

METHODS

All methods (except new) are exportable as functions. They all accept the same named parameters (see PARAMETERS below). With the exception of new, all methods return an HTML table as a scalar string.

  • new( %params )

      my $generator = Spreadsheet::HTML->new( data => $data );

    Constructs object. Accepts the same named parameters as the table generating methods below:

  • generate( %params )

  • portrait( %params )

  • north( %params )

    These three methods all generate an HTML table with its headings positioned at the top.

      heading1    heading2    heading3    heading4  
      row1col1    row1col2    row1col3    row1col4  
      row2col1    row2col2    row2col3    row2col4  
      row3col1    row3col2    row3col3    row3col4  
      print $generator->generate();
      print $generator->portrait();
      print $generator->north();
  • landscape( %params )

  • west( %params )

    These two methods generate an HTML table with its headings positioned at the left.

      heading1    row1col1    row2col1    row3col1  
      heading2    row1col2    row2col2    row3col2  
      heading3    row1col3    row2col3    row3col3  
      heading4    row1col4    row2col4    row3col4  
      print $generator->landscape();
      print $generator->west();
  • south( %params )

    This method generates an HTML table with its headings positioned at the bottom.

      row1col1    row1col2    row1col3    row1col4  
      row2col1    row2col2    row2col3    row2col4  
      row3col1    row3col2    row3col3    row3col4  
      heading1    heading2    heading3    heading4  
      print $generator->south();
  • east( %params )

    This method generates an HTML table with its headings positioned at the right.

      row1col1    row2col1    row3col1    heading1  
      row1col2    row2col2    row3col2    heading2  
      row1col3    row2col3    row3col3    heading3  
      row1col4    row2col4    row3col4    heading4  
      print $generator->east();

For most cases, portrait and landscape are all you need.

PARAMETERS

All methods/procedures accept the same named parameters. All parameters are initally undefined. You do not have to specify data, any bare array references will be collected and assigned to data.

LITERAL PARAMETERS

Literal parameters provides the means to modify the macro aspects of the table, such as indentation, encoding, data source and table orientation.

  • data

    The data to be rendered into table cells. Should be an array ref of array refs.

      data => [["a".."c"],[1..3],[4..6],[7..9]]
  • file

    The name of the data file to read. Supported formats are XLS, CSV, JSON, YAML and HTML (first table found).

      file => 'foo.json'

    If you want to filter your database queries then install DBIx::HTML which uses this module as its generator.

      use DBIx::HTML;
      my $generator = DBIx::HTML->connect( @db_credentials );
      $generator->do( $query, @bind_args );
      print $table->portrait( %params );
  • preserve

    Can be supplied in addition to file. Attempts to copy over all formatting styles from original document to table. Styles are not currently deduped, so use with care as the final output will contain a lot of redundant cell styles. Work is being done to alleviate this.

      file => 'foo.xls', preserve => 1
  • fill

    Can be supplied instead of data to generate empty cells, or in conjunction with data to pad existing cells (currently only pads the right and bottom sides.)

      fill => '5x12'
  • wrap

    Can be supplied in conjunction with a 1D data to automatically wrap into a 2D array matrix. Can also "rewrap" existed 2D array matrices, but at the expense of likely mangling the headings.

      wrap => 10 
  • theta: 0, 90, 180, 270, -90, -180, -270

    Rotates table clockwise for positive values and counter-clockwise for negative values. Default to 0: headers at top. 90: headers at right. 180: headers at bottom. 270: headers at left. To achieve landscape, use -270.

  • flip: 0 or 1

    Flips table horizontally from the perspective of the headings "row" by negating the value of theta.

  • pinhead: 0 or 1

    Works in conjunction with theta to ensure reporting readability. Without it, south() and east() would have data cells arranged in reverse order.

  • indent

    Render the table with nested indentation. Defaults to undefined which produces no indentation. Adds newlines when set to any value that is defined.

      indent => '    '
    
      indent => "\t"
  • level

    Start indentation at this level. Useful for matching nesting styles of original HTML text that you may want to insert into to.

      level => 4
  • encodes

    HTML Encode contents of <th> and/or <td> tags. Defaults to empty string which performs no encoding of entities. Pass a string like '<>&=' to perform encoding on any characters found. If the value is undef then all unsafe characters will be encoded as HTML entites. Uses HTML::Entities.

      encodes => '<>"'
  • empty

    Replace empty cells with this value. Defaults to &nbsp;. Set value to undef to avoid any substitutions.

      empty => '&#160;'
  • tgroups: 0, 1 or 2

    Group table rows into <thead>, <tbody> and <tfoot> sections.

    When tgroups is set to 1, the <tfoot> section is omitted. The last row of the data is found at the end of the <tbody> section instead. (loose)

    When tgroups is set to 2, the <tfoot> section is found in between the <thead> and <tbody> sections. (strict)

  • group

    Will chunk body rows into tbody groups of size group.

      group => 4

    Currently only accepts integers although it should be possilbe to group by changes in a column in the future.

  • cache: 0 or 1

    Preserve data after it has been processed (and loaded). Useful for loading data from files only once.

  • matrix: 0 or 1

    Treat headings as a regular row. Render the table with only td tags, no th tags.

  • headless: 0 or 1

    Render the table with without the headings row at all. The first row after the headings is still -r1, thus any reference to headings will be discarded too.

  • animate: 0 or 1

    Animate the table cells. See *Spreadsheet::HTML::Presets::Animate.

  • headings

    Apply callback subroutine to each cell in headings row.

      headings => sub { join(" ", map {ucfirst lc $_} split "_", shift) }

    Or apply hash ref as attributes:

      headings => { class => 'some-class' }

    Or both:

      headings => [ sub { uc shift }, { class => "foo" } ]

    Since headings is a natural alias for the dynamic parameter -r0, it could be considered as a dynamic parameter. Be careful not to prepend a dash to headings ... only dynamic parameters use leading dashes.

  • sorted_attrs: 0 or 1

    This is useful for ensuring that attributes within tags are rendered in alphabetical ordering, for consistancy. You will most likely never need this feature.

DYNAMIC PARAMETERS

Dynamic parameters provide a means to control the micro elements of the table, such as modifying headings by their name and rows and columns by their indices (X). They contain leading dashes to seperate them from literal and tag parameters.

  • -rX

    Apply this callback subroutine to all cells in row X. (0 index based)

      -r3 => sub { uc shift }

    Or apply hash ref as attributes:

      -r3 => { class => 'some-class' }

    Or both:

      -r3 => [ sub { uc shift }, { class => "foo" } ]
  • -cX

    Apply this callback to all cells in column X. (0 index based)

      -c4 => sub { sprintf "%02d", shift || 0 }

    Or apply hash ref as attributes:

      -c4 => { class => 'some-class' }

    Or both:

      -c4 => [ sub { uc shift }, { class => "foo" } ]

    You can alias any column number by the value of the heading name in that column:

      -occupation => sub { "<b>$_[0]"</b>" }
    
      -salary => { class => 'special-row' }
    
      -employee_no => [ sub { sprintf "%08d", shift }, { class => "foo" } ]
  • -rXcX

    Apply this callback or hash ref of attributres to the cell at row X and column X. (0 index based)

      -r3c4 => { class => 'special-cell' }

TAG PARAMETERS

Tag parameters provide a means to control the attributes of the table's tags, and in the case of <th> and <td> the contents via callback subroutines. Although similar in form, they are differentiated from litertal parameters because they share the names of the actual HTML table tags.

  • table

  • thead

  • tfoot

  • tbody

  • tr

    Hash ref. Apply these attributes to the specified tag.

      table => { class => 'spreadsheet' }
    
      tr => { style => { background => [qw( color1 color2 )]' } }
  • th

  • td

    <th> and <td> are the only Tag Parameters that may additionally accept callback subroutines.

      th => sub { uc shift }
    
      td => [ sub { uc shift }, { class => 'foo' } ]
  • caption

    Caption is special in that you can either pass a string to be used as CDATA or a hash whose only key is the string to be used as CDATA.

      caption => "Just Another Title"
    
      caption => { "A Title With Attributes" => { align => "bottom" } }
  • colgroup

    Add colgroup tag(s) to the table. Use an AoH for multiple.

      colgroup => { span => 2, style => { 'background-color' => 'orange' } }
    
      colgroup => [ { span => 20 }, { span => 1, class => 'end' } ]
  • col

    Add col tag(s) to the table. Use an AoH for multiple. Wraps tags within a colgroup tag. Same usage as colgroup.

      col => { span => 2, style => { 'background-color' => 'orange' } }
    
      col => [ { span => 20 }, { span => 1, class => 'end' } ]

PRESETS

The following presets are availble for creating tables that can be used with little to no additional coding.

  • layout( %params )

  • handson( handsonjs, jquery, css, %params )

  • banner( on, off, text, font, %params )

  • calendar( month, year, %params )

  • animate( fgdirection, bgdirection, jquery, %params )

  • conway( on, off, fade, jquery, %params )

  • calculator( jquery, %params )

  • checkerboard( colors, %params )

  • sudoku( blanks, attempts, %params )

  • maze( on, off, fill, %params )

  • chess( %params )

  • checkers( %params )

  • beadwork( %params )

See Spreadsheet::HTML::Presets for more documentation (and the source for more usage examples).

REQUIRES

OPTIONAL

The following are used to load data from various different file formats:

The following are used by some presets to enhance their output, if installed:

SEE ALSO

BUGS AND LIMITATIONS

Please report any bugs or feature requests to either

I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

GITHUB

The Github project is https://github.com/jeffa/Spreadsheet-HTML

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Spreadsheet::HTML

You can also look for information at:

ACKNOWLEDGEMENTS

Thank you very much! :)

  • Neil Bowers

    Helped with Makefile.PL suggestions and corrections.

AUTHOR

Jeff Anderson, <jeffa at cpan.org>

LICENSE AND COPYRIGHT

Copyright 2015 Jeff Anderson.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.