blib/lib/Excel/Grinder.pm

filter: Statementstmt 96.0% 72 / 75 Branchbran 54.1% 13 / 24 Conditioncond 45.4% 5 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 79.6% 102 / 128 SCAR 32.6
CC
23
Cov
82%
write_excel
11.6
read_excel
9.8
new
5.8
CRAP 26.2
↑ Summary Build »
1 package Excel::Grinder;
2  
3 1 our $VERSION = "1.1";
4  
5 # time to grow up
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # this stands on the feet of giants
11 1 ▶use Excel::Writer::XLSX;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Spreadsheet::XLSX;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # OO out of habit
15 sub new {
16 1 ▶ my ($class, $default_directory) = @_;
Subroutine new: called CC=5 SCAR=17.6
Pod documented
17
18 1 my $path_separator = '/';
19 1 my $temp_directory = '/tmp';
20  
21 1 ▶ if( $^O eq "MSWin32" ){
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 1 1
22 0 $path_separator = '\\';
23 0 $temp_directory = $ENV{'temp'};
24 }
25 # default the default directory to /tmp/excel_grinder
26 1 ▶ $default_directory ||= $temp_directory . $path_separator . 'excel_grinder';
Condition50%1/2
$default_directory ||= $temp_directory . $path_separator . 'excel_grinder'
l!l
01
MC/DC0%0/1
$default_directory ||= $temp_directory . $path_separator . 'excel_grinder'
$default_directory
Truth table50%1/2
A
A= $default_directory
Aresult
11
00
27
28 # make sure that directory exists
29 1 ▶ mkdir $default_directory if !(-d $default_directory);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $default_directory 1 0 1
30
31 # if it still does exist, bail out
32 1 ▶ croak "Error: $default_directory does not exist and cannot be auto-created." if !(-d $default_directory);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $default_directory 0 1 1
33
34 # become!
35 1 my $self = bless {
36 'default_directory' => $default_directory,
37 'path_separator' => $path_separator,
38 }, $class;
39
40 1 return $self;
41 }
42  
43 # method to convert a three-level array into a nice excel file
44 sub write_excel {
45 # required arguments are (1) the filename and (2) the data structure to turn into an XLSX file
46 1 ▶ my ($self, %args) = @_;
Subroutine write_excel: called CC=11 SCAR=24.5
Pod documented
47 # looks like:
48 # 'filename' => 'some_file.xlsx', # will be saved under /opt/majestica/tmp/DATABASE_NAME/some_file.xlsx; required
49 # 'the_data' => @$three_level_arrayref, # worksheets->rows->columns; see below; required
50 # 'headings_in_data' => 1, # if filled, first row of each worksheet will be captialized; optional
51 # 'worksheet_names' => ['Names','of','Worksheets'], # if filled, will be the names to give the worksheets
52  
53 1 my ($tmp_dir, $item, $col, @bits, $workbook, $worksheet_data, $worksheet, $n, $row_array, $row_upper, $worksheet_name);
54  
55 # fail without a filename
56 1 ▶ croak 'Error: Filename required for write_excel()' if !$args{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"filename"} 0 1 1
57  
58 # the data structure must be an array of arrays of arrays
59 # three levels: worksheets, rows, columns
60 1 ▶ croak 'Error: Must send a three-level arrayref (workbook->rows->columns) to write_excel()' if !$args{the_data}[0][0][0];
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"the_data"}[0][0][0] 0 1 1
61  
62 # place into default_directory unless they specified a directory
63  
64 1 ▶ $args{filename} = $self->{default_directory}.$self->{path_separator}.$args{filename} if $args{filename} !~ m#$self->{path_separator}#;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"filename"} =~ /$self->{"path_separator"}/ 1 0 1
65 1 ▶ $args{filename} .= '.xlsx' if $args{filename} !~ /\.xlsx$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"filename"} =~ /\.xlsx$/ 0 1 1
66  
67 # start our workbook
68 1 $workbook = Excel::Writer::XLSX->new( $args{filename} );
69  
70 # Set the format for dates.
71 1 my $date_format = $workbook->add_format( num_format => 'mm/dd/yy' );
72  
73 # start adding worksheets
74 1 foreach $worksheet_data (@{ $args{the_data} }) {
75 2 $worksheet_name = shift @{ $args{worksheet_names} }; # if it's there
76 2 $worksheet_name =~ s/[^0-9a-z\-\s]//gi; # clean it up
77 2 $worksheet = $workbook->add_worksheet($worksheet_name);
78
79 # go thru each row...
80 2 $n = 0;
81 2 foreach $row_array (@$worksheet_data) {
82  
83 # do they want the first row to the headings?
84 9 ▶ if ($args{headings_in_data} && $n == 0) { # uppercase the first row
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"headings_in_data"} and $n == 0) 2 7 9
Condition66%2/3
$args{"headings_in_data"} and $n == 0
!ll&&!rl&&r
072
MC/DC50%1/2
$args{"headings_in_data"} and $n == 0
$args{"headings_in_data"} $n == 0
Truth table66%2/3
A and B
A= $args{"headings_in_data"}B= $n == 0
ABresult
0X0
100
111
85 2 @$row_upper = map { uc($_) } @$row_array;
86 2 $row_array = $row_upper;
87 }
88
89 # now each column...
90 9 $col = 0;
91 9 foreach $item (@$row_array) {
92 # dates are no funzies
93 27 ▶ if ($item =~ /^(\d{4})-(\d{2})-(\d{2})$/) { # special routine for dates
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /^(\d{4})-(\d{2})-(\d{2})$/) { } 0 27 27
94 0 $worksheet->write_date_time( $n, $col++, $1.'-'.$2.'-'.$3.'T', $date_format );
95 } else {
96 27 $worksheet->write( $n, $col++, $item );
97 }
98  
99 }
100 9 $n++;
101 }
102 }
103  
104 # that's not so hard, now is it?
105 1 return $args{filename};
106 }
107  
108 # method to import an excel file into a nice three-level array
109 sub read_excel {
110 # require argument is the filename or full path to the excel xlsx file
111 # if it's just a filename, look in the default directory
112 1 ▶ my ($self,$filename) = @_;
Subroutine read_excel: called CC=9 SCAR=22.9
Pod documented
113  
114 1 ▶ $filename = $self->{default_directory}.$self->{path_separator}.$filename if $filename !~ m#$self->{path_separator}#;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename =~ /$self->{"path_separator"}/ 1 0 1
115 1 ▶ $filename .= '.xlsx' if $filename !~ /\.xlsx$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename =~ /\.xlsx$/ 0 1 1
116  
117 # gotta exist, after all that
118 1 ▶ croak 'Error: Must send a valid full file path to an XLSX file to read_excel()' if !(-e "$filename");
Branch: 1 branch
BranchTrueFalseTotal
unless -e "$filename" 0 1 1
119
120 1 my ($excel, $sheet_num, $sheet, $row_num, $row, @the_data, $cell, $col);
121  
122 # again, stand on the shoulders of giants
123 1 $excel = Spreadsheet::XLSX->new($filename);
124  
125 # read it in, sheet by sheet
126 1 $sheet_num = 0;
127 1 foreach $sheet (@{$excel->{Worksheet}}) {
128  
129 # set the max = 0 if there is one or none rows
130 2 ▶ $sheet->{MaxRow} ||= $sheet->{MinRow};
Condition33%1/3
$sheet->{"MaxRow"} ||= $sheet->{"MinRow"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$sheet->{"MaxRow"} ||= $sheet->{"MinRow"}
$sheet->{"MaxRow"} $sheet->{"MinRow"}
Truth table33%1/3
A ||= B
A= $sheet->{"MaxRow"}B= $sheet->{"MinRow"}
ABresult
1X1
011
000
131  
132 # same for the columns
133 2 ▶ $sheet->{MaxCol} ||= $sheet->{MinCol};
Condition33%1/3
$sheet->{"MaxCol"} ||= $sheet->{"MinCol"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$sheet->{"MaxCol"} ||= $sheet->{"MinCol"}
$sheet->{"MaxCol"} $sheet->{"MinCol"}
Truth table33%1/3
A ||= B
A= $sheet->{"MaxCol"}B= $sheet->{"MinCol"}
ABresult
1X1
011
000
134  
135 # cycle through each row
136 2 $row_num = 0;
137 2 foreach $row ($sheet->{MinRow} .. $sheet->{MaxRow}) {
138 # go through each available column
139 9 foreach $col ($sheet->{MinCol} .. $sheet->{MaxCol}) {
140  
141 # get ahold of the actual cell object
142 27 $cell = $sheet->{Cells}[$row][$col];
143
144 # next if !$cell; # skip if blank
145  
146 # add it to our nice array
147 27 push (@{ $the_data[$sheet_num][$row] }, $cell->{Val} );
148 }
149 # advance
150 9 $row_num++;
151 }
152 2 $sheet_num++;
153 }
154  
155 # send it back
156 1 return \@the_data;
157 }
158  
159 1 1;
160  
161 __END__
↑ Summary Build »