blib/lib/Excel/Grinder.pm

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