1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
package Catmandu::Importer::XLSX;
our $VERSION = '0.10';
use namespace::clean;
use Catmandu::Sane;
use Encode qw(decode);
use Types::Standard qw(Enum);
use Spreadsheet::ParseXLSX;
use Spreadsheet::ParseExcel::Utility qw(int2col);
use Moo;
with 'Catmandu::Importer';
has xlsx => (is => 'ro', builder => '_build_xlsx');
has header => (is => 'ro', default => sub {1});
has columns => (is => 'ro', default => sub {0});
has fields => (
is => 'rw',
coerce => sub {
my $fields = $_[0];
if (ref $fields eq 'ARRAY') {return $fields}
if (ref $fields eq 'HASH') {return [sort keys %$fields]}
return [split ',', $fields];
},
);
has empty => (
is => 'ro',
isa => Enum [qw(ignore string nil)],
default => sub {'string'}
);
has worksheet => (is => 'ro', default => sub {0});
has _n => (is => 'rw', default => sub {0});
has _row_min => (is => 'rw');
has _row_max => (is => 'rw');
has _col_min => (is => 'rw');
has _col_max => (is => 'rw');
sub BUILD {
my $self = shift;
if ($self->header) {
if ($self->fields) {
$self->{_n}++;
}
elsif ($self->columns) {
$self->fields([$self->_get_cols]);
$self->{_n}++;
}
else {
$self->fields([$self->_get_row]);
$self->{_n}++;
}
}
else {
if (!$self->fields || $self->columns) {
$self->fields([$self->_get_cols]);
}
}
}
sub _build_xlsx {
my ($self) = @_;
my $parser = Spreadsheet::ParseXLSX->new();
my $xlsx = $parser->parse($self->file)
or Catmandu::Error->throw(
"could not parse file \"$self->{file}\": " . $parser->error());
$xlsx = $xlsx->worksheet($self->worksheet)
or Catmandu::Error->throw(
"worksheet $self->{worksheet} does not exist.");
($self->{_row_min}, $self->{_row_max}) = $xlsx->row_range();
($self->{_col_min}, $self->{_col_max}) = $xlsx->col_range();
return $xlsx;
}
sub generator {
my ($self) = @_;
sub {
while ($self->_n <= $self->_row_max) {
my @data = $self->_get_row();
$self->{_n}++;
my @fields = @{$self->fields()};
my %hash = map {
my $key = shift @fields;
if (defined $_) {
($key => $_);
}
elsif ($self->empty eq 'ignore') {
();
}
elsif ($self->empty eq 'string') {
($key => '');
}
elsif ($self->empty eq 'nil') {
($key => undef);
}
} @data;
return \%hash;
}
return;
}
}
sub _get_row {
my ($self) = @_;
my @row;
for my $col ($self->_col_min .. $self->_col_max) {
my $cell = $self->xlsx->get_cell($self->_n, $col);
if ($cell) {
push(@row, $cell->value());
}
else {
push(@row, undef);
}
}
return @row;
}
sub _get_cols {
my ($self) = @_;
my @row;
for my $col ($self->_col_min .. $self->_col_max) {
if (!$self->header || $self->columns) {
push(@row, int2col($col));
}
else {
my $cell = $self->xlsx->get_cell($self->_n, $col);
if ($cell) {
push(@row, $cell->value());
}
else {
push(@row, undef);
}
}
}
return @row;
}
=head1 NAME
Catmandu::Importer::XLSX - Package that imports XLSX files
=head1 SYNOPSIS
# On the command line
$ catmandu convert XLSX < ./t/test.xlsx
$ catmandu convert XLSX --header 0 < ./t/test.xlsx
$ catmandu convert XLSX --fields 1,2,3 < ./t/test.xlsx
$ catmandu convert XLSX --columns 1 < ./t/test.xlsx
$ catmandu convert XLSX --worksheet 1 < ./t/test.xlsx
# Or in Perl
use Catmandu::Importer::XLSX;
my $importer = Catmandu::Importer::XLSX->new(file => "./t/test.xlsx");
my $n = $importer->each(sub {
my $hashref = $_[0];
# ...
});
=head1 DESCRIPTION
L<Catmandu> importer for XLSX files.
=head1 METHODS
This module inherits all methods of L<Catmandu::Importer> and by this
L<Catmandu::Iterable>.
=head1 CONFIGURATION
In addition to the configuration provided by L<Catmandu::Importer> (C<file>,
C<fh>, etc.) the importer can be configured with the following parameters:
=over
=item header
By default object fields are read from the XLS header line. If no header
line is avaiable object fields are named as column coordinates (A,B,C,...). Default: 1.
=item fields
Provide custom object field names as array, hash reference or comma-
separated list.
=item columns
When the 'columns' option is provided, then the object fields are named as
column coordinates (A,B,C,...). Default: 0.
=item empty
How to treat empty fields in the data. When the option value is 'string', the
empty values will be empty strings. When the option value is 'nil', the empty
values will get turned into undefined fields. When the option is 'ignore', the
empty values are ignored. Default is 'string'.
=item worksheet
If the Excel workbook contains more than one worksheet, you can select a
specific worksheet by its index number (0,1,2,...). Default: 0.
=back
=head1 SEE ALSO
L<Catmandu::Importer>, L<Catmandu::Iterable>, L<Catmandu::Importer::CSV>, L<Catmandu::Importer::XLS>.
=cut
1;
|