| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Catmandu::Exporter::XLS; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | our $VERSION = '0.08'; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 49243 | use namespace::clean; | 
|  | 2 |  |  |  |  | 171058 |  | 
|  | 2 |  |  |  |  | 9 |  | 
| 6 | 2 |  |  | 2 |  | 1828 | use Catmandu::Sane; | 
|  | 2 |  |  |  |  | 412681 |  | 
|  | 2 |  |  |  |  | 15 |  | 
| 7 | 2 |  |  | 2 |  | 4637 | use Spreadsheet::WriteExcel; | 
|  | 2 |  |  |  |  | 171765 |  | 
|  | 2 |  |  |  |  | 97 |  | 
| 8 | 2 |  |  | 2 |  | 20 | use Moo; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 17 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | with 'Catmandu::Exporter'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | has xls       => ( is => 'ro', lazy => 1, builder => '_build_xls' ); | 
| 13 |  |  |  |  |  |  | has worksheet => ( is => 'ro', lazy => 1, builder => '_build_worksheet' ); | 
| 14 |  |  |  |  |  |  | has header => ( is => 'ro', default => sub {1} ); | 
| 15 |  |  |  |  |  |  | has fields => ( | 
| 16 |  |  |  |  |  |  | is     => 'rw', | 
| 17 |  |  |  |  |  |  | coerce => sub { | 
| 18 |  |  |  |  |  |  | my $fields = $_[0]; | 
| 19 |  |  |  |  |  |  | if ( ref $fields eq 'ARRAY' ) { return $fields } | 
| 20 |  |  |  |  |  |  | return [ split ',', $fields ]; | 
| 21 |  |  |  |  |  |  | }, | 
| 22 |  |  |  |  |  |  | ); | 
| 23 |  |  |  |  |  |  | has columns => ( | 
| 24 |  |  |  |  |  |  | is     => 'rw', | 
| 25 |  |  |  |  |  |  | coerce => sub { | 
| 26 |  |  |  |  |  |  | my $columns = $_[0]; | 
| 27 |  |  |  |  |  |  | if ( ref $columns eq 'ARRAY' ) { return $columns } | 
| 28 |  |  |  |  |  |  | return [ split ',', $columns ]; | 
| 29 |  |  |  |  |  |  | }, | 
| 30 |  |  |  |  |  |  | ); | 
| 31 |  |  |  |  |  |  | has _n => ( is => 'rw', default => sub {0} ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | sub BUILD { | 
| 34 | 0 |  |  | 0 | 0 |  | my $self    = shift; | 
| 35 | 0 |  |  |  |  |  | my $columns = $self->columns; | 
| 36 | 0 |  |  |  |  |  | my $fields  = $self->fields; | 
| 37 | 0 | 0 | 0 |  |  |  | if ( $fields && $columns && scalar @{$fields} != scalar @{$columns} ) { | 
|  | 0 |  | 0 |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 38 | 0 |  |  |  |  |  | Catmandu::Error->throw( | 
| 39 |  |  |  |  |  |  | "arguments 'fields' and 'columns' have different number of elements" | 
| 40 |  |  |  |  |  |  | ); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub _build_xls { | 
| 45 | 0 |  |  | 0 |  |  | my $xls = Spreadsheet::WriteExcel->new( $_[0]->fh ); | 
| 46 | 0 |  |  |  |  |  | $xls->set_properties( utf8 => 1 ); | 
| 47 | 0 |  |  |  |  |  | $xls; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | sub _build_worksheet { | 
| 51 | 0 |  |  | 0 |  |  | $_[0]->xls->add_worksheet; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 |  |  | 0 | 0 |  | sub encoding {':raw'} | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | sub add { | 
| 57 |  |  |  |  |  |  | my ( $self, $data ) = @_; | 
| 58 |  |  |  |  |  |  | my $fields = $self->fields || $self->fields( [ sort keys %$data ] ); | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | if ( $self->header && $self->_n == 0 ) { | 
| 61 |  |  |  |  |  |  | for ( my $i = 0; $i < @$fields; $i++ ) { | 
| 62 |  |  |  |  |  |  | my $field = $self->columns ? $self->columns->[$i] : $fields->[$i]; | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | # keep for backward compatibility (header could be a hashref) | 
| 65 |  |  |  |  |  |  | $field = $self->header->{$field} | 
| 66 |  |  |  |  |  |  | if ref $self->header && defined $self->header->{$field}; | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | $self->worksheet->write_string( $self->_n, $i, $field ); | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | $self->{_n}++; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | for ( my $i = 0; $i < @$fields; $i++ ) { | 
| 74 |  |  |  |  |  |  | $self->worksheet->write_string( $self->_n, $i, | 
| 75 |  |  |  |  |  |  | $data->{ $fields->[$i] } // "" ); | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | $self->{_n}++; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | sub commit { | 
| 81 |  |  |  |  |  |  | $_[0]->xls->close; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =head1 NAME | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | Catmandu::Exporter::XLS - Package that exports XLS files | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | # On the command line | 
| 91 |  |  |  |  |  |  | $ printf "a,b,c\n1,2,3" | catmandu convert CSV to XLS --file test.xls | 
| 92 |  |  |  |  |  |  | $ printf "a,b,c\n1,2,3" | catmandu convert CSV to XLS --file test.xls --header 0 | 
| 93 |  |  |  |  |  |  | $ printf "a,b,c\n1,2,3" | catmandu convert CSV to XLS --file test.xls --fields a,c | 
| 94 |  |  |  |  |  |  | $ printf "a,b,c\n1,2,3" | catmandu convert CSV to XLS --file test.xls --fields a,c --columns ALPHA,CHARLIE | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | # Or in Perl | 
| 97 |  |  |  |  |  |  | use Catmandu::Exporter::XLS; | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | my $exporter = Catmandu::Exporter::XLS->new( | 
| 100 |  |  |  |  |  |  | file => 'test.xls', | 
| 101 |  |  |  |  |  |  | fields => 'a,b,c', | 
| 102 |  |  |  |  |  |  | columns => 'ALPHA,BRAVO,CHARLIE', | 
| 103 |  |  |  |  |  |  | header => 1); | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | $exporter->add({a => 1, b => 2, c => 3}); | 
| 106 |  |  |  |  |  |  | $exporter->add_many($arrayref); | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | $exporter->commit; | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | printf "exported %d objects\n" , $exporter->count; | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | L exporter for Excel XLS files. | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | =head1 METHODS | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | See L, L, L, | 
| 119 |  |  |  |  |  |  | L, and L for a full list of methods. | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | =head1 CONFIGURATION | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | In addition to the configuration provided by L (C, | 
| 124 |  |  |  |  |  |  | C, etc.) the importer can be configured with the following parameters: | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =over | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =item header | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | Include a header line with column names, if set to 1 (default). | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =item fields | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | List of fields to be used as columns, given as array reference or | 
| 135 |  |  |  |  |  |  | comma-separated string | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =item columns | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | List of custom column names, given as array reference or comma-separated | 
| 140 |  |  |  |  |  |  | list. | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =back | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | L, L. | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =cut | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | 1; |