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 );
print landscape( $data );

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 functions below:

  • generate( %params )

    $html = $table->generate( table => {border => 1}, encode => '<>' );
    print Spreadsheet::HTML::generate( data => $data, indent => "\t" );
  • portrait( %params )

  • north( %params )

    Headers on top. Same as

    generate( theta => 0 )
    header1header2header3
    a1a2a3
    b1b2b3
    c1c2c3
    d1d2d3
  • landscape( %params )

  • west( %params )

    Headers on left. Same as

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

    Headers on bottom. Same as

    generate( theta => -180, pinhead => 1 )
    a1a2a3
    b1b2b3
    c1c2c3
    d1d2d3
    header1header2header3
  • east( %params )

    Headers on right. Same as

    generate( theta => 90, pinhead => 1 )
    a1b1c1d1header1
    a2b2c2d2header2
    a3b3c3d3header3

    Note that tgroups are not allowed for south() because the table is inverted horizontally and not allowed for west() and east() because the table rows contain both headings and cells. You can override this behavior by using generate with params listed above instead of south(), east() or west().

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

PRESETS

See Spreadsheet::HTML::Presets for more documentation.

  • layout( %params )

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

  • calculator( jquery, %params )

  • checkerboard( colors, %params )

  • chess( %params )

  • checkers( %params )

  • dk( %params )

  • shroom( %params )

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 by negating the value of theta.

  • pinhead: 0 or 1

    Works in conjunction with theta to produces tables with headings placed on sides other than the top and perserve data alignment for reporting readability. Used by south() and east().

  • 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 is still -row1.

  • headings

    Apply anonymous 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" } ]

    headings is a natural alias for -row0. 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 anonymous 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 anonymous subroutine 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 anonymous subroutine 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 Tags because they share the names of the actual 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 sub refs.

    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

Used to load data from various different file formats.

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.