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

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

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

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

DESCRIPTION

Generate HTML4, XHTML and HTML5 tables with ease. Provides a handful of distinctly named methods to control overall table orientation. These methods in turn accept a number of distinctly named attributes for directing what tags and attributes to use.

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

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 $table = Spreadsheet::HTML->new( data => $data );

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

  • generate( %params )

  • portrait( %params )

  • north( %params )

    heading1heading2heading3
    row1col1row1col2row1col3
    row2col1row2col2row2col3
    row3col1row3col2row3col3
    $html = $table->generate( table => {border => 1}, encode => '<>' );
    print Spreadsheet::HTML::generate( data => $data, indent => "\t" );

    Headers on top. north() is an alias for portrait() which in turn calls generate like so:

    generate( theta => 0, %params )
  • landscape( %params )

  • west( %params )

    heading1row1col1row2col1row3col1
    heading2row1col2row2col2row3col2
    heading3row1col3row2col3row3col3

    Headers on left. west() is an alias for landscape() which in turn calls generate like so:

    generate( theta => -270 )
  • south( %params )

    row1col1row1col2row1col3
    row2col1row2col2row2col3
    row3col1row3col2row3col3
    heading1heading2heading3

    Headers on bottom. Same as

    generate( theta => -180, pinhead => 1 )
  • east( %params )

    row1col1row2col1row3col1heading1
    row1col2row2col2row3col2heading2
    row1col3row2col3row3col3heading3

    Headers on right. Same as

    generate( theta => 90, pinhead => 1 )

Because these methods are all essentially aliases for generate() (with theta being preset for you), you can override their behavior by calling generate() with any configuration of parameters that you like.

For most cases, portrait() and landscape() are all you need. Everything else is bells_and_whistles.

PRESETS

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

  • layout( %params )

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

  • calculator( jquery, %params )

  • checkerboard( colors, %params )

  • animate( direction, %params )

  • chess( %params )

  • checkers( %params )

  • dk( %params )

  • shroom( %params )

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

PARAMETERS

All methods/procedures accept the same named parameters. 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'
  • 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'
  • 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 <tr> 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)

  • 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 -row1, thus any reference to headings will be discarded too.

  • 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 -row0, it could be considered as a dynamic parameter. Be careful not to prepend a dash to headings ... only dynamic parameters use leading dashes.

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.

  • -rowX

    Apply this callback subroutine to the entire row X. (0 index based)

    -row3 => sub { uc shift }

    Or apply hash ref as attributes:

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

    Or both:

    -row3 => [ sub { uc shift }, { class => "foo" } ]
  • -colX

    Apply this callback to the entire column X. (0 index based)

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

    Or apply hash ref as attributes:

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

    Or both:

    -col4 => [ 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" } ]
  • -rowXcolX

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

TAG PARAMETERS

Tag parameters provide a means to control the attributes of the table's tags, and in the case of <td> and <tr> 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.

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

Support for <col> and <colgroup> has not been adequately tested as i honestly do not fully understand why two tags exist when one should do the trick. If you cannot achieve the behavior you desire from this module's generation of <col> and <colgroup> tags please feel free to submit a detailed bug report. The same goes for colspan and rowspan attributes -- very little testing has been done because this module can limit its problem domain to grid like tables of equal sized cells. But if there's a way ...

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.