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