blib/lib/SimpleXlsx.pm

filter: Statementstmt 19.2% 24 / 125 Branchbran 0.0% 0 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 54.5% 6 / 11 Podpod 0.0% 0 / 5 totaltotal 16.7% 30 / 179 CC 29 SCAR 50.8
CC
29
Cov
15%
getStyles
272.0
parse
156.0
getValues
12.0
CRAP 160.8
↑ Summary
1 package SimpleXlsx;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Archive::Zip qw( :ERROR_CODES );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Exporter;
11  
12 1 our @ISA = qw(Exporter);
13  
14 # Items to export into callers namespace by default. Note: do not export
15 # names by default without a very good reason. Use EXPORT_OK instead.
16 # Do not simply export all your public functions/methods/constants.
17  
18 1 our @EXPORT_OK = ( 'parse' );
19  
20 1 our $VERSION = '0.05';
21  
22 1 our $zip;
23  
24 # Preloaded methods go here.
25  
26 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
27 0 my $package = shift;
28
29 0 $zip = Archive::Zip->new();
30 0 return bless({}, $package);
31 }
32  
33 0 ▶sub getValues {
Subroutine getValues: not called CC=3 SCAR=24.8
Pod undocumented
34 0 my(@zStrings) = $zip->membersMatching('^xl/sharedStrings');
35
36 0 ▶ if ($#zStrings > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#zStrings > 0) 0 0 0
37 0 warn "Error: Multiple shared strings are not [yet] supported\n";
38 }
39
40 0 my($xml) = new XML::Simple;
41 0 my($sstrings) = $zStrings[0];
42 0 $sstrings = $sstrings->contents();
43 0 my($tstrings) = $xml->XMLin($sstrings);
44
45 0 my(@strings);
46 0 for my $idx (0 .. $#{$tstrings->{'si'}}) {
47 0 push @strings, $tstrings->{'si'}->[$idx]->{'t'};
48 }
49
50 0 return @strings;
51 }
52  
53 0 ▶sub getWorksheets {
Subroutine getWorksheets: not called CC=1 SCAR=6.9
Pod undocumented
54 0 return $zip->membersMatching('^xl/worksheets');
55 }
56  
57 0 ▶sub getStyles {
Subroutine getStyles: not called CC=16 SCAR=56.1
Pod undocumented
58 0 my(@zStyles) = $zip->membersMatching('^xl/styles');
59 0 my($data) = $zStyles[0]->contents();
60
61 0 my($xml) = new XML::Simple;
62 0 $data = $xml->XMLin($data);
63
64 0 my(@cellFormats);
65 my(%fonts);
66 0 my(%borders);
67
68 0 my($xcellFormats) = $data->{'cellXfs'}->{'xf'};
69 0 my($xfonts) = $data->{'fonts'}->{'font'};
70 0 my($xborders) = $data->{'borders'}->{'border'};
71
72 0 my($idx) = 0;
73 0 ▶ if (ref($xfonts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $xfonts) 0 0 0
74 0 for my $ind (0 .. $#{$xfonts}) {
75 $fonts{$idx} = {
76 'Name' => $xfonts->[$ind]->{'name'}->{'val'},
77 'Size' => $xfonts->[$ind]->{'sz'}->{'val'},
78 0 ▶ 'Bold' => defined $xfonts->[$ind]->{'b'} ? '1' : '0'
Branch: 1 branch
BranchTrueFalseTotal
defined $xfonts->[$ind]{'b'} ? : 0 0 0
79 };
80
81 0 $idx++;
82 }
83 }
84
85 0 $idx = 0;
86 0 for my $ind (0 .. $#{$xborders}) {
87 $borders{$idx} = {
88 'Left' => {
89 'Color' => defined $xborders->[$ind]->{'left'}->{'color'}->{'indexed'} ? $xborders->[$ind]->{'left'}->{'color'}->{'indexed'} : '',
90 'Style' => defined $xborders->[$ind]->{'left'}->{'style'} ? $xborders->[$ind]->{'left'}->{'style'} : ''
91 },
92 'Right' => {
93 'Color' => defined $xborders->[$ind]->{'right'}->{'color'}->{'indexed'} ? $xborders->[$ind]->{'right'}->{'color'}->{'indexed'} : '',
94 'Style' => defined $xborders->[$ind]->{'right'}->{'style'} ? $xborders->[$ind]->{'right'}->{'style'} : ''
95 },
96 'Top' => {
97 'Color' => defined $xborders->[$ind]->{'top'}->{'color'}->{'indexed'} ? $xborders->[$ind]->{'top'}->{'color'}->{'indexed'} : '',
98 'Style' => defined $xborders->[$ind]->{'top'}->{'style'} ? $xborders->[$ind]->{'left'}->{'top'} : ''
99 },
100 'Bottom' => {
101 'Color' => defined $xborders->[$ind]->{'bottom'}->{'color'}->{'indexed'} ? $xborders->[$ind]->{'bottom'}->{'color'}->{'indexed'} : '',
102 'Style' => defined $xborders->[$ind]->{'bottom'}->{'style'} ? $xborders->[$ind]->{'bottom'}->{'style'} : ''
103 },
104 'Diagonal' => {
105 'Color' => defined $xborders->[$ind]->{'diagonal'}->{'color'}->{'indexed'} ? $xborders->[$ind]->{'diagonal'}->{'color'}->{'indexed'} : '',
106 0 ▶ 'Style' => defined $xborders->[$ind]->{'diagonal'}->{'style'} ? $xborders->[$ind]->{'diagonal'}->{'style'} : ''
Branch: 10 branches
BranchTrueFalseTotal
defined $xborders->[$ind]{'left'}{'color'}{'indexed'} ? : 0 0 0
defined $xborders->[$ind]{'left'}{'style'} ? : 0 0 0
defined $xborders->[$ind]{'right'}{'color'}{'indexed'} ? : 0 0 0
defined $xborders->[$ind]{'right'}{'style'} ? : 0 0 0
defined $xborders->[$ind]{'top'}{'color'}{'indexed'} ? : 0 0 0
defined $xborders->[$ind]{'top'}{'style'} ? : 0 0 0
defined $xborders->[$ind]{'bottom'}{'color'}{'indexed'} ? : 0 0 0
defined $xborders->[$ind]{'bottom'}{'style'} ? : 0 0 0
defined $xborders->[$ind]{'diagonal'}{'color'}{'indexed'} ? : 0 0 0
defined $xborders->[$ind]{'diagonal'}{'style'} ? : 0 0 0
107 }
108 };
109
110 0 $idx++;
111 }
112
113 0 $idx = 0;
114 0 for my $ind (0 .. $#{$xcellFormats}) {
115 0 my($bix) = $xcellFormats->[$ind]->{'borderId'};
116 push @cellFormats, {
117 'fillId' => $xcellFormats->[$ind]->{'fillId'},
118 'Font' => $fonts{$xcellFormats->[$ind]->{'fontId'}},
119 'xfId' => $xcellFormats->[$ind]->{'xfId'},
120 'numFmtId' => $xcellFormats->[$ind]->{'numFmtId'},
121 0 'Border' => $borders{$bix}
122 };
123
124 0 $idx++;
125 }
126
127 0 return \@cellFormats;
128 }
129  
130 0 ▶sub parse {
Subroutine parse: not called CC=12 SCAR=50.5
Pod undocumented
131 0 my($self, $file) = @_;
132
133 0 my($ret) = $zip->read($file);
134 0 ▶ unless ($ret == AZ_OK) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret == 0) 0 0 0
135 0 warn "Unable to read file \"$file\" ($!)\n";
136 0 return undef;
137 }
138
139 # For now we are only interested in worksheets and the shared strings
140 0 my(@zWorksheets) = $self->getWorksheets();
141 0 my(@strings) = $self->getValues();
142 0 my($styles) = $self->getStyles();
143 0 my(%worksheets);
144 my(@sheetNames);
145
146 0 $worksheets{'Worksheets'} = [];
147
148 0 $worksheets{'Total Worksheets'} = ($#zWorksheets + 1);
149 0 for my $file (@zWorksheets) {
150 0 my(%worksheet);
151 0 my($contents) = $file->contents();
152 0 my($name) = basename($file->fileName());
153 0 $name =~ s/\.xml$//;
154
155 0 my($xml) = new XML::Simple;
156 0 my($data) = $xml->XMLin($contents);
157
158 0 my($sData) = $data->{'sheetData'}->{'row'};
159 0 my($sMerge) = $data->{'mergeCells'}->{'mergeCell'};
160
161 0 my(%merge);
162 0 for my $mc (@{$sMerge}) {
163 0 my($from, $to) = split(':', $mc->{'ref'});
164
165 0 $from =~ /([a-zA-Z]+)([0-9]+)/;
166 0 my($col1, $row1) = ($1, $2);
167
168 0 $to =~ /([a-zA-Z]+)([0-9]+)/;
169 0 my($col2, $row2) = ($1, $2);
170
171 0 $merge{$row1} = {
172 'From' => { 'Row' => $row1, 'Column' => $col1 },
173 'To' => { 'Row' => $row2, 'Column' => $col2 }
174 };
175 }
176
177 0 my(@tcol);
178 0 for my $col (0 .. $#{$sData->[0]->{'c'}}) {
179 0 push @tcol, $sData->[0]->{'c'}->[$col]->{'r'};
180 }
181 0 $worksheet{'Columns'} = \@tcol;
182
183 0 my(@trow);
184 my(%tdata);
185 0 for my $row (0 .. $#{$sData}) {
186 0 my($cols) = $sData->[$row]->{'c'};
187
188 0 my(@rdata);
189 my(@sdata);
190 0 for my $col (0 .. $#{$cols}) {
191 0 ▶ if (!defined $cols->[$col]->{'v'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cols->[$col]{'v'}) { } 0 0 0
192 0 push @rdata, '';
193 }
194 else {
195 0 ▶ if (defined $cols->[$col]->{'t'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cols->[$col]{"t"}) 0 0 0
196 0 ▶ push @rdata, ($cols->[$col]->{'t'} eq 's' ? $strings[$cols->[$col]->{'v'}] : $cols->[$col]->{'v'});
Branch: 1 branch
BranchTrueFalseTotal
$cols->[$col]{'t'} eq 's' ? : 0 0 0
197 }
198 }
199  
200 0 ▶ if (defined $styles->[$cols->[$col]->{'s'}]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$styles[$cols->[$col]{"s"}]) 0 0 0
201 0 push @sdata, $styles->[$cols->[$col]->{'s'}];
202 }
203 }
204  
205 0 ▶ if (defined $sData->[$row]->{'r'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sData->[$row]{"r"}) 0 0 0
206 0 push @trow, $sData->[$row]->{'r'};
207 0 $tdata{$sData->[$row]->{'r'}}{'Data'} = \@rdata;
208 0 $tdata{$sData->[$row]->{'r'}}{'Style'} = \@sdata;
209 }
210 }
211
212 0 $worksheet{'Rows'} = \@trow;
213 0 $worksheet{'Data'} = \%tdata;
214 0 $worksheet{'Merge'} = \%merge;
215
216 0 $worksheets{$name} = \%worksheet;
217
218 0 push @sheetNames, $name;
219 }
220
221 0 $worksheets{'Worksheets'} = \@sheetNames;
222
223 0 return \%worksheets;
224 }
225  
226 1 1;
227 __END__
↑ Summary