TableReader/Decoder/Spreadsheet.pm

filter: Statementstmt 79.7% 75 / 94 Branchbran 47.0% 16 / 34 Conditioncond 46.1% 12 / 26 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 87.5% 14 / 16 Podpod 100% 1 / 1 totaltotal 65.5% 122 / 186 CC 27 SCAR 21.4
CC
27
Cov
67%
_build__sheets
22.1
__ANON__
9.3
position
6.0
CRAP 8.5
« TableReader/Decoder/CSV.pm ↑ Summary TableReader/Decoder.pm »
1 package Data::TableReader::Decoder::Spreadsheet;
2 1 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 extends 'Data::TableReader::Decoder';
7  
8 # ABSTRACT: Base class for implementing spreadsheet decoders
9 1 our $VERSION = '0.023'; # VERSION
10  
11  
12 1 has workbook => ( is => 'lazy' );
13 1 has sheet => ( is => 'ro' );
14 1 has xls_formatter => ( is => 'rw' );
15  
16 # Arrayref of all sheets we can search
17 1 has _sheets => ( is => 'lazy' );
18  
19 2 ▶sub _build__sheets {
Subroutine _build__sheets: called CC=9 SCAR=31.0
20 2 my $self= shift;
21  
22 # If we have ->sheet and it is a worksheet object, then no need to do anything else
23 2 ▶ if ($self->sheet && ref($self->sheet) && ref($self->sheet)->can('get_cell')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sheet and ref $self->sheet and ref($self->sheet)->can("get_cell")) 1 1 2
Condition66%4/6
$self->sheet and ref $self->sheet and ref($self->sheet)->can("get_cell")
!ll&&!rl&&r
101
$self->sheet and ref $self->sheet
!ll&&!rl&&r
101
MC/DC33%1/3
$self->sheet and ref $self->sheet and ref($self->sheet)->can("get_cell")
$self->sheet ref $self->sheet ref($self->sheet)->can("get_cell")
Truth table50%2/4
A and B and C
A= $self->sheetB= ref $self->sheetC= ref($self->sheet)->can("get_cell")
ABCresult
0XX0
10X0
1100
1111
24 1 return [ $self->sheet ];
25 }
26  
27 # Else we need to scan sheets from the excel file. Make sure we have the file
28 1 my @sheets= $self->workbook->worksheets;
29 1 ▶ @sheets or croak "No worksheets in file?";
Branch: 1 branch
BranchTrueFalseTotal
@sheets or croak("No worksheets in file?") 1 0 1
30 1 ▶ if (defined $self->sheet) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->sheet) 0 1 1
31 0 ▶ if (ref($self->sheet) eq 'Regexp') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $self->sheet eq 'Regexp') { } 0 0 0
elsif (ref $self->sheet eq 'CODE') { } 0 0 0
elsif (not ref $self->sheet) { } 0 0 0
32 0 @sheets= grep { $_->get_name =~ $self->sheet } @sheets;
33 } elsif (ref($self->sheet) eq 'CODE') {
34 0 @sheets= grep { $self->sheet->($_) } @sheets;
35 } elsif (!ref $self->sheet) {
36 0 @sheets= grep { $_->get_name eq $self->sheet } @sheets;
37 } else {
38 0 croak "Unknown type of sheet specification: ".$self->sheet;
39 }
40 }
41  
42 1 return \@sheets;
43 }
44  
45 7 ▶sub _oo_rowmax_fix { # openoffice saves bogus rowmax, try and fix
Subroutine _oo_rowmax_fix: called CC=4 SCAR=17.2
46 7 my ($s, $rowmax)= @_;
47 7 my $final_row_max= ($s and ref $s->{Cells} eq "ARRAY" and $#{$s->{Cells}} < $rowmax) #
48 0 ▶ ? $#{$s->{Cells}} : $rowmax;
Branch: 1 branch
BranchTrueFalseTotal
$s && ref $s->{'Cells'} eq 'ARRAY' && $#{$$s{'Cells'};} < $rowmax ? : 0 7 7
Condition33%2/6
$s and ref $s->{"Cells"} eq "ARRAY" and $#{$$s{"Cells"};} < $rowmax
!ll&&!rl&&r
070
$s and ref $s->{"Cells"} eq "ARRAY"
!ll&&!rl&&r
007
MC/DC0%0/3
$s and ref $s->{"Cells"} eq "ARRAY" and $#{$$s{"Cells"};} < $rowmax
$s ref $s->{"Cells"} eq "ARRAY" $#{$$s{"Cells"};} < $rowmax
Truth table25%1/4
A and B and C
A= $sB= ref $s->{"Cells"} eq "ARRAY"C= $#{$$s{"Cells"};} < $rowmax
ABCresult
0XX0
10X0
1100
1111
49 7 return $final_row_max;
50 }
51  
52 4 ▶sub iterator {
Subroutine iterator: called CC=3 SCAR=11.7
Pod documented
53 4 my $self= shift;
54 4 my $sheets= $self->_sheets;
55 4 my $sheet= $sheets->[0];
56 4 ▶ my ($colmin, $colmax)= $sheet? $sheet->col_range() : (0,-1);
Branch: 1 branch
BranchTrueFalseTotal
$sheet ? : 4 0 4
57 4 ▶ my ($rowmin, $rowmax)= $sheet? $sheet->row_range() : (0,-1);
Branch: 1 branch
BranchTrueFalseTotal
$sheet ? : 4 0 4
58 4 $rowmax= _oo_rowmax_fix $sheet, $rowmax;
59 4 my $row= $rowmin-1;
60 Data::TableReader::Decoder::Spreadsheet::_Iter->new(
61 16 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=22.3
62 16 my $slice= shift;
63 16 ▶ return undef unless $row < $rowmax;
Branch: 1 branch
BranchTrueFalseTotal
unless $row < $rowmax 3 13 16
64 13 ++$row;
65 13 my $x;
66 13 ▶ if ($slice) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slice) { } 0 13 13
67 return [ map {
68 0 ▶ $x= ($x= $sheet->get_cell($row, $_)) && $x->value;
Condition0%0/3
($x = $sheet->get_cell($row, $_)) && $x->value
!ll&&!rl&&r
000
MC/DC0%0/2
($x = $sheet->get_cell($row, $_)) && $x->value
($x = $sheet->get_cell($row, $_)) $x->value
Truth table0%0/3
A && B
A= ($x = $sheet->get_cell($row, $_))B= $x->value
ABresult
0X0
100
111
69 0 ▶ defined $x? $x : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $x ? : 0 0 0
70 } @$slice ];
71 } else {
72 return [ map {
73 13 ▶ $x= ($x= $sheet->get_cell($row, $_)) && $x->value;
Condition100%3/3
($x = $sheet->get_cell($row, $_)) && $x->value
!ll&&!rl&&r
17648
MC/DC100%2/2
($x = $sheet->get_cell($row, $_)) && $x->value
($x = $sheet->get_cell($row, $_)) $x->value
Truth table100%3/3
A && B
A= ($x = $sheet->get_cell($row, $_))B= $x->value
ABresult
0X0
100
111
74 71 ▶ defined $x? $x : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $x ? : 54 17 71
75 } 0 .. $colmax ];
76 }
77 },
78 {
79 4 sheets => $sheets,
80 sheet_idx => 0,
81 sheet_ref => \$sheet,
82 row_ref => \$row,
83 colmax_ref => \$colmax,
84 rowmax_ref => \$rowmax,
85 origin => [ $sheet, $row ],
86 }
87 );
88 }
89  
90 # If you need to subclass this iterator, don't. Just implement your own.
91 # i.e. I'm not declaring this implementation stable, yet.
92 1 ▶use Data::TableReader::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1 ▶BEGIN { @Data::TableReader::Decoder::Spreadsheet::_Iter::ISA= ('Data::TableReader::Iterator'); }
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 0 ▶sub Data::TableReader::Decoder::Spreadsheet::_Iter::position {
Subroutine position: not called CC=2 SCAR=17.9
96 0 my $f= shift->_fields;
97 ($f->{sheet_idx} > 0? 'sheet '.($f->{sheet_idx}+1).' ' : '')
98 0 ▶ .'row '.(1+${ $f->{row_ref} });
Branch: 1 branch
BranchTrueFalseTotal
$f->{'sheet_idx'} > 0 ? : 0 0 0
99 }
100  
101 6 ▶sub Data::TableReader::Decoder::Spreadsheet::_Iter::row {
Subroutine row: called CC=1 SCAR=0.0
102 6 1 + ${ shift->_fields->{row_ref} };
103 }
104  
105 6 ▶sub Data::TableReader::Decoder::Spreadsheet::_Iter::dataset_idx {
Subroutine dataset_idx: called CC=1 SCAR=0.0
106 6 shift->_fields->{sheet_idx};
107 }
108  
109 0 ▶sub Data::TableReader::Decoder::Spreadsheet::_Iter::progress {
Subroutine progress: not called CC=2 SCAR=17.9
110 0 my $f= shift->_fields;
111 0 ▶ return ${ $f->{row_ref} } / (${ $f->{rowmax_ref} } || 1);
Condition0%0/2
${$f->{'rowmax_ref'};} || 1
l!l
00
MC/DC0%0/1
${$f->{'rowmax_ref'};} || 1
${$f->{'rowmax_ref'};}
Truth table0%0/2
A
A= ${$f->{'rowmax_ref'};}
Aresult
11
00
112 }
113  
114 2 ▶sub Data::TableReader::Decoder::Spreadsheet::_Iter::tell {
Subroutine tell: called CC=1 SCAR=0.0
115 2 my $f= shift->_fields;
116 2 return [ $f->{sheet_idx}, ${$f->{row_ref}} ];
117 }
118  
119 3 ▶sub Data::TableReader::Decoder::Spreadsheet::_Iter::seek {
Subroutine seek: called CC=5 SCAR=16.2
120 3 my ($self, $to)= @_;
121 3 my $f= $self->_fields;
122 3 ▶ $to ||= $f->{origin};
Condition33%1/3
$to ||= $f->{"origin"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$to ||= $f->{"origin"}
$to $f->{"origin"}
Truth table33%1/3
A ||= B
A= $toB= $f->{"origin"}
ABresult
1X1
011
000
123 3 my ($sheet_idx, $row)= @$to;
124 3 my $sheet= $f->{sheets}[$sheet_idx];
125 3 ▶ my ($colmin, $colmax)= $sheet? $sheet->col_range() : (0,-1);
Branch: 1 branch
BranchTrueFalseTotal
$sheet ? : 3 0 3
126 3 ▶ my ($rowmin, $rowmax)= $sheet? $sheet->row_range() : (0,-1);
Branch: 1 branch
BranchTrueFalseTotal
$sheet ? : 3 0 3
127 3 $rowmax= _oo_rowmax_fix $sheet, $rowmax;
128 3 ▶ $row= $rowmin-1 unless defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 2 1 3
129 3 $f->{sheet_idx}= $sheet_idx;
130 3 ${$f->{sheet_ref}}= $sheet;
131 3 ${$f->{row_ref}}= $row;
132 3 ${$f->{colmax_ref}}= $colmax;
133 3 ${$f->{rowmax_ref}}= $rowmax;
134 3 1;
135 }
136  
137 3 ▶sub Data::TableReader::Decoder::Spreadsheet::_Iter::next_dataset {
Subroutine next_dataset: called CC=2 SCAR=7.0
138 3 my $self= shift;
139 3 my $f= $self->_fields;
140 return defined $f->{sheets}[ $f->{sheet_idx}+1 ]
141 3 ▶ && $self->seek([ $f->{sheet_idx}+1 ]);
Condition66%2/3
defined $f->{'sheets'}[$f->{'sheet_idx'} + 1] && $self->seek([$f->{'sheet_idx'} + 1])
!ll&&!rl&&r
102
MC/DC50%1/2
defined $f->{'sheets'}[$f->{'sheet_idx'} + 1] && $self->seek([$f->{'sheet_idx'} + 1])
defined $f->{'sheets'}[$f->{'sheet_idx'} + 1] $self->seek([$f->{'sheet_idx'} + 1])
Truth table66%2/3
A && B
A= defined $f->{'sheets'}[$f->{'sheet_idx'} + 1]B= $self->seek([$f->{'sheet_idx'} + 1])
ABresult
0X0
100
111
142 }
143  
144 1 1;
145  
146 __END__
« TableReader/Decoder/CSV.pm ↑ Summary TableReader/Decoder.pm »