Spreadsheet-GenerateXLSX

 view release on metacpan or  search on metacpan

lib/Spreadsheet/GenerateXLSX.pm  view on Meta::CPAN

package Spreadsheet::GenerateXLSX;
$Spreadsheet::GenerateXLSX::VERSION = '0.05';
use 5.008;
use strict;
use warnings;

use parent 'Exporter';

use Carp                qw/ croak /;
use Ref::Util           qw/ is_arrayref is_ref /;
use Excel::Writer::XLSX;

our @EXPORT_OK = qw/ generate_xlsx /;

my $MAX_EXCEL_COLUMN_WIDTH = 80;


my $define_formats = sub {
    my $workbook = shift;
    my $formats  = {};

    my @common_settings = (
             size => 12,
        text_wrap => 1,
            align => 'left',
    );

    $formats->{header} = $workbook->add_format(@common_settings,
                              bold => 1,
                             color => 'black',
                         );

    $formats->{cell}   = $workbook->add_format(@common_settings,
                              bold => 0,
                             color => 'gray',
                         );

    return $formats;
};

my $find_sheet_dimensions = sub {
    my $data          = shift;
    my $nrows         = int(@$data);
    my $n_header_cols = int(@{ $data->[0] });
    my $ncols         = $n_header_cols;
    my $s             = $n_header_cols == 1 ? '' : 's';

    foreach my $row (@$data) {
        $ncols = int(@$row) if int(@$row) > $ncols;
    }

    # If there are data rows with more columns than there are
    # header columns, then we let the caller know, because the
    # auto filters will look a bit goofy
    if ($ncols > $n_header_cols) {
        # TODO: this should be a carp, but need to skip a call frame
        warn "generate_xlsx(): you gave me $n_header_cols header column$s, ",
             "but at least one row has $ncols columns.\n";
    }

    return ($nrows, $ncols);
};

my $set_column_widths = sub {
    my ($sheet, $widths_ref) = @_;
    my $col_num = 0;

    foreach my $width (@$widths_ref) {
        # This is a heuristic (ok, nasty hack) for approximating the column
        # width in whatever these excel units are, based on the number of chars.



( run in 0.734 second using v1.01-cache-2.11-cpan-39bf76dae61 )