| 1 | package Spreadsheet::XLSX; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use 5.006000; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use base 'Spreadsheet::ParseExcel::Workbook'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 12 | our $VERSION = '0.20'; | |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 12 | our $MAX_MEMBER_SIZE = 64 * 1024 * 1024; | |||||||||||||||||||||||||||||||
| 12 | 12 | our $MAX_TOTAL_SIZE = 256 * 1024 * 1024; | |||||||||||||||||||||||||||||||
| 13 | 12 | our $MAX_MEMBERS = 10000; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 12 | ▶ | use Archive::Zip; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 12 | ▶ | use Spreadsheet::ParseExcel; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 12 | ▶ | use Spreadsheet::XLSX::Fmt2007; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | ################################################################################ | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 67 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 22 | 67 | my ($class, $filename, $converter) = @_; | |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 67 | my %shared_info; # shared_strings, styles, style_info, rels, converter | |||||||||||||||||||||||||||||||
| 25 | 67 | $shared_info{converter} = $converter; | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 67 | my $self = bless Spreadsheet::ParseExcel::Workbook->new(), $class; | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 67 | my $zip = __load_zip($filename); | |||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 64 | $shared_info{shared_strings}= __load_shared_strings($zip, $shared_info{converter}); | |||||||||||||||||||||||||||||||
| 32 | 61 | my ($styles, $style_info) = __load_styles($zip); | |||||||||||||||||||||||||||||||
| 33 | 37 | $shared_info{styles} = $styles; | |||||||||||||||||||||||||||||||
| 34 | 37 | $shared_info{style_info} = $style_info; | |||||||||||||||||||||||||||||||
| 35 | 37 | $shared_info{rels} = __load_rels($zip); | |||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 35 | $self->_load_workbook($zip, \%shared_info); | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 19 | return $self; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 35 | ▶ | sub _load_workbook { | ||||||||||||||||||||||||||||||
|
Subroutine
_load_workbook: called CC=37 SCAR=36.3
| |||||||||||||||||||||||||||||||||
| 43 | 35 | my ($self, $zip, $shared_info) = @_; | |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 35 | ▶ | my $member_workbook = $zip->memberNamed('xl/workbook.xml') or die("xl/workbook.xml not found in this zip\n"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 35 | $self->{SheetCount} = 0; | |||||||||||||||||||||||||||||||
| 47 | 35 | $self->{FmtClass} = Spreadsheet::XLSX::Fmt2007->new; | |||||||||||||||||||||||||||||||
| 48 | 35 | $self->{Flg1904} = 0; | |||||||||||||||||||||||||||||||
| 49 | 35 | my $workbook_xml = __member_contents($member_workbook); | |||||||||||||||||||||||||||||||
| 50 | 33 | ▶ | if ($workbook_xml =~ /date1904="1"/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 0 | $self->{Flg1904} = 1; | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 33 | foreach ($workbook_xml =~ /\<(.*?)\/?\>/g) { | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 214 | /^(\w+)\s+/; | |||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 214 | my ($tag, $other) = ($1, $'); | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 214 | my @pairs = split /\" /, $other; | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 214 | ▶ | $tag eq 'sheet' or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 42 | my $sheet = { | |||||||||||||||||||||||||||||||
| 65 | MaxRow => 0, | ||||||||||||||||||||||||||||||||
| 66 | MaxCol => 0, | ||||||||||||||||||||||||||||||||
| 67 | MinRow => 1000000, | ||||||||||||||||||||||||||||||||
| 68 | MinCol => 1000000, | ||||||||||||||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 42 | foreach ($other =~ /(\S+=".*?")/gsm) { | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 129 | my ($k, $v) = split /=?"/; #" | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 129 | ▶ | if ($k eq 'name') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 76 | 42 | $sheet->{Name} = $v; | |||||||||||||||||||||||||||||||
| 77 | 42 | ▶ | $sheet->{Name} = $shared_info->{converter}->convert($sheet->{Name}) if defined $shared_info->{converter}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | } elsif ($k eq 'r:id') { | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 42 | $sheet->{path} = $shared_info->{rels}->{$v}; | |||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 42 | my $wsheet = Spreadsheet::ParseExcel::Worksheet->new(%$sheet); | |||||||||||||||||||||||||||||||
| 87 | 42 | $self->{Worksheet}[$self->{SheetCount}] = $wsheet; | |||||||||||||||||||||||||||||||
| 88 | 42 | $self->{SheetCount} += 1; | |||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 33 | foreach my $sheet (@{$self->{Worksheet}}) { | |||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 42 | ▶ | my $member_sheet = $zip->memberNamed("xl/$sheet->{path}") or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 42 | my ($row, $col); | |||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 42 | my $parsing_v_tag = 0; | |||||||||||||||||||||||||||||||
| 100 | 42 | my $s = 0; | |||||||||||||||||||||||||||||||
| 101 | 42 | my $s2 = 0; | |||||||||||||||||||||||||||||||
| 102 | 42 | my $sty = 0; | |||||||||||||||||||||||||||||||
| 103 | 42 | my $sheet_xml = __member_contents($member_sheet); | |||||||||||||||||||||||||||||||
| 104 | 40 | foreach ($sheet_xml =~ /(\<.*?\/?\>|.*?(?=\<))/g) { | |||||||||||||||||||||||||||||||
| 105 | 10238 | ▶ | if (/^<c(?:\s|\/?>)/) { | ||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 1666 | my ($reference) = /\sr="([^"]*)"/; | |||||||||||||||||||||||||||||||
| 108 | 1666 | ($row, $col) = __decode_cell_name($reference); | |||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 1655 | ▶ | $s = m/t=\"s\"/ ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 1655 | ▶ | $s2 = m/t=\"str\"/ ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | 1655 | ▶ | $sty = m/s="([0-9]+)"/ ? $1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | } elsif (/^<v>/) { | ||||||||||||||||||||||||||||||||
| 115 | 1653 | $parsing_v_tag = 1; | |||||||||||||||||||||||||||||||
| 116 | } elsif (/^<\/v>/) { | ||||||||||||||||||||||||||||||||
| 117 | 1652 | $parsing_v_tag = 0; | |||||||||||||||||||||||||||||||
| 118 | ▶ | } elsif (length($_) && $parsing_v_tag) { | |||||||||||||||||||||||||||||||
|
Condition66%2/3
length $_ and $parsing_v_tag
MC/DC50%1/2
length $_ and $parsing_v_tag
length $_
$parsing_v_tag
Truth table66%2/3
A and B
A= length $_B= $parsing_v_tag
| |||||||||||||||||||||||||||||||||
| 119 | 1653 | ▶ | my $v = $s ? $shared_info->{shared_strings}->[$_] : $_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 1653 | ▶ | if ($v eq "</c>") { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | 0 | $v = ""; | |||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | 1653 | my $type = "Text"; | |||||||||||||||||||||||||||||||
| 125 | 1653 | my $thisstyle = ""; | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 1653 | ▶ | if (not($s) && not($s2)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$s or $s2
MC/DC50%1/2
$s or $s2
$s
$s2
Truth table66%2/3
A or B
A= $sB= $s2
| |||||||||||||||||||||||||||||||||
| 128 | 817 | $type = "Numeric"; | |||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 817 | ▶ | if (defined $sty && defined $shared_info->{styles}->[$sty]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $sty and defined $shared_info->{"styles"}[$sty]
MC/DC50%1/2
defined $sty and defined $shared_info->{"styles"}[$sty]
defined $sty
defined $shared_info->{"styles"}[$sty]
Truth table66%2/3
A and B
A= defined $styB= defined $shared_info->{"styles"}[$sty]
| |||||||||||||||||||||||||||||||||
| 131 | 793 | $thisstyle = $shared_info->{style_info}->{$shared_info->{styles}->[$sty]}; | |||||||||||||||||||||||||||||||
| 132 | 793 | ▶ | if ($thisstyle =~ /\b(mmm|m|d|yy|h|hh|mm|ss)\b/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 133 | 4 | $type = "Date"; | |||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 1653 | ▶ | $sheet->{MaxRow} = $row if $sheet->{MaxRow} < $row; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 140 | 1653 | ▶ | $sheet->{MaxCol} = $col if $sheet->{MaxCol} < $col; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | 1653 | ▶ | $sheet->{MinRow} = $row if $sheet->{MinRow} > $row; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 142 | 1653 | ▶ | $sheet->{MinCol} = $col if $sheet->{MinCol} > $col; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 1653 | ▶ | if ($v =~ /(.*)E\-(.*)/gsm && $type eq "Numeric") { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$v =~ /(.*)E\-(.*)/gms and $type eq "Numeric"
MC/DC50%1/2
$v =~ /(.*)E\-(.*)/gms and $type eq "Numeric"
$v =~ /(.*)E\-(.*)/gms
$type eq "Numeric"
Truth table66%2/3
A and B
A= $v =~ /(.*)E\-(.*)/gmsB= $type eq "Numeric"
| |||||||||||||||||||||||||||||||||
| 145 | 97 | $v = $1 / (10**$2); # this handles scientific notation for very small numbers | |||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 1653 | my $cell = Spreadsheet::ParseExcel::Cell->new( | |||||||||||||||||||||||||||||||
| 149 | Val => $v, | ||||||||||||||||||||||||||||||||
| 150 | Format => $thisstyle, | ||||||||||||||||||||||||||||||||
| 151 | Type => $type | ||||||||||||||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | 1653 | $cell->{_Value} = $self->{FmtClass}->ValFmt($cell, $self); | |||||||||||||||||||||||||||||||
| 155 | 1652 | ▶ | if ($type eq "Date") { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 156 | 3 | ▶ | if ($v < 1) { #then this is Excel time field | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 157 | 1 | $cell->{Type} = "Text"; | |||||||||||||||||||||||||||||||
| 158 | 1 | $cell->{Val} = $cell->{_Value}; | |||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | 1652 | $sheet->{Cells}[$row][$col] = $cell; | |||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 28 | ▶ | $sheet->{MinRow} = 0 if $sheet->{MinRow} > $sheet->{MaxRow}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 166 | 28 | ▶ | $sheet->{MinCol} = 0 if $sheet->{MinCol} > $sheet->{MaxCol}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 19 | return $self; | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | # Validate before numeric conversion or using a reference as an array index. | ||||||||||||||||||||||||||||||||
| 174 | # XLSX worksheets have at most 1,048,576 rows and 16,384 columns (XFD). | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 1666 | ▶ | sub __decode_cell_name { | ||||||||||||||||||||||||||||||
|
Subroutine
__decode_cell_name: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 177 | 1666 | my ($reference) = @_; | |||||||||||||||||||||||||||||||
| 178 | 1666 | ▶ | die "Invalid XLSX cell reference\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $reference and $reference =~ /\A([A-Z]{1,3})([1-9][0-9]{0,6})\z/
MC/DC100%2/2
defined $reference and $reference =~ /\A([A-Z]{1,3})([1-9][0-9]{0,6})\z/
defined $reference
$reference =~ /\A([A-Z]{1,3})([1-9][0-9]{0,6})\z/
Truth table100%3/3
A and B
A= defined $referenceB= $reference =~ /\A([A-Z]{1,3})([1-9][0-9]{0,6})\z/
| |||||||||||||||||||||||||||||||||
| 179 | unless defined($reference) | ||||||||||||||||||||||||||||||||
| 180 | && $reference =~ /\A([A-Z]{1,3})([1-9][0-9]{0,6})\z/; | ||||||||||||||||||||||||||||||||
| 181 | 1658 | my ($letters, $digits) = ($1, $2); | |||||||||||||||||||||||||||||||
| 182 | 1658 | ▶ | die "XLSX cell row exceeds worksheet limit\n" if $digits > 1048576; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 1656 | my $col = 0; | |||||||||||||||||||||||||||||||
| 185 | 1656 | $col = $col * 26 + ord($_) - ord('A') + 1 for split //, $letters; | |||||||||||||||||||||||||||||||
| 186 | 1656 | ▶ | die "XLSX cell column exceeds worksheet limit\n" if $col > 16384; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | 1655 | return ($digits - 1, $col - 1); | |||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | 64 | ▶ | sub __load_shared_strings { | ||||||||||||||||||||||||||||||
|
Subroutine
__load_shared_strings: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 193 | 64 | my ($zip, $converter) = @_; | |||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | 64 | my $member_shared_strings = $zip->memberNamed('xl/sharedStrings.xml'); | |||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | 64 | my @shared_strings = (); | |||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | 64 | ▶ | if ($member_shared_strings) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | 20 | my $mstr = __member_contents($member_shared_strings); | |||||||||||||||||||||||||||||||
| 202 | 17 | $mstr =~ s/<t\/>/<t><\/t>/gsm; # this handles an empty t tag in the xml <t/> | |||||||||||||||||||||||||||||||
| 203 | 17 | foreach my $si ($mstr =~ /<si.*?>(.*?)<\/si/gsm) { | |||||||||||||||||||||||||||||||
| 204 | 843 | my $str; | |||||||||||||||||||||||||||||||
| 205 | 843 | foreach my $t ($si =~ /<t.*?>(.*?)<\/t/gsm) { | |||||||||||||||||||||||||||||||
| 206 | 843 | ▶ | $t = $converter->convert($t) if defined $converter; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 207 | 843 | $str .= $t; | |||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||
| 209 | 843 | push @shared_strings, $str; | |||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | 61 | return \@shared_strings; | |||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | 61 | ▶ | sub __load_styles { | ||||||||||||||||||||||||||||||
|
Subroutine
__load_styles: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||
| 218 | 61 | my ($zip) = @_; | |||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | 61 | my $member_styles = $zip->memberNamed('xl/styles.xml'); | |||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | 61 | my @styles = (); | |||||||||||||||||||||||||||||||
| 223 | 61 | my %style_info = (); | |||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | 61 | ▶ | if ($member_styles) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 226 | 45 | my $formatter = Spreadsheet::XLSX::Fmt2007->new(); | |||||||||||||||||||||||||||||||
| 227 | 45 | my $styles_xml = __member_contents($member_styles); | |||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | # IDs from both format declarations and style references are data, | ||||||||||||||||||||||||||||||||
| 230 | # never regular-expression syntax. | ||||||||||||||||||||||||||||||||
| 231 | 43 | foreach my $id ($styles_xml =~ /\bnumFmtId="([^"]*)"/g) { | |||||||||||||||||||||||||||||||
| 232 | 260 | ▶ | die "Invalid XLSX number format ID\n" unless $id =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | 21 | foreach my $t ($styles_xml =~ /xf\ numFmtId="([^"]*)"(?!.*\/cellStyleXfs)/gsm) { #" | |||||||||||||||||||||||||||||||
| 236 | 73 | push @styles, $t; | |||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | 21 | ▶ | my $default = $1 || ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$1 || ''
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||
| 241 | 21 | foreach my $t1 (@styles) { | |||||||||||||||||||||||||||||||
| 242 | 73 | $styles_xml =~ /numFmtId="\Q$t1\E" formatCode="([^"]*)/; | |||||||||||||||||||||||||||||||
| 243 | 73 | ▶ | my $formatCode = $1 || ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$1 || ''
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||||||||||||||||
| 244 | 73 | ▶ | if ($formatCode eq $default || not($formatCode)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$formatCode eq $default or not $formatCode
MC/DC100%2/2
$formatCode eq $default or not $formatCode
$formatCode eq $default
not $formatCode
Truth table100%3/3
A or not(B)
A= $formatCode eq $defaultB= not $formatCode
| |||||||||||||||||||||||||||||||||
| 245 | 55 | ▶ | if ($t1 == 9 || $t1 == 10) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$t1 == 9 or $t1 == 10
MC/DC50%1/2
$t1 == 9 or $t1 == 10
$t1 == 9
$t1 == 10
Truth table66%2/3
A or B
A= $t1 == 9B= $t1 == 10
| |||||||||||||||||||||||||||||||||
| 246 | 1 | $formatCode = '0.00000%'; | |||||||||||||||||||||||||||||||
| 247 | } elsif ($t1 == 14) { | ||||||||||||||||||||||||||||||||
| 248 | 3 | $formatCode = 'yyyy-mm-dd'; | |||||||||||||||||||||||||||||||
| 249 | } else { | ||||||||||||||||||||||||||||||||
| 250 | 51 | $formatCode = ''; | |||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||
| 252 | # $formatCode = $formatter->FmtStringDef($t1); | ||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||
| 254 | 73 | $style_info{$t1} = $formatCode; | |||||||||||||||||||||||||||||||
| 255 | 73 | ▶ | $default = $1 || ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$1 || ''
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||
| 259 | 37 | return (\@styles, \%style_info); | |||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | 37 | ▶ | sub __load_rels { | ||||||||||||||||||||||||||||||
|
Subroutine
__load_rels: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||||||
| 264 | 37 | my ($zip) = @_; | |||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||
| 266 | 37 | ▶ | my $member_rels = $zip->memberNamed('xl/_rels/workbook.xml.rels') or die("xl/_rels/workbook.xml.rels not found in this zip\n"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | 37 | my %rels = (); | |||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | 37 | my $rels_xml = __member_contents($member_rels); | |||||||||||||||||||||||||||||||
| 271 | 35 | foreach ($rels_xml =~ /\<Relationship (.*?)\/?\>/g) { | |||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||
| 273 | 58 | my ($id, $target); | |||||||||||||||||||||||||||||||
| 274 | 58 | ($id) = /Id="(.*?)"/; | |||||||||||||||||||||||||||||||
| 275 | 58 | ($target) = /Target="(.*?)"/; | |||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||
| 277 | 58 | ▶ | if (defined $id and defined $target) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $id and defined $target
MC/DC0%0/2
defined $id and defined $target
defined $id
defined $target
Truth table33%1/3
A and B
A= defined $idB= defined $target
| |||||||||||||||||||||||||||||||||
| 278 | 58 | $rels{$id} = $target; | |||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | 35 | return \%rels; | |||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | 67 | ▶ | sub __load_zip { | ||||||||||||||||||||||||||||||
|
Subroutine
__load_zip: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||
| 287 | 67 | my ($filename) = @_; | |||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | 67 | my $zip = Archive::Zip->new(); | |||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | 67 | ▶ | if (ref $filename) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 292 | 2 | ▶ | $zip->readFromFileHandle($filename) == Archive::Zip::AZ_OK or die("Cannot open data as Zip archive"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 293 | } else { | ||||||||||||||||||||||||||||||||
| 294 | 65 | ▶ | $zip->read($filename) == Archive::Zip::AZ_OK or die("Cannot open $filename as Zip archive"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | 67 | my @members = $zip->members(); | |||||||||||||||||||||||||||||||
| 298 | 67 | ▶ | die "XLSX ZIP entry count exceeds limit\n" if @members > $MAX_MEMBERS; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 299 | 66 | my $total = 0; | |||||||||||||||||||||||||||||||
| 300 | 66 | foreach my $member (@members) { | |||||||||||||||||||||||||||||||
| 301 | 308 | my $size = $member->uncompressedSize(); | |||||||||||||||||||||||||||||||
| 302 | 308 | ▶ | die "XLSX ZIP member size exceeds limit\n" if $size > $MAX_MEMBER_SIZE; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 303 | 307 | $total += $size; | |||||||||||||||||||||||||||||||
| 304 | 307 | ▶ | die "XLSX ZIP total size exceeds limit\n" if $total > $MAX_TOTAL_SIZE; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | 64 | return $zip; | |||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | # readChunk() limits compressed input, not inflated output. Small input chunks | ||||||||||||||||||||||||||||||||
| 311 | # bound temporary inflate buffers; check output before appending it. Never trust | ||||||||||||||||||||||||||||||||
| 312 | # central-directory sizes alone: actual output must not exceed the declared size. | ||||||||||||||||||||||||||||||||
| 313 | 179 | ▶ | sub __member_contents { | ||||||||||||||||||||||||||||||
|
Subroutine
__member_contents: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||
| 314 | 179 | my ($member) = @_; | |||||||||||||||||||||||||||||||
| 315 | 179 | my $declared = $member->uncompressedSize(); | |||||||||||||||||||||||||||||||
| 316 | 179 | ▶ | die "XLSX ZIP member size exceeds limit\n" if $declared > $MAX_MEMBER_SIZE; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 317 | 179 | my $old_method = $member->desiredCompressionMethod(Archive::Zip::COMPRESSION_STORED); | |||||||||||||||||||||||||||||||
| 318 | 179 | my $contents = ''; | |||||||||||||||||||||||||||||||
| 319 | 179 | my $ok = eval { | |||||||||||||||||||||||||||||||
| 320 | 179 | ▶ | die "Cannot read XLSX ZIP member\n" unless $member->rewindData() == Archive::Zip::AZ_OK; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 321 | 179 | while (1) { | |||||||||||||||||||||||||||||||
| 322 | 304 | my ($buffer, $status) = $member->readChunk(1024); | |||||||||||||||||||||||||||||||
| 323 | 304 | ▶ | die "Cannot read XLSX ZIP member\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status == 0 or $status == 1
MC/DC0%0/2
$status == 0 or $status == 1
$status == 0
$status == 1
Truth table66%2/3
A or B
A= $status == 0B= $status == 1
| |||||||||||||||||||||||||||||||||
| 324 | unless $status == Archive::Zip::AZ_OK || $status == Archive::Zip::AZ_STREAM_END; | ||||||||||||||||||||||||||||||||
| 325 | 304 | my $size = length($contents) + length($$buffer); | |||||||||||||||||||||||||||||||
| 326 | 304 | ▶ | die "XLSX ZIP member size exceeds limit\n" if $size > $MAX_MEMBER_SIZE; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 327 | 303 | ▶ | die "XLSX ZIP member size does not match metadata\n" if $size > $declared; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 328 | 298 | $contents .= $$buffer; | |||||||||||||||||||||||||||||||
| 329 | 298 | ▶ | last if $status == Archive::Zip::AZ_STREAM_END; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||
| 331 | 173 | ▶ | die "XLSX ZIP member size does not match metadata\n" if length($contents) != $declared; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 332 | 168 | 1; | |||||||||||||||||||||||||||||||
| 333 | }; | ||||||||||||||||||||||||||||||||
| 334 | 179 | my $error = $@; | |||||||||||||||||||||||||||||||
| 335 | 179 | $member->endRead(); | |||||||||||||||||||||||||||||||
| 336 | 179 | $member->desiredCompressionMethod($old_method); | |||||||||||||||||||||||||||||||
| 337 | 179 | ▶ | die $error unless $ok; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 338 | 168 | return $contents; | |||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | 12 | 1; | |||||||||||||||||||||||||||||||
| 343 | __END__ | ||||||||||||||||||||||||||||||||