ParseExcel/Utility.pm

filter: Statementstmt 74.0% 420 / 567 Branchbran 68.6% 302 / 440 Conditioncond 72.3% 141 / 195 MC/DCmcdc 52.6% 50 / 95 Subroutinesub 85.7% 12 / 14 Podpod 63.6% 7 / 11 totaltotal 70.5% 932 / 1322 CC 290 SCAR 63.7
CC
290
Cov
72%
ExcelFmt
769.2
xls2csv
420.0
sheetRef
20.0
CRAP 586.2
↑ Summary ParseExcel/SaveParser/Workbook.pm »
1 package Spreadsheet::ParseExcel::Utility;
2  
3 ###############################################################################
4 #
5 # Spreadsheet::ParseExcel::Utility - Utility functions for ParseExcel.
6 #
7 # Used in conjunction with Spreadsheet::ParseExcel.
8 #
9 # Copyright (c) 2014 Douglas Wilson
10 # Copyright (c) 2009-2013 John McNamara
11 # Copyright (c) 2006-2008 Gabor Szabo
12 # Copyright (c) 2000-2006 Kawai Takanori
13 #
14 # perltidy with standard settings.
15 #
16 # Documentation after __END__
17 #
18  
19 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 28 require Exporter;
23 28 ▶use vars qw(@ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 28 @ISA = qw(Exporter);
25 28 @EXPORT_OK = qw(ExcelFmt LocaltimeExcel ExcelLocaltime
26 col2int int2col sheetRef xls2csv);
27  
28 28 our $VERSION = '0.66';
29  
30 28 my $qrNUMBER = qr/(^[+-]?\d+(\.\d+)?$)|(^[+-]?\d+\.?(\d*)[eE][+-](\d+))$/;
31  
32 ###############################################################################
33 #
34 # ExcelFmt()
35 #
36 # This function takes an Excel style number format and converts a number into
37 # that format. for example: 'hh:mm:ss AM/PM' + 0.01023148 = '12:14:44 AM'.
38 #
39 # It does this with a type of templating mechanism. The format string is parsed
40 # to identify tokens that need to be replaced and their position within the
41 # string is recorded. These can be thought of as placeholders. The number is
42 # then converted to the required formats and substituted into the placeholders.
43 #
44 # Interested parties should refer to the Excel documentation on cell formats for
45 # more information: http://office.microsoft.com/en-us/excel/HP051995001033.aspx
46 # The Microsoft documentation for the Excel Binary File Format, [MS-XLS].pdf,
47 # also contains a ABNF grammar for number format strings.
48 #
49 # Maintainers notes:
50 # ==================
51 #
52 # Note on format subsections:
53 # A format string can contain 4 possible sub-sections separated by semi-colons:
54 # Positive numbers, negative numbers, zero values, and text.
55 # For example: _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)
56 #
57 # Note on conditional formats.
58 # A number format in Excel can have a conditional expression such as:
59 # [>9999999](000)000-0000;000-0000
60 # This is equivalent to the following in Perl:
61 # $format = $number > 9999999 ? '(000)000-0000' : '000-0000';
62 # Nested conditionals are also possible but we don't support them.
63 #
64 # Efficiency: The excessive use of substr() isn't very efficient. However,
65 # it probably doesn't merit rewriting this function with a parser or regular
66 # expressions and \G.
67 #
68 # TODO: I think the single quote handling may not be required. Check.
69 #
70 782 ▶sub ExcelFmt {
Subroutine ExcelFmt: called CC=219 SCAR=66.5
Pod documented
71  
72 782 my ( $format_str, $number, $is_1904, $number_type, $want_subformats ) = @_;
73  
74 # Return text strings without further formatting.
75 782 ▶ return $number unless $number =~ $qrNUMBER;
Branch: 1 branch
BranchTrueFalseTotal
unless $number =~ /$qrNUMBER/ 0 782 782
76  
77 # Handle OpenOffice.org GENERAL format.
78 782 ▶ $format_str = '@' if uc($format_str) eq "GENERAL";
Branch: 1 branch
BranchTrueFalseTotal
if uc $format_str eq "GENERAL" 250 532 782
79  
80 # Check for a conditional at the start of the format. See notes above.
81 782 my $conditional_op;
82 my $conditional_value;
83 782 ▶ if ( $format_str =~ /^\[([<>=]+)([^\]]+)\](.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format_str =~ /^\[([<>=]+)([^\]]+)\](.*)$/) 0 782 782
84 0 $conditional_op = $1;
85 0 $conditional_value = $2;
86 0 $format_str = $3;
87 }
88  
89 # Ignore the underscore token which is used to indicate a padding space.
90 782 $format_str =~ s/_/ /g;
91  
92 # Split the format string into 4 possible sub-sections: positive numbers,
93 # negative numbers, zero values, and text. See notes above.
94 782 my @formats;
95 782 my $section = 0;
96 782 my $double_quote = 0;
97 782 my $single_quote = 0;
98  
99 # Initial parsing of the format string to remove escape characters. This
100 # also handles quoted strings. See note about single quotes above.
101 CHARACTER:
102 782 for my $char ( split //, $format_str ) {
103  
104 6665 ▶ if ( $double_quote or $single_quote ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($double_quote or $single_quote) 102 6563 6665
Condition100%3/3
$double_quote or $single_quote
l!l&&r!l&&!r
58446563
MC/DC100%2/2
$double_quote or $single_quote
$double_quote $single_quote
Truth table100%3/3
A or B
A= $double_quoteB= $single_quote
ABresult
1X1
011
000
105 102 $formats[$section] .= $char;
106 102 ▶ $double_quote = 0 if $char eq '"';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\"" 29 73 102
107 102 $single_quote = 0;
108 102 next CHARACTER;
109 }
110  
111 6563 ▶ if ( $char eq ';' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($char eq ';') { } 45 6518 6563
elsif ($char eq '"') { } 29 6489 6518
elsif ($char eq '!') { } 0 6489 6489
elsif ($char eq '\\') { } 44 6445 6489
elsif ($char eq '(') { } 70 6375 6445
elsif ($char eq ')') { } 63 6312 6375
112 45 $section++;
113 45 next CHARACTER;
114 }
115 elsif ( $char eq '"' ) {
116 29 $double_quote = 1;
117 }
118 elsif ( $char eq '!' ) {
119 0 $single_quote = 1;
120 }
121 elsif ( $char eq '\\' ) {
122 44 $single_quote = 1;
123 }
124 elsif ( $char eq '(' ) {
125 70 next CHARACTER; # Ignore.
126 }
127 elsif ( $char eq ')' ) {
128 63 next CHARACTER; # Ignore.
129 }
130  
131 # Convert upper case OpenOffice.org date/time formats to lowercase..
132 6385 ▶ $char = lc($char) if $char =~ /[DMYHS]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[DMYHS]/ 60 6325 6385
133  
134 6385 $formats[$section] .= $char;
135 }
136  
137 # Select the appropriate format from the 4 possible sub-sections:
138 # positive numbers, negative numbers, zero values, and text.
139 # We ignore the Text section since non-numeric values are returned
140 # unformatted at the start of the function.
141 782 my $format;
142 782 $section = 0;
143  
144 782 ▶ if ( @formats == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@formats == 1) { } 754 28 782
elsif (@formats == 2) { } 18 10 28
elsif (@formats == 3) { } 3 7 10
145 754 $section = 0;
146 }
147 elsif ( @formats == 2 ) {
148 18 ▶ if ( $number < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number < 0) { } 8 10 18
149 8 $section = 1;
150 }
151 else {
152 10 $section = 0;
153 }
154 }
155 elsif ( @formats == 3 ) {
156 3 ▶ if ( $number == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($number == 0) { } 1 2 3
elsif ($number < 0) { } 1 1 2
157 1 $section = 2;
158 }
159 elsif ( $number < 0 ) {
160 1 $section = 1;
161 }
162 else {
163 1 $section = 0;
164 }
165 }
166 else {
167 7 $section = 0;
168 }
169  
170 # Override the previous choice if the format is conditional.
171 782 ▶ if ($conditional_op) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conditional_op) 0 782 782
172 0 ▶ if ($conditional_op eq '>') {
Branch: 7 branches
BranchTrueFalseTotal
if ($conditional_op eq '>') { } 0 0 0
elsif ($conditional_op eq '>=') { } 0 0 0
elsif ($conditional_op eq '<') { } 0 0 0
elsif ($conditional_op eq '<=') { } 0 0 0
elsif ($conditional_op eq '=') { } 0 0 0
elsif ($conditional_op eq '==') { } 0 0 0
elsif ($conditional_op eq '<>') { } 0 0 0
173 0 ▶ $section = $number > $conditional_value ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$number > $conditional_value ? : 0 0 0
174 } elsif ($conditional_op eq '>=') {
175 0 ▶ $section = $number >= $conditional_value ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$number >= $conditional_value ? : 0 0 0
176 } elsif ($conditional_op eq '<') {
177 0 ▶ $section = $number < $conditional_value ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$number < $conditional_value ? : 0 0 0
178 } elsif ($conditional_op eq '<=') {
179 0 ▶ $section = $number <= $conditional_value ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$number <= $conditional_value ? : 0 0 0
180 } elsif ($conditional_op eq '=') {
181 0 ▶ $section = $number == $conditional_value ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$number == $conditional_value ? : 0 0 0
182 } elsif ($conditional_op eq '==') {
183 0 ▶ $section = $number == $conditional_value ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$number == $conditional_value ? : 0 0 0
184 } elsif ($conditional_op eq '<>') {
185 0 ▶ $section = $number != $conditional_value ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$number != $conditional_value ? : 0 0 0
186 }
187 }
188 # We now have the required format.
189 782 $format = $formats[$section];
190  
191 # The format string can contain one of the following colours:
192 # [Black] [Blue] [Cyan] [Green] [Magenta] [Red] [White] [Yellow]
193 # or the string [ColorX] where x is a colour index from 1 to 56.
194 # We don't use the colour but we return it to the caller.
195 #
196 782 my $color = '';
197 782 ▶ if ( $format =~ s/^(\[[A-Za-z]{3,}(\d{1,2})?\])// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ s/^(\[[A-Za-z]{3,}(\d{1,2})?\])//) 4 778 782
198 4 $color = $1;
199 }
200  
201 # Remove the locale, such as [$-409], from the format string.
202 782 my $locale = '';
203 782 ▶ if ( $format =~ s/^(\[\$?-F?\d+\])// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ s/^(\[\$?-F?\d+\])//) 3 779 782
204 3 $locale = $1;
205 }
206  
207 # Replace currency locale, such as [$$-409], with $ in the format string.
208 # See the RT#60547 test cases in 21_number_format_user.t.
209 782 ▶ if ( $format =~ s/(\[\$([^-]+)(-\d+)?\])/$2/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ s/(\[\$([^-]+)(-\d+)?\])/$2/s) 3 779 782
210 3 $locale = $1;
211 }
212  
213  
214 # Remove leading # from '# ?/?', '# ??/??' fraction formats.
215 782 $format =~ s{# \?}{?}g;
216  
217 # Parse the format string and create an AoA of placeholders that contain
218 # the parts of the string to be replaced. The format of the information
219 # stored is: [ $token, $start_pos, $end_pos, $option_info ].
220 #
221 782 my $format_mode = ''; # Either: '', 'number', 'date'
222 782 my $pos = 0; # Character position within format string.
223 782 my @placeholders = (); # Arefs with parts of the format to be replaced.
224 782 my $token = ''; # The actual format extracted from the total str.
225 782 my $start_pos; # A position variable. Initial parser position.
226 782 my $token_start = -1; # A position variable.
227 782 my $decimal_pos = -1; # Position of the punctuation char "." or ",".
228 782 my $comma_count = 0; # Count of the commas in the format.
229 782 my $is_fraction = 0; # Number format is a fraction.
230 782 my $is_currency = 0; # Number format is a currency.
231 782 my $is_percent = 0; # Number format is a percentage.
232 782 my $is_12_hour = 0; # Time format is using 12 hour clock.
233 782 my $seen_dot = 0; # Treat only the first "." as the decimal point.
234  
235 # Parse the format.
236 PARSER:
237 782 ▶ while ( $pos < length $format ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $format) 4185 782 4967
238 4185 $start_pos = $pos;
239 4185 my $char = substr( $format, $pos, 1 );
240  
241 # Ignore control format characters such as '#0+-.?eE,%'. However,
242 # only ignore '.' if it is the first one encountered. RT 45502.
243 4185 ▶ if ( ( !$seen_dot && $char !~ /[#0\+\-\.\?eE\,\%]/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_dot || $char =~ /[#0\+\-\.\?eE\,\%]/ and $char =~ /[#0\+\-\?eE\,\%]/) 2840 1345 4185
Condition100%6/6
$seen_dot || $char =~ /[#0\+\-\.\?eE\,\%]/ and $char =~ /[#0\+\-\?eE\,\%]/
!ll&&!rl&&r
25013391345
$seen_dot or $char =~ /[#0\+\-\.\?eE\,\%]/
l!l&&r!l&&!r
8508342501
MC/DC100%3/3
$seen_dot || $char =~ /[#0\+\-\.\?eE\,\%]/ and $char =~ /[#0\+\-\?eE\,\%]/
$seen_dot $char =~ /[#0\+\-\.\?eE\,\%]/ $char =~ /[#0\+\-\?eE\,\%]/
Truth table100%5/5
A or B and C
A= $seen_dotB= $char =~ /[#0\+\-\.\?eE\,\%]/C= $char =~ /[#0\+\-\?eE\,\%]/
ABCresult
00X0
1X00
0100
1X11
0111
244 || $char !~ /[#0\+\-\?eE\,\%]/ )
245 {
246  
247 2840 ▶ if ( $token_start != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_start != -1) 111 2729 2840
248 111 push @placeholders,
249 [
250 substr( $format, $token_start, $pos - $token_start ),
251 $decimal_pos, $pos - $token_start
252 ];
253 111 $token_start = -1;
254 }
255 }
256  
257 # Processing for quoted strings within the format. See notes above.
258 4185 ▶ if ( $char eq '"' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq '"') { } 44 4141 4185
elsif ($char eq '!') { } 0 4141 4141
elsif ($char eq '\\') { } 44 4097 4141
259 44 ▶ $double_quote = $double_quote ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$double_quote ? : 22 22 44
260 44 $pos++;
261 44 next PARSER;
262 }
263 elsif ( $char eq '!' ) {
264 0 $single_quote = 1;
265 0 $pos++;
266 0 next PARSER;
267 }
268 elsif ( $char eq '\\' ) {
269 44 ▶ if ( $single_quote != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($single_quote != 1) 44 0 44
270 44 $single_quote = 1;
271 44 $pos++;
272 44 next PARSER;
273 }
274 }
275  
276 4097 ▶ if ( ( defined($double_quote) and ($double_quote) )
Branch: 7 branches
BranchTrueFalseTotal
if (defined $double_quote and $double_quote or defined $single_quote and $single_quote or $seen_dot and $char eq '.') { } 75 4022 4097
elsif ($char =~ /[#0\+\.\?eE\,\%]/ or $format_mode ne 'date' and $char eq '-' || $char eq '(' || $char eq ')') { } 1354 2668 4022
elsif ($char =~ /[ymdhsapg]/i) { } 1415 1253 2668
elsif (substr($format, $pos, 3) eq '[h]') { } 10 1243 1253
elsif (substr($format, $pos, 4) eq '[mm]') { } 3 1240 1243
elsif ($char eq '@') { } 251 989 1240
elsif ($char eq '*') { } 7 982 989
Condition86%13/15
defined $double_quote and $double_quote or defined $single_quote and $single_quote or $seen_dot and $char eq "."
l!l&&r!l&&!r
6694022
defined $double_quote and $double_quote or defined $single_quote and $single_quote
l!l&&r!l&&!r
22444031
defined $double_quote and $double_quote
!ll&&!rl&&r
0407522
defined $single_quote and $single_quote
!ll&&!rl&&r
0403144
$seen_dot and $char eq "."
!ll&&!rl&&r
31918319
MC/DC66%4/6
defined $double_quote and $double_quote or defined $single_quote and $single_quote or $seen_dot and $char eq "."
defined $double_quote $double_quote defined $single_quote $single_quote $seen_dot $char eq "."
Truth table33%5/15
A and B or C and D or E and F
A= defined $double_quoteB= $double_quoteC= defined $single_quoteD= $single_quoteE= $seen_dotF= $char eq "."
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
277 or ( defined($single_quote) and ($single_quote) )
278 or ( $seen_dot && $char eq '.' ) )
279 {
280 75 $single_quote = 0;
281 75 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($format_mode ne 'date' and substr($format, $pos, 2) eq "\201\242" || substr($format, $pos, 2) eq "\201\243" || substr($format, $pos, 2) eq "\242\244" || substr($format, $pos, 2) eq "\242\245") { } 0 75 75
Condition41%5/12
$format_mode ne "date" and substr($format, $pos, 2) eq "\201\242" || substr($format, $pos, 2) eq "\201\243" || substr($format, $pos, 2) eq "\242\244" || substr($format, $pos, 2) eq "\242\245"
!ll&&!rl&&r
65100
substr($format, $pos, 2) eq "\201\242" or substr($format, $pos, 2) eq "\201\243" or substr($format, $pos, 2) eq "\242\244" or substr($format, $pos, 2) eq "\242\245"
l!l&&r!l&&!r
0010
substr($format, $pos, 2) eq "\201\242" or substr($format, $pos, 2) eq "\201\243" or substr($format, $pos, 2) eq "\242\244"
l!l&&r!l&&!r
0010
substr($format, $pos, 2) eq "\201\242" or substr($format, $pos, 2) eq "\201\243"
l!l&&r!l&&!r
0010
MC/DC0%0/5
$format_mode ne "date" and substr($format, $pos, 2) eq "\201\242" || substr($format, $pos, 2) eq "\201\243" || substr($format, $pos, 2) eq "\242\244" || substr($format, $pos, 2) eq "\242\245"
$format_mode ne "date" substr($format, $pos, 2) eq "\201\242" substr($format, $pos, 2) eq "\201\243" substr($format, $pos, 2) eq "\242\244" substr($format, $pos, 2) eq "\242\245"
Truth table33%2/6
A and B or C or D or E
A= $format_mode ne "date"B= substr($format, $pos, 2) eq "\201\242"C= substr($format, $pos, 2) eq "\201\243"D= substr($format, $pos, 2) eq "\242\244"E= substr($format, $pos, 2) eq "\242\245"
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
282 ( $format_mode ne 'date' )
283 and ( ( substr( $format, $pos, 2 ) eq "\x81\xA2" )
284 || ( substr( $format, $pos, 2 ) eq "\x81\xA3" )
285 || ( substr( $format, $pos, 2 ) eq "\xA2\xA4" )
286 || ( substr( $format, $pos, 2 ) eq "\xA2\xA5" ) )
287 )
288 {
289  
290 # The above matches are currency symbols.
291 0 push @placeholders,
292 [ substr( $format, $pos, 2 ), length($token), 2 ];
293 0 $is_currency = 1;
294 0 $pos += 2;
295 }
296 else {
297 75 $pos++;
298 }
299 }
300 ▶ elsif (
Condition50%6/12
$char =~ /[#0\+\.\?eE\,\%]/ or $format_mode ne "date" and $char eq "-" || $char eq "(" || $char eq ")"
l!l&&r!l&&!r
135402668
$format_mode ne "date" and $char eq "-" || $char eq "(" || $char eq ")"
!ll&&!rl&&r
19467220
$char eq "-" or $char eq "(" or $char eq ")"
l!l&&r!l&&!r
00722
$char eq "-" or $char eq "("
l!l&&r!l&&!r
00722
MC/DC20%1/5
$char =~ /[#0\+\.\?eE\,\%]/ or $format_mode ne "date" and $char eq "-" || $char eq "(" || $char eq ")"
$char =~ /[#0\+\.\?eE\,\%]/ $format_mode ne "date" $char eq "-" $char eq "(" $char eq ")"
Truth table50%3/6
A or B and C or D or E
A= $char =~ /[#0\+\.\?eE\,\%]/B= $format_mode ne "date"C= $char eq "-"D= $char eq "("E= $char eq ")"
ABCDEresult
1XXXX1
011XX1
0101X1
010011
00XXX0
010000
301 ( $char =~ /[#0\+\.\?eE\,\%]/ )
302 || ( ( $format_mode ne 'date' )
303 and ( ( $char eq '-' ) || ( $char eq '(' ) || ( $char eq ')' ) )
304 )
305 )
306 {
307 1354 ▶ $format_mode = 'number' unless $format_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $format_mode 118 1236 1354
308 1354 ▶ if ( substr( $format, $pos, 1 ) =~ /[#0]/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if (substr($format, $pos, 1) =~ /[#0]/) { } 1012 342 1354
elsif (substr($format, $pos, 1) eq '?') { } 2 340 342
elsif (substr($format, $pos, 3) =~ /^[eE][\+\-][0#]$/) { } 0 340 340
309 1012 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (substr($format, $pos) =~ /^([#0]+[\.]?[0#]*[eE][\+\-][0#]+)/) { } 2 1010 1012
310 substr( $format, $pos ) =~
311 /^([#0]+[\.]?[0#]*[eE][\+\-][0#]+)/ )
312 {
313 2 push @placeholders, [ $1, $pos, length($1) ];
314 2 $pos += length($1);
315 }
316 else {
317 1010 ▶ if ( $token_start == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_start == -1) 438 572 1010
318 438 $token_start = $pos;
319 438 $decimal_pos = length($token);
320 }
321 }
322 }
323 elsif ( substr( $format, $pos, 1 ) eq '?' ) {
324  
325 # Look for a fraction format like ?/? or ??/??
326 2 ▶ if ( $token_start != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_start != -1) 0 2 2
327 0 push @placeholders,
328 [
329 substr(
330 $format, $token_start, $pos - $token_start + 1
331 ),
332 $decimal_pos,
333 $pos - $token_start + 1
334 ];
335 }
336 2 $token_start = $pos;
337  
338 # Find the end of the fraction format.
339 FRACTION:
340 2 ▶ while ( $pos < length($format) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $format) 8 2 10
341 8 ▶ if ( substr( $format, $pos, 1 ) eq '/' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($format, $pos, 1) eq '/') { } 2 6 8
elsif (substr($format, $pos, 1) eq '?') { } 6 0 6
342 2 $is_fraction = 1;
343 }
344 elsif ( substr( $format, $pos, 1 ) eq '?' ) {
345 6 $pos++;
346 6 next FRACTION;
347 }
348 else {
349 0 ▶ if ( $is_fraction
Branch: 1 branch
BranchTrueFalseTotal
if ($is_fraction and substr($format, $pos, 1) =~ /[0-9]/) { } 0 0 0
Condition0%0/3
$is_fraction and substr($format, $pos, 1) =~ /[0-9]/
!ll&&!rl&&r
000
MC/DC0%0/2
$is_fraction and substr($format, $pos, 1) =~ /[0-9]/
$is_fraction substr($format, $pos, 1) =~ /[0-9]/
Truth table0%0/3
A and B
A= $is_fractionB= substr($format, $pos, 1) =~ /[0-9]/
ABresult
0X0
100
111
350 && ( substr( $format, $pos, 1 ) =~ /[0-9]/ ) )
351 {
352  
353 # TODO: Could invert if() logic and remove this.
354 0 $pos++;
355 0 next FRACTION;
356 }
357 else {
358 0 last FRACTION;
359 }
360 }
361 2 $pos++;
362 }
363 2 $pos--;
364  
365 2 push @placeholders,
366 [
367 substr( $format, $token_start, $pos - $token_start + 1 ),
368 length($token), $pos - $token_start + 1
369 ];
370 2 $token_start = -1;
371 }
372 elsif ( substr( $format, $pos, 3 ) =~ /^[eE][\+\-][0#]$/ ) {
373 0 ▶ if ( substr( $format, $pos ) =~ /([eE][\+\-][0#]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($format, $pos) =~ /([eE][\+\-][0#]+)/) 0 0 0
374 0 push @placeholders, [ $1, $pos, length($1) ];
375 0 $pos += length($1);
376 }
377 0 $token_start = -1;
378 }
379 else {
380 340 ▶ if ( $token_start != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_start != -1) 38 302 340
381 38 push @placeholders,
382 [
383 substr( $format, $token_start, $pos - $token_start ),
384 $decimal_pos, $pos - $token_start
385 ];
386 38 $token_start = -1;
387 }
388 340 ▶ if ( substr( $format, $pos, 1 ) =~ /[\+\-]/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if (substr($format, $pos, 1) =~ /[\+\-]/) { } 2 338 340
elsif (substr($format, $pos, 1) eq '.') { } 296 42 338
elsif (substr($format, $pos, 1) eq ',') { } 33 9 42
elsif (substr($format, $pos, 1) eq '%') { } 9 0 9
elsif (substr($format, $pos, 1) eq '(' or substr($format, $pos, 1) eq ')') { } 0 0 0
389 2 push @placeholders,
390 [ substr( $format, $pos, 1 ), length($token), 1 ];
391 2 $is_currency = 1;
392 }
393 elsif ( substr( $format, $pos, 1 ) eq '.' ) {
394 296 push @placeholders,
395 [ substr( $format, $pos, 1 ), length($token), 1 ];
396 296 $seen_dot = 1;
397 }
398 elsif ( substr( $format, $pos, 1 ) eq ',' ) {
399 33 $comma_count++;
400 33 push @placeholders,
401 [ substr( $format, $pos, 1 ), length($token), 1 ];
402 }
403 elsif ( substr( $format, $pos, 1 ) eq '%' ) {
404 9 $is_percent = 1;
405 }
406 ▶ elsif (( substr( $format, $pos, 1 ) eq '(' )
Condition0%0/3
substr($format, $pos, 1) eq "(" or substr($format, $pos, 1) eq ")"
l!l&&r!l&&!r
000
MC/DC0%0/2
substr($format, $pos, 1) eq "(" or substr($format, $pos, 1) eq ")"
substr($format, $pos, 1) eq "(" substr($format, $pos, 1) eq ")"
Truth table0%0/3
A or B
A= substr($format, $pos, 1) eq "("B= substr($format, $pos, 1) eq ")"
ABresult
1X1
011
000
407 || ( substr( $format, $pos, 1 ) eq ')' ) )
408 {
409 0 push @placeholders,
410 [ substr( $format, $pos, 1 ), length($token), 1 ];
411 0 $is_currency = 1;
412 }
413 }
414 1354 $pos++;
415 }
416 elsif ( $char =~ /[ymdhsapg]/i ) {
417 1415 ▶ $format_mode = 'date' unless $format_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $format_mode 400 1015 1415
418 1415 ▶ if ( substr( $format, $pos, 5 ) =~ /am\/pm/i ) {
Branch: 7 branches
BranchTrueFalseTotal
if (substr($format, $pos, 5) =~ m[am/pm]i) { } 9 1406 1415
elsif (substr($format, $pos, 3) =~ m[a/p]i) { } 0 1406 1406
elsif (substr($format, $pos, 5) eq 'mmmmm') { } 12 1394 1406
elsif (substr($format, $pos, 4) eq 'mmmm' or substr($format, $pos, 4) eq 'dddd' or substr($format, $pos, 4) eq 'yyyy' or substr($format, $pos, 4) eq 'ggge') { } 223 1171 1394
elsif (substr($format, $pos, 3) eq 'ddd' or substr($format, $pos, 3) eq 'mmm' or substr($format, $pos, 3) eq 'yyy') { } 48 1123 1171
elsif (substr($format, $pos, 2) eq 'yy' or substr($format, $pos, 2) eq 'mm' or substr($format, $pos, 2) eq 'dd' or substr($format, $pos, 2) eq 'hh' or substr($format, $pos, 2) eq 'ss' or substr($format, $pos, 2) eq 'ge') { } 1034 89 1123
elsif (substr($format, $pos, 1) eq 'm' or substr($format, $pos, 1) eq 'd' or substr($format, $pos, 1) eq 'h' or substr($format, $pos, 1) eq 's') { } 89 0 89
419 9 push @placeholders, [ 'am/pm', length($token), 5 ];
420 9 $is_12_hour = 1;
421 9 $pos += 5;
422 }
423 elsif ( substr( $format, $pos, 3 ) =~ /a\/p/i ) {
424 0 push @placeholders, [ 'a/p', length($token), 3 ];
425 0 $is_12_hour = 1;
426 0 $pos += 3;
427 }
428 elsif ( substr( $format, $pos, 5 ) eq 'mmmmm' ) {
429 12 push @placeholders, [ 'mmmmm', length($token), 5 ];
430 12 $pos += 5;
431 }
432 ▶ elsif (( substr( $format, $pos, 4 ) eq 'mmmm' )
Condition100%9/9
substr($format, $pos, 4) eq "mmmm" or substr($format, $pos, 4) eq "dddd" or substr($format, $pos, 4) eq "yyyy" or substr($format, $pos, 4) eq "ggge"
l!l&&r!l&&!r
21941171
substr($format, $pos, 4) eq "mmmm" or substr($format, $pos, 4) eq "dddd" or substr($format, $pos, 4) eq "yyyy"
l!l&&r!l&&!r
231961175
substr($format, $pos, 4) eq "mmmm" or substr($format, $pos, 4) eq "dddd"
l!l&&r!l&&!r
1491371
MC/DC100%4/4
substr($format, $pos, 4) eq "mmmm" or substr($format, $pos, 4) eq "dddd" or substr($format, $pos, 4) eq "yyyy" or substr($format, $pos, 4) eq "ggge"
substr($format, $pos, 4) eq "mmmm" substr($format, $pos, 4) eq "dddd" substr($format, $pos, 4) eq "yyyy" substr($format, $pos, 4) eq "ggge"
Truth table100%5/5
A or B or C or D
A= substr($format, $pos, 4) eq "mmmm"B= substr($format, $pos, 4) eq "dddd"C= substr($format, $pos, 4) eq "yyyy"D= substr($format, $pos, 4) eq "ggge"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
433 || ( substr( $format, $pos, 4 ) eq 'dddd' )
434 || ( substr( $format, $pos, 4 ) eq 'yyyy' )
435 || ( substr( $format, $pos, 4 ) eq 'ggge' ) )
436 {
437 223 push @placeholders,
438 [ substr( $format, $pos, 4 ), length($token), 4 ];
439 223 $pos += 4;
440 }
441 ▶ elsif (( substr( $format, $pos, 3 ) eq 'ddd' )
Condition83%5/6
substr($format, $pos, 3) eq "ddd" or substr($format, $pos, 3) eq "mmm" or substr($format, $pos, 3) eq "yyy"
l!l&&r!l&&!r
4801123
substr($format, $pos, 3) eq "ddd" or substr($format, $pos, 3) eq "mmm"
l!l&&r!l&&!r
14341123
MC/DC66%2/3
substr($format, $pos, 3) eq "ddd" or substr($format, $pos, 3) eq "mmm" or substr($format, $pos, 3) eq "yyy"
substr($format, $pos, 3) eq "ddd" substr($format, $pos, 3) eq "mmm" substr($format, $pos, 3) eq "yyy"
Truth table75%3/4
A or B or C
A= substr($format, $pos, 3) eq "ddd"B= substr($format, $pos, 3) eq "mmm"C= substr($format, $pos, 3) eq "yyy"
ABCresult
1XX1
01X1
0011
0000
442 || ( substr( $format, $pos, 3 ) eq 'mmm' )
443 || ( substr( $format, $pos, 3 ) eq 'yyy' ) )
444 {
445 48 push @placeholders,
446 [ substr( $format, $pos, 3 ), length($token), 3 ];
447 48 $pos += 3;
448 }
449 ▶ elsif (( substr( $format, $pos, 2 ) eq 'yy' )
Condition100%15/15
substr($format, $pos, 2) eq "yy" or substr($format, $pos, 2) eq "mm" or substr($format, $pos, 2) eq "dd" or substr($format, $pos, 2) eq "hh" or substr($format, $pos, 2) eq "ss" or substr($format, $pos, 2) eq "ge"
l!l&&r!l&&!r
1030489
substr($format, $pos, 2) eq "yy" or substr($format, $pos, 2) eq "mm" or substr($format, $pos, 2) eq "dd" or substr($format, $pos, 2) eq "hh" or substr($format, $pos, 2) eq "ss"
l!l&&r!l&&!r
80722393
substr($format, $pos, 2) eq "yy" or substr($format, $pos, 2) eq "mm" or substr($format, $pos, 2) eq "dd" or substr($format, $pos, 2) eq "hh"
l!l&&r!l&&!r
602205316
substr($format, $pos, 2) eq "yy" or substr($format, $pos, 2) eq "mm" or substr($format, $pos, 2) eq "dd"
l!l&&r!l&&!r
420182521
substr($format, $pos, 2) eq "yy" or substr($format, $pos, 2) eq "mm"
l!l&&r!l&&!r
12408703
MC/DC100%6/6
substr($format, $pos, 2) eq "yy" or substr($format, $pos, 2) eq "mm" or substr($format, $pos, 2) eq "dd" or substr($format, $pos, 2) eq "hh" or substr($format, $pos, 2) eq "ss" or substr($format, $pos, 2) eq "ge"
substr($format, $pos, 2) eq "yy" substr($format, $pos, 2) eq "mm" substr($format, $pos, 2) eq "dd" substr($format, $pos, 2) eq "hh" substr($format, $pos, 2) eq "ss" substr($format, $pos, 2) eq "ge"
Truth table100%7/7
A or B or C or D or E or F
A= substr($format, $pos, 2) eq "yy"B= substr($format, $pos, 2) eq "mm"C= substr($format, $pos, 2) eq "dd"D= substr($format, $pos, 2) eq "hh"E= substr($format, $pos, 2) eq "ss"F= substr($format, $pos, 2) eq "ge"
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
450 || ( substr( $format, $pos, 2 ) eq 'mm' )
451 || ( substr( $format, $pos, 2 ) eq 'dd' )
452 || ( substr( $format, $pos, 2 ) eq 'hh' )
453 || ( substr( $format, $pos, 2 ) eq 'ss' )
454 || ( substr( $format, $pos, 2 ) eq 'ge' ) )
455 {
456 1034 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (substr($format, $pos, 2) eq 'mm' and @placeholders and $placeholders[-1][0] eq 'h' || $placeholders[-1][0] eq 'hh') { } 221 813 1034
Condition100%9/9
substr($format, $pos, 2) eq "mm" and @placeholders and $placeholders[-1][0] eq "h" || $placeholders[-1][0] eq "hh"
!ll&&!rl&&r
641172221
substr($format, $pos, 2) eq "mm" and @placeholders
!ll&&!rl&&r
62615393
$placeholders[-1][0] eq "h" or $placeholders[-1][0] eq "hh"
l!l&&r!l&&!r
16205172
MC/DC100%4/4
substr($format, $pos, 2) eq "mm" and @placeholders and $placeholders[-1][0] eq "h" || $placeholders[-1][0] eq "hh"
substr($format, $pos, 2) eq "mm" @placeholders $placeholders[-1][0] eq "h" $placeholders[-1][0] eq "hh"
Truth table100%5/5
A and B and C or D
A= substr($format, $pos, 2) eq "mm"B= @placeholdersC= $placeholders[-1][0] eq "h"D= $placeholders[-1][0] eq "hh"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
457 ( substr( $format, $pos, 2 ) eq 'mm' )
458 && (@placeholders)
459 && ( ( $placeholders[-1]->[0] eq 'h' )
460 or ( $placeholders[-1]->[0] eq 'hh' ) )
461 )
462 {
463  
464 # For this case 'm' is minutes not months.
465 221 push @placeholders, [ 'mm', length($token), 2, 'minutes' ];
466 }
467 else {
468 813 push @placeholders,
469 [ substr( $format, $pos, 2 ), length($token), 2 ];
470 }
471 1034 ▶ if ( ( substr( $format, $pos, 2 ) eq 'ss' )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($format, $pos, 2) eq "ss" and @placeholders > 1) 223 811 1034
Condition66%2/3
substr($format, $pos, 2) eq "ss" and @placeholders > 1
!ll&&!rl&&r
8110223
MC/DC50%1/2
substr($format, $pos, 2) eq "ss" and @placeholders > 1
substr($format, $pos, 2) eq "ss" @placeholders > 1
Truth table66%2/3
A and B
A= substr($format, $pos, 2) eq "ss"B= @placeholders > 1
ABresult
0X0
100
111
472 && ( @placeholders > 1 ) )
473 {
474 223 ▶ if ( ( $placeholders[-2]->[0] eq 'm' )
Branch: 1 branch
BranchTrueFalseTotal
if ($placeholders[-2][0] eq "m" or $placeholders[-2][0] eq "mm") 223 0 223
Condition33%1/3
$placeholders[-2][0] eq "m" or $placeholders[-2][0] eq "mm"
l!l&&r!l&&!r
02230
MC/DC0%0/2
$placeholders[-2][0] eq "m" or $placeholders[-2][0] eq "mm"
$placeholders[-2][0] eq "m" $placeholders[-2][0] eq "mm"
Truth table33%1/3
A or B
A= $placeholders[-2][0] eq "m"B= $placeholders[-2][0] eq "mm"
ABresult
1X1
011
000
475 || ( $placeholders[-2]->[0] eq 'mm' ) )
476 {
477  
478 # For this case 'm' is minutes not months.
479 223 push( @{ $placeholders[-2] }, 'minutes' );
480 }
481 }
482 1034 $pos += 2;
483 }
484 ▶ elsif (( substr( $format, $pos, 1 ) eq 'm' )
Condition66%6/9
substr($format, $pos, 1) eq "m" or substr($format, $pos, 1) eq "d" or substr($format, $pos, 1) eq "h" or substr($format, $pos, 1) eq "s"
l!l&&r!l&&!r
8900
substr($format, $pos, 1) eq "m" or substr($format, $pos, 1) eq "d" or substr($format, $pos, 1) eq "h"
l!l&&r!l&&!r
73160
substr($format, $pos, 1) eq "m" or substr($format, $pos, 1) eq "d"
l!l&&r!l&&!r
284516
MC/DC0%0/4
substr($format, $pos, 1) eq "m" or substr($format, $pos, 1) eq "d" or substr($format, $pos, 1) eq "h" or substr($format, $pos, 1) eq "s"
substr($format, $pos, 1) eq "m" substr($format, $pos, 1) eq "d" substr($format, $pos, 1) eq "h" substr($format, $pos, 1) eq "s"
Truth table60%3/5
A or B or C or D
A= substr($format, $pos, 1) eq "m"B= substr($format, $pos, 1) eq "d"C= substr($format, $pos, 1) eq "h"D= substr($format, $pos, 1) eq "s"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
485 || ( substr( $format, $pos, 1 ) eq 'd' )
486 || ( substr( $format, $pos, 1 ) eq 'h' )
487 || ( substr( $format, $pos, 1 ) eq 's' ) )
488 {
489 89 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (substr($format, $pos, 1) eq 'm' and @placeholders and $placeholders[-1][0] eq 'h' || $placeholders[-1][0] eq 'hh') { } 0 89 89
Condition66%6/9
substr($format, $pos, 1) eq "m" and @placeholders and $placeholders[-1][0] eq "h" || $placeholders[-1][0] eq "hh"
!ll&&!rl&&r
72170
substr($format, $pos, 1) eq "m" and @placeholders
!ll&&!rl&&r
611117
$placeholders[-1][0] eq "h" or $placeholders[-1][0] eq "hh"
l!l&&r!l&&!r
0017
MC/DC0%0/4
substr($format, $pos, 1) eq "m" and @placeholders and $placeholders[-1][0] eq "h" || $placeholders[-1][0] eq "hh"
substr($format, $pos, 1) eq "m" @placeholders $placeholders[-1][0] eq "h" $placeholders[-1][0] eq "hh"
Truth table60%3/5
A and B and C or D
A= substr($format, $pos, 1) eq "m"B= @placeholdersC= $placeholders[-1][0] eq "h"D= $placeholders[-1][0] eq "hh"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
490 ( substr( $format, $pos, 1 ) eq 'm' )
491 && (@placeholders)
492 && ( ( $placeholders[-1]->[0] eq 'h' )
493 or ( $placeholders[-1]->[0] eq 'hh' ) )
494 )
495 {
496  
497 # For this case 'm' is minutes not months.
498 0 push @placeholders, [ 'm', length($token), 1, 'minutes' ];
499 }
500 else {
501 89 push @placeholders,
502 [ substr( $format, $pos, 1 ), length($token), 1 ];
503 }
504 89 ▶ if ( ( substr( $format, $pos, 1 ) eq 's' )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($format, $pos, 1) eq "s" and @placeholders > 1) 0 89 89
Condition33%1/3
substr($format, $pos, 1) eq "s" and @placeholders > 1
!ll&&!rl&&r
8900
MC/DC0%0/2
substr($format, $pos, 1) eq "s" and @placeholders > 1
substr($format, $pos, 1) eq "s" @placeholders > 1
Truth table33%1/3
A and B
A= substr($format, $pos, 1) eq "s"B= @placeholders > 1
ABresult
0X0
100
111
505 && ( @placeholders > 1 ) )
506 {
507 0 ▶ if ( ( $placeholders[-2]->[0] eq 'm' )
Branch: 1 branch
BranchTrueFalseTotal
if ($placeholders[-2][0] eq "m" or $placeholders[-2][0] eq "mm") 0 0 0
Condition0%0/3
$placeholders[-2][0] eq "m" or $placeholders[-2][0] eq "mm"
l!l&&r!l&&!r
000
MC/DC0%0/2
$placeholders[-2][0] eq "m" or $placeholders[-2][0] eq "mm"
$placeholders[-2][0] eq "m" $placeholders[-2][0] eq "mm"
Truth table0%0/3
A or B
A= $placeholders[-2][0] eq "m"B= $placeholders[-2][0] eq "mm"
ABresult
1X1
011
000
508 || ( $placeholders[-2]->[0] eq 'mm' ) )
509 {
510  
511 # For this case 'm' is minutes not months.
512 0 push( @{ $placeholders[-2] }, 'minutes' );
513 }
514 }
515 89 $pos += 1;
516 }
517 }
518 elsif ( ( substr( $format, $pos, 3 ) eq '[h]' ) ) {
519 10 ▶ $format_mode = 'date' unless $format_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $format_mode 10 0 10
520 10 push @placeholders, [ '[h]', length($token), 3 ];
521 10 $pos += 3;
522 }
523 elsif ( ( substr( $format, $pos, 4 ) eq '[mm]' ) ) {
524 3 ▶ $format_mode = 'date' unless $format_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $format_mode 3 0 3
525 3 push @placeholders, [ '[mm]', length($token), 4 ];
526 3 $pos += 4;
527 }
528 elsif ( $char eq '@' ) {
529 251 push @placeholders, [ '@', length($token), 1 ];
530 251 $pos++;
531 }
532 elsif ( $char eq '*' ) {
533 7 push @placeholders,
534 [ substr( $format, $pos, 1 ), length($token), 1 ];
535 }
536 else {
537 982 $pos++;
538 }
539 4097 ▶ $pos++ if ( $pos == $start_pos ); #No Format match
Branch: 1 branch
BranchTrueFalseTotal
if $pos == $start_pos 7 4090 4097
540 4097 $token .= substr( $format, $start_pos, $pos - $start_pos );
541  
542 } # End of parsing.
543  
544 # Copy the located format string to a result string that we will perform
545 # the substitutions on and return to the user.
546 782 my $result = $token;
547  
548 # Add a placeholder between the decimal/comma and end of the token, if any.
549 782 ▶ if ( $token_start != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_start != -1) 289 493 782
550 289 push @placeholders,
551 [
552 substr( $format, $token_start, $pos - $token_start + 1 ),
553 $decimal_pos, $pos - $token_start + 1
554 ];
555 }
556  
557 #
558 # In the next sections we process date, number and text formats. We take a
559 # format such as yyyy/mm/dd and replace it with something like 2008/12/25.
560 #
561 782 ▶ if ( ( $format_mode eq 'date' ) && ( $number =~ $qrNUMBER ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($format_mode eq 'date' and $number =~ /$qrNUMBER/) { } 413 369 782
elsif ($format_mode eq 'number' and $number =~ /$qrNUMBER/) { } 118 251 369
Condition66%2/3
$format_mode eq "date" and $number =~ /$qrNUMBER/
!ll&&!rl&&r
3690413
MC/DC50%1/2
$format_mode eq "date" and $number =~ /$qrNUMBER/
$format_mode eq "date" $number =~ /$qrNUMBER/
Truth table66%2/3
A and B
A= $format_mode eq "date"B= $number =~ /$qrNUMBER/
ABresult
0X0
100
111
562  
563 # The maximum allowable date in Excel is 9999-12-31T23:59:59.000 which
564 # equates to 2958465.999+ in the 1900 epoch and 2957003.999+ in the
565 # 1904 epoch. We use 0 as the minimum in both epochs. The 1904 system
566 # actually supports negative numbers but that isn't worth the effort.
567 413 my $min_date = 0;
568 413 my $max_date = 2958466;
569 413 ▶ $max_date = 2957004 if $is_1904;
Branch: 1 branch
BranchTrueFalseTotal
if $is_1904 3 410 413
570  
571 413 ▶ if ( $number < $min_date || $number >= $max_date ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number < $min_date or $number >= $max_date) 4 409 413
Condition100%3/3
$number < $min_date or $number >= $max_date
l!l&&r!l&&!r
13409
MC/DC100%2/2
$number < $min_date or $number >= $max_date
$number < $min_date $number >= $max_date
Truth table100%3/3
A or B
A= $number < $min_dateB= $number >= $max_date
ABresult
1X1
011
000
572 4 return $number; # Return unformatted number.
573 }
574  
575 # Process date formats.
576 409 my @time = ExcelLocaltime( $number, $is_1904 );
577  
578 # 0 1 2 3 4 5 6 7
579 409 my ( $sec, $min, $hour, $day, $month, $year, $wday, $msec ) = @time;
580  
581 409 $month++; # localtime() zero indexed month.
582 409 $year += 1900; # localtime() year.
583  
584 409 my @full_month_name = qw(
585 None January February March April May June July
586 August September October November December
587 );
588 409 my @short_month_name = qw(
589 None Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
590 );
591 409 my @full_day_name = qw(
592 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
593 );
594 409 my @short_day_name = qw(
595 Sun Mon Tue Wed Thu Fri Sat
596 );
597  
598 # Replace the placeholders in the template such as yyyy mm dd with
599 # actual numbers or strings.
600 409 my $replacement;
601 409 for my $placeholder ( reverse @placeholders ) {
602  
603 1828 ▶ if ( $placeholder->[-1] eq 'minutes' ) {
Branch: 26 branches
BranchTrueFalseTotal
if ($placeholder->[-1] eq 'minutes') { } 230 1598 1828
elsif ($placeholder->[0] eq 'yyyy') { } 192 1406 1598
elsif ($placeholder->[0] eq 'yy') { } 12 1394 1406
elsif ($placeholder->[0] eq 'mmmmm') { } 12 1382 1394
elsif ($placeholder->[0] eq 'mmmm') { } 14 1368 1382
elsif ($placeholder->[0] eq 'mmm') { } 34 1334 1368
elsif ($placeholder->[0] eq 'mm') { } 174 1160 1334
elsif ($placeholder->[0] eq 'm') { } 28 1132 1160
elsif ($placeholder->[0] eq 'dddd') { } 9 1123 1132
elsif ($placeholder->[0] eq 'ddd') { } 14 1109 1123
elsif ($placeholder->[0] eq 'dd') { } 178 931 1109
elsif ($placeholder->[0] eq 'd') { } 45 886 931
elsif ($placeholder->[0] eq 'hh') { } 205 681 886
elsif ($placeholder->[0] eq 'h') { } 16 665 681
elsif ($placeholder->[0] eq 'ss') { } 223 442 665
elsif ($placeholder->[0] eq 's') { } 0 442 442
elsif ($placeholder->[0] eq 'am/pm') { } 9 433 442
elsif ($placeholder->[0] eq 'a/p') { } 0 433 433
elsif ($placeholder->[0] eq '.') { } 206 227 433
elsif ($placeholder->[0] =~ /(^0+$)/) { } 202 25 227
elsif ($placeholder->[0] eq '[h]') { } 10 15 25
elsif ($placeholder->[0] eq '[mm]') { } 3 12 15
elsif ($placeholder->[0] eq 'ge') { } 4 8 12
elsif ($placeholder->[0] eq 'ggge') { } 4 4 8
elsif ($placeholder->[0] eq '@') { } 0 4 4
elsif ($placeholder->[0] eq ',') { } 4 0 4
604  
605 # For this case 'm/mm' is minutes not months.
606 230 ▶ if ( $placeholder->[0] eq 'mm' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($placeholder->[0] eq 'mm') { } 230 0 230
607 230 $replacement = sprintf( "%02d", $min );
608 }
609 else {
610 0 $replacement = sprintf( "%d", $min );
611 }
612 }
613 elsif ( $placeholder->[0] eq 'yyyy' ) {
614  
615 # 4 digit Year. 2000 -> 2000.
616 192 $replacement = sprintf( '%04d', $year );
617 }
618 elsif ( $placeholder->[0] eq 'yy' ) {
619  
620 # 2 digit Year. 2000 -> 00.
621 12 $replacement = sprintf( '%02d', $year % 100 );
622 }
623 elsif ( $placeholder->[0] eq 'mmmmm' ) {
624  
625 # First character of the month name. 1 -> J.
626 12 $replacement = substr( $short_month_name[$month], 0, 1 );
627 }
628 elsif ( $placeholder->[0] eq 'mmmm' ) {
629  
630 # Full month name. 1 -> January.
631 14 $replacement = $full_month_name[$month];
632 }
633 elsif ( $placeholder->[0] eq 'mmm' ) {
634  
635 # Short month name. 1 -> Jan.
636 34 $replacement = $short_month_name[$month];
637 }
638 elsif ( $placeholder->[0] eq 'mm' ) {
639  
640 # 2 digit month. 1 -> 01.
641 174 $replacement = sprintf( '%02d', $month );
642 }
643 elsif ( $placeholder->[0] eq 'm' ) {
644  
645 # 1 digit month. 1 -> 1.
646 28 $replacement = sprintf( '%d', $month );
647 }
648 elsif ( $placeholder->[0] eq 'dddd' ) {
649  
650 # Full day name. Wednesday (for example.)
651 9 $replacement = $full_day_name[$wday];
652 }
653 elsif ( $placeholder->[0] eq 'ddd' ) {
654  
655 # Short day name. Wed (for example.)
656 14 $replacement = $short_day_name[$wday];
657 }
658 elsif ( $placeholder->[0] eq 'dd' ) {
659  
660 # 2 digit day. 1 -> 01.
661 178 $replacement = sprintf( '%02d', $day );
662 }
663 elsif ( $placeholder->[0] eq 'd' ) {
664  
665 # 1 digit day. 1 -> 1.
666 45 $replacement = sprintf( '%d', $day );
667 }
668 elsif ( $placeholder->[0] eq 'hh' ) {
669  
670 # 2 digit hour.
671 205 ▶ if ($is_12_hour) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_12_hour) { } 4 201 205
672 4 my $hour_tmp = $hour % 12;
673 4 ▶ $hour_tmp = 12 if $hour % 12 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $hour % 12 == 0 4 0 4
674 4 $replacement = sprintf( '%d', $hour_tmp );
675 }
676 else {
677 201 $replacement = sprintf( '%02d', $hour );
678 }
679 }
680 elsif ( $placeholder->[0] eq 'h' ) {
681  
682 # 1 digit hour.
683 16 ▶ if ($is_12_hour) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_12_hour) { } 5 11 16
684 5 my $hour_tmp = $hour % 12;
685 5 ▶ $hour_tmp = 12 if $hour % 12 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $hour % 12 == 0 5 0 5
686 5 $replacement = sprintf( '%2d', $hour_tmp );
687 }
688 else {
689 11 $replacement = sprintf( '%d', $hour );
690 }
691 }
692 elsif ( $placeholder->[0] eq 'ss' ) {
693  
694 # 2 digit seconds.
695 223 $replacement = sprintf( '%02d', $sec );
696 }
697 elsif ( $placeholder->[0] eq 's' ) {
698  
699 # 1 digit seconds.
700 0 $replacement = sprintf( '%d', $sec );
701 }
702 elsif ( $placeholder->[0] eq 'am/pm' ) {
703  
704 # AM/PM.
705 9 ▶ $replacement = ( $hour >= 12 ) ? 'PM' : 'AM';
Branch: 1 branch
BranchTrueFalseTotal
$hour >= 12 ? : 3 6 9
706 }
707 elsif ( $placeholder->[0] eq 'a/p' ) {
708  
709 # AM/PM.
710 0 ▶ $replacement = ( $hour >= 12 ) ? 'P' : 'A';
Branch: 1 branch
BranchTrueFalseTotal
$hour >= 12 ? : 0 0 0
711 }
712 elsif ( $placeholder->[0] eq '.' ) {
713  
714 # Decimal point for seconds.
715 206 $replacement = '.';
716 }
717 elsif ( $placeholder->[0] =~ /(^0+$)/ ) {
718  
719 # Milliseconds. For example h:ss.000.
720 202 my $length = length($1);
721 202 $replacement =
722 substr( sprintf( "%.${length}f", $msec / 1000 ), 2, $length );
723 }
724 elsif ( $placeholder->[0] eq '[h]' ) {
725  
726 # Hours modulus 24. 25 displays as 25 not as 1.
727 10 $replacement = sprintf( '%d', int($number) * 24 + $hour );
728 }
729 elsif ( $placeholder->[0] eq '[mm]' ) {
730  
731 # Mins modulus 60. 72 displays as 72 not as 12.
732 3 $replacement =
733 sprintf( '%d', ( int($number) * 24 + $hour ) * 60 + $min );
734 }
735 elsif ( $placeholder->[0] eq 'ge' ) {
736 4 require Spreadsheet::ParseExcel::FmtJapan;
737 # Japanese Nengo (aka Gengo) in initialism (abbr. name)
738 4 $replacement =
739 Spreadsheet::ParseExcel::FmtJapan::CnvNengo( abbr_name => @time );
740 }
741 elsif ( $placeholder->[0] eq 'ggge' ) {
742 4 require Spreadsheet::ParseExcel::FmtJapan;
743 # Japanese Nengo (aka Gengo) in Kanji (full name)
744 4 $replacement =
745 Spreadsheet::ParseExcel::FmtJapan::CnvNengo( name => @time );
746 }
747 elsif ( $placeholder->[0] eq '@' ) {
748  
749 # Text format.
750 0 $replacement = $number;
751 }
752 elsif ( $placeholder->[0] eq ',' ) {
753 4 next;
754 }
755  
756 # Substitute the replacement string back into the template.
757 1824 substr( $result, $placeholder->[1], $placeholder->[2],
758 $replacement );
759 }
760 }
761 ▶ elsif ( ( $format_mode eq 'number' ) && ( $number =~ $qrNUMBER ) ) {
Condition66%2/3
$format_mode eq "number" and $number =~ /$qrNUMBER/
!ll&&!rl&&r
2510118
MC/DC50%1/2
$format_mode eq "number" and $number =~ /$qrNUMBER/
$format_mode eq "number" $number =~ /$qrNUMBER/
Truth table66%2/3
A and B
A= $format_mode eq "number"B= $number =~ /$qrNUMBER/
ABresult
0X0
100
111
762  
763 # Process non date formats.
764 118 ▶ if (@placeholders) {
Branch: 1 branch
BranchTrueFalseTotal
if (@placeholders) 118 0 118
765 118 ▶ while ( $placeholders[-1]->[0] eq ',' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($placeholders[-1][0] eq ",") 0 118 118
766 0 $comma_count--;
767 0 substr(
768 $result,
769 $placeholders[-1]->[1],
770 $placeholders[-1]->[2], ''
771 );
772 0 $number /= 1000;
773 0 pop @placeholders;
774 }
775  
776 118 my $number_format = join( '', map { $_->[0] } @placeholders );
777 118 my $number_result;
778 118 my $str_length = 0;
779 118 my $engineering = 0;
780 118 my $is_decimal = 0;
781 118 my $is_integer = 0;
782 118 my $after_decimal = undef;
783  
784 118 for my $token ( split //, $number_format ) {
785 558 ▶ if ( $token eq '.' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($token eq '.') { } 92 466 558
elsif ($token eq 'E' or $token eq 'e') { } 2 464 466
elsif ($token eq '0') { } 315 149 464
elsif ($token eq '#') { } 101 48 149
elsif ($token eq '?') { } 6 42 48
786 92 $str_length++;
787 92 $is_decimal = 1;
788 }
789 ▶ elsif ( ( $token eq 'E' ) || ( $token eq 'e' ) ) {
Condition66%2/3
$token eq "E" or $token eq "e"
l!l&&r!l&&!r
20464
MC/DC50%1/2
$token eq "E" or $token eq "e"
$token eq "E" $token eq "e"
Truth table66%2/3
A or B
A= $token eq "E"B= $token eq "e"
ABresult
1X1
011
000
790 2 $engineering = 1;
791 }
792 elsif ( $token eq '0' ) {
793 315 $str_length++;
794 315 ▶ $after_decimal++ if $is_decimal;
Branch: 1 branch
BranchTrueFalseTotal
if $is_decimal 200 115 315
795 315 $is_integer = 1;
796 }
797 elsif ( $token eq '#' ) {
798 101 ▶ $after_decimal++ if $is_decimal;
Branch: 1 branch
BranchTrueFalseTotal
if $is_decimal 8 93 101
799 101 $is_integer = 1;
800 }
801 elsif ( $token eq '?' ) {
802 6 ▶ $after_decimal++ if $is_decimal;
Branch: 1 branch
BranchTrueFalseTotal
if $is_decimal 0 6 6
803 }
804 }
805  
806 118 ▶ $number *= 100.0 if $is_percent;
Branch: 1 branch
BranchTrueFalseTotal
if $is_percent 9 109 118
807  
808 118 ▶ my $data = ($is_currency) ? abs($number) : $number + 0;
Branch: 1 branch
BranchTrueFalseTotal
$is_currency ? : 2 116 118
809  
810 118 ▶ if ($is_fraction) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_fraction) { } 2 116 118
811 2 $number_result = sprintf( "%0${str_length}d", int($data) );
812 }
813 else {
814 116 ▶ if ($is_decimal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_decimal) { } 92 24 116
815  
816 92 ▶ if ( defined $after_decimal ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $after_decimal) { } 92 0 92
817 92 $number_result =
818 sprintf "%0${str_length}.${after_decimal}f", $data;
819 }
820 else {
821 0 $number_result = sprintf "%0${str_length}f", $data;
822 }
823  
824 # Fix for Perl and sprintf not rounding up like Excel.
825 # http://rt.cpan.org/Public/Bug/Display.html?id=45626
826 92 ▶ if ( $data =~ /^${number_result}5/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^${number_result}5/) 13 79 92
827 13 $number_result =
828 sprintf "%0${str_length}.${after_decimal}f",
829 $data . '1';
830 }
831 }
832 else {
833 24 $number_result = sprintf( "%0${str_length}.0f", $data );
834 }
835 }
836  
837 118 ▶ $number_result = AddComma($number_result) if $comma_count > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $comma_count > 0 29 89 118
838  
839 118 my $number_length = length($number_result);
840 118 my $decimal_pos = -1;
841 118 my $replacement;
842  
843 118 ▶ for ( my $i = @placeholders - 1 ; $i >= 0 ; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 368 118 486
844 368 my $placeholder = $placeholders[$i];
845  
846 368 ▶ if ( $placeholder->[0] =~
Branch: 9 branches
BranchTrueFalseTotal
if ($placeholder->[0] =~ /([#0]*)([\.]?)([0#]*)([eE])([\+\-])([0#]+)/) { } 2 366 368
elsif ($placeholder->[0] =~ m[/]) { } 2 364 366
elsif ($placeholder->[0] eq '.') { } 90 274 364
elsif ($placeholder->[0] eq '+') { } 2 272 274
elsif ($placeholder->[0] eq '-') { } 0 272 272
elsif ($placeholder->[0] eq '@') { } 0 272 272
elsif ($placeholder->[0] eq '*') { } 7 265 272
elsif ($placeholder->[0] eq "\242\244" or $placeholder->[0] eq "\242\245" or $placeholder->[0] eq "\201\242" or $placeholder->[0] eq "\201\243") { } 0 265 265
elsif ($placeholder->[0] eq '(' or $placeholder->[0] eq ')') { } 0 265 265
847 /([#0]*)([\.]?)([0#]*)([eE])([\+\-])([0#]+)/ )
848 {
849 2 substr( $result, $placeholder->[1], $placeholder->[2],
850 MakeE( $placeholder->[0], $number ) );
851 }
852 elsif ( $placeholder->[0] =~ /\// ) {
853 2 substr( $result, $placeholder->[1], $placeholder->[2],
854 MakeFraction( $placeholder->[0], $number, $is_integer )
855 );
856 }
857 elsif ( $placeholder->[0] eq '.' ) {
858 90 $number_length--;
859 90 $decimal_pos = $number_length;
860 }
861 elsif ( $placeholder->[0] eq '+' ) {
862 2 ▶ substr( $result, $placeholder->[1], $placeholder->[2],
Branch: 2 branches
BranchTrueFalseTotal
$number > 0 ? : 2 0 2
$number == 0 ? : 0 0 0
863 ( $number > 0 )
864 ? '+'
865 : ( ( $number == 0 ) ? '+' : '-' ) );
866 }
867 elsif ( $placeholder->[0] eq '-' ) {
868 0 ▶ substr( $result, $placeholder->[1], $placeholder->[2],
Branch: 2 branches
BranchTrueFalseTotal
$number > 0 ? : 0 0 0
$number == 0 ? : 0 0 0
869 ( $number > 0 )
870 ? ''
871 : ( ( $number == 0 ) ? '' : '-' ) );
872 }
873 elsif ( $placeholder->[0] eq '@' ) {
874 0 substr( $result, $placeholder->[1], $placeholder->[2],
875 $number );
876 }
877 elsif ( $placeholder->[0] eq '*' ) {
878 7 substr( $result, $placeholder->[1], $placeholder->[2], '' );
879 }
880 ▶ elsif (( $placeholder->[0] eq "\xA2\xA4" )
Condition33%3/9
$placeholder->[0] eq "\242\244" or $placeholder->[0] eq "\242\245" or $placeholder->[0] eq "\201\242" or $placeholder->[0] eq "\201\243"
l!l&&r!l&&!r
00265
$placeholder->[0] eq "\242\244" or $placeholder->[0] eq "\242\245" or $placeholder->[0] eq "\201\242"
l!l&&r!l&&!r
00265
$placeholder->[0] eq "\242\244" or $placeholder->[0] eq "\242\245"
l!l&&r!l&&!r
00265
MC/DC0%0/4
$placeholder->[0] eq "\242\244" or $placeholder->[0] eq "\242\245" or $placeholder->[0] eq "\201\242" or $placeholder->[0] eq "\201\243"
$placeholder->[0] eq "\242\244" $placeholder->[0] eq "\242\245" $placeholder->[0] eq "\201\242" $placeholder->[0] eq "\201\243"
Truth table20%1/5
A or B or C or D
A= $placeholder->[0] eq "\242\244"B= $placeholder->[0] eq "\242\245"C= $placeholder->[0] eq "\201\242"D= $placeholder->[0] eq "\201\243"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
881 or ( $placeholder->[0] eq "\xA2\xA5" )
882 or ( $placeholder->[0] eq "\x81\xA2" )
883 or ( $placeholder->[0] eq "\x81\xA3" ) )
884 {
885 0 substr(
886 $result, $placeholder->[1],
887 $placeholder->[2], $placeholder->[0]
888 );
889 }
890 ▶ elsif (( $placeholder->[0] eq '(' )
Condition33%1/3
$placeholder->[0] eq "(" or $placeholder->[0] eq ")"
l!l&&r!l&&!r
00265
MC/DC0%0/2
$placeholder->[0] eq "(" or $placeholder->[0] eq ")"
$placeholder->[0] eq "(" $placeholder->[0] eq ")"
Truth table33%1/3
A or B
A= $placeholder->[0] eq "("B= $placeholder->[0] eq ")"
ABresult
1X1
011
000
891 or ( $placeholder->[0] eq ')' ) )
892 {
893 0 substr(
894 $result, $placeholder->[1],
895 $placeholder->[2], $placeholder->[0]
896 );
897 }
898 else {
899 265 ▶ if ( $number_length > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number_length > 0) { } 265 0 265
900 265 ▶ if ( $i <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 0) { } 105 160 265
901 105 $replacement =
902 substr( $number_result, 0, $number_length );
903 105 $number_length = 0;
904 }
905 else {
906 160 my $real_part_length = length( $placeholder->[0] );
907 160 ▶ if ( $decimal_pos >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decimal_pos >= 0) { } 35 125 160
908 35 my $format = $placeholder->[0];
909 35 $format =~ s/^#+//;
910 35 $real_part_length = length $format;
911 35 ▶ $real_part_length =
Branch: 1 branch
BranchTrueFalseTotal
$number_length <= $real_part_length ? : 0 35 35
912 ( $number_length <= $real_part_length )
913 ? $number_length
914 : $real_part_length;
915 }
916 else {
917 125 ▶ $real_part_length =
Branch: 1 branch
BranchTrueFalseTotal
$number_length <= $real_part_length ? : 4 121 125
918 ( $number_length <= $real_part_length )
919 ? $number_length
920 : $real_part_length;
921 }
922 160 $replacement =
923 substr( $number_result,
924 $number_length - $real_part_length,
925 $real_part_length );
926 160 $number_length -= $real_part_length;
927 }
928 }
929 else {
930 0 $replacement = '';
931 }
932 265 substr( $result, $placeholder->[1], $placeholder->[2],
933 "\x00" . $replacement );
934 }
935 }
936 118 ▶ $replacement =
Branch: 1 branch
BranchTrueFalseTotal
$number_length > 0 ? : 9 109 118
937 ( $number_length > 0 )
938 ? substr( $number_result, 0, $number_length )
939 : '';
940 118 $result =~ s/\x00/$replacement/;
941 118 $result =~ s/\x00//g;
942 }
943 }
944 else {
945  
946 # Process text formats
947 251 my $is_text = 0;
948 251 ▶ for ( my $i = @placeholders - 1 ; $i >= 0 ; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 251 251 502
949 251 my $placeholder = $placeholders[$i];
950 251 ▶ if ( $placeholder->[0] eq '@' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($placeholder->[0] eq '@') { } 251 0 251
951 251 substr( $result, $placeholder->[1], $placeholder->[2],
952 $number );
953 251 $is_text++;
954 }
955 else {
956 0 substr( $result, $placeholder->[1], $placeholder->[2], '' );
957 }
958 }
959  
960 251 ▶ $result = $number unless $is_text;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_text 0 251 251
961  
962 } # End of placeholder substitutions.
963  
964 # Trim the leading and trailing whitespace from the results.
965 778 $result =~ s/^\s+//;
966 778 $result =~ s/\s+$//;
967  
968 # Fix for negative currency.
969 778 $result =~ s/^\$\-/\-\$/;
970 778 $result =~ s/^\$ \-/\-\$ /;
971  
972 # Return color and locale strings if required.
973 778 ▶ if ($want_subformats) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_subformats) { } 0 778 778
974 0 return ( $result, $color, $locale );
975 }
976 else {
977 778 return $result;
978 }
979 }
980  
981 #------------------------------------------------------------------------------
982 # AddComma (for Spreadsheet::ParseExcel::Utility)
983 #------------------------------------------------------------------------------
984 29 ▶sub AddComma {
Subroutine AddComma: called CC=3 SCAR=11.2
Pod undocumented
985 29 my ($sNum) = @_;
986  
987 29 ▶ if ( $sNum =~ /^([^\d]*)(\d\d\d\d+)(\.*.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sNum =~ /^([^\d]*)(\d\d\d\d+)(\.*.*)$/) { } 29 0 29
988 29 my ( $sPre, $sObj, $sAft ) = ( $1, $2, $3 );
989 29 ▶ for ( my $i = length($sObj) - 3 ; $i > 0 ; $i -= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 29 29 58
990 29 substr( $sObj, $i, 0, ',' );
991 }
992 29 return $sPre . $sObj . $sAft;
993 }
994 else {
995 0 return $sNum;
996 }
997 }
998  
999 #------------------------------------------------------------------------------
1000 # MakeFraction (for Spreadsheet::ParseExcel::Utility)
1001 #------------------------------------------------------------------------------
1002 2 ▶sub MakeFraction {
Subroutine MakeFraction: called CC=7 SCAR=20.5
Pod undocumented
1003 2 my ( $sFmt, $iData, $iFlg ) = @_;
1004 2 my $iBunbo;
1005 my $iShou;
1006  
1007 #1. Init
1008 # print "FLG: $iFlg\n";
1009 2 ▶ if ($iFlg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iFlg) { } 0 2 2
1010 0 $iShou = $iData - int($iData);
1011 0 ▶ return '' if ( $iShou == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $iShou == 0 0 0 0
1012 }
1013 else {
1014 2 $iShou = $iData;
1015 }
1016 2 $iShou = abs($iShou);
1017 2 my $sSWk;
1018  
1019 #2.Calc BUNBO
1020 #2.1 BUNBO defined
1021 2 ▶ if ( $sFmt =~ /\/(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sFmt =~ m[/(\d+)$]) { } 0 2 2
1022 0 $iBunbo = $1;
1023 0 return sprintf( "%d/%d", $iShou * $iBunbo, $iBunbo );
1024 }
1025 else {
1026  
1027 #2.2 Calc BUNBO
1028 2 $sFmt =~ /\/(\?+)$/;
1029 2 my $iKeta = length($1);
1030 2 my $iSWk = 1;
1031 2 my $sSWk = '';
1032 2 my $iBunsi;
1033 2 ▶ for ( my $iBunbo = 2 ; $iBunbo < 10**$iKeta ; $iBunbo++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iBunbo < 10 ** $iKeta) 18 0 18
1034 18 $iBunsi = int( $iShou * $iBunbo + 0.5 );
1035 18 my $iCmp = abs( $iShou - ( $iBunsi / $iBunbo ) );
1036 18 ▶ if ( $iCmp < $iSWk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iCmp < $iSWk) 8 10 18
1037 8 $iSWk = $iCmp;
1038 8 $sSWk = sprintf( "%d/%d", $iBunsi, $iBunbo );
1039 8 ▶ last if ( $iSWk == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $iSWk == 0 2 6 8
1040 }
1041 }
1042 2 return $sSWk;
1043 }
1044 }
1045  
1046 #------------------------------------------------------------------------------
1047 # MakeE (for Spreadsheet::ParseExcel::Utility)
1048 #------------------------------------------------------------------------------
1049 2 ▶sub MakeE {
Subroutine MakeE: called CC=4 SCAR=14.1
Pod undocumented
1050 2 my ( $sFmt, $iData ) = @_;
1051  
1052 2 $sFmt =~ /(([#0]*)[\.]?[#0]*)([eE])([\+\-][0#]+)/;
1053 2 my ( $sKari, $iKeta, $sE, $sSisu ) = ( $1, length($2), $3, $4 );
1054 2 ▶ $iKeta = 1 if ( $iKeta <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $iKeta <= 0 0 2 2
1055  
1056 2 my $iLog10 = 0;
1057 2 ▶ $iLog10 = ( $iData == 0 ) ? 0 : ( log( abs($iData) ) / log(10) );
Branch: 1 branch
BranchTrueFalseTotal
$iData == 0 ? : 0 2 2
1058 2 ▶ $iLog10 = (
Branch: 1 branch
BranchTrueFalseTotal
$iLog10 - int($iLog10 / $iKeta) < 0 ? : 0 2 2
1059 int( $iLog10 / $iKeta ) +
1060 ( ( ( $iLog10 - int( $iLog10 / $iKeta ) ) < 0 ) ? -1 : 0 ) ) * $iKeta;
1061  
1062 2 my $sUe = ExcelFmt( $sKari, $iData * ( 10**( $iLog10 * -1 ) ), 0 );
1063 2 my $sShita = ExcelFmt( $sSisu, $iLog10, 0 );
1064 2 return $sUe . $sE . $sShita;
1065 }
1066  
1067 #------------------------------------------------------------------------------
1068 # LeapYear (for Spreadsheet::ParseExcel::Utility)
1069 #------------------------------------------------------------------------------
1070 393065 ▶sub LeapYear {
Subroutine LeapYear: called CC=5 SCAR=16.1
Pod undocumented
1071 393065 my ($iYear) = @_;
1072 393065 ▶ return 1 if ( $iYear == 1900 ); #Special for Excel
Branch: 1 branch
BranchTrueFalseTotal
if $iYear == 1900 0 393065 393065
1073 393065 ▶ return ( ( ( $iYear % 4 ) == 0 )
Branch: 1 branch
BranchTrueFalseTotal
$iYear % 4 == 0 && ($iYear % 100 || $iYear % 400 == 0) ? : 95355 297710 393065
Condition100%6/6
$iYear % 4 == 0 and $iYear % 100 || $iYear % 400 == 0
!ll&&!rl&&r
294836287495355
$iYear % 100 or $iYear % 400 == 0
l!l&&r!l&&!r
9434810072874
MC/DC100%3/3
$iYear % 4 == 0 and $iYear % 100 || $iYear % 400 == 0
$iYear % 4 == 0 $iYear % 100 $iYear % 400 == 0
Truth table100%4/4
A and B or C
A= $iYear % 4 == 0B= $iYear % 100C= $iYear % 400 == 0
ABCresult
0XX0
1000
11X1
1011
1074 && ( ( $iYear % 100 ) || ( $iYear % 400 ) == 0 ) )
1075 ? 1
1076 : 0;
1077 }
1078  
1079 #------------------------------------------------------------------------------
1080 # LocaltimeExcel (for Spreadsheet::ParseExcel::Utility)
1081 #------------------------------------------------------------------------------
1082 101 ▶sub LocaltimeExcel {
Subroutine LocaltimeExcel: called CC=19 SCAR=29.6
Pod documented
1083 101 my ( $iSec, $iMin, $iHour, $iDay, $iMon, $iYear, $iwDay, $iMSec, $flg1904 )
1084 = @_;
1085  
1086 #0. Init
1087 101 $iMon++;
1088 101 $iYear += 1900;
1089  
1090 #1. Calc Time
1091 101 my $iTime;
1092 101 $iTime = $iHour;
1093 101 $iTime *= 60;
1094 101 $iTime += $iMin;
1095 101 $iTime *= 60;
1096 101 $iTime += $iSec;
1097 101 ▶ $iTime += $iMSec / 1000.0 if ( defined($iMSec) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $iMSec 0 101 101
1098 101 $iTime /= 86400.0; #3600*24(1day in seconds)
1099 101 my $iY;
1100 my $iYDays;
1101  
1102 #2. Calc Days
1103 101 ▶ if ($flg1904) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flg1904) { } 0 101 101
1104 0 $iY = 1904;
1105 0 $iTime--; #Start from Jan 1st
1106 0 $iYDays = 366;
1107 }
1108 else {
1109 101 $iY = 1900;
1110 101 $iYDays = 366; #In Excel 1900 is leap year (That's not TRUE!)
1111 }
1112 101 ▶ while ( $iY < $iYear ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iY < $iYear) 392984 101 393085
1113 392984 $iTime += $iYDays;
1114 392984 $iY++;
1115 392984 ▶ $iYDays = ( LeapYear($iY) ) ? 366 : 365;
Branch: 1 branch
BranchTrueFalseTotal
LeapYear($iY) ? : 95335 297649 392984
1116 }
1117 101 ▶ for ( my $iM = 1 ; $iM < $iMon ; $iM++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iM < $iMon) 542 101 643
1118 542 ▶ if ( $iM == 1
Branch: 3 branches
BranchTrueFalseTotal
if ($iM == 1 or $iM == 3 or $iM == 5 or $iM == 7 or $iM == 8 or $iM == 10 or $iM == 12) { } 312 230 542
elsif ($iM == 4 or $iM == 6 or $iM == 9 or $iM == 11) { } 149 81 230
elsif ($iM == 2) { } 81 0 81
Condition94%17/18
$iM == 1 or $iM == 3 or $iM == 5 or $iM == 7 or $iM == 8 or $iM == 10 or $iM == 12
l!l&&r!l&&!r
3120230
$iM == 1 or $iM == 3 or $iM == 5 or $iM == 7 or $iM == 8 or $iM == 10
l!l&&r!l&&!r
29616230
$iM == 1 or $iM == 3 or $iM == 5 or $iM == 7 or $iM == 8
l!l&&r!l&&!r
26234246
$iM == 1 or $iM == 3 or $iM == 5 or $iM == 7
l!l&&r!l&&!r
22042280
$iM == 1 or $iM == 3 or $iM == 5
l!l&&r!l&&!r
16258322
$iM == 1 or $iM == 3
l!l&&r!l&&!r
8973380
MC/DC85%6/7
$iM == 1 or $iM == 3 or $iM == 5 or $iM == 7 or $iM == 8 or $iM == 10 or $iM == 12
$iM == 1 $iM == 3 $iM == 5 $iM == 7 $iM == 8 $iM == 10 $iM == 12
Truth table87%7/8
A or B or C or D or E or F or G
A= $iM == 1B= $iM == 3C= $iM == 5D= $iM == 7E= $iM == 8F= $iM == 10G= $iM == 12
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
1119 || $iM == 3
1120 || $iM == 5
1121 || $iM == 7
1122 || $iM == 8
1123 || $iM == 10
1124 || $iM == 12 )
1125 {
1126 312 $iTime += 31;
1127 }
1128 ▶ elsif ( $iM == 4 || $iM == 6 || $iM == 9 || $iM == 11 ) {
Condition100%9/9
$iM == 4 or $iM == 6 or $iM == 9 or $iM == 11
l!l&&r!l&&!r
141881
$iM == 4 or $iM == 6 or $iM == 9
l!l&&r!l&&!r
1152689
$iM == 4 or $iM == 6
l!l&&r!l&&!r
6550115
MC/DC100%4/4
$iM == 4 or $iM == 6 or $iM == 9 or $iM == 11
$iM == 4 $iM == 6 $iM == 9 $iM == 11
Truth table100%5/5
A or B or C or D
A= $iM == 4B= $iM == 6C= $iM == 9D= $iM == 11
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1129 149 $iTime += 30;
1130 }
1131 elsif ( $iM == 2 ) {
1132 81 ▶ $iTime += ( LeapYear($iYear) ) ? 29 : 28;
Branch: 1 branch
BranchTrueFalseTotal
LeapYear($iYear) ? : 20 61 81
1133 }
1134 }
1135 101 $iTime += $iDay;
1136 101 return $iTime;
1137 }
1138  
1139 28 my @month_days = qw(
1140 0 31 28 31 30 31 30 31 31 30 31 30 31
1141 );
1142  
1143 #------------------------------------------------------------------------------
1144 # ExcelLocaltime (for Spreadsheet::ParseExcel::Utility)
1145 #------------------------------------------------------------------------------
1146 507 ▶sub ExcelLocaltime {
Subroutine ExcelLocaltime: called CC=14 SCAR=26.5
Pod documented
1147  
1148 507 my ( $dObj, $flg1904 ) = @_;
1149 507 my ( $iSec, $iMin, $iHour, $iDay, $iMon, $iYear, $iwDay, $iMSec );
1150 507 my ( $iDt, $iTime, $iYDays, $iMD );
1151  
1152 507 $iDt = int($dObj);
1153 507 $iTime = $dObj - $iDt;
1154  
1155 #1. Calc Days
1156 507 ▶ if ($flg1904) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flg1904) { } 2 505 507
1157 2 $iYear = 1904;
1158 2 $iDt++; #Start from Jan 1st
1159 2 $iYDays = 366;
1160 2 $iwDay = ( ( $iDt + 4 ) % 7 );
1161 }
1162 else {
1163 505 $iYear = 1900;
1164 505 $iYDays = 366; #In Excel 1900 is leap year (That's not TRUE!)
1165 505 $iwDay = ( ( $iDt + 6 ) % 7 );
1166 }
1167 507 ▶ while ( $iDt > $iYDays ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iDt > $iYDays) 827275 507 827782
1168 827275 $iDt -= $iYDays;
1169 827275 $iYear++;
1170 827275 ▶ $iYDays =
Branch: 1 branch
BranchTrueFalseTotal
$iYear % 4 == 0 && ($iYear % 100 || $iYear % 400 == 0) ? : 200795 626480 827275
Condition100%6/6
$iYear % 4 == 0 and $iYear % 100 || $iYear % 400 == 0
!ll&&!rl&&r
6205525928200795
$iYear % 100 or $iYear % 400 == 0
l!l&&r!l&&!r
19857322225928
MC/DC100%3/3
$iYear % 4 == 0 and $iYear % 100 || $iYear % 400 == 0
$iYear % 4 == 0 $iYear % 100 $iYear % 400 == 0
Truth table100%4/4
A and B or C
A= $iYear % 4 == 0B= $iYear % 100C= $iYear % 400 == 0
ABCresult
0XX0
1000
11X1
1011
1171 ( ( ( $iYear % 4 ) == 0 )
1172 && ( ( $iYear % 100 ) || ( $iYear % 400 ) == 0 ) ) ? 366 : 365;
1173 }
1174 507 $iYear -= 1900; # Localtime year is relative to 1900.
1175  
1176 507 ▶ for ( $iMon = 1 ; $iMon <= 12 ; $iMon++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iMon <= 12) 2571 0 2571
1177 2571 $iMD = $month_days[$iMon];
1178 2571 ▶ $iMD++ if $iMon == 2 and $iYear % 4 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $iMon == 2 and $iYear % 4 == 0 166 2405 2571
Condition100%3/3
$iMon == 2 and $iYear % 4 == 0
!ll&&!rl&&r
2232173166
MC/DC100%2/2
$iMon == 2 and $iYear % 4 == 0
$iMon == 2 $iYear % 4 == 0
Truth table100%3/3
A and B
A= $iMon == 2B= $iYear % 4 == 0
ABresult
0X0
100
111
1179  
1180 2571 ▶ last if ( $iDt <= $iMD );
Branch: 1 branch
BranchTrueFalseTotal
if $iDt <= $iMD 507 2064 2571
1181 2064 $iDt -= $iMD;
1182 }
1183  
1184 #2. Calc Time
1185 507 $iDay = $iDt;
1186 507 $iTime += ( 0.0005 / 86400.0 );
1187 507 ▶ if ($iTime >= 1.0)
Branch: 1 branch
BranchTrueFalseTotal
if ($iTime >= 1) 1 506 507
1188 {
1189 1 $iTime -= int($iTime);
1190 1 ▶ $iwDay = ($iwDay == 6) ? 0 : $iwDay + 1;
Branch: 1 branch
BranchTrueFalseTotal
$iwDay == 6 ? : 0 1 1
1191 1 ▶ if ($iDay == $iMD)
Branch: 1 branch
BranchTrueFalseTotal
if ($iDay == $iMD) { } 1 0 1
1192 {
1193 1 ▶ if ($iMon == 12)
Branch: 1 branch
BranchTrueFalseTotal
if ($iMon == 12) { } 1 0 1
1194 {
1195 1 $iMon = 1;
1196 1 $iYear++;
1197 }
1198 else
1199 {
1200 0 $iMon++;
1201 }
1202 1 $iDay = 1;
1203 }
1204 else
1205 {
1206 0 $iDay++;
1207 }
1208 }
1209  
1210 # Localtime month is 0 based.
1211 507 $iMon -= 1;
1212 507 $iTime *= 24.0;
1213 507 $iHour = int($iTime);
1214 507 $iTime -= $iHour;
1215 507 $iTime *= 60.0;
1216 507 $iMin = int($iTime);
1217 507 $iTime -= $iMin;
1218 507 $iTime *= 60.0;
1219 507 $iSec = int($iTime);
1220 507 $iTime -= $iSec;
1221 507 $iTime *= 1000.0;
1222 507 $iMSec = int($iTime);
1223  
1224 507 return ( $iSec, $iMin, $iHour, $iDay, $iMon, $iYear, $iwDay, $iMSec );
1225 }
1226  
1227 # -----------------------------------------------------------------------------
1228 # col2int (for Spreadsheet::ParseExcel::Utility)
1229 #------------------------------------------------------------------------------
1230 # converts a excel row letter into an int for use in an array
1231 256 ▶sub col2int {
Subroutine col2int: called CC=3 SCAR=11.0
Pod documented
1232 256 my $result = 0;
1233 256 my $str = shift;
1234 256 my $incr = 0;
1235  
1236 256 ▶ for ( my $i = length($str) ; $i > 0 ; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 486 256 742
1237 486 my $char = substr( $str, $i - 1 );
1238 486 my $curr += ord( lc($char) ) - ord('a') + 1;
1239 486 ▶ $curr *= $incr if ($incr);
Branch: 1 branch
BranchTrueFalseTotal
if $incr 230 256 486
1240 486 $result += $curr;
1241 486 $incr += 26;
1242 }
1243  
1244 # this is one out as we range 0..x-1 not 1..x
1245 256 $result--;
1246  
1247 256 return $result;
1248 }
1249  
1250 # -----------------------------------------------------------------------------
1251 # int2col (for Spreadsheet::ParseExcel::Utility)
1252 #------------------------------------------------------------------------------
1253 ### int2col
1254 # convert a column number into column letters
1255 # @note this is quite a brute force coarse method
1256 # does not manage values over 701 (ZZ)
1257 # @arg number, to convert
1258 # @returns string, column name
1259 #
1260 257 ▶sub int2col {
Subroutine int2col: called CC=2 SCAR=6.9
Pod documented
1261 257 my $out = "";
1262 257 my $val = shift;
1263  
1264 257 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$val >= 0 and do { do { $out .= chr($val % 26 + 65); $val = int($val / 26) - 1 } } 231 257 488
1265 488 $out .= chr( ( $val % 26 ) + ord('A') );
1266 488 $val = int( $val / 26 ) - 1;
1267 } while ( $val >= 0 );
1268  
1269 257 return scalar reverse $out;
1270 }
1271  
1272 # -----------------------------------------------------------------------------
1273 # sheetRef (for Spreadsheet::ParseExcel::Utility)
1274 #------------------------------------------------------------------------------
1275 # -----------------------------------------------------------------------------
1276 ### sheetRef
1277 # convert an excel letter-number address into a useful array address
1278 # @note that also Excel uses X-Y notation, we normally use Y-X in arrays
1279 # @args $str, excel coord eg. A2
1280 # @returns an array - 2 elements - column, row, or undefined
1281 #
1282 0 ▶sub sheetRef {
Subroutine sheetRef: not called CC=4 SCAR=30.0
Pod documented
1283 0 my $str = shift;
1284 0 my @ret;
1285  
1286 0 $str =~ m/^(\D+)(\d+)$/;
1287  
1288 0 ▶ if ( $1 && $2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 and $2) 0 0 0
Condition0%0/3
$1 and $2
!ll&&!rl&&r
000
MC/DC0%0/2
$1 and $2
$1 $2
Truth table0%0/3
A and B
A= $1B= $2
ABresult
0X0
100
111
1289 0 push( @ret, $2 - 1, col2int($1) );
1290 }
1291 0 ▶ if ( $ret[0] < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret[0] < 0) 0 0 0
1292 0 undef @ret;
1293 }
1294  
1295 0 return @ret;
1296 }
1297  
1298 # -----------------------------------------------------------------------------
1299 # xls2csv (for Spreadsheet::ParseExcel::Utility)
1300 #------------------------------------------------------------------------------
1301 ### xls2csv
1302 # convert a chunk of an excel file into csv text chunk
1303 # @args $param, sheet-colrow:colrow (1-A1:B2 or A1:B2 for sheet 1
1304 # @args $rotate, 0 or 1 decides if output should be rotated or not
1305 # @returns string containing a chunk of csv
1306 #
1307 0 ▶sub xls2csv {
Subroutine xls2csv: not called CC=20 SCAR=60.4
Pod documented
1308 0 my ( $filename, $regions, $rotate ) = @_;
1309 0 my $sheet = 0;
1310  
1311 # We need Text::CSV_XS for proper CSV handling.
1312 0 require Text::CSV_XS;
1313  
1314 # extract any sheet number from the region string
1315 0 $regions =~ m/^(\d+)-(.*)/;
1316  
1317 0 ▶ if ($2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2) 0 0 0
1318 0 $sheet = $1 - 1;
1319 0 $regions = $2;
1320 }
1321  
1322 # now extract the start and end regions
1323 0 $regions =~ m/(.*):(.*)/;
1324  
1325 0 ▶ if ( !$1 || !$2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($1 and $2) 0 0 0
Condition0%0/3
$1 and $2
!ll&&!rl&&r
000
MC/DC0%0/2
$1 and $2
$1 $2
Truth table0%0/3
A and B
A= $1B= $2
ABresult
0X0
100
111
1326 0 print STDERR "Bad Params";
1327 0 return "";
1328 }
1329  
1330 0 my @start = sheetRef($1);
1331 0 my @end = sheetRef($2);
1332 0 ▶ if ( !@start ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@start) 0 0 0
1333 0 print STDERR "Bad coorinates - $1";
1334 0 return "";
1335 }
1336 0 ▶ if ( !@end ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@end) 0 0 0
1337 0 print STDERR "Bad coorinates - $2";
1338 0 return "";
1339 }
1340  
1341 0 ▶ if ( $start[1] > $end[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start[1] > $end[1]) 0 0 0
1342 0 print STDERR "Bad COLUMN ordering\n";
1343 0 print STDERR "Start column " . int2col( $start[1] );
1344 0 print STDERR " after end column " . int2col( $end[1] ) . "\n";
1345 0 return "";
1346 }
1347 0 ▶ if ( $start[0] > $end[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start[0] > $end[0]) 0 0 0
1348 0 print STDERR "Bad ROW ordering\n";
1349 0 print STDERR "Start row " . ( $start[0] + 1 );
1350 0 print STDERR " after end row " . ( $end[0] + 1 ) . "\n";
1351 0 exit;
1352 }
1353  
1354 # start the excel object now
1355 0 my $oExcel = new Spreadsheet::ParseExcel;
1356 0 my $oBook = $oExcel->Parse($filename);
1357  
1358 # open the sheet
1359 0 my $oWkS = $oBook->{Worksheet}[$sheet];
1360  
1361 # now check that the region exists in the file
1362 # if not truncate to the possible region
1363 # output a warning msg
1364 0 ▶ if ( $start[1] < $oWkS->{MinCol} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start[1] < $oWkS->{"MinCol"}) 0 0 0
1365 print STDERR int2col( $start[1] )
1366 . " < min col "
1367 . int2col( $oWkS->{MinCol} )
1368 0 . " Resetting\n";
1369 0 $start[1] = $oWkS->{MinCol};
1370 }
1371 0 ▶ if ( $end[1] > $oWkS->{MaxCol} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end[1] > $oWkS->{"MaxCol"}) 0 0 0
1372 print STDERR int2col( $end[1] )
1373 . " > max col "
1374 . int2col( $oWkS->{MaxCol} )
1375 0 . " Resetting\n";
1376 0 $end[1] = $oWkS->{MaxCol};
1377 }
1378 0 ▶ if ( $start[0] < $oWkS->{MinRow} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start[0] < $oWkS->{"MinRow"}) 0 0 0
1379 print STDERR ""
1380 . ( $start[0] + 1 )
1381 . " < min row "
1382 0 . ( $oWkS->{MinRow} + 1 )
1383 . " Resetting\n";
1384 0 $start[0] = $oWkS->{MinCol};
1385 }
1386 0 ▶ if ( $end[0] > $oWkS->{MaxRow} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end[0] > $oWkS->{"MaxRow"}) 0 0 0
1387 print STDERR ""
1388 . ( $end[0] + 1 )
1389 . " > max row "
1390 0 . ( $oWkS->{MaxRow} + 1 )
1391 . " Resetting\n";
1392 0 $end[0] = $oWkS->{MaxRow};
1393  
1394 }
1395  
1396 0 my $x1 = $start[1];
1397 0 my $y1 = $start[0];
1398 0 my $x2 = $end[1];
1399 0 my $y2 = $end[0];
1400  
1401 0 my @cell_data;
1402 0 my $row = 0;
1403  
1404 0 ▶ if ( !$rotate ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rotate) { } 0 0 0
1405 0 ▶ for ( my $y = $y1 ; $y <= $y2 ; $y++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y <= $y2) 0 0 0
1406 0 ▶ for ( my $x = $x1 ; $x <= $x2 ; $x++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $x2) 0 0 0
1407 0 my $cell = $oWkS->{Cells}[$y][$x];
1408  
1409 0 my $value;
1410 0 ▶ if ( defined $cell ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cell) { } 0 0 0
1411 0 $value .= $cell->value();
1412 }
1413 else {
1414 0 $value = '';
1415 }
1416  
1417 0 push @{ $cell_data[$row] }, $value;
1418 }
1419 0 $row++;
1420 }
1421 }
1422 else {
1423 0 ▶ for ( my $x = $x1 ; $x <= $x2 ; $x++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $x2) 0 0 0
1424 0 ▶ for ( my $y = $y1 ; $y <= $y2 ; $y++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y <= $y2) 0 0 0
1425 0 my $cell = $oWkS->{Cells}[$y][$x];
1426  
1427 0 my $value;
1428 0 ▶ if ( defined $cell ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cell) { } 0 0 0
1429 0 $value .= $cell->value();
1430 }
1431 else {
1432 0 $value = '';
1433 }
1434  
1435 0 push @{ $cell_data[$row] }, $value;
1436 }
1437 0 $row++;
1438 }
1439 }
1440  
1441 # Create the CSV output string.
1442 0 my $csv = Text::CSV_XS->new( { binary => 1, eol => $/ } );
1443 0 my $output = "";
1444  
1445 0 for my $row (@cell_data) {
1446 0 $csv->combine(@$row);
1447 0 $output .= $csv->string();
1448 }
1449  
1450 0 return $output;
1451 }
1452  
1453 28 1;
1454  
1455 __END__
↑ Summary ParseExcel/SaveParser/Workbook.pm »