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