XLSX.pm

filter: Statementstmt 98.1% 159 / 162 Branchbran 84.3% 54 / 64 Conditioncond 61.5% 16 / 26 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 100% 26 / 26 Podpod 88.8% 8 / 9 totaltotal 88.4% 269 / 304 CC 45 SCAR 22.5
CC
45
Cov
91%
_table
21.1
formatted_date
10.2
__ANON__
7.0
CRAP 9.5
« XLSX/Backend/Regex.pm ↑ Summary XLSX/Backend.pm »
1 package Excel::ValueReader::XLSX;
2 2 ▶use 5.12.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Moose::Util::TypeConstraints qw/union/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Module::Load qw/load/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Date::Calc qw/Add_Delta_Days/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use POSIX qw/strftime modf/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Iterator::Simple qw/iter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #======================================================================
14 # GLOBALS
15 #======================================================================
16  
17 2 our $VERSION = '1.17';
18 2 our %A1_to_num_memoized;
19  
20 #======================================================================
21 # TYPES AND ATTRIBUTES
22 #======================================================================
23  
24 # TYPES
25 2 my $XlsxSource = union([qw/Str FileHandle/]);
26  
27 # PUBLIC ATTRIBUTES
28 2 has 'xlsx' => (is => 'ro', isa => $XlsxSource, required => 1); # path of xlsx file
29 2 has 'using' => (is => 'ro', isa => 'Str', default => 'Regex'); # name of backend class
30 2 has 'date_format' => (is => 'ro', isa => 'Str', default => '%d.%m.%Y');
31 2 has 'time_format' => (is => 'ro', isa => 'Str', default => '%H:%M:%S');
32 2 has 'datetime_format' => (is => 'ro', isa => 'Str', builder => '_datetime_format', lazy => 1);
33 2 has 'date_formatter' => (is => 'ro', isa => 'Maybe[CodeRef]', builder => '_date_formatter', lazy => 1);
34  
35 # ATTRIBUTES USED INTERNALLY, NOT DOCUMENTED
36 2 has 'backend' => (is => 'ro', isa => 'Object', builder => '_backend', lazy => 1,
37 init_arg => undef, handles => [qw/base_year sheets active_sheet table_info/]);
38  
39 #======================================================================
40 # BUILDING
41 #======================================================================
42  
43 # syntactic sugar for supporting ->new($path) instead of ->new(xlsx => $path)
44 30 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
45 30 my $orig = shift;
46 30 my $class = shift;
47  
48 30 ▶ unshift @_, 'xlsx' if scalar(@_) % 2 and $XlsxSource->check($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 and $XlsxSource->check($_[0]) 2 28 30
Condition66%2/3
scalar(@_) % 2 and $XlsxSource->check($_[0])
!ll&&!rl&&r
2802
MC/DC50%1/2
scalar(@_) % 2 and $XlsxSource->check($_[0])
scalar(@_) % 2 $XlsxSource->check($_[0])
Truth table66%2/3
A and B
A= scalar(@_) % 2B= $XlsxSource->check($_[0])
ABresult
0X0
100
111
49  
50 30 $class->$orig(@_);
51 2 };
52  
53  
54 #======================================================================
55 # ATTRIBUTE CONSTRUCTORS
56 #======================================================================
57  
58 30 ▶sub _backend {
Subroutine _backend: called CC=1 SCAR=0.0
59 30 my $self = shift;
60  
61 30 my $backend_class = ref($self) . '::Backend::' . $self->using;
62 30 load $backend_class;
63  
64 30 return $backend_class->new(frontend => $self);
65 }
66  
67 24 ▶sub _datetime_format {
Subroutine _datetime_format: called CC=1 SCAR=0.0
68 24 my ($self) = @_;
69 24 return $self->date_format . ' ' . $self->time_format;
70 }
71  
72 24 ▶sub _date_formatter {
Subroutine _date_formatter: called CC=1 SCAR=0.0
73 24 my ($self) = @_;
74  
75 # local copies of the various formats so that we can build a closure
76 24 my @formats = (undef, # 0 -- error
77 $self->date_format, # 1 -- just a date
78 $self->time_format, # 2 -- just a time
79 $self->datetime_format); # 3 -- date and time
80  
81 312 ▶ my $strftime_formatter = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
82 312 my ($xl_date_format, $y, $m, $d, $h, $min, $s, $ms) = @_;
83  
84 # choose the proper format for strftime
85 312 my $ix = 0; # index into the @formats array
86 312 ▶ $ix += 1 if $xl_date_format =~ /[dy]/; # the Excel format contains a date portion
Branch: 1 branch
BranchTrueFalseTotal
if $xl_date_format =~ /[dy]/u 280 32 312
87 312 ▶ $ix += 2 if $xl_date_format =~ /[hs]/; # the Excel format contains a time portion
Branch: 1 branch
BranchTrueFalseTotal
if $xl_date_format =~ /[hs]/u 48 264 312
88 312 ▶ my $strftime_format = $formats[$ix]
Branch: 1 branch
BranchTrueFalseTotal
my $strftime_format = $formats[$ix] or die "cell with unexpected Excel date format : $xl_date_format" 312 0 312
89 or die "cell with unexpected Excel date format : $xl_date_format";
90  
91 # formatting through strftime
92 312 my $formatted_date = strftime($strftime_format, $s, $min, $h, $d, $m-1, $y-1900);
93  
94 312 return $formatted_date;
95 24 };
96  
97 24 return $strftime_formatter;
98 }
99  
100  
101 #======================================================================
102 # GENERAL METHODS
103 #======================================================================
104  
105  
106 6 ▶sub sheet_names {
Subroutine sheet_names: called CC=1 SCAR=0.0
Pod documented
107 6 my ($self) = @_;
108  
109 6 my $sheets = $self->sheets; # hashref of shape {$name => $sheet_position}
110  
111 6 my @sorted_names = sort {$sheets->{$a} <=> $sheets->{$b}} keys %$sheets;
112 6 return @sorted_names;
113 }
114  
115  
116 44 ▶sub values { my ($self, $sheet) = @_; $self->backend->_values($sheet, 0)}
Subroutine values: called CC=1 SCAR=0.0
Pod documented
117 44 ▶sub ivalues { my ($self, $sheet) = @_; $self->backend->_values($sheet, 1)}
Subroutine ivalues: called CC=1 SCAR=0.0
Pod documented
118  
119 312 ▶sub formatted_date {
Subroutine formatted_date: called CC=9 SCAR=23.2
Pod documented
120 312 my ($self, $val, $date_format, $date_formatter) = @_;
121  
122 # separate date (integer part) from time (fractional part)
123 312 my ($time, $n_days) = modf($val);
124  
125 # Convert $n_days into a date in Date::Calc format (year, month, day).
126 # The algorithm is quite odd because in the 1900 system, 01.01.1900 == 0 while
127 # in the 1904 system, 01.01.1904 == 1; furthermore, in the 1900 system,
128 # Excel treats 1900 as a leap year.
129 312 my $base_year = $self->base_year;
130 312 ▶ if ($base_year == 1900) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_year == 1900) 228 84 312
131 228 my $is_after_february_1900 = $n_days > 60;
132 228 ▶ $n_days -= $is_after_february_1900 ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$is_after_february_1900 ? : 168 60 228
133 }
134 312 my @d = Add_Delta_Days($base_year, 1, 1, $n_days);
135  
136 # decode the fractional part (the time) into hours, minutes, seconds, milliseconds
137 312 my @t;
138 312 foreach my $subdivision (24, 60, 60, 1000) {
139 1248 $time *= $subdivision;
140 1248 ($time, my $time_portion) = modf($time);
141 1248 push @t, $time_portion;
142 }
143  
144 # dirty hack to deal with float imprecisions : if 999 millisecs, round to the next second
145 312 my ($h, $m, $s, $ms) = @t;
146 312 ▶ if ($ms == 999) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ms == 999) 4 308 312
147 4 $s += 1, $ms = 0;
148 4 ▶ if ($s == 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s == 60) 0 4 4
149 0 $m += 1, $s = 0;
150 0 ▶ if ($m == 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m == 60) 0 0 0
151 0 $h += 1, $m = 0;
152 }
153 }
154 }
155 # NOTE : because of this hack, theoretically we could end up with a value
156 # like 01.01.2000 24:00:00, semantically equal to 02.01.2000 00:00:00 but different
157 # in its rendering.
158  
159 # call the date_formatter subroutine
160 312 ▶ $date_formatter //= $self->date_formatter
Branch: 1 branch
BranchTrueFalseTotal
$date_formatter //= $self->date_formatter or die ref($self) . ' has no date_formatter subroutine' 312 0 312
Condition33%1/3
$date_formatter //= $self->date_formatter
l!l&&r!l&&!r
03120
MC/DC0%0/2
$date_formatter //= $self->date_formatter
$date_formatter $self->date_formatter
Truth table33%1/3
A //= B
A= $date_formatterB= $self->date_formatter
ABresult
1X1
011
000
161 or die ref($self) . " has no date_formatter subroutine";
162 312 my $formatted_date = $date_formatter->($date_format, @d, $h, $m, $s, $ms);
163  
164 312 return $formatted_date;
165 }
166  
167 #======================================================================
168 # METHODS FOR PARSING EXCEL TABLES
169 #======================================================================
170  
171  
172 4 ▶sub table_names {
Subroutine table_names: called CC=1 SCAR=0.0
Pod documented
173 4 my ($self) = @_;
174  
175 4 my $table_info = $self->table_info;
176  
177 # sort on table id (field [1] in table_info arrayrefs)
178 4 my @table_names = sort {$table_info->{$a}{id} <=> $table_info->{$b}{id}} keys %$table_info;
179  
180 4 return @table_names;
181 }
182  
183 20 ▶sub table {shift->_table(0, @_);} # 0 = does not want an iterator, just a regular arrayref
Subroutine table: called CC=1 SCAR=0.0
Pod documented
184 20 ▶sub itable {shift->_table(1, @_);} # 1 = does want an iterator
Subroutine itable: called CC=1 SCAR=0.0
Pod undocumented
185  
186 2 my %valid_table_arg = map {$_ => 1} qw/name sheet ref columns no_headers with_totals want_records/;
187 40 ▶sub _table {
Subroutine _table: called CC=21 SCAR=30.5
188 40 my $self = shift;
189 40 my $want_iterator = shift;
190  
191 # syntactic sugar : ->table('foo') is treated as ->table(name => 'foo')
192 40 ▶ unshift @_, 'name' if scalar(@_) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 32 8 40
193 40 my %args = @_;
194  
195 # check for invalid args
196 40 my @invalid_args = grep {!$valid_table_arg{$_}} keys %args;
197 40 ▶ croak "invalid table args: ", join(", ", @invalid_args) if @invalid_args;
Branch: 1 branch
BranchTrueFalseTotal
if @invalid_args 0 40 40
198  
199 # defaults
200 40 ▶ $args{want_records} //= 1;
Condition100%2/2
$args{"want_records"} //= 1
l!l
832
MC/DC100%1/1
$args{"want_records"} //= 1
$args{"want_records"}
Truth table100%2/2
A
A= $args{"want_records"}
Aresult
11
00
201  
202 # if called with a table name, derive positional args from the internal workbook info
203 40 ▶ if (my $table_name = delete $args{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $table_name = delete $args{"name"}) 32 8 40
204 32 ▶ my $table_info = $self->table_info->{$table_name}
Branch: 1 branch
BranchTrueFalseTotal
my $table_info = $self->table_info->{$table_name} or croak(sprintf("Excel file '%s' contains no table named '%s'", $self->xlsx, $table_name)) 32 0 32
205 or croak sprintf "Excel file '%s' contains no table named '%s'", $self->xlsx, $table_name;
206 32 ▶ $args{$_} //= $table_info->{$_} for keys %$table_info;
Condition66%2/3
$args{$_} //= $table_info->{$_}
l!l&&r!l&&!r
020024
MC/DC50%1/2
$args{$_} //= $table_info->{$_}
$args{$_} $table_info->{$_}
Truth table66%2/3
A //= B
A= $args{$_}B= $table_info->{$_}
ABresult
1X1
011
000
207 }
208  
209 # get values from the sheet
210 my ($sheet_ref, $vals_or_it) = $want_iterator ? $self->ivalues($args{sheet})
211 40 ▶ : $self->values($args{sheet});
Branch: 1 branch
BranchTrueFalseTotal
$want_iterator ? : 20 20 40
212  
213 # table boundaries
214 40 my ($scol1, $srow1, $scol2, $srow2) = $self->range_from_ref($sheet_ref);
215 my ($tcol1, $trow1, $tcol2, $trow2) = $args{ref} ? $self->range_from_ref($args{ref})
216 40 ▶ : ($scol1, $srow1, $scol2, $srow2);
Branch: 1 branch
BranchTrueFalseTotal
$args{'ref'} ? : 36 4 40
217 40 my $skip_initial_rows = $trow1 - $srow1;
218 40 my $keep_rows = $trow2 - $trow1 + 1;
219 40 my $skip_initial_cols = $tcol1 - $scol1;
220 40 my $keep_cols = $tcol2 - $tcol1 + 1;
221  
222 # if a totals row is present, skip it, unless the 'with_totals' arg is present
223 40 ▶ $keep_rows -=1 if $args{has_totals} and !$args{with_totals};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"has_totals"} and not $args{"with_totals"} 4 36 40
Condition100%3/3
$args{"has_totals"} and not $args{"with_totals"}
!ll&&!rl&&r
3244
MC/DC100%2/2
$args{"has_totals"} and not $args{"with_totals"}
$args{"has_totals"} not $args{"with_totals"}
Truth table100%3/3
A and not(B)
A= $args{"has_totals"}B= not $args{"with_totals"}
ABresult
0X0
100
111
224  
225 # skip initial rows if the table does not start at top row
226 40 ▶ if ($skip_initial_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_initial_rows) 20 20 40
227 20 ▶ if ($want_iterator) {$vals_or_it->() while $skip_initial_rows-- > 0;}
Branch: 2 branches
BranchTrueFalseTotal
if ($want_iterator) { } 10 10 20
$skip_initial_rows-- > 0 and do { &$vals_or_it() } 44 10 54
228 10 else {splice @$vals_or_it, 0, $skip_initial_rows}
229 }
230  
231 # read headers from first row -- even if this may be redundant with the 'columns' list declared in the table description
232 40 my $headers;
233 40 ▶ unless ($args{no_headers}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"no_headers"}) 36 4 40
234 36 $keep_rows--;
235 36 ▶ $headers = $want_iterator ? $vals_or_it->() : shift @$vals_or_it;
Branch: 1 branch
BranchTrueFalseTotal
$want_iterator ? : 18 18 36
236 36 ▶ splice @$headers, 0, $skip_initial_cols if $skip_initial_cols;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_initial_cols 20 16 36
237 36 splice @$headers, $keep_cols;
238 }
239 40 ▶ $args{columns} //= $headers;
Condition66%2/3
$args{"columns"} //= $headers
l!l&&r!l&&!r
3280
MC/DC0%0/2
$args{"columns"} //= $headers
$args{"columns"} $headers
Truth table66%2/3
A //= B
A= $args{"columns"}B= $headers
ABresult
1X1
011
000
240 40 ▶ croak "table contains undefined columns" if grep {!defined $_} @{$args{columns}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @{$args{"columns"};} 0 40 40
241  
242 # dual closure : can be used as an iterator or can compute all values in one call
243 40 my @rows;
244 348 ▶ my $get_values = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
245 348 while (1) {
246 656 ▶ $keep_rows--
Branch: 2 branches
BranchTrueFalseTotal
$keep_rows-- and my $vals = $want_iterator ? &$vals_or_it() : shift @$vals_or_it or return 616 40 656
$want_iterator ? : 308 308 616
Condition66%2/3
$keep_rows-- and my $vals = $want_iterator ? &$vals_or_it() : shift @$vals_or_it
!ll&&!rl&&r
400616
MC/DC50%1/2
$keep_rows-- and my $vals = $want_iterator ? &$vals_or_it() : shift @$vals_or_it
$keep_rows-- my $vals = $want_iterator ? &$vals_or_it() : shift @$vals_or_it
Truth table66%2/3
A and B
A= $keep_rows--B= my $vals = $want_iterator ? &$vals_or_it() : shift @$vals_or_it
ABresult
0X0
100
111
247 and my $vals = $want_iterator ? $vals_or_it->() : shift @$vals_or_it
248 or return; # no more records -- end of iterator
249 616 ▶ splice @$vals, 0, $skip_initial_cols if $skip_initial_cols;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_initial_cols 64 552 616
250 616 splice @$vals, $keep_cols;
251 616 ▶ my $row = $args{want_records} ? do {my %r; @r{@{$args{columns}}} = @$vals; \%r}
Branch: 1 branch
BranchTrueFalseTotal
$args{'want_records'} ? : 608 8 616
252 : $vals;
253 616 ▶ if ($want_iterator) {return $row} # current iteration successful
Branch: 1 branch
BranchTrueFalseTotal
if ($want_iterator) { } 308 308 616
254 308 else {push @rows, $row};
255 }
256 40 };
257  
258 # return either an iterator or the accumulated table records
259 return ($args{columns}, $want_iterator ? iter($get_values)
260 40 ▶ : do {$get_values->(); \@rows});
Branch: 1 branch
BranchTrueFalseTotal
$want_iterator ? : 20 20 40
261 }
262  
263  
264 15 ▶sub A1_to_num { # convert Excel A1 reference format to a number
Subroutine A1_to_num: called CC=2 SCAR=6.9
Pod documented
265 15 my ($self, $A1) = @_;
266 15 my $num = 0;
267 15 foreach my $digit (unpack "C*", $A1) {
268 17 $num = $num*26 + $digit-64;
269 }
270  
271 15 return $num;
272 }
273  
274  
275 76 ▶sub range_from_ref { # convert a range reference like 'C9:E21' into ($col1, $row1, $col2, $row2)
Subroutine range_from_ref: called CC=5 SCAR=18.9
Pod documented
276 76 my ($self, $range) = @_;
277  
278 76 ▶ $range =~ /^([A-Z]+)(\d+) # mandatory 1st col and row
Branch: 1 branch
BranchTrueFalseTotal
$range =~ /^([A-Z]+)(\d+) # mandatory 1st col and row (?: # .. optionally followed by : # colon ([A-Z]+)(\d+) # and 2nd col and row )? $/ux or croak("->range_from_ref($range) : invalid ref") 76 0 76
279 (?: # .. optionally followed by
280 : # colon
281 ([A-Z]+)(\d+) # and 2nd col and row
282 )?
283 $/x
284 or croak "->range_from_ref($range) : invalid ref";
285  
286 76 ▶ my @range = ($A1_to_num_memoized{$1} //= $self->A1_to_num($1), $2); # col, row of topleft cell
Condition33%1/3
$Excel::ValueReader::XLSX::A1_to_num_memoized{$1} //= $self->A1_to_num($1)
l!l&&r!l&&!r
7600
MC/DC0%0/2
$Excel::ValueReader::XLSX::A1_to_num_memoized{$1} //= $self->A1_to_num($1)
$Excel::ValueReader::XLSX::A1_to_num_memoized{$1} $self->A1_to_num($1)
Truth table33%1/3
A //= B
A= $Excel::ValueReader::XLSX::A1_to_num_memoized{$1}B= $self->A1_to_num($1)
ABresult
1X1
011
000
287 76 ▶ push @range, ($3 ? ($A1_to_num_memoized{$3} //= $self->A1_to_num($3), $4) # col, row of bottomright cell, or ..
Branch: 1 branch
BranchTrueFalseTotal
$3 ? : 76 0 76
Condition33%1/3
$Excel::ValueReader::XLSX::A1_to_num_memoized{$3} //= $self->A1_to_num($3)
l!l&&r!l&&!r
7600
MC/DC0%0/2
$Excel::ValueReader::XLSX::A1_to_num_memoized{$3} //= $self->A1_to_num($3)
$Excel::ValueReader::XLSX::A1_to_num_memoized{$3} $self->A1_to_num($3)
Truth table33%1/3
A //= B
A= $Excel::ValueReader::XLSX::A1_to_num_memoized{$3}B= $self->A1_to_num($3)
ABresult
1X1
011
000
288 : @range); # .. copy of topleft cell
289 76 return @range;
290 }
291  
292  
293  
294  
295  
296  
297 2 1;
298  
299  
300 __END__
« XLSX/Backend/Regex.pm ↑ Summary XLSX/Backend.pm »