blib/lib/Excel/Table.pm

filter: Statementstmt 98.4% 254 / 258 Branchbran 80.7% 92 / 114 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 30 / 30 Podpod 90.0% 9 / 10 totaltotal 92.6% 391 / 422 SCAR 42.2
CC
66
Cov
93%
extract
17.2
select
10.0
open_re
7.0
CRAP 67.8
↑ Summary
1 package Excel::Table;
2 #
3 # Excel::Table.pm - spreadsheet table processing class.
4 #
5 =head1 NAME
6  
7 Excel::Table
8  
9 =head1 AUTHOR
10  
11 Copyright (C) 2012 Tom McMeekin E<lt>tmcmeeki@cpan.orgE<gt>
12  
13 =head1 SYNOPSIS
14  
15 use Excel::Table;
16  
17 my $xs = Excel::Table->new('dir' => '/cygdrive/c/Users/self/Desktop');
18  
19 for ($xs->list_workbooks) {
20 print "workbook [$_]\n";
21 }
22  
23 $xs->open('mybook.xls');
24  
25 my $wb1 = $xs->open_re('foo*bar*');
26  
27 for my $worksheet ($wb1->worksheets) {
28 print "worksheet: " . $worksheet->get_name() . "\n";
29 }
30  
31 $xs->null("this is a null value");
32 $xs->force_null(1);
33  
34 $xs->rowid(0);
35  
36 $xs->trim(0);
37  
38 my @data = $xs->extract('Sheet1');
39  
40 for (@data) {
41 printf "rowid [%s] title [%s] max_width [%d] value [%s]\n",
42 $_->[0],
43 $xs->titles->[0],
44 $xs->widths->[0],
45 $data{$_}->[0];
46 }
47  
48 @data = $xs->extract_hash('Sheet1');
49  
50 @data = $xs->select("column1,column2,column3", 'Sheet1');
51  
52 @data = $xs->select_hash("column1,column2,column3", 'Sheet1');
53  
54 printf "columns %d rows %d title_row %d\n",
55 $xs->columns, $xs->rows, $xs->title_row;
56  
57 printf "regexp [%s] pathname [%s] sheet_name [%s]\n",
58 $xs->regexp, $xs->pathname, $xs->sheet_name;
59  
60 printf "colid2title(0) = [%s]\n", $xs->colid2title(0);
61  
62 printf "title2colid('Foo') = %d\n", $xs->title2colid('Foo');
63  
64 =head1 DESCRIPTION
65  
66 Excel::Table.pm - spreadsheet table processing. Retrieves worksheets as
67 if they are structured tables array-format.
68  
69 =over 4
70  
71 =item 1a. OBJ->dir(EXPR)
72  
73 Override the directory location in which to look for workbooks.
74 Defaults to "." (i.e. the current working directory).
75 This location is critical to the B<list_workbooks>, B<open>,
76 and B<open_re> methods.
77  
78 =item 1b. OBJ->list_workbooks
79  
80 Returns an array of workbook files in the directory defined by the
81 B<dir> property.
82  
83 =item 2a. OBJ->open(EXPR)
84  
85 Parses the filename specified by EXPR. The B<dir> property
86 will designate the search path.
87 Once opened, via this method (or B<open_re>) the
88 workbook is available for use by the B<extract> method.
89  
90 =item 2b. OBJ->open_re(EXPR)
91  
92 This will search for a file which has a filename matching the regexp EXPR.
93 A warning will be issued if multiple matches are found, only the first will
94 be opened.
95  
96 =item 3. OBJ->regexp
97  
98 Returns the regexp used to search for the workbook on the filesystem.
99  
100 =item 4. OBJ->pathname
101  
102 Returns the pathname of the opened workbook.
103  
104 =item 5a. OBJ->extract(EXPR,[TITLE_ROW])
105  
106 This will extract all data from the worksheet named EXPR. Data is extracted
107 into an array and returned. Format of data is per below:
108  
109 [ value1, value2, value3, ... ],
110 [ value1, value2, value3, ... ],
111 [ value1, value2, value3, ... ],
112 ...
113  
114 The object OBJ will be populated with various properties to assist you to
115 access the data in the array, including column titles and widths.
116  
117 A worksheet object is temporarily created in order to populate the array.
118 Once a worksheet is extracted, the associated worksheet object is destroyed.
119 This routine can be called again on any worksheet in the workbook.
120  
121 If the TITLE_ROW argument is specified, then the B<title_row> property will
122 also be updated prior to extraction.
123  
124 =item 5b. OBJ->extract_hash(EXPR,[TITLE_ROW])
125  
126 Per the B<extract> method, but returns an array of hashes, with the hash
127 keys corresponding to the titles.
128  
129 =item 5c. OBJ->select(CLAUSE,EXPR,[TITLE_ROW])
130  
131 Similar to the B<extract> method, this will extract all rows from the worksheet EXPR, constraining the columns to those specified by the B<clause> argument,
132 which is a comma-separated string, e.g. "column1,column2,column3".
133  
134 As with the B<extract> method, the B<titles> and B<widths> properties will
135 be revised.
136  
137 =item 5d. OBJ->select_hash(CLAUSE,EXPR,[TITLE_ROW])
138  
139 Per the B<select> method, but returns an array of hashes.
140  
141 =item 6. OBJ->columns or OBJ->rows
142  
143 Returns the number of columns or rows available in the sheet extracted via the
144 B<extract> method.
145  
146 =item 7a. OBJ->force_null
147  
148 Flag which determines if whitespace fields should be
149 replaced by specific text (see OBJ->null).
150  
151 =item 7b. OBJ->null
152  
153 String to replace whitespace fields with. Defaults to "(null)".
154  
155 =item 8. OBJ->rowid
156  
157 Flag which determines whether a pseudo-column "rowid" is included in each
158 tuple. The value will take the form "999999999" Defaults to FALSE.
159  
160 =item 9. OBJ->sheet_name
161  
162 Returns the sheet_name against which data was extracted via B<extract>.
163  
164 =item 10. OBJ->trim
165  
166 Flag which determines if trailing whitespace fields should be trimmed.
167  
168 =item 11a. OBJ->title_row
169  
170 Returns the title row of the worksheet (defaults to zero), following extract.
171  
172 =item 11b. OBJ->titles
173  
174 Returns an array of title fields, the title row number having been defined
175 as OBJ->title_row.
176  
177 =item 11c. OBJ->colid2title(colid)
178  
179 Converts the column number (colid) to a string column title (i.e.
180 the offset within the title_row array).
181 If no match, then returns undef.
182  
183 =item 11d. OBJ->title2colid(REGEXP)
184  
185 Returns the column number of the title identified by REGEXP.
186 If no match, then returns undef.
187  
188 =item 12. OBJ->widths
189  
190 Returns an array of maximum lengths of any (non-title) data in each column.
191  
192 =back
193  
194 =cut
195  
196 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
197 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
198  
199 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
200 9 ▶use Spreadsheet::ParseExcel 0.57;
Subroutine BEGIN: called CC=1 SCAR=0.0
201 9 ▶use Spreadsheet::XLSX;
Subroutine BEGIN: called CC=1 SCAR=0.0
202 9 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
203  
204 9 ▶use Carp qw(cluck confess); # only use stack backtrace within class
Subroutine BEGIN: called CC=1 SCAR=0.0
205 9 ▶use Log::Log4perl qw/ get_logger /;
Subroutine BEGIN: called CC=1 SCAR=0.0
206  
207 9 ▶use vars qw/ @EXPORT $VERSION /;
Subroutine BEGIN: called CC=1 SCAR=0.0
208  
209 9 $VERSION = "1.021"; # update this on new release
210  
211 #@ISA = qw(Exporter);
212 #@EXPORT = qw();
213  
214 # package constants
215 9 ▶use constant S_RID => "rowid";
Subroutine BEGIN: called CC=1 SCAR=0.0
216 9 ▶use constant S_NULL => "(null)";
Subroutine BEGIN: called CC=1 SCAR=0.0
217 9 use constant EXT_EXCEL => qw/
218 \.xls \.xla \.xlb \.xlc \.xld \.xlk \.xll \.xlm \.xlt
219 \.xlv \.xlw \.xls \.xlt
220 9 ▶/; # known extensions for EXCEL file
Subroutine BEGIN: called CC=1 SCAR=0.0
221  
222 # need the Spreadsheet::XLSX module for the following:
223 9 use constant EXT_EXCEL_2007 => qw/
224 \.xlsx \.xlsm \.xlsb \.xltm \.xlam
225 9 ▶/; # known extensions for EXCEL 2007 file
Subroutine BEGIN: called CC=1 SCAR=0.0
226  
227  
228 # package globals
229  
230 9 our $AUTOLOAD;
231  
232  
233 # package locals
234 9 my $n_Objects = 0; # counter of objects created.
235  
236 9 my %attribute = (
237 _n_objects => \$n_Objects,
238 _xl_vers => undef,
239 columns => undef,
240 dir => ".",
241 _log => get_logger("Excel::Table"),
242 null => S_NULL,
243 pathname => undef,
244 regexp => undef,
245 force_null => 0,
246 rows => undef,
247 rowid => 0,
248 sheet_name => undef,
249 title_row => 0, # if title row is zero, first data row is 1
250 titles => undef,
251 trim => 0,
252 widths => undef,
253 workbook => undef,
254 );
255  
256  
257 #INIT { };
258  
259  
260 sub AUTOLOAD {
261 18985 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=4 SCAR=14.2
262 18985 ▶ my $type = ref($self) or croak("self is not an object");
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $self or croak("self is not an object") 0 18985 18985
263  
264 18985 my $name = $AUTOLOAD;
265 18985 $name =~ s/.*://; # strip fully−qualified portion
266  
267 18985 ▶ unless (exists $self->{_permitted}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_permitted"}{$name}) 0 18985 18985
268 0 confess "no attribute [$name] in class [$type]";
269 }
270  
271 18985 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 328 18657 18985
272 328 return $self->{$name} = shift;
273 } else {
274 18657 return $self->{$name};
275 }
276 }
277  
278  
279 sub new {
280 24 ▶ my ($class) = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
281 #my $self = $class->SUPER::new(@_);
282 24 my $self = { _permitted => \%attribute, %attribute };
283  
284 24 ++ ${ $self->{_n_objects} };
285  
286 24 bless ($self, $class);
287  
288 24 my %args = @_; # start processing any parameters passed
289 24 my ($method,$value); # start processing any parameters passed
290 24 ▶ while (($method, $value) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 31 24 55
291  
292 31 ▶ confess "SYNTAX new(method => value, ...) value not specified"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 31 31
293 unless (defined $value);
294  
295 31 $self->_log->debug("method [self->$method($value)]");
296  
297 31 $self->$method($value);
298 }
299
300 24 return $self;
301 }
302  
303  
304 sub _determine_xl_vers {
305 290 ▶ my ($self,$pn)=@_;
Subroutine _determine_xl_vers: called CC=5 SCAR=16.1
306 290 ▶ $self->_log->logcroak("SYNTAX: _determine_xl_vers(path)")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pn 0 290 290
307 unless defined ($pn);
308 # return version string or undef for given pathname
309 290 my $extension;
310 my @extensions;
311 290 my $retval = undef;
312  
313 290 $self->_log->debug("pn [$pn]");
314  
315 290 @extensions = EXT_EXCEL;
316 290 (undef,undef,$extension) = fileparse($pn,@extensions);
317 #$self->_log->debug(sprintf " extension [%s] \@extensions [%s]", $extension, Dumper(\@extensions));
318  
319 290 ▶ $retval = 'xl2003' if ($extension ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $extension ne "" 30 260 290
320  
321 290 @extensions = EXT_EXCEL_2007;
322 290 (undef,undef,$extension) = fileparse($pn,@extensions);
323  
324 290 ▶ $retval = 'xl2007' if ($extension ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $extension ne "" 26 264 290
325  
326 290 ▶ if (defined $retval) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $retval) 56 234 290
327 56 $self->_log->debug("pn [$pn] returning [$retval]");
328 }
329  
330 290 return $retval;
331 }
332  
333  
334 sub list_workbooks {
335 13 ▶ my $self = shift;
Subroutine list_workbooks: called CC=4 SCAR=13.9
Pod documented
336  
337 13 my $dn = $self->dir;
338 13 my ($dh,$fn);
339 13 my @workbooks;
340  
341 13 $self->_log->debug("dn [$dn]");
342  
343 13 ▶ opendir($dh, $dn) || $self->_log->logcroak("opendir($dn)");
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $dn or $self->_log->logcroak("opendir($dn)") 0 13 13
344  
345 13 ▶ while ($fn = readdir($dh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($fn = readdir $dh)) 260 13 273
346 260 my $pn = File::Spec->catfile($dn, $fn);
347  
348 # need to remember just the filename here, not the path because
349 # open will use the self->dir property to make the path
350 260 ▶ push @workbooks, $fn
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_determine_xl_vers($pn) 26 234 260
351 if (defined($self->_determine_xl_vers($pn)));
352 }
353  
354 13 closedir $dh;
355  
356 13 $self->_log->debug(sprintf '@workbooks [%s]', Dumper(\@workbooks));
357  
358 13 return @workbooks;
359 }
360  
361  
362 sub open {
363 30 ▶ my ($self,$fn)=@_;
Subroutine open: called CC=6 SCAR=18.3
Pod documented
364 30 ▶ $self->_log->logcroak("SYNTAX: open(file)") unless defined ($fn);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fn 0 30 30
365 30 my $pn = File::Spec->catfile($self->dir, $fn);
366  
367 # to look for the file in the cwd, is not good behaviour,
368 # so dir must be explicit, thus the default to "."
369  
370 30 ▶ if (-f $pn) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $pn) { } 30 0 30
371 30 $self->pathname($pn);
372 } else {
373 0 $self->_log->logcroak("no such path [$pn]");
374 }
375  
376 30 $self->_log->debug("parsing [$pn]");
377  
378 30 $self->{'_xl_vers'} = $self->_determine_xl_vers($pn);
379  
380 30 my $parser;
381  
382 30 ▶ if ($self->_xl_vers eq 'xl2007') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_xl_vers eq 'xl2007') { } 13 17 30
383 13 $parser = Spreadsheet::XLSX->new($pn);
384  
385 13 ▶ $self->_log->logcroak("Spreadsheet::XLSX->new($pn) failed")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parser 0 13 13
386 unless defined $parser;
387  
388 13 $self->workbook($parser);
389 } else {
390 17 $parser = Spreadsheet::ParseExcel->new();
391 17 $self->workbook($parser->Parse($pn));
392  
393 17 ▶ $self->_log->logcroak("Parse() failed, error: " . $self->workbook->error())
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->workbook 0 17 17
394 unless defined $self->workbook;
395 }
396  
397  
398 30 return $self->workbook;
399 }
400  
401  
402 sub open_re {
403 7 ▶ my $self = shift;
Subroutine open_re: called CC=7 SCAR=19.5
Pod documented
404 7 ▶ if (@_) { $self->regexp(shift); } else { $self->_log->logcroak("SYNTAX: open_re(regexp)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 7 0 7
405 7 my $re = $self->regexp;
406 7 my $matches = 0;
407 7 my $wb = undef;
408  
409 7 $self->_log->debug(sprintf "regexp [%s]", $self->regexp);
410 7 for ( $self->list_workbooks ) {
411 14 $self->_log->debug(" file [$_]");
412 14 ▶ if ($_ =~ /$re/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /$re/) 10 4 14
413 10 $self->_log->debug(" FOUND [$_]");
414 10 ▶ $wb = $_ unless ($matches++); # remember first occurence
Branch: 1 branch
BranchTrueFalseTotal
unless $matches++ 6 4 10
415 }
416 }
417  
418 7 ▶ unless (defined $wb) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $wb) 1 6 7
419 1 $self->_log->logcarp("could not find file matching [$re]");
420 1 return undef;
421 }
422  
423 6 ▶ $self->_log->logwarn("non-unique match on [$re]")
Branch: 1 branch
BranchTrueFalseTotal
if $matches > 1 4 2 6
424 if ($matches > 1);
425  
426 6 return $self->open($wb);
427 }
428  
429  
430 sub _prepend_rowid {
431 110 ▶ my ($self, $ra_columns, $id)=@_;
Subroutine _prepend_rowid: called CC=2 SCAR=6.9
432  
433 110 ▶ my $rowid = ($id == $self->title_row) ? S_RID : sprintf "%09d", $id;
Branch: 1 branch
BranchTrueFalseTotal
$id == $self->title_row ? : 10 100 110
434  
435 110 push @$ra_columns, $rowid;
436  
437 110 return $rowid;
438 }
439  
440  
441 sub extract {
442 36 ▶ my $self = shift;
Subroutine extract: called CC=17 SCAR=28.4
Pod documented
443 36 ▶ if (@_) { $self->sheet_name(shift); }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 36 0 36
444 36 ▶ if (@_) { $self->title_row(shift); }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 36 36
445  
446 36 ▶ $self->_log->logcroak("SYNTAX: extract(sheet_name,title_row)")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->sheet_name and defined $self->title_row 0 36 36
Condition33%1/3
defined $self->sheet_name and defined $self->title_row
!ll&&!rl&&r
0036
MC/DC0%0/2
defined $self->sheet_name and defined $self->title_row
defined $self->sheet_name defined $self->title_row
Truth table33%1/3
A and B
A= defined $self->sheet_nameB= defined $self->title_row
ABresult
0X0
100
111
447 unless (defined $self->sheet_name && defined $self->title_row);
448  
449 36 $self->_log->debug(sprintf "opening [%s]", $self->sheet_name);
450  
451 36 my $ws = $self->workbook->worksheet($self->sheet_name);
452  
453 36 my ($minr, $maxr) = $ws->row_range();
454 36 my ($minc, $maxc) = $ws->col_range();
455  
456 36 $self->rows($maxr);
457 36 $self->columns($maxc + 1);
458  
459 36 ▶ $self->title_row($minr) # fix minimum row
Branch: 1 branch
BranchTrueFalseTotal
if $self->title_row < $minr 4 32 36
460 if ($self->title_row < $minr);
461  
462 36 $self->_log->debug(sprintf "sheet_name [%s] minr [%d] maxr [%d] minc [%d] maxc [%d]",
463 $self->sheet_name, $minr, $maxr, $minc, $maxc);
464  
465 36 my ($subr,$subc,$value);
466 36 my @data;
467 36 my (@columns,@widths);
468  
469 36 ▶ for ($subr = $self->title_row; $subr <= $maxr; $subr++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subr <= $maxr) 368 36 404
470  
471 368 ▶ $self->_prepend_rowid(\@columns, $subr)
Branch: 1 branch
BranchTrueFalseTotal
if $self->rowid 110 258 368
472 if ($self->rowid);
473  
474 368 ▶ for ($subc = $minc; $subc <= $maxc; $subc++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subc <= $maxc) 3584 368 3952
475  
476 3584 my $cell = $ws->get_cell($subr, $subc);
477  
478 3584 ▶ if (defined $cell) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cell) { } 3552 32 3584
479 3552 ▶ $value = ($self->trim) ? $self->_trim_whitespace($cell->value) : $cell->value;
Branch: 1 branch
BranchTrueFalseTotal
$self->trim ? : 1526 2026 3552
480 } else {
481 32 $value = undef;
482 }
483  
484 3584 ▶ $value = $self->_resolve_null($value, $self->null)
Branch: 1 branch
BranchTrueFalseTotal
if $self->force_null 440 3144 3584
485 if ($self->force_null);
486  
487 3584 push @columns, $value;
488 }
489  
490 # adjust widths, including rowid column
491 368 $subc = 0;
492 368 for $value (@columns) {
493  
494 # calculate width, ignoring title_row
495  
496 3694 ▶ if ($subr == $self->title_row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subr == $self->title_row) { } 346 3348 3694
497 346 $widths[$subc] = 0;
498 } else {
499 3348 ▶ $widths[$subc] = length($value)
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and length $value > $widths[$subc] 764 2584 3348
Condition100%3/3
defined $value and length $value > $widths[$subc]
!ll&&!rl&&r
282556764
MC/DC100%2/2
defined $value and length $value > $widths[$subc]
defined $value length $value > $widths[$subc]
Truth table100%3/3
A and B
A= defined $valueB= length $value > $widths[$subc]
ABresult
0X0
100
111
500 if (defined($value) &&
501 length($value) > $widths[$subc]);
502 }
503  
504 3694 $subc++;
505 }
506  
507 368 $self->_log->debug(sprintf '@columns [%s]', Dumper(\@columns));
508 368 $self->_log->debug(sprintf '@widths [%s]', Dumper(\@widths));
509
510 368 ▶ if ($subr == $self->title_row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subr == $self->title_row) { } 36 332 368
511 36 $self->titles([ @columns ]);
512 } else {
513 332 push @data, [ @columns ];
514 }
515 368 @columns = ();
516 }
517 36 $self->widths([ @widths ]);
518  
519 36 @widths = $ws = ();
520  
521 36 return @data;
522 }
523  
524  
525 sub colid2title {
526 4 ▶ my ($self,$colid)=@_;
Subroutine colid2title: called CC=4 SCAR=14.0
Pod documented
527  
528 4 ▶ $self->_log->logcroak("SYNTAX: colid2title2(colid)")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $colid 0 4 4
529 unless (defined $colid);
530  
531 4 $self->_log->debug("colid [$colid]");
532  
533 return undef
534 4 ▶ if ($colid < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $colid < 0 0 4 4
535  
536 return undef
537 4 ▶ unless ($colid < scalar @{ $self->titles });
Branch: 1 branch
BranchTrueFalseTotal
unless $colid < scalar @{$self->titles;} 2 2 4
538  
539 2 return $self->titles->[$colid];
540 }
541  
542  
543 sub title2colid {
544 44 ▶ my ($self,$title)=@_;
Subroutine title2colid: called CC=4 SCAR=13.9
Pod documented
545  
546 44 ▶ $self->_log->logcroak("SYNTAX: title2colid(title)")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title 0 44 44
547 unless (defined $title);
548  
549 44 $self->_log->debug("title [$title] ");
550  
551 44 my $tmax = scalar @{ $self->titles };
552  
553 44 ▶ for (my $tsub = 0; $tsub < $tmax; $tsub++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tsub < $tmax) 294 14 308
554 294 ▶ if ($self->titles->[$tsub] =~ /$title/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->titles->[$tsub] =~ /$title/) 30 264 294
555 30 $self->_log->debug("match at colid $tsub");
556 30 return $tsub;
557 }
558 }
559 14 $self->_log->debug("NO MATCH");
560  
561 14 return undef;
562 }
563  
564  
565 sub _trim_whitespace {
566 1526 ▶ my ($self,$s_value)=@_;
Subroutine _trim_whitespace: called CC=2 SCAR=7.0
567  
568 1526 ▶ if (defined $s_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s_value) 1526 0 1526
569 1526 $self->_log->debug("s_value [$s_value]");
570  
571 1526 $s_value =~ s/^[[:cntrl:][:space:]]+//; # trim leading
572 1526 $s_value =~ s/[[:cntrl:][:space:]]+$//; # trim trailing
573  
574 1526 $self->_log->debug("after s_value [$s_value]");
575 }
576  
577 1526 return $s_value;
578 }
579  
580  
581 sub _resolve_null {
582 440 ▶ my ($self, $s_value, $s_null)=@_;
Subroutine _resolve_null: called CC=5 SCAR=16.1
583  
584 440 ▶ $self->_log->debug(sprintf "s_value [%s] s_null [%s]",
Branch: 2 branches
BranchTrueFalseTotal
defined $s_value ? : 436 4 440
defined $s_null ? : 440 0 440
585 (defined $s_value) ? $s_value : "not defined",
586 (defined $s_null) ? $s_null : "not defined",
587 );
588  
589 440 ▶ if (defined $s_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s_value) { } 436 4 440
590 436 ▶ $s_value = $s_null
Branch: 1 branch
BranchTrueFalseTotal
if $s_value eq "" 2 434 436
591 if ($s_value eq "");
592 } else {
593 4 $s_value = $s_null;
594 }
595  
596 440 return $s_value;
597 }
598  
599  
600 sub _array_to_hash {
601 4 ▶ my $self = shift;
Subroutine _array_to_hash: called CC=4 SCAR=13.9
602 4 my @data;
603  
604 4 for my $row (@_) {
605 40 $self->_log->debug(sprintf '$row [%s]', Dumper($row));
606  
607 40 my %data;
608 40 my $unique = 0;
609 40 my $m_value = scalar(@$row);
610  
611 40 ▶ for (my $ss_value = 0; $ss_value < $m_value; $ss_value++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ss_value < $m_value) 260 40 300
612  
613 260 my $column = $self->titles->[$ss_value];
614 260 my $value = $row->[$ss_value];
615  
616 260 ▶ my $key = (exists $data{$column}) ? $column . $unique++ : $column;
Branch: 1 branch
BranchTrueFalseTotal
exists $data{$column} ? : 20 240 260
617 260 $data{$key} = $value;
618 }
619  
620 40 $self->_log->debug(sprintf 'data [%s]', Dumper(\%data));
621  
622 40 push @data, { %data };
623  
624 40 %data = ();
625 }
626  
627 4 return @data;
628 }
629  
630  
631 sub extract_hash {
632 2 ▶ my $self = shift;
Subroutine extract_hash: called CC=2 SCAR=7.1
Pod documented
633  
634 2 ▶ $self->_log->logcroak("SYNTAX: extract_hash(sheet_name,[title_row])")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 0 0 2 2
635 unless (@_ > 0);
636  
637 2 return $self->_array_to_hash($self->extract(@_));
638 }
639  
640  
641 sub select_hash {
642 2 ▶ my $self = shift;
Subroutine select_hash: called CC=2 SCAR=7.0
Pod documented
643 2 my $clause = shift;
644  
645 2 ▶ $self->_log->logcroak("SYNTAX: select_hash(clause,[sheet_name,title_row])")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 0 0 2 2
646 unless (@_ > 0);
647  
648 2 return $self->_array_to_hash($self->select($clause, @_));
649 }
650  
651  
652 sub select {
653 14 ▶ my $self = shift;
Subroutine select: called CC=10 SCAR=23.0
Pod documented
654 14 my $clause = shift;
655  
656 14 ▶ $self->_log->logcroak("SYNTAX: select(clause,[sheet_name,title_row])")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $clause 0 14 14
657 unless (defined $clause);
658  
659 14 my @pre = $self->extract(@_);
660 14 my (@post, @id);
661 14 my (@columns, @widths);
662  
663 14 ▶ $clause = join(',', S_RID, $clause)
Branch: 1 branch
BranchTrueFalseTotal
if $self->rowid 6 8 14
664 if ($self->rowid);
665  
666 14 for my $column (split(/,/, $clause)) {
667 40 $self->_log->debug("column [$column]");
668  
669 40 my $id = $self->title2colid($column);
670  
671 40 ▶ if (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) { } 28 12 40
672 28 push @id, $id;
673 28 push @columns, $column;
674 28 push @widths, $self->widths->[$id];
675 } else {
676 12 $self->_log->logwarn("invalid column [$column]");
677 }
678 }
679  
680 14 $self->_log->debug(sprintf '@id [%s]', Dumper(\@id));
681  
682 14 ▶ my $f_no_columns = ($self->rowid) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->rowid ? : 6 8 14
683  
684 14 ▶ unless (scalar(@columns) == $f_no_columns) { # no columns, thus no rows
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @columns == $f_no_columns) 10 4 14
685 10 for my $row (@pre) {
686 100 $self->_log->debug(sprintf 'row [%s]', Dumper($row));
687  
688 100 my @wanted = ();
689  
690 100 for my $id (@id) {
691 260 push @wanted, $row->[$id];
692 }
693  
694 100 $self->_log->debug(sprintf '@wanted [%s]', Dumper(\@wanted));
695  
696 100 ▶ push @post, [ @wanted ]
Branch: 1 branch
BranchTrueFalseTotal
if scalar @wanted 100 0 100
697 if (scalar(@wanted)); # account for null case
698 }
699 }
700  
701 14 $self->_log->debug(sprintf '@columns [%s]', Dumper(\@columns));
702 14 $self->_log->debug(sprintf '@widths [%s]', Dumper(\@widths));
703  
704 14 $self->titles([ @columns ]);
705 14 $self->widths([ @widths ]);
706  
707 14 return @post;
708 }
709  
710  
711 DESTROY {
712 24 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
713 24 -- ${ $self->{_n_objects} };
714 };
715  
716 #END { }
717  
718 9 1;
719  
720 __END__
721  
722 =head1 VERSION
723  
724 Build V1.021
725  
726 =head1 LICENSE
727  
728 This program is free software; you can redistribute it and/or modify
729 it under the terms of the GNU General Public License as published
730 by the Free Software Foundation; either version 2 of the License,
731 or any later version.
732  
733 This program is distributed in the hope that it will be useful,
734 but WITHOUT ANY WARRANTY; without even the implied warranty of
735 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
736 General Public License for more details.
737  
738 You should have received a copy of the GNU General Public License
739 along with this program; if not, write to the Free Software
740 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
741  
742 =head1 SEE ALSO
743  
744 L<perl>, Spreadsheet::ParseExcel, Spreadsheet::XLSX.
745  
746 =cut
747  
↑ Summary