WriteExcel/Workbook.pm

filter: Statementstmt 73.4% 977 / 1331 Branchbran 52.7% 194 / 368 Conditioncond 35.8% 48 / 134 MC/DCmcdc 30.3% 24 / 79 Subroutinesub 79.4% 62 / 78 Podpod 0.0% 0 / 19 totaltotal 64.9% 1305 / 2009 CC 271 SCAR 36.6
CC
271
Cov
67%
set_custom_color
240.0
_store_shared_strings
120.6
_process_images
86.5
CRAP 38.8
« WriteExcel/Format.pm ↑ Summary WriteExcel/Formula.pm »
1 package Spreadsheet::WriteExcel::Workbook;
2  
3 ###############################################################################
4 #
5 # Workbook - A writer class for Excel Workbooks.
6 #
7 #
8 # Used in conjunction with Spreadsheet::WriteExcel
9 #
10 # Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
11 #
12 # Documentation after __END__
13 #
14 30 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 30 ▶use Spreadsheet::WriteExcel::BIFFwriter;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 30 ▶use Spreadsheet::WriteExcel::OLEwriter;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 30 ▶use Spreadsheet::WriteExcel::Worksheet;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 30 ▶use Spreadsheet::WriteExcel::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 30 ▶use Spreadsheet::WriteExcel::Chart;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 30 ▶use Spreadsheet::WriteExcel::Properties ':property_sets';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 30 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 30 @ISA = qw(Spreadsheet::WriteExcel::BIFFwriter Exporter);
26  
27 30 $VERSION = '2.40';
28  
29 ###############################################################################
30 #
31 # new()
32 #
33 # Constructor. Creates a new Workbook object from a BIFFwriter object.
34 #
35 61 ▶sub new {
Subroutine new: called CC=7 SCAR=19.6
Pod undocumented
36  
37 61 my $class = shift;
38 61 my $self = Spreadsheet::WriteExcel::BIFFwriter->new();
39 61 my $byte_order = $self->{_byte_order};
40 61 my $parser = Spreadsheet::WriteExcel::Formula->new($byte_order);
41  
42 61 ▶ $self->{_filename} = $_[0] || '';
Condition50%1/2
$_[0] || ''
l!l
610
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
43 61 $self->{_parser} = $parser;
44 61 $self->{_tempdir} = undef;
45 61 $self->{_1904} = 0;
46 61 $self->{_activesheet} = 0;
47 61 $self->{_firstsheet} = 0;
48 61 $self->{_selected} = 0;
49 61 $self->{_xf_index} = 0;
50 61 $self->{_fileclosed} = 0;
51 61 $self->{_biffsize} = 0;
52 61 $self->{_sheet_name} = 'Sheet';
53 61 $self->{_chart_name} = 'Chart';
54 61 $self->{_sheet_count} = 0;
55 61 $self->{_chart_count} = 0;
56 61 $self->{_url_format} = '';
57 61 $self->{_codepage} = 0x04E4;
58 61 $self->{_country} = 1;
59 61 $self->{_worksheets} = [];
60 61 $self->{_sheetnames} = [];
61 61 $self->{_formats} = [];
62 61 $self->{_palette} = [];
63  
64 61 $self->{_using_tmpfile} = 1;
65 61 $self->{_filehandle} = "";
66 61 $self->{_temp_file} = "";
67 61 $self->{_internal_fh} = 0;
68 61 $self->{_fh_out} = "";
69  
70 61 $self->{_str_total} = 0;
71 61 $self->{_str_unique} = 0;
72 61 $self->{_str_table} = {};
73 61 $self->{_str_array} = [];
74 61 $self->{_str_block_sizes} = [];
75 61 $self->{_extsst_offsets} = [];
76 61 $self->{_extsst_buckets} = 0;
77 61 $self->{_extsst_bucket_size} = 0;
78  
79 61 $self->{_ext_ref_count} = 0;
80 61 $self->{_ext_refs} = {};
81  
82 61 $self->{_mso_clusters} = [];
83 61 $self->{_mso_size} = 0;
84  
85 61 $self->{_hideobj} = 0;
86 61 $self->{_compatibility} = 0;
87  
88 61 $self->{_add_doc_properties} = 0;
89 61 $self->{_localtime} = [localtime()];
90  
91 61 $self->{_defined_names} = [];
92  
93 61 bless $self, $class;
94  
95  
96 # Add the in-built style formats and the default cell format.
97 61 $self->add_format(type => 1); # 0 Normal
98 61 $self->add_format(type => 1); # 1 RowLevel 1
99 61 $self->add_format(type => 1); # 2 RowLevel 2
100 61 $self->add_format(type => 1); # 3 RowLevel 3
101 61 $self->add_format(type => 1); # 4 RowLevel 4
102 61 $self->add_format(type => 1); # 5 RowLevel 5
103 61 $self->add_format(type => 1); # 6 RowLevel 6
104 61 $self->add_format(type => 1); # 7 RowLevel 7
105 61 $self->add_format(type => 1); # 8 ColLevel 1
106 61 $self->add_format(type => 1); # 9 ColLevel 2
107 61 $self->add_format(type => 1); # 10 ColLevel 3
108 61 $self->add_format(type => 1); # 11 ColLevel 4
109 61 $self->add_format(type => 1); # 12 ColLevel 5
110 61 $self->add_format(type => 1); # 13 ColLevel 6
111 61 $self->add_format(type => 1); # 14 ColLevel 7
112 61 $self->add_format(); # 15 Cell XF
113 61 $self->add_format(type => 1, num_format => 0x2B); # 16 Comma
114 61 $self->add_format(type => 1, num_format => 0x29); # 17 Comma[0]
115 61 $self->add_format(type => 1, num_format => 0x2C); # 18 Currency
116 61 $self->add_format(type => 1, num_format => 0x2A); # 19 Currency[0]
117 61 $self->add_format(type => 1, num_format => 0x09); # 20 Percent
118  
119  
120 # Add the default format for hyperlinks
121 61 $self->{_url_format} = $self->add_format(color => 'blue', underline => 1);
122  
123  
124 # Check for a filename unless it is an existing filehandle
125 61 ▶ if (not ref $self->{_filename} and $self->{_filename} eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $self->{"_filename"} and $self->{"_filename"} eq "") 0 61 61
Condition33%1/3
not ref $self->{"_filename"} and $self->{"_filename"} eq ""
!ll&&!rl&&r
0610
MC/DC0%0/2
not ref $self->{"_filename"} and $self->{"_filename"} eq ""
not ref $self->{"_filename"} $self->{"_filename"} eq ""
Truth table33%1/3
not(A) and B
A= not ref $self->{"_filename"}B= $self->{"_filename"} eq ""
ABresult
0X0
100
111
126 0 carp 'Filename required by Spreadsheet::WriteExcel->new()';
127 0 return undef;
128 }
129  
130  
131 # Convert the filename to a filehandle to pass to the OLE writer when the
132 # file is closed. If the filename is a reference it is assumed that it is
133 # a valid filehandle.
134 #
135 61 ▶ if (not ref $self->{_filename}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $self->{'_filename'}) { } 61 0 61
136  
137 61 my $fh = FileHandle->new('>'. $self->{_filename});
138  
139 61 ▶ if (not defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 61 61
140 carp "Can't open " .
141 $self->{_filename} .
142 0 ". It may be in use or protected";
143 0 return undef;
144 }
145  
146 # binmode file whether platform requires it or not
147 61 binmode($fh);
148 61 $self->{_internal_fh} = 1;
149 61 $self->{_fh_out} = $fh;
150 }
151 else {
152 0 $self->{_internal_fh} = 0;
153 0 $self->{_fh_out} = $self->{_filename};
154  
155 }
156  
157  
158 # Set colour palette.
159 61 $self->set_palette_xl97();
160  
161 # Load Encode if we can.
162 61 ▶ require Encode if $] >= 5.008;
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008" 61 0 61
163  
164 61 $self->_initialize();
165 61 $self->_get_checksum_method();
166 61 return $self;
167 }
168  
169  
170 ###############################################################################
171 #
172 # _initialize()
173 #
174 # Open a tmp file to store the majority of the Worksheet data. If this fails,
175 # for example due to write permissions, store the data in memory. This can be
176 # slow for large files.
177 #
178 # TODO: Move this and other methods shared with Worksheet up into BIFFwriter.
179 #
180 61 ▶sub _initialize {
Subroutine _initialize: called CC=9 SCAR=35.9
181  
182 61 my $self = shift;
183 61 my $fh;
184 my $tmp_dir;
185  
186 # The following code is complicated by Windows limitations. Porters can
187 # choose a more direct method.
188  
189  
190  
191 # In the default case we use IO::File->new_tmpfile(). This may fail, in
192 # particular with IIS on Windows, so we allow the user to specify a temp
193 # directory via File::Temp.
194 #
195 61 ▶ if (defined $self->{_tempdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_tempdir'}) { } 0 61 61
196  
197 # Delay loading File:Temp to reduce the module dependencies.
198 0 eval { require File::Temp };
199 0 ▶ die "The File::Temp module must be installed in order ".
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
200 "to call set_tempdir().\n" if $@;
201  
202  
203 # Trap but ignore File::Temp errors.
204 0 eval { $fh = File::Temp::tempfile(DIR => $self->{_tempdir}) };
205  
206 # Store the failed tmp dir in case of errors.
207 0 ▶ $tmp_dir = $self->{_tempdir} || File::Spec->tmpdir if not $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
Condition0%0/3
$self->{'_tempdir'} || 'File::Spec'->tmpdir
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_tempdir'} || 'File::Spec'->tmpdir
$self->{'_tempdir'} 'File::Spec'->tmpdir
Truth table0%0/3
A || B
A= $self->{'_tempdir'}B= 'File::Spec'->tmpdir
ABresult
1X1
011
000
208 }
209 else {
210  
211 61 $fh = IO::File->new_tmpfile();
212  
213 # Store the failed tmp dir in case of errors.
214 61 ▶ $tmp_dir = "POSIX::tmpnam() directory" if not $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 61 61
215 }
216  
217  
218 # Check if the temp file creation was successful. Else store data in memory.
219 61 ▶ if ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) { } 61 0 61
220  
221 # binmode file whether platform requires it or not.
222 61 binmode($fh);
223  
224 # Store filehandle
225 61 $self->{_filehandle} = $fh;
226 }
227 else {
228  
229 # Set flag to store data in memory if XX::tempfile() failed.
230 0 $self->{_using_tmpfile} = 0;
231  
232 0 ▶ if ($^W) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^W) 0 0 0
233 0 ▶ my $dir = $self->{_tempdir} || File::Spec->tmpdir();
Condition0%0/3
$self->{'_tempdir'} || 'File::Spec'->tmpdir
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_tempdir'} || 'File::Spec'->tmpdir
$self->{'_tempdir'} 'File::Spec'->tmpdir
Truth table0%0/3
A || B
A= $self->{'_tempdir'}B= 'File::Spec'->tmpdir
ABresult
1X1
011
000
234  
235 0 warn "Unable to create temp files in $tmp_dir. Data will be ".
236 "stored in memory. Refer to set_tempdir() in the ".
237 "Spreadsheet::WriteExcel documentation.\n" ;
238 }
239 }
240 }
241  
242  
243 ###############################################################################
244 #
245 # _get_checksum_method.
246 #
247 # Check for modules available to calculate image checksum. Excel uses MD4 but
248 # MD5 will also work.
249 #
250 61 ▶sub _get_checksum_method {
Subroutine _get_checksum_method: called CC=4 SCAR=15.4
251  
252 61 my $self = shift;
253  
254 61 eval { require Digest::MD4};
255 61 ▶ if (not $@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 61 61
256 0 $self->{_checksum_method} = 1;
257 0 return;
258 }
259  
260  
261 61 eval { require Digest::Perl::MD4};
262 61 ▶ if (not $@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 61 61
263 0 $self->{_checksum_method} = 2;
264 0 return;
265 }
266  
267  
268 61 eval { require Digest::MD5};
269 61 ▶ if (not $@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 61 0 61
270 61 $self->{_checksum_method} = 3;
271 61 return;
272 }
273  
274 # Default.
275 0 $self->{_checksum_method} = 0;
276 }
277  
278  
279 ###############################################################################
280 #
281 # _append(), overridden.
282 #
283 # Store Worksheet data in memory using the base class _append() or to a
284 # temporary file, the default.
285 #
286 3145 ▶sub _append {
Subroutine _append: called CC=3 SCAR=11.0
287  
288 3145 my $self = shift;
289 3145 my $data = '';
290  
291 3145 ▶ if ($self->{_using_tmpfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_using_tmpfile'}) { } 2211 934 3145
292 2211 $data = join('', @_);
293  
294 # Add CONTINUE records if necessary
295 2211 ▶ $data = $self->_add_continue($data) if length($data) > $self->{_limit};
Branch: 1 branch
BranchTrueFalseTotal
if length $data > $self->{"_limit"} 0 2211 2211
296  
297 # Protect print() from -l on the command line.
298 2211 local $\ = undef;
299  
300 2211 print {$self->{_filehandle}} $data;
301 2211 $self->{_datasize} += length($data);
302 }
303 else {
304 934 $data = $self->SUPER::_append(@_);
305 }
306  
307 3145 return $data;
308 }
309  
310  
311 ###############################################################################
312 #
313 # get_data().
314 #
315 # Retrieves data from memory in one chunk, or from disk in $buffer
316 # sized chunks.
317 #
318 166 ▶sub get_data {
Subroutine get_data: called CC=5 SCAR=16.1
Pod undocumented
319  
320 166 my $self = shift;
321 166 my $buffer = 4096;
322 166 my $tmp;
323  
324 # Return data stored in memory
325 166 ▶ if (defined $self->{_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_data"}) 61 105 166
326 61 $tmp = $self->{_data};
327 61 $self->{_data} = undef;
328 61 my $fh = $self->{_filehandle};
329 61 ▶ seek($fh, 0, 0) if $self->{_using_tmpfile};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_using_tmpfile"} 44 17 61
330 61 return $tmp;
331 }
332  
333 # Return data stored on disk
334 105 ▶ if ($self->{_using_tmpfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_using_tmpfile"}) 88 17 105
335 88 ▶ return $tmp if read($self->{_filehandle}, $tmp, $buffer);
Branch: 1 branch
BranchTrueFalseTotal
if read $self->{"_filehandle"}, $tmp, $buffer 44 44 88
336 }
337  
338 # No data to return
339 61 return undef;
340 }
341  
342  
343 ###############################################################################
344 #
345 # close()
346 #
347 # Calls finalization methods and explicitly close the OLEwriter file
348 # handle.
349 #
350 62 ▶sub close {
Subroutine close: called CC=2 SCAR=6.9
Pod undocumented
351  
352 62 my $self = shift;
353  
354 62 ▶ return if $self->{_fileclosed}; # Prevent close() from being called twice.
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_fileclosed"} 1 61 62
355  
356 61 $self->{_fileclosed} = 1;
357  
358 61 return $self->_store_workbook();
359 }
360  
361  
362 ###############################################################################
363 #
364 # DESTROY()
365 #
366 # Close the workbook if it hasn't already been explicitly closed.
367 #
368 61 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
369  
370 61 my $self = shift;
371  
372 61 local ($@, $!, $^E, $?);
373  
374 61 ▶ $self->close() if not $self->{_fileclosed};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"_fileclosed"} 2 59 61
375 }
376  
377  
378 ###############################################################################
379 #
380 # sheets(slice,...)
381 #
382 # An accessor for the _worksheets[] array
383 #
384 # Returns: an optionally sliced list of the worksheet objects in a workbook.
385 #
386 18 ▶sub sheets {
Subroutine sheets: called CC=2 SCAR=7.9
Pod undocumented
387  
388 18 my $self = shift;
389  
390 18 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 18 18
391 # Return a slice of the array
392 0 return @{$self->{_worksheets}}[@_];
393 }
394 else {
395 # Return the entire list
396 18 return @{$self->{_worksheets}};
397 }
398 }
399  
400  
401 ###############################################################################
402 #
403 # worksheets()
404 #
405 # An accessor for the _worksheets[] array.
406 # This method is now deprecated. Use the sheets() method instead.
407 #
408 # Returns: an array reference
409 #
410 0 ▶sub worksheets {
Subroutine worksheets: not called CC=1 SCAR=6.9
Pod undocumented
411  
412 0 my $self = shift;
413  
414 0 return $self->{_worksheets};
415 }
416  
417  
418 ###############################################################################
419 #
420 # add_worksheet($name, $encoding)
421 #
422 # Add a new worksheet to the Excel workbook.
423 #
424 # Returns: reference to a worksheet object
425 #
426 133 ▶sub add_worksheet {
Subroutine add_worksheet: called CC=1 SCAR=0.0
Pod undocumented
427  
428 133 my $self = shift;
429 133 my $index = @{$self->{_worksheets}};
430  
431 133 my ($name, $encoding) = $self->_check_sheetname($_[0], $_[1]);
432  
433  
434 # Porters take note, the following scheme of passing references to Workbook
435 # data (in the \$self->{_foo} cases) instead of a reference to the Workbook
436 # itself is a workaround to avoid circular references between Workbook and
437 # Worksheet objects. Feel free to implement this in any way the suits your
438 # language.
439 #
440 my @init_data = (
441 $name,
442 $index,
443 $encoding,
444 \$self->{_activesheet},
445 \$self->{_firstsheet},
446 $self->{_url_format},
447 $self->{_parser},
448 $self->{_tempdir},
449 \$self->{_str_total},
450 \$self->{_str_unique},
451 \$self->{_str_table},
452 $self->{_1904},
453 $self->{_compatibility},
454 undef, # Palette. Not used yet. See add_chart().
455 133 );
456  
457 133 my $worksheet = Spreadsheet::WriteExcel::Worksheet->new(@init_data);
458 133 $self->{_worksheets}->[$index] = $worksheet; # Store ref for iterator
459 133 $self->{_sheetnames}->[$index] = $name; # Store EXTERNSHEET names
460 133 $self->{_parser}->set_ext_sheets($name, $index); # Store names in Formula.pm
461 133 return $worksheet;
462 }
463  
464 # Older method name for backwards compatibility.
465 30 *addworksheet = *add_worksheet;
466  
467  
468 ###############################################################################
469 #
470 # add_chart(%args)
471 #
472 # Create a chart for embedding or as a new sheet.
473 #
474 #
475 2 ▶sub add_chart {
Subroutine add_chart: called CC=5 SCAR=16.8
Pod undocumented
476  
477 2 my $self = shift;
478 2 my %arg = @_;
479 2 my $name = '';
480 2 my $encoding = 0;
481 2 my $index = @{ $self->{_worksheets} };
482  
483 # Type must be specified so we can create the required chart instance.
484 2 my $type = $arg{type};
485 2 ▶ if ( !defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 2 2
486 0 croak "Must define chart type in add_chart()";
487 }
488  
489 # Ensure that the chart defaults to non embedded.
490 2 ▶ my $embedded = $arg{embedded} ||= 0;
Condition50%1/2
$arg{"embedded"} ||= 0
l!l
02
MC/DC0%0/1
$arg{"embedded"} ||= 0
$arg{"embedded"}
Truth table50%1/2
A
A= $arg{"embedded"}
Aresult
11
00
491  
492 # Check the worksheet name for non-embedded charts.
493 2 ▶ if ( !$embedded ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($embedded) 2 0 2
494 ( $name, $encoding ) =
495 2 $self->_check_sheetname( $arg{name}, $arg{name_encoding}, 1 );
496 }
497  
498 my @init_data = (
499 $name,
500 $index,
501 $encoding,
502 \$self->{_activesheet},
503 \$self->{_firstsheet},
504 $self->{_url_format},
505 $self->{_parser},
506 $self->{_tempdir},
507 \$self->{_str_total},
508 \$self->{_str_unique},
509 \$self->{_str_table},
510 $self->{_1904},
511 $self->{_compatibility},
512 $self->{_palette},
513 2 );
514  
515 2 my $chart = Spreadsheet::WriteExcel::Chart->factory( $type, @init_data );
516  
517 # If the chart isn't embedded let the workbook control it.
518 2 ▶ if ( !$embedded ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $embedded) { } 2 0 2
519 2 $self->{_worksheets}->[$index] = $chart; # Store ref for iterator
520 2 $self->{_sheetnames}->[$index] = $name; # Store EXTERNSHEET names
521 }
522 else {
523 # Set index to 0 so that the activate() and set_first_sheet() methods
524 # point back to the first worksheet if used for embedded charts.
525 0 $chart->{_index} = 0;
526  
527 0 $chart->_set_embedded_config_data();
528 }
529  
530 2 return $chart;
531 }
532  
533  
534 ###############################################################################
535 #
536 # add_chart_ext($filename, $name)
537 #
538 # Add an externally created chart.
539 #
540 #
541 0 ▶sub add_chart_ext {
Subroutine add_chart_ext: not called CC=1 SCAR=6.9
Pod undocumented
542  
543 0 my $self = shift;
544 0 my $filename = $_[0];
545 0 my $index = @{$self->{_worksheets}};
546 0 my $type = 'external';
547  
548 0 my ($name, $encoding) = $self->_check_sheetname($_[1], $_[2]);
549  
550  
551 my @init_data = (
552 $filename,
553 $name,
554 $index,
555 $encoding,
556 \$self->{_activesheet},
557 \$self->{_firstsheet},
558 0 );
559  
560 0 my $chart = Spreadsheet::WriteExcel::Chart->factory($type, @init_data);
561 0 $self->{_worksheets}->[$index] = $chart; # Store ref for iterator
562 0 $self->{_sheetnames}->[$index] = $name; # Store EXTERNSHEET names
563  
564 0 return $chart;
565 }
566  
567  
568 ###############################################################################
569 #
570 # _check_sheetname($name, $encoding)
571 #
572 # Check for valid worksheet names. We check the length, if it contains any
573 # invalid characters and if the name is unique in the workbook.
574 #
575 204 ▶sub _check_sheetname {
Subroutine _check_sheetname: called CC=37 SCAR=38.4
576  
577 204 my $self = shift;
578 204 ▶ my $name = $_[0] || "";
Condition100%2/2
$_[0] || ''
l!l
70134
MC/DC100%1/1
$_[0] || ''
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
579 204 ▶ my $encoding = $_[1] || 0;
Condition100%2/2
$_[1] || 0
l!l
21183
MC/DC100%1/1
$_[1] || 0
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
580 204 ▶ my $chart = $_[2] || 0;
Condition100%2/2
$_[2] || 0
l!l
2202
MC/DC100%1/1
$_[2] || 0
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
581 204 ▶ my $limit = $encoding ? 62 : 31;
Branch: 1 branch
BranchTrueFalseTotal
$encoding ? : 21 183 204
582 204 my $invalid_char = qr([\[\]:*?/\\]);
583  
584 # Increment the Sheet/Chart number used for default sheet names below.
585 204 ▶ if ( $chart ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chart) { } 2 202 204
586 2 $self->{_chart_count}++;
587 }
588 else {
589 202 $self->{_sheet_count}++;
590 }
591  
592 # Supply default Sheet/Chart name if none has been defined.
593 204 ▶ if ( $name eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "") 134 70 204
594 134 $encoding = 0;
595  
596 134 ▶ if ( $chart ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chart) { } 2 132 134
597 2 $name = $self->{_chart_name} . $self->{_chart_count};
598 }
599 else {
600 132 $name = $self->{_sheet_name} . $self->{_sheet_count};
601 }
602 }
603  
604  
605 # Check that sheetname is <= 31 (1 or 2 byte chars). Excel limit.
606 204 ▶ croak "Sheetname $name must be <= 31 chars" if length $name > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if length $name > $limit 4 200 204
607  
608 # Check that Unicode sheetname has an even number of bytes
609 200 ▶ croak 'Odd number of bytes in Unicode worksheet name:' . $name
Branch: 1 branch
BranchTrueFalseTotal
if $encoding == 1 and length($name) % 2 0 200 200
Condition66%2/3
$encoding == 1 and length($name) % 2
!ll&&!rl&&r
182180
MC/DC0%0/2
$encoding == 1 and length($name) % 2
$encoding == 1 length($name) % 2
Truth table66%2/3
A and B
A= $encoding == 1B= length($name) % 2
ABresult
0X0
100
111
610 if $encoding == 1 and length($name) % 2;
611  
612  
613 # Check that sheetname doesn't contain any invalid characters
614 200 ▶ if ($encoding != 1 and $name =~ $invalid_char) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding != 1 and $name =~ /$invalid_char/) { } 21 179 200
Condition100%3/3
$encoding != 1 and $name =~ /$invalid_char/
!ll&&!rl&&r
1816121
MC/DC100%2/2
$encoding != 1 and $name =~ /$invalid_char/
$encoding != 1 $name =~ /$invalid_char/
Truth table100%3/3
A and B
A= $encoding != 1B= $name =~ /$invalid_char/
ABresult
0X0
100
111
615 # Check ASCII names
616 21 croak 'Invalid character []:*?/\\ in worksheet name: ' . $name;
617 }
618 else {
619 # Extract any 8bit clean chars from the UTF16 name and validate them.
620 179 for my $wchar ($name =~ /../sg) {
621 606 my ($hi, $lo) = unpack "aa", $wchar;
622 606 ▶ if ($hi eq "\0" and $lo =~ $invalid_char) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hi eq "\0" and $lo =~ /$invalid_char/) 7 599 606
Condition100%3/3
$hi eq "\0" and $lo =~ /$invalid_char/
!ll&&!rl&&r
507927
MC/DC100%2/2
$hi eq "\0" and $lo =~ /$invalid_char/
$hi eq "\0" $lo =~ /$invalid_char/
Truth table100%3/3
A and B
A= $hi eq "\0"B= $lo =~ /$invalid_char/
ABresult
0X0
100
111
623 7 croak 'Invalid character []:*?/\\ in worksheet name: ' . $name;
624 }
625 }
626 }
627  
628  
629 # Handle utf8 strings in perl 5.8.
630 172 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 172 0 172
631 172 require Encode;
632  
633 172 ▶ if (Encode::is_utf8($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($name)) 5 167 172
634 5 $name = Encode::encode("UTF-16BE", $name);
635 5 $encoding = 1;
636 }
637 }
638  
639  
640 # Check that the worksheet name doesn't already exist since this is a fatal
641 # error in Excel 97. The check must also exclude case insensitive matches
642 # since the names 'Sheet1' and 'sheet1' are equivalent. The tests also have
643 # to take the encoding into account.
644 #
645 172 foreach my $worksheet (@{$self->{_worksheets}}) {
646 618 my $name_a = $name;
647 618 my $encd_a = $encoding;
648 618 my $name_b = $worksheet->{_name};
649 618 my $encd_b = $worksheet->{_encoding};
650 618 my $error = 0;
651  
652 618 ▶ if ($encd_a == 0 and $encd_b == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($encd_a == 0 and $encd_b == 0) { } 552 66 618
elsif ($encd_a == 0 and $encd_b == 1) { } 12 54 66
elsif ($encd_a == 1 and $encd_b == 0) { } 41 13 54
elsif ($encd_a == 1 and $encd_b == 1) { } 13 0 13
Condition100%3/3
$encd_a == 0 and $encd_b == 0
!ll&&!rl&&r
5412552
MC/DC100%2/2
$encd_a == 0 and $encd_b == 0
$encd_a == 0 $encd_b == 0
Truth table100%3/3
A and B
A= $encd_a == 0B= $encd_b == 0
ABresult
0X0
100
111
653 552 ▶ $error = 1 if lc($name_a) eq lc($name_b);
Branch: 1 branch
BranchTrueFalseTotal
if lc $name_a eq lc $name_b 12 540 552
654 }
655 ▶ elsif ($encd_a == 0 and $encd_b == 1) {
Condition66%2/3
$encd_a == 0 and $encd_b == 1
!ll&&!rl&&r
54012
MC/DC50%1/2
$encd_a == 0 and $encd_b == 1
$encd_a == 0 $encd_b == 1
Truth table66%2/3
A and B
A= $encd_a == 0B= $encd_b == 1
ABresult
0X0
100
111
656 12 $name_a = pack "n*", unpack "C*", $name_a;
657 12 ▶ $error = 1 if lc($name_a) eq lc($name_b);
Branch: 1 branch
BranchTrueFalseTotal
if lc $name_a eq lc $name_b 2 10 12
658 }
659 ▶ elsif ($encd_a == 1 and $encd_b == 0) {
Condition66%2/3
$encd_a == 1 and $encd_b == 0
!ll&&!rl&&r
01341
MC/DC50%1/2
$encd_a == 1 and $encd_b == 0
$encd_a == 1 $encd_b == 0
Truth table66%2/3
A and B
A= $encd_a == 1B= $encd_b == 0
ABresult
0X0
100
111
660 41 $name_b = pack "n*", unpack "C*", $name_b;
661 41 ▶ $error = 1 if lc($name_a) eq lc($name_b);
Branch: 1 branch
BranchTrueFalseTotal
if lc $name_a eq lc $name_b 5 36 41
662 }
663 ▶ elsif ($encd_a == 1 and $encd_b == 1) {
Condition33%1/3
$encd_a == 1 and $encd_b == 1
!ll&&!rl&&r
0013
MC/DC0%0/2
$encd_a == 1 and $encd_b == 1
$encd_a == 1 $encd_b == 1
Truth table33%1/3
A and B
A= $encd_a == 1B= $encd_b == 1
ABresult
0X0
100
111
664 # We can do a true case insensitive test with Perl 5.8 and utf8.
665 13 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.008) { } 13 0 13
666 13 $name_a = Encode::decode("UTF-16BE", $name_a);
667 13 $name_b = Encode::decode("UTF-16BE", $name_b);
668 13 ▶ $error = 1 if lc($name_a) eq lc($name_b);
Branch: 1 branch
BranchTrueFalseTotal
if lc $name_a eq lc $name_b 4 9 13
669 }
670 else {
671 # We can't easily do a case insensitive test of the UTF16 names.
672 # As a special case we check if all of the high bytes are nulls and
673 # then do an ASCII style case insensitive test.
674  
675 # Strip out the high bytes (funkily).
676 0 my $hi_a = grep {ord} $name_a =~ /(.)./sg;
677 0 my $hi_b = grep {ord} $name_b =~ /(.)./sg;
678  
679 0 ▶ if ($hi_a or $hi_b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hi_a or $hi_b) { } 0 0 0
Condition0%0/3
$hi_a or $hi_b
l!l&&r!l&&!r
000
MC/DC0%0/2
$hi_a or $hi_b
$hi_a $hi_b
Truth table0%0/3
A or B
A= $hi_aB= $hi_b
ABresult
1X1
011
000
680 0 ▶ $error = 1 if $name_a eq $name_b;
Branch: 1 branch
BranchTrueFalseTotal
if $name_a eq $name_b 0 0 0
681 }
682 else {
683 0 ▶ $error = 1 if lc($name_a) eq lc($name_b);
Branch: 1 branch
BranchTrueFalseTotal
if lc $name_a eq lc $name_b 0 0 0
684 }
685 }
686 }
687  
688 # If any of the cases failed we throw the error here.
689 618 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 23 595 618
690 23 croak "Worksheet name '$name', with case ignored, " .
691 "is already in use";
692 }
693 }
694  
695 149 return ($name, $encoding);
696 }
697  
698  
699 ###############################################################################
700 #
701 # add_format(%properties)
702 #
703 # Add a new format to the Excel workbook. This adds an XF record and
704 # a FONT record. Also, pass any properties to the Format::new().
705 #
706 1346 ▶sub add_format {
Subroutine add_format: called CC=1 SCAR=0.0
Pod undocumented
707  
708 1346 my $self = shift;
709  
710 1346 my $format = Spreadsheet::WriteExcel::Format->new($self->{_xf_index}, @_);
711  
712 1346 $self->{_xf_index} += 1;
713 1346 push @{$self->{_formats}}, $format; # Store format reference
714  
715 1346 return $format;
716 }
717  
718 # Older method name for backwards compatibility.
719 30 *addformat = *add_format;
720  
721  
722 ###############################################################################
723 #
724 # compatibility_mode()
725 #
726 # Set the compatibility mode.
727 #
728 # Excel doesn't require every possible Biff record to be present in a file.
729 # In particular if the indexing records INDEX, ROW and DBCELL aren't present
730 # it just ignores the fact and reads the cells anyway. This is also true of
731 # the EXTSST record. Gnumeric and OOo also take this approach. This allows
732 # WriteExcel to ignore these records in order to minimise the amount of data
733 # stored in memory. However, other third party applications that read Excel
734 # files often expect these records to be present. In "compatibility mode"
735 # WriteExcel writes these records and tries to be as close to an Excel
736 # generated file as possible.
737 #
738 # This requires additional data to be stored in memory until the file is
739 # about to be written. This incurs a memory and speed penalty and may not be
740 # suitable for very large files.
741 #
742 1 ▶sub compatibility_mode {
Subroutine compatibility_mode: called CC=3 SCAR=12.0
Pod undocumented
743  
744 1 my $self = shift;
745  
746 1 ▶ croak "compatibility_mode() must be called before add_worksheet()"
Branch: 1 branch
BranchTrueFalseTotal
if $self->sheets 0 1 1
747 if $self->sheets();
748  
749 1 ▶ if (defined($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 1 0 1
750 1 $self->{_compatibility} = $_[0];
751 }
752 else {
753 0 $self->{_compatibility} = 1;
754 }
755 }
756  
757  
758 ###############################################################################
759 #
760 # set_1904()
761 #
762 # Set the date system: 0 = 1900 (the default), 1 = 1904
763 #
764 0 ▶sub set_1904 {
Subroutine set_1904: not called CC=3 SCAR=24.8
Pod undocumented
765  
766 0 my $self = shift;
767  
768 0 ▶ croak "set_1904() must be called before add_worksheet()"
Branch: 1 branch
BranchTrueFalseTotal
if $self->sheets 0 0 0
769 if $self->sheets();
770  
771  
772 0 ▶ if (defined($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
773 0 $self->{_1904} = $_[0];
774 }
775 else {
776 0 $self->{_1904} = 1;
777 }
778 }
779  
780  
781 ###############################################################################
782 #
783 # get_1904()
784 #
785 # Return the date system: 0 = 1900, 1 = 1904
786 #
787 0 ▶sub get_1904 {
Subroutine get_1904: not called CC=1 SCAR=6.9
Pod undocumented
788  
789 0 my $self = shift;
790  
791 0 return $self->{_1904};
792 }
793  
794  
795 ###############################################################################
796 #
797 # set_custom_color()
798 #
799 # Change the RGB components of the elements in the colour palette.
800 #
801 0 ▶sub set_custom_color {
Subroutine set_custom_color: not called CC=15 SCAR=54.8
Pod undocumented
802  
803 0 my $self = shift;
804  
805  
806 # Match a HTML #xxyyzz style parameter
807 0 ▶ if (defined $_[1] and $_[1] =~ /^#(\w\w)(\w\w)(\w\w)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1] and $_[1] =~ /^#(\w\w)(\w\w)(\w\w)/) 0 0 0
Condition0%0/3
defined $_[1] and $_[1] =~ /^#(\w\w)(\w\w)(\w\w)/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[1] and $_[1] =~ /^#(\w\w)(\w\w)(\w\w)/
defined $_[1] $_[1] =~ /^#(\w\w)(\w\w)(\w\w)/
Truth table0%0/3
A and B
A= defined $_[1]B= $_[1] =~ /^#(\w\w)(\w\w)(\w\w)/
ABresult
0X0
100
111
808 0 @_ = ($_[0], hex $1, hex $2, hex $3);
809 }
810  
811  
812 0 ▶ my $index = $_[0] || 0;
Condition0%0/2
$_[0] || 0
l!l
00
MC/DC0%0/1
$_[0] || 0
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
813 0 ▶ my $red = $_[1] || 0;
Condition0%0/2
$_[1] || 0
l!l
00
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
814 0 ▶ my $green = $_[2] || 0;
Condition0%0/2
$_[2] || 0
l!l
00
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
815 0 ▶ my $blue = $_[3] || 0;
Condition0%0/2
$_[3] || 0
l!l
00
MC/DC0%0/1
$_[3] || 0
$_[3]
Truth table0%0/2
A
A= $_[3]
Aresult
11
00
816  
817 0 my $aref = $self->{_palette};
818  
819 # Check that the colour index is the right range
820 0 ▶ if ($index < 8 or $index > 64) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < 8 or $index > 64) 0 0 0
Condition0%0/3
$index < 8 or $index > 64
l!l&&r!l&&!r
000
MC/DC0%0/2
$index < 8 or $index > 64
$index < 8 $index > 64
Truth table0%0/3
A or B
A= $index < 8B= $index > 64
ABresult
1X1
011
000
821 0 carp "Color index $index outside range: 8 <= index <= 64";
822 0 return 0;
823 }
824  
825 # Check that the colour components are in the right range
826 0 ▶ if ( ($red < 0 or $red > 255) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($red < 0 or $red > 255 or ($green < 0 or $green > 255) or ($blue < 0 or $blue > 255)) 0 0 0
Condition0%0/15
$red < 0 or $red > 255 or ($green < 0 or $green > 255) or ($blue < 0 or $blue > 255)
l!l&&r!l&&!r
000
$red < 0 or $red > 255 or ($green < 0 or $green > 255)
l!l&&r!l&&!r
000
$red < 0 or $red > 255
l!l&&r!l&&!r
000
$green < 0 or $green > 255
l!l&&r!l&&!r
000
$blue < 0 or $blue > 255
l!l&&r!l&&!r
000
MC/DC0%0/6
$red < 0 or $red > 255 or ($green < 0 or $green > 255) or ($blue < 0 or $blue > 255)
$red < 0 $red > 255 $green < 0 $green > 255 $blue < 0 $blue > 255
Truth table0%0/7
A or B or C or D or E or F
A= $red < 0B= $red > 255C= $green < 0D= $green > 255E= $blue < 0F= $blue > 255
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
827 ($green < 0 or $green > 255) ||
828 ($blue < 0 or $blue > 255) )
829 {
830 0 carp "Color component outside range: 0 <= color <= 255";
831 0 return 0;
832 }
833  
834 0 $index -=8; # Adjust colour index (wingless dragonfly)
835  
836 # Set the RGB value
837 0 $aref->[$index] = [$red, $green, $blue, 0];
838  
839 0 return $index +8;
840 }
841  
842  
843 ###############################################################################
844 #
845 # set_palette_xl97()
846 #
847 # Sets the colour palette to the Excel 97+ default.
848 #
849 61 ▶sub set_palette_xl97 {
Subroutine set_palette_xl97: called CC=1 SCAR=0.0
Pod undocumented
850  
851 61 my $self = shift;
852  
853 $self->{_palette} = [
854 61 [0x00, 0x00, 0x00, 0x00], # 8
855 [0xff, 0xff, 0xff, 0x00], # 9
856 [0xff, 0x00, 0x00, 0x00], # 10
857 [0x00, 0xff, 0x00, 0x00], # 11
858 [0x00, 0x00, 0xff, 0x00], # 12
859 [0xff, 0xff, 0x00, 0x00], # 13
860 [0xff, 0x00, 0xff, 0x00], # 14
861 [0x00, 0xff, 0xff, 0x00], # 15
862 [0x80, 0x00, 0x00, 0x00], # 16
863 [0x00, 0x80, 0x00, 0x00], # 17
864 [0x00, 0x00, 0x80, 0x00], # 18
865 [0x80, 0x80, 0x00, 0x00], # 19
866 [0x80, 0x00, 0x80, 0x00], # 20
867 [0x00, 0x80, 0x80, 0x00], # 21
868 [0xc0, 0xc0, 0xc0, 0x00], # 22
869 [0x80, 0x80, 0x80, 0x00], # 23
870 [0x99, 0x99, 0xff, 0x00], # 24
871 [0x99, 0x33, 0x66, 0x00], # 25
872 [0xff, 0xff, 0xcc, 0x00], # 26
873 [0xcc, 0xff, 0xff, 0x00], # 27
874 [0x66, 0x00, 0x66, 0x00], # 28
875 [0xff, 0x80, 0x80, 0x00], # 29
876 [0x00, 0x66, 0xcc, 0x00], # 30
877 [0xcc, 0xcc, 0xff, 0x00], # 31
878 [0x00, 0x00, 0x80, 0x00], # 32
879 [0xff, 0x00, 0xff, 0x00], # 33
880 [0xff, 0xff, 0x00, 0x00], # 34
881 [0x00, 0xff, 0xff, 0x00], # 35
882 [0x80, 0x00, 0x80, 0x00], # 36
883 [0x80, 0x00, 0x00, 0x00], # 37
884 [0x00, 0x80, 0x80, 0x00], # 38
885 [0x00, 0x00, 0xff, 0x00], # 39
886 [0x00, 0xcc, 0xff, 0x00], # 40
887 [0xcc, 0xff, 0xff, 0x00], # 41
888 [0xcc, 0xff, 0xcc, 0x00], # 42
889 [0xff, 0xff, 0x99, 0x00], # 43
890 [0x99, 0xcc, 0xff, 0x00], # 44
891 [0xff, 0x99, 0xcc, 0x00], # 45
892 [0xcc, 0x99, 0xff, 0x00], # 46
893 [0xff, 0xcc, 0x99, 0x00], # 47
894 [0x33, 0x66, 0xff, 0x00], # 48
895 [0x33, 0xcc, 0xcc, 0x00], # 49
896 [0x99, 0xcc, 0x00, 0x00], # 50
897 [0xff, 0xcc, 0x00, 0x00], # 51
898 [0xff, 0x99, 0x00, 0x00], # 52
899 [0xff, 0x66, 0x00, 0x00], # 53
900 [0x66, 0x66, 0x99, 0x00], # 54
901 [0x96, 0x96, 0x96, 0x00], # 55
902 [0x00, 0x33, 0x66, 0x00], # 56
903 [0x33, 0x99, 0x66, 0x00], # 57
904 [0x00, 0x33, 0x00, 0x00], # 58
905 [0x33, 0x33, 0x00, 0x00], # 59
906 [0x99, 0x33, 0x00, 0x00], # 60
907 [0x99, 0x33, 0x66, 0x00], # 61
908 [0x33, 0x33, 0x99, 0x00], # 62
909 [0x33, 0x33, 0x33, 0x00], # 63
910 ];
911  
912 61 return 0;
913 }
914  
915  
916 ###############################################################################
917 #
918 # set_tempdir()
919 #
920 # Change the default temp directory used by _initialize() in Worksheet.pm.
921 #
922 0 ▶sub set_tempdir {
Subroutine set_tempdir: not called CC=5 SCAR=34.0
Pod undocumented
923  
924 0 my $self = shift;
925  
926 # Windows workaround. See Worksheet::_initialize()
927 0 ▶ my $dir = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
928  
929 0 ▶ croak "$dir is not a valid directory" if $dir ne '' and not -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir ne "" and not -d $dir 0 0 0
Condition0%0/3
$dir ne "" and not -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
$dir ne "" and not -d $dir
$dir ne "" not -d $dir
Truth table0%0/3
A and not(B)
A= $dir ne ""B= not -d $dir
ABresult
0X0
100
111
930 0 ▶ croak "set_tempdir must be called before add_worksheet" if $self->sheets();
Branch: 1 branch
BranchTrueFalseTotal
if $self->sheets 0 0 0
931  
932 0 $self->{_tempdir} = $dir ;
933 }
934  
935  
936 ###############################################################################
937 #
938 # set_codepage()
939 #
940 # See also the _store_codepage method. This is used to store the code page, i.e.
941 # the character set used in the workbook.
942 #
943 0 ▶sub set_codepage {
Subroutine set_codepage: not called CC=4 SCAR=30.0
Pod undocumented
944  
945 0 my $self = shift;
946  
947 0 ▶ my $codepage = $_[0] || 1;
Condition0%0/2
$_[0] || 1
l!l
00
MC/DC0%0/1
$_[0] || 1
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
948 0 ▶ $codepage = 0x04E4 if $codepage == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $codepage == 1 0 0 0
949 0 ▶ $codepage = 0x8000 if $codepage == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $codepage == 2 0 0 0
950  
951 0 $self->{_codepage} = $codepage;
952 }
953  
954  
955 ###############################################################################
956 #
957 # set_country()
958 #
959 # See also the _store_country method. This is used to store the country code.
960 # Some non-english versions of Excel may need this set to some value other
961 # than 1 = "United States". In general the country code is equal to the
962 # international dialling code.
963 #
964 0 ▶sub set_country {
Subroutine set_country: not called CC=2 SCAR=17.9
Pod undocumented
965  
966 0 my $self = shift;
967  
968 0 ▶ $self->{_country} = $_[0] || 1;
Condition0%0/2
$_[0] || 1
l!l
00
MC/DC0%0/1
$_[0] || 1
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
969 }
970  
971  
972  
973  
974  
975  
976  
977 ###############################################################################
978 #
979 # define_name()
980 #
981 # TODO.
982 #
983 0 ▶sub define_name {
Subroutine define_name: not called CC=6 SCAR=37.4
Pod undocumented
984  
985 0 my $self = shift;
986 0 my $name = shift;
987 0 my $formula = shift;
988 0 ▶ my $encoding = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
989 0 my $sheet_index = 0;
990 0 my @tokens;
991  
992 0 my $full_name = $name;
993  
994 0 ▶ if ($name =~ /^(.*)!(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(.*)!(.*)$/) 0 0 0
995 0 my $sheetname = $1;
996 0 $name = $2;
997 0 $sheet_index = 1 + $self->{_parser}->_get_sheet_index($sheetname);
998 }
999  
1000  
1001  
1002 # Strip the = sign at the beginning of the formula string
1003 0 $formula =~ s(^=)();
1004  
1005 # Parse the formula using the parser in Formula.pm
1006 0 my $parser = $self->{_parser};
1007  
1008 # In order to raise formula errors from the point of view of the calling
1009 # program we use an eval block and re-raise the error from here.
1010 #
1011 0 eval { @tokens = $parser->parse_formula($formula) };
1012  
1013 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1014 0 $@ =~ s/\n$//; # Strip the \n used in the Formula.pm die()
1015 0 croak $@; # Re-raise the error
1016 }
1017  
1018 # Force 2d ranges to be a reference class.
1019 0 s/_ref3d/_ref3dR/ for @tokens;
1020 0 s/_range3d/_range3dR/ for @tokens;
1021  
1022  
1023 # Parse the tokens into a formula string.
1024 0 $formula = $parser->parse_tokens(@tokens);
1025  
1026  
1027  
1028 0 $full_name = lc $full_name;
1029  
1030 0 push @{$self->{_defined_names}}, {
1031 name => $name,
1032 encoding => $encoding,
1033 sheet_index => $sheet_index,
1034 formula => $formula,
1035 };
1036  
1037 0 my $index = scalar @{$self->{_defined_names}};
1038  
1039 0 $parser->set_ext_name($name, $index);
1040 }
1041  
1042  
1043  
1044  
1045  
1046  
1047  
1048  
1049  
1050 ###############################################################################
1051 #
1052 # set_properties()
1053 #
1054 # Set the document properties such as Title, Author etc. These are written to
1055 # property sets in the OLE container.
1056 #
1057 14 ▶sub set_properties {
Subroutine set_properties: called CC=12 SCAR=25.0
Pod undocumented
1058  
1059 14 my $self = shift;
1060 14 my %param;
1061  
1062 # Ignore if no args were passed.
1063 14 ▶ return -1 unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 14 14
1064  
1065  
1066 # Allow the parameters to be passed as a hash or hash ref.
1067 14 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 1 13 14
1068 1 %param = %{$_[0]};
1069 }
1070 else {
1071 13 %param = @_;
1072 }
1073  
1074  
1075 # List of valid input parameters.
1076 14 my %properties = (
1077 codepage => [0x0001, 'VT_I2' ],
1078 title => [0x0002, 'VT_LPSTR' ],
1079 subject => [0x0003, 'VT_LPSTR' ],
1080 author => [0x0004, 'VT_LPSTR' ],
1081 keywords => [0x0005, 'VT_LPSTR' ],
1082 comments => [0x0006, 'VT_LPSTR' ],
1083 last_author => [0x0008, 'VT_LPSTR' ],
1084 created => [0x000C, 'VT_FILETIME'],
1085 category => [0x0002, 'VT_LPSTR' ],
1086 manager => [0x000E, 'VT_LPSTR' ],
1087 company => [0x000F, 'VT_LPSTR' ],
1088 utf8 => 1,
1089 );
1090  
1091 # Check for valid input parameters.
1092 14 for my $parameter (keys %param) {
1093 60 ▶ if (not exists $properties{$parameter}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $properties{$parameter}) 0 60 60
1094 0 carp "Unknown parameter '$parameter' in set_properties()";
1095 0 return -1;
1096 }
1097 }
1098  
1099  
1100 # Set the creation time unless specified by the user.
1101 14 ▶ if (!exists $param{created}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param{"created"}) 1 13 14
1102 1 $param{created} = $self->{_localtime};
1103 }
1104  
1105  
1106 #
1107 # Create the SummaryInformation property set.
1108 #
1109  
1110 # Get the codepage of the strings in the property set.
1111 14 my @strings = qw(title subject author keywords comments last_author);
1112 14 $param{codepage} = $self->_get_property_set_codepage(\%param,
1113 \@strings);
1114  
1115 # Create an array of property set values.
1116 14 my @property_sets;
1117  
1118 14 for my $property (qw(codepage title subject author
1119 keywords comments last_author created))
1120 {
1121 112 ▶ if (exists $param{$property} && defined $param{$property}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{$property} and defined $param{$property}) 63 49 112
Condition100%3/3
exists $param{$property} and defined $param{$property}
!ll&&!rl&&r
381163
MC/DC100%2/2
exists $param{$property} and defined $param{$property}
exists $param{$property} defined $param{$property}
Truth table100%3/3
A and B
A= exists $param{$property}B= defined $param{$property}
ABresult
0X0
100
111
1122 push @property_sets, [
1123 $properties{$property}->[0],
1124 $properties{$property}->[1],
1125 63 $param{$property}
1126 ];
1127 }
1128 }
1129  
1130 # Pack the property sets.
1131 14 $self->{summary} = create_summary_property_set(\@property_sets);
1132  
1133  
1134 #
1135 # Create the DocSummaryInformation property set.
1136 #
1137  
1138 # Get the codepage of the strings in the property set.
1139 14 @strings = qw(category manager company);
1140 14 $param{codepage} = $self->_get_property_set_codepage(\%param,
1141 \@strings);
1142  
1143 # Create an array of property set values.
1144 14 @property_sets = ();
1145  
1146 14 for my $property (qw(codepage category manager company))
1147 {
1148 56 ▶ if (exists $param{$property} && defined $param{$property}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{$property} and defined $param{$property}) 14 42 56
Condition66%2/3
exists $param{$property} and defined $param{$property}
!ll&&!rl&&r
42014
MC/DC50%1/2
exists $param{$property} and defined $param{$property}
exists $param{$property} defined $param{$property}
Truth table66%2/3
A and B
A= exists $param{$property}B= defined $param{$property}
ABresult
0X0
100
111
1149 push @property_sets, [
1150 $properties{$property}->[0],
1151 $properties{$property}->[1],
1152 14 $param{$property}
1153 ];
1154 }
1155 }
1156  
1157 # Pack the property sets.
1158 14 $self->{doc_summary} = create_doc_summary_property_set(\@property_sets);
1159  
1160 # Set a flag for when the files is written.
1161 14 $self->{_add_doc_properties} = 1;
1162 }
1163  
1164  
1165 ###############################################################################
1166 #
1167 # _get_property_set_codepage()
1168 #
1169 # Get the character codepage used by the strings in a property set. If one of
1170 # the strings used is utf8 then the codepage is marked as utf8. Otherwise
1171 # Latin 1 is used (although in our case this is limited to 7bit ASCII).
1172 #
1173 31 ▶sub _get_property_set_codepage {
Subroutine _get_property_set_codepage: called CC=6 SCAR=17.9
1174  
1175 31 my $self = shift;
1176  
1177 31 my $params = $_[0];
1178 31 my $strings = $_[1];
1179  
1180 # Allow for manually marked utf8 strings.
1181 31 ▶ return 0xFDE9 if defined $params->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"utf8"} 3 28 31
1182  
1183 # Check for utf8 strings in perl 5.8.
1184 28 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 28 0 28
1185 28 require Encode;
1186 28 for my $string (@{$strings }) {
1187 110 ▶ next unless exists $params->{$string};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$string} 60 50 110
1188 50 ▶ return 0xFDE9 if Encode::is_utf8($params->{$string});
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($params->{$string}) 4 46 50
1189 }
1190 }
1191  
1192 24 return 0x04E4; # Default codepage, Latin 1.
1193 }
1194  
1195  
1196 ###############################################################################
1197 #
1198 # _store_workbook()
1199 #
1200 # Assemble worksheets into a workbook and send the BIFF data to an OLE
1201 # storage.
1202 #
1203 61 ▶sub _store_workbook {
Subroutine _store_workbook: called CC=8 SCAR=20.8
1204  
1205 61 my $self = shift;
1206  
1207 # Add a default worksheet if non have been added.
1208 61 ▶ $self->add_worksheet() if not @{$self->{_worksheets}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"_worksheets"};} 3 58 61
1209  
1210 # Calculate size required for MSO records and update worksheets.
1211 61 $self->_calc_mso_sizes();
1212  
1213 # Ensure that at least one worksheet has been selected.
1214 61 ▶ if ($self->{_activesheet} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_activesheet"} == 0) 61 0 61
1215 61 @{$self->{_worksheets}}[0]->{_selected} = 1;
1216 61 @{$self->{_worksheets}}[0]->{_hidden} = 0;
1217 }
1218  
1219 # Calculate the number of selected sheet tabs and set the active sheet.
1220 61 foreach my $sheet (@{$self->{_worksheets}}) {
1221 135 ▶ $self->{_selected}++ if $sheet->{_selected};
Branch: 1 branch
BranchTrueFalseTotal
if $sheet->{"_selected"} 61 74 135
1222 135 ▶ $sheet->{_active} = 1 if $sheet->{_index} == $self->{_activesheet};
Branch: 1 branch
BranchTrueFalseTotal
if $sheet->{"_index"} == $self->{"_activesheet"} 61 74 135
1223 }
1224  
1225 # Add Workbook globals
1226 61 $self->_store_bof(0x0005);
1227 61 $self->_store_codepage();
1228 61 $self->_store_window1();
1229 61 $self->_store_hideobj();
1230 61 $self->_store_1904();
1231 61 $self->_store_all_fonts();
1232 61 $self->_store_all_num_formats();
1233 61 $self->_store_all_xfs();
1234 61 $self->_store_all_styles();
1235 61 $self->_store_palette();
1236  
1237 # Calculate the offsets required by the BOUNDSHEET records
1238 61 $self->_calc_sheet_offsets();
1239  
1240 # Add BOUNDSHEET records.
1241 61 foreach my $sheet (@{$self->{_worksheets}}) {
1242 $self->_store_boundsheet($sheet->{_name},
1243 $sheet->{_offset},
1244 $sheet->{_sheet_type},
1245 $sheet->{_hidden},
1246 135 $sheet->{_encoding});
1247 }
1248  
1249 # NOTE: If any records are added between here and EOF the
1250 # _calc_sheet_offsets() should be updated to include the new length.
1251 61 $self->_store_country();
1252 61 ▶ if ($self->{_ext_ref_count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ext_ref_count"}) 17 44 61
1253 17 $self->_store_supbook();
1254 17 $self->_store_externsheet();
1255 17 $self->_store_names();
1256 }
1257 61 $self->_add_mso_drawing_group();
1258 61 $self->_store_shared_strings();
1259 61 $self->_store_extsst();
1260  
1261 # End Workbook globals
1262 61 $self->_store_eof();
1263  
1264 # Store the workbook in an OLE container
1265 61 return $self->_store_OLE_file();
1266 }
1267  
1268  
1269 ###############################################################################
1270 #
1271 # _store_OLE_file()
1272 #
1273 # Store the workbook in an OLE container using the default handler or using
1274 # OLE::Storage_Lite if the workbook data is > ~ 7MB.
1275 #
1276 61 ▶sub _store_OLE_file {
Subroutine _store_OLE_file: called CC=12 SCAR=24.9
1277  
1278 61 my $self = shift;
1279 61 my $maxsize = 7_087_104;
1280  
1281 61 ▶ if (!$self->{_add_doc_properties} && $self->{_biffsize} <= $maxsize) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'_add_doc_properties'} and $self->{'_biffsize'} <= $maxsize) { } 60 1 61
Condition66%2/3
not $self->{"_add_doc_properties"} and $self->{"_biffsize"} <= $maxsize
!ll&&!rl&&r
1060
MC/DC50%1/2
not $self->{"_add_doc_properties"} and $self->{"_biffsize"} <= $maxsize
not $self->{"_add_doc_properties"} $self->{"_biffsize"} <= $maxsize
Truth table66%2/3
not(A) and B
A= not $self->{"_add_doc_properties"}B= $self->{"_biffsize"} <= $maxsize
ABresult
0X0
100
111
1282 # Write the OLE file using OLEwriter if data <= 7MB
1283 60 my $OLE = Spreadsheet::WriteExcel::OLEwriter->new($self->{_fh_out});
1284  
1285 # Write the BIFF data without the OLE container for testing.
1286 60 $OLE->{_biff_only} = $self->{_biff_only};
1287  
1288 # Indicate that we created the filehandle and want to close it.
1289 60 $OLE->{_internal_fh} = $self->{_internal_fh};
1290  
1291 60 $OLE->set_size($self->{_biffsize});
1292 60 $OLE->write_header();
1293  
1294 60 ▶ while (my $tmp = $self->get_data()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tmp = $self->get_data) 103 60 163
1295 103 $OLE->write($tmp);
1296 }
1297  
1298 60 foreach my $worksheet (@{$self->{_worksheets}}) {
1299 134 ▶ while (my $tmp = $worksheet->get_data()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tmp = $worksheet->get_data) 265 134 399
1300 265 $OLE->write($tmp);
1301 }
1302 }
1303  
1304 60 return $OLE->close();
1305 }
1306 else {
1307 # Write the OLE file using OLE::Storage_Lite if data > 7MB
1308 1 eval { require OLE::Storage_Lite };
1309  
1310 1 ▶ if (not $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 1 0 1
1311  
1312 # Protect print() from -l on the command line.
1313 1 local $\ = undef;
1314  
1315 1 my @streams;
1316  
1317 # Create the Workbook stream.
1318 1 my $stream = pack 'v*', unpack 'C*', 'Workbook';
1319 1 my $workbook = OLE::Storage_Lite::PPS::File->newFile($stream);
1320  
1321 1 ▶ while (my $tmp = $self->get_data()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tmp = $self->get_data) 2 1 3
1322 2 $workbook->append($tmp);
1323 }
1324  
1325 1 foreach my $worksheet (@{$self->{_worksheets}}) {
1326 1 ▶ while (my $tmp = $worksheet->get_data()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tmp = $worksheet->get_data) 2 1 3
1327 2 $workbook->append($tmp);
1328 }
1329 }
1330  
1331 1 push @streams, $workbook;
1332  
1333  
1334 # Create the properties streams, if any.
1335 1 ▶ if ($self->{_add_doc_properties}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_add_doc_properties"}) 1 0 1
1336 1 my $stream;
1337 my $summary;
1338  
1339 1 $stream = pack 'v*', unpack 'C*', "\5SummaryInformation";
1340 1 $summary = $self->{summary};
1341 1 $summary = OLE::Storage_Lite::PPS::File->new($stream, $summary);
1342 1 push @streams, $summary;
1343  
1344 1 $stream = pack 'v*', unpack 'C*', "\5DocumentSummaryInformation";
1345 1 $summary = $self->{doc_summary};
1346 1 $summary = OLE::Storage_Lite::PPS::File->new($stream, $summary);
1347 1 push @streams, $summary;
1348 }
1349  
1350 # Create the OLE root document and add the substreams.
1351 1 my @localtime = @{ $self->{_localtime} };
1352 1 splice(@localtime, 6);
1353  
1354 1 my $ole_root = OLE::Storage_Lite::PPS::Root->new(\@localtime,
1355 \@localtime,
1356 \@streams);
1357 1 $ole_root->save($self->{_filename});
1358  
1359  
1360 # Close the filehandle if it was created internally.
1361 1 ▶ return CORE::close($self->{_fh_out}) if $self->{_internal_fh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_internal_fh"} 1 0 1
1362 }
1363 else {
1364 # File in greater than limit, set $! to "File too large"
1365 0 $! = 27; # Perl error code "File too large"
1366  
1367 0 croak "Maximum Spreadsheet::WriteExcel filesize, $maxsize bytes, ".
1368 "exceeded. To create files bigger than this limit please " .
1369 "install OLE::Storage_Lite\n";
1370  
1371 # return 0;
1372 }
1373 }
1374 }
1375  
1376  
1377 ###############################################################################
1378 #
1379 # _calc_sheet_offsets()
1380 #
1381 # Calculate Worksheet BOF offsets records for use in the BOUNDSHEET records.
1382 #
1383 61 ▶sub _calc_sheet_offsets {
Subroutine _calc_sheet_offsets: called CC=3 SCAR=11.0
1384  
1385 61 my $self = shift;
1386 61 my $BOF = 12;
1387 61 my $EOF = 4;
1388 61 my $offset = $self->{_datasize};
1389  
1390 # Add the length of the COUNTRY record
1391 61 $offset += 8;
1392  
1393 # Add the length of the SST and associated CONTINUEs
1394 61 $offset += $self->_calculate_shared_string_sizes();
1395  
1396 # Add the length of the EXTSST record.
1397 61 $offset += $self->_calculate_extsst_size();
1398  
1399 # Add the length of the SUPBOOK, EXTERNSHEET and NAME records
1400 61 $offset += $self->_calculate_extern_sizes();
1401  
1402 # Add the length of the MSODRAWINGGROUP records including an extra 4 bytes
1403 # for any CONTINUE headers. See _add_mso_drawing_group_continue().
1404 61 my $mso_size = $self->{_mso_size};
1405 61 $mso_size += 4 * int(($mso_size -1) / $self->{_limit});
1406 61 $offset += $mso_size ;
1407  
1408 61 foreach my $sheet (@{$self->{_worksheets}}) {
1409 135 $offset += $BOF + length($sheet->{_name});
1410 }
1411  
1412 61 $offset += $EOF;
1413  
1414 61 foreach my $sheet (@{$self->{_worksheets}}) {
1415 135 $sheet->{_offset} = $offset;
1416 135 $sheet->_close();
1417 135 $offset += $sheet->{_datasize};
1418 }
1419  
1420 61 $self->{_biffsize} = $offset;
1421 }
1422  
1423  
1424 ###############################################################################
1425 #
1426 # _calc_mso_sizes()
1427 #
1428 # Calculate the MSODRAWINGGROUP sizes and the indexes of the Worksheet
1429 # MSODRAWING records.
1430 #
1431 # In the following SPID is shape id, according to Escher nomenclature.
1432 #
1433 78 ▶sub _calc_mso_sizes {
Subroutine _calc_mso_sizes: called CC=10 SCAR=23.0
1434  
1435 78 my $self = shift;
1436  
1437 78 my $mso_size = 0; # Size of the MSODRAWINGGROUP record
1438 78 my $start_spid = 1024; # Initial spid for each sheet
1439 78 my $max_spid = 1024; # spidMax
1440 78 my $num_clusters = 1; # cidcl
1441 78 my $shapes_saved = 0; # cspSaved
1442 78 my $drawings_saved = 0; # cdgSaved
1443 78 my @clusters = ();
1444  
1445  
1446 78 $self->_process_images();
1447  
1448 # Add Bstore container size if there are images.
1449 78 ▶ $mso_size += 8 if @{$self->{_images_data}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"_images_data"};} 0 78 78
1450  
1451  
1452 # Iterate through the worksheets, calculate the MSODRAWINGGROUP parameters
1453 # and space required to store the record and the MSODRAWING parameters
1454 # required by each worksheet.
1455 #
1456 78 foreach my $sheet (@{$self->{_worksheets}}) {
1457 168 ▶ next unless $sheet->{_sheet_type} == 0x0000; # Ignore charts.
Branch: 1 branch
BranchTrueFalseTotal
unless $sheet->{"_sheet_type"} == 0 2 166 168
1458  
1459 166 ▶ my $num_images = $sheet->{_num_images} || 0;
Condition50%1/2
$sheet->{'_num_images'} || 0
l!l
0166
MC/DC0%0/1
$sheet->{'_num_images'} || 0
$sheet->{'_num_images'}
Truth table50%1/2
A
A= $sheet->{'_num_images'}
Aresult
11
00
1460 166 ▶ my $image_mso_size = $sheet->{_image_mso_size} || 0;
Condition50%1/2
$sheet->{'_image_mso_size'} || 0
l!l
0166
MC/DC0%0/1
$sheet->{'_image_mso_size'} || 0
$sheet->{'_image_mso_size'}
Truth table50%1/2
A
A= $sheet->{'_image_mso_size'}
Aresult
11
00
1461 166 my $num_comments = $sheet->_prepare_comments();
1462 166 my $num_charts = $sheet->_prepare_charts();
1463 166 my $num_filters = $sheet->{_filter_count};
1464  
1465 166 ▶ next unless $num_images + $num_comments + $num_charts +$num_filters;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_images + $num_comments + $num_charts + $num_filters 129 37 166
1466  
1467  
1468 # Include 1 parent MSODRAWING shape, per sheet, in the shape count.
1469 37 my $num_shapes = 1 + $num_images
1470 + $num_comments
1471 + $num_charts
1472 + $num_filters;
1473 37 $shapes_saved += $num_shapes;
1474 37 $mso_size += $image_mso_size;
1475  
1476  
1477 # Add a drawing object for each sheet with comments.
1478 37 $drawings_saved++;
1479  
1480  
1481 # For each sheet start the spids at the next 1024 interval.
1482 37 $max_spid = 1024 * (1 + int(($max_spid -1)/1024));
1483 37 $start_spid = $max_spid;
1484  
1485  
1486 # Max spid for each sheet and eventually for the workbook.
1487 37 $max_spid += $num_shapes;
1488  
1489  
1490 # Store the cluster ids
1491 37 ▶ for (my $i = $num_shapes; $i > 0; $i -= 1024) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 49 37 86
1492 49 $num_clusters += 1;
1493 49 $mso_size += 8;
1494 49 ▶ my $size = $i > 1024 ? 1024 : $i;
Branch: 1 branch
BranchTrueFalseTotal
$i > 1024 ? : 12 37 49
1495  
1496 49 push @clusters, [$drawings_saved, $size];
1497 }
1498  
1499  
1500 # Pass calculated values back to the worksheet
1501 37 $sheet->{_object_ids} = [$start_spid, $drawings_saved,
1502 $num_shapes, $max_spid -1];
1503 }
1504  
1505  
1506 # Calculate the MSODRAWINGGROUP size if we have stored some shapes.
1507 78 ▶ $mso_size += 86 if $mso_size; # Smallest size is 86+8=94
Branch: 1 branch
BranchTrueFalseTotal
if $mso_size 20 58 78
1508  
1509  
1510 78 $self->{_mso_size} = $mso_size;
1511 $self->{_mso_clusters} = [
1512 78 $max_spid, $num_clusters, $shapes_saved,
1513 $drawings_saved, [@clusters]
1514 ];
1515 }
1516  
1517  
1518  
1519 ###############################################################################
1520 #
1521 # _process_images()
1522 #
1523 # We need to process each image in each worksheet and extract information.
1524 # Some of this information is stored and used in the Workbook and some is
1525 # passed back into each Worksheet. The overall size for the image related
1526 # BIFF structures in the Workbook is calculated here.
1527 #
1528 # MSO size = 8 bytes for bstore_container +
1529 # 44 bytes for blip_store_entry +
1530 # 25 bytes for blip
1531 # = 77 + image size.
1532 #
1533 78 ▶sub _process_images {
Subroutine _process_images: called CC=12 SCAR=44.6
1534  
1535 78 my $self = shift;
1536  
1537 78 my %images_seen;
1538 my @image_data;
1539 78 my @previous_images;
1540 78 my $image_id = 1;
1541 78 my $images_size = 0;
1542  
1543  
1544 78 foreach my $sheet (@{$self->{_worksheets}}) {
1545 168 ▶ next unless $sheet->{_sheet_type} == 0x0000; # Ignore charts.
Branch: 1 branch
BranchTrueFalseTotal
unless $sheet->{"_sheet_type"} == 0 2 166 168
1546 166 ▶ next unless $sheet->_prepare_images();
Branch: 1 branch
BranchTrueFalseTotal
unless $sheet->_prepare_images 166 0 166
1547  
1548 0 my $num_images = 0;
1549 0 my $image_mso_size = 0;
1550  
1551  
1552 0 for my $image_ref (@{$sheet->{_images_array}}) {
1553 0 my $filename = $image_ref->[2];
1554 0 $num_images++;
1555  
1556 #
1557 # For each Worksheet image we get a structure like this
1558 # [
1559 # $row,
1560 # $col,
1561 # $name,
1562 # $x_offset,
1563 # $y_offset,
1564 # $scale_x,
1565 # $scale_y,
1566 # ]
1567 #
1568 # And we add additional information:
1569 #
1570 # $image_id,
1571 # $type,
1572 # $width,
1573 # $height;
1574  
1575 0 ▶ if (not exists $images_seen{$filename}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $images_seen{$filename}) { } 0 0 0
1576 # TODO should also match seen images based on checksum.
1577  
1578 # Open the image file and import the data.
1579 0 my $fh = FileHandle->new($filename);
1580 0 ▶ croak "Couldn't import $filename: $!" unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 0 0
1581 0 binmode $fh;
1582  
1583 # Slurp the file into a string and do some size calcs.
1584 0 my $data = do {local $/; <$fh>};
1585 0 my $size = length $data;
1586 0 my $checksum1 = $self->_image_checksum($data, $image_id);
1587 0 my $checksum2 = $checksum1;
1588 0 my $ref_count = 1;
1589  
1590  
1591 # Process the image and extract dimensions.
1592 0 my ($type, $width, $height);
1593  
1594 # Test for PNGs...
1595 0 ▶ if (unpack('x A3', $data) eq 'PNG') {
Branch: 3 branches
BranchTrueFalseTotal
if (unpack('x A3', $data) eq 'PNG') { } 0 0 0
elsif (unpack('n', $data) == 65496 and unpack('x6 A4', $data) eq 'JFIF' || unpack('x6 A4', $data) eq 'Exif') { } 0 0 0
elsif (unpack('A2', $data) eq 'BM') { } 0 0 0
1596 0 ($type, $width, $height) = $self->_process_png($data);
1597 }
1598 # Test for JFIF and Exif JPEGs...
1599 ▶ elsif ( (unpack('n', $data) == 0xFFD8) &&
Condition0%0/6
unpack("n", $data) == 65496 and unpack("x6 A4", $data) eq "JFIF" || unpack("x6 A4", $data) eq "Exif"
!ll&&!rl&&r
000
unpack("x6 A4", $data) eq "JFIF" or unpack("x6 A4", $data) eq "Exif"
l!l&&r!l&&!r
000
MC/DC0%0/3
unpack("n", $data) == 65496 and unpack("x6 A4", $data) eq "JFIF" || unpack("x6 A4", $data) eq "Exif"
unpack("n", $data) == 65496 unpack("x6 A4", $data) eq "JFIF" unpack("x6 A4", $data) eq "Exif"
Truth table0%0/4
A and B or C
A= unpack("n", $data) == 65496B= unpack("x6 A4", $data) eq "JFIF"C= unpack("x6 A4", $data) eq "Exif"
ABCresult
0XX0
1000
11X1
1011
1600 ( (unpack('x6 A4', $data) eq 'JFIF') ||
1601 (unpack('x6 A4', $data) eq 'Exif')
1602 )
1603 )
1604 {
1605 0 ($type, $width, $height) = $self->_process_jpg($data, $filename);
1606 }
1607 # Test for BMPs...
1608 elsif (unpack('A2', $data) eq 'BM') {
1609 0 ($type, $width, $height) = $self->_process_bmp($data,
1610 $filename);
1611 # The 14 byte header of the BMP is stripped off.
1612 0 $data = substr $data, 14;
1613  
1614 # A checksum of the new image data is also required.
1615 0 $checksum2 = $self->_image_checksum($data,
1616 $image_id,
1617 $image_id
1618 );
1619  
1620 # Adjust size -14 (header) + 16 (extra checksum).
1621 0 $size += 2;
1622 }
1623 else {
1624 0 croak "Unsupported image format for file: $filename\n";
1625 }
1626  
1627  
1628 # Push the new data back into the Worksheet array;
1629 0 push @$image_ref, $image_id, $type, $width, $height;
1630  
1631 # Also store new data for use in duplicate images.
1632 0 push @previous_images, [$image_id, $type, $width, $height];
1633  
1634  
1635 # Store information required by the Workbook.
1636 0 push @image_data, [$ref_count, $type, $data, $size,
1637 $checksum1, $checksum2];
1638  
1639 # Keep track of overall data size.
1640 0 $images_size += $size +61; # Size for bstore container.
1641 0 $image_mso_size += $size +69; # Size for dgg container.
1642  
1643 0 $images_seen{$filename} = $image_id++;
1644 0 $fh->close;
1645 }
1646 else {
1647 # We've processed this file already.
1648 0 my $index = $images_seen{$filename} -1;
1649  
1650 # Increase image reference count.
1651 0 $image_data[$index]->[0]++;
1652  
1653 # Add previously calculated data back onto the Worksheet array.
1654 # $image_id, $type, $width, $height
1655 0 my $a_ref = $sheet->{_images_array}->[$index];
1656 0 push @$image_ref, @{$previous_images[$index]};
1657 }
1658 }
1659  
1660 # Store information required by the Worksheet.
1661 0 $sheet->{_num_images} = $num_images;
1662 0 $sheet->{_image_mso_size} = $image_mso_size;
1663  
1664 }
1665  
1666  
1667 # Store information required by the Workbook.
1668 78 $self->{_images_size} = $images_size;
1669 78 $self->{_images_data} = \@image_data; # Store the data for MSODRAWINGGROUP.
1670  
1671 }
1672  
1673  
1674 ###############################################################################
1675 #
1676 # _image_checksum()
1677 #
1678 # Generate a checksum for the image using whichever module is available..The
1679 # available modules are checked in _get_checksum_method(). Excel uses an MD4
1680 # checksum but any other will do. In the event of no checksum module being
1681 # available we simulate a checksum using the image index.
1682 #
1683 0 ▶sub _image_checksum {
Subroutine _image_checksum: not called CC=5 SCAR=34.0
1684  
1685 0 my $self = shift;
1686  
1687 0 my $data = $_[0];
1688 0 my $index1 = $_[1];
1689 0 ▶ my $index2 = $_[2] || 0;
Condition0%0/2
$_[2] || 0
l!l
00
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
1690  
1691 0 ▶ if ($self->{_checksum_method} == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'_checksum_method'} == 1) { } 0 0 0
elsif ($self->{'_checksum_method'} == 2) { } 0 0 0
elsif ($self->{'_checksum_method'} == 3) { } 0 0 0
1692 # Digest::MD4
1693 0 return Digest::MD4::md4_hex($data);
1694 }
1695 elsif ($self->{_checksum_method} == 2) {
1696 # Digest::Perl::MD4
1697 0 return Digest::Perl::MD4::md4_hex($data);
1698 }
1699 elsif ($self->{_checksum_method} == 3) {
1700 # Digest::MD5
1701 0 return Digest::MD5::md5_hex($data);
1702 }
1703 else {
1704 # Default
1705 0 return sprintf '%016X%016X', $index2, $index1;
1706 }
1707 }
1708  
1709  
1710 ###############################################################################
1711 #
1712 # _process_png()
1713 #
1714 # Extract width and height information from a PNG file.
1715 #
1716 0 ▶sub _process_png {
Subroutine _process_png: not called CC=1 SCAR=6.9
1717  
1718 0 my $self = shift;
1719  
1720 0 my $type = 6; # Excel Blip type (MSOBLIPTYPE).
1721 0 my $width = unpack "N", substr $_[0], 16, 4;
1722 0 my $height = unpack "N", substr $_[0], 20, 4;
1723  
1724 0 return ($type, $width, $height);
1725 }
1726  
1727  
1728 ###############################################################################
1729 #
1730 # _process_bmp()
1731 #
1732 # Extract width and height information from a BMP file.
1733 #
1734 # Most of these checks came from the old Worksheet::_process_bitmap() method.
1735 #
1736 0 ▶sub _process_bmp {
Subroutine _process_bmp: not called CC=7 SCAR=40.3
1737  
1738 0 my $self = shift;
1739 0 my $data = $_[0];
1740 0 my $filename = $_[1];
1741 0 my $type = 7; # Excel Blip type (MSOBLIPTYPE).
1742  
1743  
1744 # Check that the file is big enough to be a bitmap.
1745 0 ▶ if (length $data <= 0x36) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data <= 54) 0 0 0
1746 0 croak "$filename doesn't contain enough data.";
1747 }
1748  
1749  
1750 # Read the bitmap width and height. Verify the sizes.
1751 0 my ($width, $height) = unpack "x18 V2", $data;
1752  
1753 0 ▶ if ($width > 0xFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width > 65535) 0 0 0
1754 0 croak "$filename: largest image width $width supported is 65k.";
1755 }
1756  
1757 0 ▶ if ($height > 0xFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($height > 65535) 0 0 0
1758 0 croak "$filename: largest image height supported is 65k.";
1759 }
1760  
1761 # Read the bitmap planes and bpp data. Verify them.
1762 0 my ($planes, $bitcount) = unpack "x26 v2", $data;
1763  
1764 0 ▶ if ($bitcount != 24) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitcount != 24) 0 0 0
1765 0 croak "$filename isn't a 24bit true color bitmap.";
1766 }
1767  
1768 0 ▶ if ($planes != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($planes != 1) 0 0 0
1769 0 croak "$filename: only 1 plane supported in bitmap image.";
1770 }
1771  
1772  
1773 # Read the bitmap compression. Verify compression.
1774 0 my $compression = unpack "x30 V", $data;
1775  
1776 0 ▶ if ($compression != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compression != 0) 0 0 0
1777 0 croak "$filename: compression not supported in bitmap image.";
1778 }
1779  
1780 0 return ($type, $width, $height);
1781 }
1782  
1783  
1784 ###############################################################################
1785 #
1786 # _process_jpg()
1787 #
1788 # Extract width and height information from a JPEG file.
1789 #
1790 5 ▶sub _process_jpg {
Subroutine _process_jpg: called CC=6 SCAR=17.9
1791  
1792 5 my $self = shift;
1793 5 my $data = $_[0];
1794 5 my $filename = $_[1];
1795 5 my $type = 5; # Excel Blip type (MSOBLIPTYPE).
1796 5 my $width;
1797 my $height;
1798  
1799 5 my $offset = 2;
1800 5 my $data_length = length $data;
1801  
1802 # Search through the image data to find the 0xFFC0 marker. The height and
1803 # width are contained in the data for that sub element.
1804 5 ▶ while ($offset < $data_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $data_length) 24 1 25
1805  
1806 24 my $marker = unpack "n", substr $data, $offset, 2;
1807 24 my $length = unpack "n", substr $data, $offset +2, 2;
1808  
1809 24 ▶ if ($marker == 0xFFC0 || $marker == 0xFFC2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($marker == 65472 or $marker == 65474) 3 21 24
Condition100%3/3
$marker == 65472 or $marker == 65474
l!l&&r!l&&!r
2121
MC/DC100%2/2
$marker == 65472 or $marker == 65474
$marker == 65472 $marker == 65474
Truth table100%3/3
A or B
A= $marker == 65472B= $marker == 65474
ABresult
1X1
011
000
1810 3 $height = unpack "n", substr $data, $offset +5, 2;
1811 3 $width = unpack "n", substr $data, $offset +7, 2;
1812 3 last;
1813 }
1814  
1815 21 $offset = $offset + $length + 2;
1816 21 ▶ last if $marker == 0xFFDA;
Branch: 1 branch
BranchTrueFalseTotal
if $marker == 65498 1 20 21
1817 }
1818  
1819 5 ▶ if (not defined $height) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $height) 2 3 5
1820 2 croak "$filename: no size data found in jpeg image.\n";
1821 }
1822  
1823 3 return ($type, $width, $height);
1824 }
1825  
1826  
1827 ###############################################################################
1828 #
1829 # _store_all_fonts()
1830 #
1831 # Store the Excel FONT records.
1832 #
1833 61 ▶sub _store_all_fonts {
Subroutine _store_all_fonts: called CC=6 SCAR=17.9
1834  
1835 61 my $self = shift;
1836  
1837 61 my $format = $self->{_formats}->[15]; # The default cell format.
1838 61 my $font = $format->get_font();
1839  
1840 # Fonts are 0-indexed. According to the SDK there is no index 4,
1841 61 for (0..3) {
1842 244 $self->_append($font);
1843 }
1844  
1845  
1846 # Add the default fonts for charts and comments. This aren't connected
1847 # to XF formats. Note, the font size, and some other properties of
1848 # chart fonts are set in the FBI record of the chart.
1849 61 my $tmp_format;
1850  
1851 # Index 5. Axis numbers.
1852 61 $tmp_format = Spreadsheet::WriteExcel::Format->new(
1853 undef,
1854 font_only => 1,
1855 );
1856 61 $self->_append( $tmp_format->get_font() );
1857  
1858 # Index 6. Series names.
1859 61 $tmp_format = Spreadsheet::WriteExcel::Format->new(
1860 undef,
1861 font_only => 1,
1862 );
1863 61 $self->_append( $tmp_format->get_font() );
1864  
1865 # Index 7. Title.
1866 61 $tmp_format = Spreadsheet::WriteExcel::Format->new(
1867 undef,
1868 font_only => 1,
1869 bold => 1,
1870 );
1871 61 $self->_append( $tmp_format->get_font() );
1872  
1873 # Index 8. Axes.
1874 61 $tmp_format = Spreadsheet::WriteExcel::Format->new(
1875 undef,
1876 font_only => 1,
1877 bold => 1,
1878 );
1879 61 $self->_append( $tmp_format->get_font() );
1880  
1881 # Index 9. Comments.
1882 61 $tmp_format = Spreadsheet::WriteExcel::Format->new(
1883 undef,
1884 font_only => 1,
1885 font => 'Tahoma',
1886 size => 8,
1887 );
1888 61 $self->_append( $tmp_format->get_font() );
1889  
1890  
1891 # Iterate through the XF objects and write a FONT record if it isn't the
1892 # same as the default FONT and if it hasn't already been used.
1893 #
1894 61 my %fonts;
1895 my $key;
1896 61 my $index = 10; # The first user defined FONT
1897  
1898 61 $key = $format->get_font_key(); # The default font for cell formats.
1899 61 $fonts{$key} = 0; # Index of the default font
1900  
1901 # Fonts that are marked as '_font_only' are always stored. These are used
1902 # mainly for charts and may not have an associated XF record.
1903  
1904 61 foreach $format (@{$self->{_formats}}) {
1905 1346 $key = $format->get_font_key();
1906  
1907 1346 ▶ if (not $format->{_font_only} and exists $fonts{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $format->{'_font_only'} and exists $fonts{$key}) { } 1284 62 1346
Condition66%2/3
not $format->{"_font_only"} and exists $fonts{$key}
!ll&&!rl&&r
0621284
MC/DC50%1/2
not $format->{"_font_only"} and exists $fonts{$key}
not $format->{"_font_only"} exists $fonts{$key}
Truth table66%2/3
not(A) and B
A= not $format->{"_font_only"}B= exists $fonts{$key}
ABresult
0X0
100
111
1908 # FONT has already been used
1909 1284 $format->{_font_index} = $fonts{$key};
1910 }
1911 else {
1912 # Add a new FONT record
1913  
1914 62 ▶ if (not $format->{_font_only}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format->{"_font_only"}) 62 0 62
1915 62 $fonts{$key} = $index;
1916 }
1917  
1918 62 $format->{_font_index} = $index;
1919 62 $index++;
1920 62 $font = $format->get_font();
1921 62 $self->_append($font);
1922 }
1923 }
1924 }
1925  
1926  
1927 ###############################################################################
1928 #
1929 # _store_all_num_formats()
1930 #
1931 # Store user defined numerical formats i.e. FORMAT records
1932 #
1933 61 ▶sub _store_all_num_formats {
Subroutine _store_all_num_formats: called CC=5 SCAR=19.9
1934  
1935 61 my $self = shift;
1936  
1937 61 my %num_formats;
1938 my @num_formats;
1939 61 my $num_format;
1940 61 my $index = 164; # User defined FORMAT records start from 0xA4
1941  
1942  
1943 # Iterate through the XF objects and write a FORMAT record if it isn't a
1944 # built-in format type and if the FORMAT string hasn't already been used.
1945 #
1946 61 foreach my $format (@{$self->{_formats}}) {
1947 1346 my $num_format = $format->{_num_format};
1948 1346 my $encoding = $format->{_num_format_enc};
1949  
1950 # Check if $num_format is an index to a built-in format.
1951 # Also check for a string of zeros, which is a valid format string
1952 # but would evaluate to zero.
1953 #
1954 1346 ▶ if ($num_format !~ m/^0+\d/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num_format =~ /^0+\d/) 1346 0 1346
1955 1346 ▶ next if $num_format =~ m/^\d+$/; # built-in
Branch: 1 branch
BranchTrueFalseTotal
if $num_format =~ /^\d+$/ 1346 0 1346
1956 }
1957  
1958 0 ▶ if (exists($num_formats{$num_format})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $num_formats{$num_format}) { } 0 0 0
1959 # FORMAT has already been used
1960 0 $format->{_num_format} = $num_formats{$num_format};
1961 }
1962 else{
1963 # Add a new FORMAT
1964 0 $num_formats{$num_format} = $index;
1965 0 $format->{_num_format} = $index;
1966 0 $self->_store_num_format($num_format, $index, $encoding);
1967 0 $index++;
1968 }
1969 }
1970 }
1971  
1972  
1973 ###############################################################################
1974 #
1975 # _store_all_xfs()
1976 #
1977 # Write all XF records.
1978 #
1979 61 ▶sub _store_all_xfs {
Subroutine _store_all_xfs: called CC=2 SCAR=6.9
1980  
1981 61 my $self = shift;
1982  
1983 61 foreach my $format (@{$self->{_formats}}) {
1984 1346 my $xf = $format->get_xf();
1985 1346 $self->_append($xf);
1986 }
1987 }
1988  
1989  
1990 ###############################################################################
1991 #
1992 # _store_all_styles()
1993 #
1994 # Write all STYLE records.
1995 #
1996 61 ▶sub _store_all_styles {
Subroutine _store_all_styles: called CC=2 SCAR=6.9
1997  
1998 61 my $self = shift;
1999  
2000 # Excel adds the built-in styles in alphabetical order.
2001 61 my @built_ins = (
2002 [0x03, 16], # Comma
2003 [0x06, 17], # Comma[0]
2004 [0x04, 18], # Currency
2005 [0x07, 19], # Currency[0]
2006 [0x00, 0], # Normal
2007 [0x05, 20], # Percent
2008  
2009 # We don't deal with these styles yet.
2010 #[0x08, 21], # Hyperlink
2011 #[0x02, 8], # ColLevel_n
2012 #[0x01, 1], # RowLevel_n
2013 );
2014  
2015  
2016 61 for my $aref (@built_ins) {
2017 366 my $type = $aref->[0];
2018 366 my $xf_index = $aref->[1];
2019  
2020 366 $self->_store_style($type, $xf_index);
2021 }
2022 }
2023  
2024  
2025 ###############################################################################
2026 #
2027 # _store_names()
2028 #
2029 # Write the NAME record to define the print area and the repeat rows and cols.
2030 #
2031 34 ▶sub _store_names {
Subroutine _store_names: called CC=11 SCAR=24.0
2032  
2033 34 my $self = shift;
2034 34 my $index;
2035 34 my %ext_refs = %{$self->{_ext_refs}};
2036  
2037  
2038 # Create the user defined names.
2039 34 for my $defined_name (@{$self->{_defined_names}}) {
2040  
2041 $self->_store_name(
2042 $defined_name->{name},
2043 $defined_name->{encoding},
2044 $defined_name->{sheet_index},
2045 $defined_name->{formula},
2046 0 );
2047 }
2048  
2049 # Sort the worksheets into alphabetical order by name. This is a
2050 # requirement for some non-English language Excel patch levels.
2051 34 my @worksheets = @{$self->{_worksheets}};
2052 34 @worksheets = sort { $a->{_name} cmp $b->{_name} } @worksheets;
2053  
2054 # Create the autofilter NAME records
2055 34 foreach my $worksheet (@worksheets) {
2056 60 $index = $worksheet->{_index};
2057 60 my $key = "$index:$index";
2058 60 my $ref = $ext_refs{$key};
2059  
2060 # Write a Name record if Autofilter has been defined
2061 60 ▶ if ($worksheet->{_filter_count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($worksheet->{"_filter_count"}) 6 54 60
2062 $self->_store_name_short(
2063 $worksheet->{_index},
2064 0x0D, # NAME type = Filter Database
2065 $ref,
2066 $worksheet->{_filter_area}->[0],
2067 $worksheet->{_filter_area}->[1],
2068 $worksheet->{_filter_area}->[2],
2069 6 $worksheet->{_filter_area}->[3],
2070 1, # Hidden
2071 );
2072 }
2073 }
2074  
2075 # Create the print area NAME records
2076 34 foreach my $worksheet (@worksheets) {
2077 60 $index = $worksheet->{_index};
2078 60 my $key = "$index:$index";
2079 60 my $ref = $ext_refs{$key};
2080  
2081 # Write a Name record if the print area has been defined
2082 60 ▶ if (defined $worksheet->{_print_rowmin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $worksheet->{"_print_rowmin"}) 20 40 60
2083 $self->_store_name_short(
2084 $worksheet->{_index},
2085 0x06, # NAME type = Print_Area
2086 $ref,
2087 $worksheet->{_print_rowmin},
2088 $worksheet->{_print_rowmax},
2089 $worksheet->{_print_colmin},
2090 $worksheet->{_print_colmax}
2091 20 );
2092 }
2093 }
2094  
2095 # Create the print title NAME records
2096 34 foreach my $worksheet (@worksheets) {
2097 60 $index = $worksheet->{_index};
2098  
2099 60 my $rowmin = $worksheet->{_title_rowmin};
2100 60 my $rowmax = $worksheet->{_title_rowmax};
2101 60 my $colmin = $worksheet->{_title_colmin};
2102 60 my $colmax = $worksheet->{_title_colmax};
2103 60 my $key = "$index:$index";
2104 60 my $ref = $ext_refs{$key};
2105  
2106 # Determine if row + col, row, col or nothing has been defined
2107 # and write the appropriate record
2108 #
2109 60 ▶ if (defined $rowmin && defined $colmin) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $rowmin and defined $colmin) { } 2 58 60
elsif (defined $rowmin) { } 16 42 58
elsif (defined $colmin) { } 8 34 42
Condition100%3/3
defined $rowmin and defined $colmin
!ll&&!rl&&r
42162
MC/DC100%2/2
defined $rowmin and defined $colmin
defined $rowmin defined $colmin
Truth table100%3/3
A and B
A= defined $rowminB= defined $colmin
ABresult
0X0
100
111
2110 # Row and column titles have been defined.
2111 # Row title has been defined.
2112 $self->_store_name_long(
2113 $worksheet->{_index},
2114 2 0x07, # NAME type = Print_Titles
2115 $ref,
2116 $rowmin,
2117 $rowmax,
2118 $colmin,
2119 $colmax
2120 );
2121 }
2122 elsif (defined $rowmin) {
2123 # Row title has been defined.
2124 $self->_store_name_short(
2125 $worksheet->{_index},
2126 16 0x07, # NAME type = Print_Titles
2127 $ref,
2128 $rowmin,
2129 $rowmax,
2130 0x00,
2131 0xff
2132 );
2133 }
2134 elsif (defined $colmin) {
2135 # Column title has been defined.
2136 $self->_store_name_short(
2137 $worksheet->{_index},
2138 8 0x07, # NAME type = Print_Titles
2139 $ref,
2140 0x0000,
2141 0xffff,
2142 $colmin,
2143 $colmax
2144 );
2145 }
2146 else {
2147 # Nothing left to do
2148 }
2149 }
2150 }
2151  
2152  
2153  
2154  
2155 ###############################################################################
2156 ###############################################################################
2157 #
2158 # BIFF RECORDS
2159 #
2160  
2161  
2162 ###############################################################################
2163 #
2164 # _store_window1()
2165 #
2166 # Write Excel BIFF WINDOW1 record.
2167 #
2168 61 ▶sub _store_window1 {
Subroutine _store_window1: called CC=1 SCAR=0.0
2169  
2170 61 my $self = shift;
2171  
2172 61 my $record = 0x003D; # Record identifier
2173 61 my $length = 0x0012; # Number of bytes to follow
2174  
2175 61 my $xWn = 0x0000; # Horizontal position of window
2176 61 my $yWn = 0x0000; # Vertical position of window
2177 61 my $dxWn = 0x355C; # Width of window
2178 61 my $dyWn = 0x30ED; # Height of window
2179  
2180 61 my $grbit = 0x0038; # Option flags
2181 61 my $ctabsel = $self->{_selected}; # Number of workbook tabs selected
2182 61 my $wTabRatio = 0x0258; # Tab to scrollbar ratio
2183  
2184 61 my $itabFirst = $self->{_firstsheet}; # 1st displayed worksheet
2185 61 my $itabCur = $self->{_activesheet}; # Active worksheet
2186  
2187 61 my $header = pack("vv", $record, $length);
2188 61 my $data = pack("vvvvvvvvv", $xWn, $yWn, $dxWn, $dyWn,
2189 $grbit,
2190 $itabCur, $itabFirst,
2191 $ctabsel, $wTabRatio);
2192  
2193 61 $self->_append($header, $data);
2194 }
2195  
2196  
2197 ###############################################################################
2198 #
2199 # _store_boundsheet()
2200 #
2201 # Writes Excel BIFF BOUNDSHEET record.
2202 #
2203 135 ▶sub _store_boundsheet {
Subroutine _store_boundsheet: called CC=3 SCAR=11.0
2204  
2205 135 my $self = shift;
2206  
2207 135 my $record = 0x0085; # Record identifier
2208 135 my $length = 0x08 + length($_[0]); # Number of bytes to follow
2209  
2210 135 my $sheetname = $_[0]; # Worksheet name
2211 135 my $offset = $_[1]; # Location of worksheet BOF
2212 135 my $type = $_[2]; # Worksheet type
2213 135 my $hidden = $_[3]; # Worksheet hidden flag
2214 135 my $encoding = $_[4]; # Sheet name encoding
2215 135 my $cch = length($sheetname); # Length of sheet name
2216  
2217 135 my $grbit = $type | $hidden;
2218  
2219 # Character length is num of chars not num of bytes
2220 135 ▶ $cch /= 2 if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 3 132 135
2221  
2222 # Change the UTF-16 name from BE to LE
2223 135 ▶ $sheetname = pack 'n*', unpack 'v*', $sheetname if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 3 132 135
2224  
2225 135 my $header = pack("vv", $record, $length);
2226 135 my $data = pack("VvCC", $offset, $grbit, $cch, $encoding);
2227  
2228 135 $self->_append($header, $data, $sheetname);
2229 }
2230  
2231  
2232 ###############################################################################
2233 #
2234 # _store_style()
2235 #
2236 # Write Excel BIFF STYLE records.
2237 #
2238 366 ▶sub _store_style {
Subroutine _store_style: called CC=1 SCAR=0.0
2239  
2240 366 my $self = shift;
2241  
2242 366 my $record = 0x0293; # Record identifier
2243 366 my $length = 0x0004; # Bytes to follow
2244  
2245 366 my $type = $_[0]; # Built-in style
2246 366 my $xf_index = $_[1]; # Index to style XF
2247 366 my $level = 0xff; # Outline style level
2248  
2249 366 $xf_index |= 0x8000; # Add flag to indicate built-in style.
2250  
2251  
2252 366 my $header = pack("vv", $record, $length);
2253 366 my $data = pack("vCC", $xf_index, $type, $level);
2254  
2255 366 $self->_append($header, $data);
2256 }
2257  
2258  
2259 ###############################################################################
2260 #
2261 # _store_num_format()
2262 #
2263 # Writes Excel FORMAT record for non "built-in" numerical formats.
2264 #
2265 0 ▶sub _store_num_format {
Subroutine _store_num_format: not called CC=8 SCAR=42.8
2266  
2267 0 my $self = shift;
2268  
2269 0 my $record = 0x041E; # Record identifier
2270 0 my $length; # Number of bytes to follow
2271  
2272 0 my $format = $_[0]; # Custom format string
2273 0 my $ifmt = $_[1]; # Format index code
2274 0 my $encoding = $_[2]; # Char encoding for format string
2275  
2276  
2277 # Handle utf8 strings in perl 5.8.
2278 0 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 0 0 0
2279 0 require Encode;
2280  
2281 0 ▶ if (Encode::is_utf8($format)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($format)) 0 0 0
2282 0 $format = Encode::encode("UTF-16BE", $format);
2283 0 $encoding = 1;
2284 }
2285 }
2286  
2287  
2288 # Char length of format string
2289 0 my $cch = length $format;
2290  
2291  
2292 # Handle Unicode format strings.
2293 0 ▶ if ($encoding == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding == 1) 0 0 0
2294 0 ▶ croak "Uneven number of bytes in Unicode font name" if $cch % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $cch % 2 0 0 0
2295 0 ▶ $cch /= 2 if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 0 0 0
2296 0 $format = pack 'v*', unpack 'n*', $format;
2297 }
2298  
2299  
2300 # Special case to handle Euro symbol, 0x80, in non-Unicode strings.
2301 0 ▶ if ($encoding == 0 and $format =~ /\x80/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding == 0 and $format =~ /\x80/) 0 0 0
Condition0%0/3
$encoding == 0 and $format =~ /\x80/
!ll&&!rl&&r
000
MC/DC0%0/2
$encoding == 0 and $format =~ /\x80/
$encoding == 0 $format =~ /\x80/
Truth table0%0/3
A and B
A= $encoding == 0B= $format =~ /\x80/
ABresult
0X0
100
111
2302 0 $format = pack 'v*', unpack 'C*', $format;
2303 0 $format =~ s/\x80\x00/\xAC\x20/g;
2304 0 $encoding = 1;
2305 }
2306  
2307 0 $length = 0x05 + length $format;
2308  
2309 0 my $header = pack("vv", $record, $length);
2310 0 my $data = pack("vvC", $ifmt, $cch, $encoding);
2311  
2312 0 $self->_append($header, $data, $format);
2313 }
2314  
2315  
2316 ###############################################################################
2317 #
2318 # _store_1904()
2319 #
2320 # Write Excel 1904 record to indicate the date system in use.
2321 #
2322 61 ▶sub _store_1904 {
Subroutine _store_1904: called CC=1 SCAR=0.0
2323  
2324 61 my $self = shift;
2325  
2326 61 my $record = 0x0022; # Record identifier
2327 61 my $length = 0x0002; # Bytes to follow
2328  
2329 61 my $f1904 = $self->{_1904}; # Flag for 1904 date system
2330  
2331 61 my $header = pack("vv", $record, $length);
2332 61 my $data = pack("v", $f1904);
2333  
2334 61 $self->_append($header, $data);
2335 }
2336  
2337  
2338 ###############################################################################
2339 #
2340 # _store_supbook()
2341 #
2342 # Write BIFF record SUPBOOK to indicate that the workbook contains external
2343 # references, in our case, formula, print area and print title refs.
2344 #
2345 17 ▶sub _store_supbook {
Subroutine _store_supbook: called CC=1 SCAR=0.0
2346  
2347 17 my $self = shift;
2348  
2349 17 my $record = 0x01AE; # Record identifier
2350 17 my $length = 0x0004; # Number of bytes to follow
2351  
2352 17 my $ctabs = @{$self->{_worksheets}}; # Number of worksheets
2353 17 my $StVirtPath = 0x0401; # Encoded workbook filename
2354  
2355 17 my $header = pack("vv", $record, $length);
2356 17 my $data = pack("vv", $ctabs, $StVirtPath);
2357  
2358 17 $self->_append($header, $data);
2359 }
2360  
2361  
2362 ###############################################################################
2363 #
2364 # _store_externsheet()
2365 #
2366 # Writes the Excel BIFF EXTERNSHEET record. These references are used by
2367 # formulas. TODO NAME record is required to define the print area and the
2368 # repeat rows and columns.
2369 #
2370 34 ▶sub _store_externsheet {
Subroutine _store_externsheet: called CC=3 SCAR=11.0
2371  
2372 34 my $self = shift;
2373  
2374 34 my $record = 0x0017; # Record identifier
2375 34 my $length; # Number of bytes to follow
2376  
2377  
2378 # Get the external refs
2379 34 my %ext_refs = %{$self->{_ext_refs}};
2380 34 my @ext_refs = sort {$ext_refs{$a} <=> $ext_refs{$b}} keys %ext_refs;
2381  
2382 # Change the external refs from stringified "1:1" to [1, 1]
2383 34 foreach my $ref (@ext_refs) {
2384 52 $ref = [split /:/, $ref];
2385 }
2386  
2387  
2388 34 my $cxti = scalar @ext_refs; # Number of Excel XTI structures
2389 34 my $rgxti = ''; # Array of XTI structures
2390  
2391 # Write the XTI structs
2392 34 foreach my $ext_ref (@ext_refs) {
2393 52 $rgxti .= pack("vvv", 0, $ext_ref->[0], $ext_ref->[1])
2394 }
2395  
2396  
2397 34 my $data = pack("v", $cxti) . $rgxti;
2398 34 my $header = pack("vv", $record, length $data);
2399  
2400 34 $self->_append($header, $data);
2401 }
2402  
2403  
2404 ###############################################################################
2405 #
2406 # _store_name()
2407 #
2408 #
2409 # Store the NAME record used for storing the print area, repeat rows, repeat
2410 # columns, autofilters and defined names.
2411 #
2412 # TODO. This is a more generic version that will replace _store_name_short()
2413 # and _store_name_long().
2414 #
2415 8 ▶sub _store_name {
Subroutine _store_name: called CC=6 SCAR=17.9
2416  
2417 8 my $self = shift;
2418  
2419 8 my $record = 0x0018; # Record identifier
2420 8 my $length; # Number of bytes to follow
2421  
2422 8 my $name = shift;
2423 8 my $encoding = shift;
2424 8 my $sheet_index = shift;
2425 8 my $formula = shift;
2426  
2427 8 my $text_length = length $name;
2428 8 my $formula_length = length $formula;
2429  
2430 # UTF-16 string length is in characters not bytes.
2431 8 ▶ $text_length /= 2 if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 0 8 8
2432  
2433  
2434 8 my $grbit = 0x0000; # Option flags
2435 8 my $shortcut = 0x00; # Keyboard shortcut
2436 8 my $ixals = 0x0000; # Unused index.
2437 8 my $menu_length = 0x00; # Length of cust menu text
2438 8 my $desc_length = 0x00; # Length of description text
2439 8 my $help_length = 0x00; # Length of help topic text
2440 8 my $status_length = 0x00; # Length of status bar text
2441  
2442 # Set grbit built-in flag and the hidden flag for autofilters.
2443 8 ▶ if ($text_length == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_length == 1) 7 1 8
2444 7 ▶ $grbit = 0x0020 if ord $name == 0x06; # Print area
Branch: 1 branch
BranchTrueFalseTotal
if ord $name == 6 2 5 7
2445 7 ▶ $grbit = 0x0020 if ord $name == 0x07; # Print titles
Branch: 1 branch
BranchTrueFalseTotal
if ord $name == 7 4 3 7
2446 7 ▶ $grbit = 0x0021 if ord $name == 0x0D; # Autofilter
Branch: 1 branch
BranchTrueFalseTotal
if ord $name == 13 1 6 7
2447 }
2448  
2449 8 my $data = pack "v", $grbit;
2450 8 $data .= pack "C", $shortcut;
2451 8 $data .= pack "C", $text_length;
2452 8 $data .= pack "v", $formula_length;
2453 8 $data .= pack "v", $ixals;
2454 8 $data .= pack "v", $sheet_index;
2455 8 $data .= pack "C", $menu_length;
2456 8 $data .= pack "C", $desc_length;
2457 8 $data .= pack "C", $help_length;
2458 8 $data .= pack "C", $status_length;
2459 8 $data .= pack "C", $encoding;
2460 8 $data .= $name;
2461 8 $data .= $formula;
2462  
2463 8 my $header = pack "vv", $record, length $data;
2464  
2465 8 $self->_append($header, $data);
2466 }
2467  
2468  
2469 ###############################################################################
2470 #
2471 # _store_name_short()
2472 #
2473 #
2474 # Store the NAME record in the short format that is used for storing the print
2475 # area, repeat rows only and repeat columns only.
2476 #
2477 50 ▶sub _store_name_short {
Subroutine _store_name_short: called CC=2 SCAR=6.9
2478  
2479 50 my $self = shift;
2480  
2481 50 my $record = 0x0018; # Record identifier
2482 50 my $length = 0x001b; # Number of bytes to follow
2483  
2484 50 my $index = shift; # Sheet index
2485 50 my $type = shift;
2486 50 my $ext_ref = shift; # TODO
2487  
2488 50 my $grbit = 0x0020; # Option flags
2489 50 my $chKey = 0x00; # Keyboard shortcut
2490 50 my $cch = 0x01; # Length of text name
2491 50 my $cce = 0x000b; # Length of text definition
2492 50 my $unknown01 = 0x0000; #
2493 50 my $ixals = $index +1; # Sheet index
2494 50 my $unknown02 = 0x00; #
2495 50 my $cchCustMenu = 0x00; # Length of cust menu text
2496 50 my $cchDescription = 0x00; # Length of description text
2497 50 my $cchHelptopic = 0x00; # Length of help topic text
2498 50 my $cchStatustext = 0x00; # Length of status bar text
2499 50 my $rgch = $type; # Built-in name type
2500 50 my $unknown03 = 0x3b; #
2501  
2502 50 my $rowmin = $_[0]; # Start row
2503 50 my $rowmax = $_[1]; # End row
2504 50 my $colmin = $_[2]; # Start column
2505 50 my $colmax = $_[3]; # end column
2506  
2507 50 my $hidden = $_[4]; # Name is hidden
2508 50 ▶ $grbit = 0x0021 if $hidden;
Branch: 1 branch
BranchTrueFalseTotal
if $hidden 6 44 50
2509  
2510 50 my $header = pack("vv", $record, $length);
2511 50 my $data = pack("v", $grbit);
2512 50 $data .= pack("C", $chKey);
2513 50 $data .= pack("C", $cch);
2514 50 $data .= pack("v", $cce);
2515 50 $data .= pack("v", $unknown01);
2516 50 $data .= pack("v", $ixals);
2517 50 $data .= pack("C", $unknown02);
2518 50 $data .= pack("C", $cchCustMenu);
2519 50 $data .= pack("C", $cchDescription);
2520 50 $data .= pack("C", $cchHelptopic);
2521 50 $data .= pack("C", $cchStatustext);
2522 50 $data .= pack("C", $rgch);
2523 50 $data .= pack("C", $unknown03);
2524 50 $data .= pack("v", $ext_ref);
2525  
2526 50 $data .= pack("v", $rowmin);
2527 50 $data .= pack("v", $rowmax);
2528 50 $data .= pack("v", $colmin);
2529 50 $data .= pack("v", $colmax);
2530  
2531 50 $self->_append($header, $data);
2532 }
2533  
2534  
2535 ###############################################################################
2536 #
2537 # _store_name_long()
2538 #
2539 #
2540 # Store the NAME record in the long format that is used for storing the repeat
2541 # rows and columns when both are specified. This share a lot of code with
2542 # _store_name_short() but we use a separate method to keep the code clean.
2543 # Code abstraction for reuse can be carried too far, and I should know. ;-)
2544 #
2545 2 ▶sub _store_name_long {
Subroutine _store_name_long: called CC=1 SCAR=0.0
2546  
2547 2 my $self = shift;
2548  
2549 2 my $record = 0x0018; # Record identifier
2550 2 my $length = 0x002a; # Number of bytes to follow
2551  
2552 2 my $index = shift; # Sheet index
2553 2 my $type = shift;
2554 2 my $ext_ref = shift; # TODO
2555  
2556 2 my $grbit = 0x0020; # Option flags
2557 2 my $chKey = 0x00; # Keyboard shortcut
2558 2 my $cch = 0x01; # Length of text name
2559 2 my $cce = 0x001a; # Length of text definition
2560 2 my $unknown01 = 0x0000; #
2561 2 my $ixals = $index +1; # Sheet index
2562 2 my $unknown02 = 0x00; #
2563 2 my $cchCustMenu = 0x00; # Length of cust menu text
2564 2 my $cchDescription = 0x00; # Length of description text
2565 2 my $cchHelptopic = 0x00; # Length of help topic text
2566 2 my $cchStatustext = 0x00; # Length of status bar text
2567 2 my $rgch = $type; # Built-in name type
2568  
2569 2 my $unknown03 = 0x29;
2570 2 my $unknown04 = 0x0017;
2571 2 my $unknown05 = 0x3b;
2572  
2573 2 my $rowmin = $_[0]; # Start row
2574 2 my $rowmax = $_[1]; # End row
2575 2 my $colmin = $_[2]; # Start column
2576 2 my $colmax = $_[3]; # end column
2577  
2578  
2579 2 my $header = pack("vv", $record, $length);
2580 2 my $data = pack("v", $grbit);
2581 2 $data .= pack("C", $chKey);
2582 2 $data .= pack("C", $cch);
2583 2 $data .= pack("v", $cce);
2584 2 $data .= pack("v", $unknown01);
2585 2 $data .= pack("v", $ixals);
2586 2 $data .= pack("C", $unknown02);
2587 2 $data .= pack("C", $cchCustMenu);
2588 2 $data .= pack("C", $cchDescription);
2589 2 $data .= pack("C", $cchHelptopic);
2590 2 $data .= pack("C", $cchStatustext);
2591 2 $data .= pack("C", $rgch);
2592  
2593 # Column definition
2594 2 $data .= pack("C", $unknown03);
2595 2 $data .= pack("v", $unknown04);
2596 2 $data .= pack("C", $unknown05);
2597 2 $data .= pack("v", $ext_ref);
2598 2 $data .= pack("v", 0x0000);
2599 2 $data .= pack("v", 0xffff);
2600 2 $data .= pack("v", $colmin);
2601 2 $data .= pack("v", $colmax);
2602  
2603 # Row definition
2604 2 $data .= pack("C", $unknown05);
2605 2 $data .= pack("v", $ext_ref);
2606 2 $data .= pack("v", $rowmin);
2607 2 $data .= pack("v", $rowmax);
2608 2 $data .= pack("v", 0x00);
2609 2 $data .= pack("v", 0xff);
2610 # End of data
2611 2 $data .= pack("C", 0x10);
2612  
2613 2 $self->_append($header, $data);
2614 }
2615  
2616  
2617 ###############################################################################
2618 #
2619 # _store_palette()
2620 #
2621 # Stores the PALETTE biff record.
2622 #
2623 61 ▶sub _store_palette {
Subroutine _store_palette: called CC=2 SCAR=6.9
2624  
2625 61 my $self = shift;
2626  
2627 61 my $aref = $self->{_palette};
2628  
2629 61 my $record = 0x0092; # Record identifier
2630 61 my $length = 2 + 4 * @$aref; # Number of bytes to follow
2631 61 my $ccv = @$aref; # Number of RGB values to follow
2632 61 my $data; # The RGB data
2633  
2634 # Pack the RGB data
2635 61 $data .= pack "CCCC", @$_ for @$aref;
2636  
2637 61 my $header = pack("vvv", $record, $length, $ccv);
2638  
2639 61 $self->_append($header, $data);
2640 }
2641  
2642  
2643 ###############################################################################
2644 #
2645 # _store_codepage()
2646 #
2647 # Stores the CODEPAGE biff record.
2648 #
2649 61 ▶sub _store_codepage {
Subroutine _store_codepage: called CC=1 SCAR=0.0
2650  
2651 61 my $self = shift;
2652  
2653 61 my $record = 0x0042; # Record identifier
2654 61 my $length = 0x0002; # Number of bytes to follow
2655 61 my $cv = $self->{_codepage}; # The code page
2656  
2657 61 my $header = pack("vv", $record, $length);
2658 61 my $data = pack("v", $cv);
2659  
2660 61 $self->_append($header, $data);
2661 }
2662  
2663  
2664 ###############################################################################
2665 #
2666 # _store_country()
2667 #
2668 # Stores the COUNTRY biff record.
2669 #
2670 61 ▶sub _store_country {
Subroutine _store_country: called CC=1 SCAR=0.0
2671  
2672 61 my $self = shift;
2673  
2674 61 my $record = 0x008C; # Record identifier
2675 61 my $length = 0x0004; # Number of bytes to follow
2676 61 my $country_default = $self->{_country};
2677 61 my $country_win_ini = $self->{_country};
2678  
2679 61 my $header = pack("vv", $record, $length);
2680 61 my $data = pack("vv", $country_default, $country_win_ini);
2681  
2682 61 $self->_append($header, $data);
2683 }
2684  
2685  
2686 ###############################################################################
2687 #
2688 # _store_hideobj()
2689 #
2690 # Stores the HIDEOBJ biff record.
2691 #
2692 61 ▶sub _store_hideobj {
Subroutine _store_hideobj: called CC=1 SCAR=0.0
2693  
2694 61 my $self = shift;
2695  
2696 61 my $record = 0x008D; # Record identifier
2697 61 my $length = 0x0002; # Number of bytes to follow
2698 61 my $hide = $self->{_hideobj}; # Option to hide objects
2699  
2700 61 my $header = pack("vv", $record, $length);
2701 61 my $data = pack("v", $hide);
2702  
2703 61 $self->_append($header, $data);
2704 }
2705  
2706  
2707 ###############################################################################
2708 ###############################################################################
2709 ###############################################################################
2710  
2711  
2712  
2713 ###############################################################################
2714 #
2715 # _calculate_extern_sizes()
2716 #
2717 # We need to calculate the space required by the SUPBOOK, EXTERNSHEET and NAME
2718 # records so that it can be added to the BOUNDSHEET offsets.
2719 #
2720 78 ▶sub _calculate_extern_sizes {
Subroutine _calculate_extern_sizes: called CC=16 SCAR=28.6
2721  
2722 78 my $self = shift;
2723  
2724  
2725 78 my %ext_refs = $self->{_parser}->get_ext_sheets();
2726 78 my $ext_ref_count = scalar keys %ext_refs;
2727 78 my $length = 0;
2728 78 my $index = 0;
2729  
2730  
2731 78 ▶ if (@{$self->{_defined_names}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_defined_names"};}) 0 78 78
2732 0 my $index = 0;
2733 0 my $key = "$index:$index";
2734  
2735 0 ▶ if (not exists $ext_refs{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ext_refs{$key}) 0 0 0
2736 0 $ext_refs{$key} = $ext_ref_count++;
2737 }
2738 }
2739  
2740 78 for my $defined_name (@{$self->{_defined_names}}) {
2741 $length += 19
2742 + length($defined_name->{name})
2743 0 + length($defined_name->{formula});
2744 }
2745  
2746  
2747 78 foreach my $worksheet (@{$self->{_worksheets}}) {
2748  
2749 165 my $rowmin = $worksheet->{_title_rowmin};
2750 165 my $colmin = $worksheet->{_title_colmin};
2751 165 my $filter = $worksheet->{_filter_count};
2752 165 my $key = "$index:$index";
2753 165 $index++;
2754  
2755  
2756 # Add area NAME records
2757 #
2758 165 ▶ if (defined $worksheet->{_print_rowmin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $worksheet->{"_print_rowmin"}) 20 145 165
2759 20 ▶ $ext_refs{$key} = $ext_ref_count++ if not exists $ext_refs{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ext_refs{$key} 20 0 20
2760  
2761 20 $length += 31 ;
2762 }
2763  
2764  
2765 # Add title NAME records
2766 #
2767 165 ▶ if (defined $rowmin and defined $colmin) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rowmin and defined $colmin) { } 2 163 165
elsif (defined $rowmin or defined $colmin) { } 24 139 163
Condition100%3/3
defined $rowmin and defined $colmin
!ll&&!rl&&r
147162
MC/DC100%2/2
defined $rowmin and defined $colmin
defined $rowmin defined $colmin
Truth table100%3/3
A and B
A= defined $rowminB= defined $colmin
ABresult
0X0
100
111
2768 2 ▶ $ext_refs{$key} = $ext_ref_count++ if not exists $ext_refs{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ext_refs{$key} 2 0 2
2769  
2770 2 $length += 46;
2771 }
2772 ▶ elsif (defined $rowmin or defined $colmin) {
Condition100%3/3
defined $rowmin or defined $colmin
l!l&&r!l&&!r
168139
MC/DC100%2/2
defined $rowmin or defined $colmin
defined $rowmin defined $colmin
Truth table100%3/3
A or B
A= defined $rowminB= defined $colmin
ABresult
1X1
011
000
2773 24 ▶ $ext_refs{$key} = $ext_ref_count++ if not exists $ext_refs{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ext_refs{$key} 24 0 24
2774  
2775 24 $length += 31;
2776 }
2777 else {
2778 # TODO, may need this later.
2779 }
2780  
2781  
2782 # Add Autofilter NAME records
2783 #
2784 165 ▶ if ($filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 6 159 165
2785 6 ▶ $ext_refs{$key} = $ext_ref_count++ if not exists $ext_refs{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ext_refs{$key} 6 0 6
2786  
2787 6 $length += 31;
2788 }
2789 }
2790  
2791  
2792 # Update the ref counts.
2793 78 $self->{_ext_ref_count} = $ext_ref_count;
2794 78 $self->{_ext_refs} = {%ext_refs};
2795  
2796  
2797 # If there are no external refs then we don't write, SUPBOOK, EXTERNSHEET
2798 # and NAME. Therefore the length is 0.
2799  
2800 78 ▶ return $length = 0 if $ext_ref_count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ext_ref_count == 0 44 34 78
2801  
2802  
2803 # The SUPBOOK record is 8 bytes
2804 34 $length += 8;
2805  
2806 # The EXTERNSHEET record is 6 bytes + 6 bytes for each external ref
2807 34 $length += 6 * (1 + $ext_ref_count);
2808  
2809 34 return $length;
2810 }
2811  
2812  
2813 ###############################################################################
2814 #
2815 # _calculate_shared_string_sizes()
2816 #
2817 # Handling of the SST continue blocks is complicated by the need to include an
2818 # additional continuation byte depending on whether the string is split between
2819 # blocks or whether it starts at the beginning of the block. (There are also
2820 # additional complications that will arise later when/if Rich Strings are
2821 # supported). As such we cannot use the simple CONTINUE mechanism provided by
2822 # the _add_continue() method in BIFFwriter.pm. Thus we have to make two passes
2823 # through the strings data. The first is to calculate the required block sizes
2824 # and the second, in _store_shared_strings(), is to write the actual strings.
2825 # The first pass through the data is also used to calculate the size of the SST
2826 # and CONTINUE records for use in setting the BOUNDSHEET record offsets. The
2827 # downside of this is that the same algorithm repeated in _store_shared_strings.
2828 #
2829 61 ▶sub _calculate_shared_string_sizes {
Subroutine _calculate_shared_string_sizes: called CC=17 SCAR=43.3
2830  
2831 61 my $self = shift;
2832  
2833 61 my @strings;
2834 61 $#strings = $self->{_str_unique} -1; # Pre-extend array
2835  
2836 61 ▶ while (my $key = each %{$self->{_str_table}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = each %{$$self{"_str_table"};})) 7 61 68
2837 7 $strings[$self->{_str_table}->{$key}] = $key;
2838 }
2839  
2840 # The SST data could be very large, free some memory (maybe).
2841 61 $self->{_str_table} = undef;
2842 61 $self->{_str_array} = [@strings];
2843  
2844  
2845 # Iterate through the strings to calculate the CONTINUE block sizes.
2846 #
2847 # The SST blocks requires a specialised CONTINUE block, so we have to
2848 # ensure that the maximum data block size is less than the limit used by
2849 # _add_continue() in BIFFwriter.pm. For simplicity we use the same size
2850 # for the SST and CONTINUE records:
2851 # 8228 : Maximum Excel97 block size
2852 # -4 : Length of block header
2853 # -8 : Length of additional SST header information
2854 # -8 : Arbitrary number to keep within _add_continue() limit
2855 # = 8208
2856 #
2857 61 my $continue_limit = 8208;
2858 61 my $block_length = 0;
2859 61 my $written = 0;
2860 61 my @block_sizes;
2861 61 my $continue = 0;
2862  
2863 61 for my $string (@strings) {
2864  
2865 7 my $string_length = length $string;
2866 7 my $encoding = unpack "xx C", $string;
2867 7 my $split_string = 0;
2868  
2869  
2870 # Block length is the total length of the strings that will be
2871 # written out in a single SST or CONTINUE block.
2872 #
2873 7 $block_length += $string_length;
2874  
2875  
2876 # We can write the string if it doesn't cross a CONTINUE boundary
2877 7 ▶ if ($block_length < $continue_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_length < $continue_limit) 7 0 7
2878 7 $written += $string_length;
2879 7 next;
2880 }
2881  
2882  
2883 # Deal with the cases where the next string to be written will exceed
2884 # the CONTINUE boundary. If the string is very long it may need to be
2885 # written in more than one CONTINUE record.
2886 #
2887 0 ▶ while ($block_length >= $continue_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_length >= $continue_limit) 0 0 0
2888  
2889 # We need to avoid the case where a string is continued in the first
2890 # n bytes that contain the string header information.
2891 #
2892 0 my $header_length = 3; # Min string + header size -1
2893 0 my $space_remaining = $continue_limit -$written -$continue;
2894  
2895  
2896 # Unicode data should only be split on char (2 byte) boundaries.
2897 # Therefore, in some cases we need to reduce the amount of available
2898 # space by 1 byte to ensure the correct alignment.
2899 0 my $align = 0;
2900  
2901 # Only applies to Unicode strings
2902 0 ▶ if ($encoding == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding == 1) 0 0 0
2903 # Min string + header size -1
2904 0 $header_length = 4;
2905  
2906 0 ▶ if ($space_remaining > $header_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space_remaining > $header_length) 0 0 0
2907 # String contains 3 byte header => split on odd boundary
2908 0 ▶ if (not $split_string and $space_remaining % 2 != 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $split_string and $space_remaining % 2 != 1) { } 0 0 0
elsif ($split_string and $space_remaining % 2 == 1) { } 0 0 0
Condition0%0/3
not $split_string and $space_remaining % 2 != 1
!ll&&!rl&&r
000
MC/DC0%0/2
not $split_string and $space_remaining % 2 != 1
not $split_string $space_remaining % 2 != 1
Truth table0%0/3
not(A) and B
A= not $split_stringB= $space_remaining % 2 != 1
ABresult
0X0
100
111
2909 0 $space_remaining--;
2910 0 $align = 1;
2911 }
2912 # Split section without header => split on even boundary
2913 ▶ elsif ($split_string and $space_remaining % 2 == 1) {
Condition0%0/3
$split_string and $space_remaining % 2 == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$split_string and $space_remaining % 2 == 1
$split_string $space_remaining % 2 == 1
Truth table0%0/3
A and B
A= $split_stringB= $space_remaining % 2 == 1
ABresult
0X0
100
111
2914 0 $space_remaining--;
2915 0 $align = 1;
2916 }
2917  
2918 0 $split_string = 1;
2919 }
2920 }
2921  
2922  
2923 0 ▶ if ($space_remaining > $header_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space_remaining > $header_length) { } 0 0 0
2924 # Write as much as possible of the string in the current block
2925 0 $written += $space_remaining;
2926  
2927 # Reduce the current block length by the amount written
2928 0 $block_length -= $continue_limit -$continue -$align;
2929  
2930 # Store the max size for this block
2931 0 push @block_sizes, $continue_limit -$align;
2932  
2933 # If the current string was split then the next CONTINUE block
2934 # should have the string continue flag (grbit) set unless the
2935 # split string fits exactly into the remaining space.
2936 #
2937 0 ▶ if ($block_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_length > 0) { } 0 0 0
2938 0 $continue = 1;
2939 }
2940 else {
2941 0 $continue = 0;
2942 }
2943  
2944 }
2945 else {
2946 # Store the max size for this block
2947 0 push @block_sizes, $written +$continue;
2948  
2949 # Not enough space to start the string in the current block
2950 0 $block_length -= $continue_limit -$space_remaining -$continue;
2951 0 $continue = 0;
2952  
2953 }
2954  
2955 # If the string (or substr) is small enough we can write it in the
2956 # new CONTINUE block. Else, go through the loop again to write it in
2957 # one or more CONTINUE blocks
2958 #
2959 0 ▶ if ($block_length < $continue_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_length < $continue_limit) { } 0 0 0
2960 0 $written = $block_length;
2961 }
2962 else {
2963 0 $written = 0;
2964 }
2965 }
2966 }
2967  
2968 # Store the max size for the last block unless it is empty
2969 61 ▶ push @block_sizes, $written +$continue if $written +$continue;
Branch: 1 branch
BranchTrueFalseTotal
if $written + $continue 3 58 61
2970  
2971  
2972 61 $self->{_str_block_sizes} = [@block_sizes];
2973  
2974  
2975 # Calculate the total length of the SST and associated CONTINUEs (if any).
2976 # The SST record will have a length even if it contains no strings.
2977 # This length is required to set the offsets in the BOUNDSHEET records since
2978 # they must be written before the SST records
2979 #
2980 61 my $length = 12;
2981 61 ▶ $length += shift @block_sizes if @block_sizes; # SST
Branch: 1 branch
BranchTrueFalseTotal
if @block_sizes 3 58 61
2982 61 ▶ $length += 4 + shift @block_sizes while @block_sizes; # CONTINUEs
Branch: 1 branch
BranchTrueFalseTotal
@block_sizes and do { $length += 4 + shift(@block_sizes) } 0 61 61
2983  
2984 61 return $length;
2985 }
2986  
2987  
2988 ###############################################################################
2989 #
2990 # _store_shared_strings()
2991 #
2992 # Write all of the workbooks strings into an indexed array.
2993 #
2994 # See the comments in _calculate_shared_string_sizes() for more information.
2995 #
2996 # We also use this routine to record the offsets required by the EXTSST table.
2997 # In order to do this we first identify the first string in an EXTSST bucket
2998 # and then store its global and local offset within the SST table. The offset
2999 # occurs wherever the start of the bucket string is written out via append().
3000 #
3001 61 ▶sub _store_shared_strings {
Subroutine _store_shared_strings: called CC=20 SCAR=47.9
3002  
3003 61 my $self = shift;
3004  
3005 61 my @strings = @{$self->{_str_array}};
3006  
3007  
3008 61 my $record = 0x00FC; # Record identifier
3009 61 my $length = 0x0008; # Number of bytes to follow
3010 61 my $total = 0x0000;
3011  
3012 # Iterate through the strings to calculate the CONTINUE block sizes
3013 61 my $continue_limit = 8208;
3014 61 my $block_length = 0;
3015 61 my $written = 0;
3016 61 my $continue = 0;
3017  
3018 # The SST and CONTINUE block sizes have been pre-calculated by
3019 # _calculate_shared_string_sizes()
3020 61 my @block_sizes = @{$self->{_str_block_sizes}};
3021  
3022  
3023 # The SST record is required even if it contains no strings. Thus we will
3024 # always have a length
3025 #
3026 61 ▶ if (@block_sizes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@block_sizes) { } 3 58 61
3027 3 $length = 8 + shift @block_sizes;
3028 }
3029 else {
3030 # No strings
3031 58 $length = 8;
3032 }
3033  
3034  
3035 # Initialise variables used to track EXTSST bucket offsets.
3036 61 my $extsst_str_num = -1;
3037 61 my $sst_block_start = $self->{_datasize};
3038  
3039  
3040 # Write the SST block header information
3041 61 my $header = pack("vv", $record, $length);
3042 61 my $data = pack("VV", $self->{_str_total}, $self->{_str_unique});
3043 61 $self->_append($header, $data);
3044  
3045  
3046 # Iterate through the strings and write them out
3047 61 for my $string (@strings) {
3048  
3049 7 my $string_length = length $string;
3050 7 my $encoding = unpack "xx C", $string;
3051 7 my $split_string = 0;
3052 7 my $bucket_string = 0; # Used to track EXTSST bucket offsets.
3053  
3054  
3055 # Check if the string is at the start of a EXTSST bucket.
3056 7 ▶ if (++$extsst_str_num % $self->{_extsst_bucket_size} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$extsst_str_num % $self->{"_extsst_bucket_size"} == 0) 3 4 7
3057 3 $bucket_string = 1;
3058 }
3059  
3060  
3061 # Block length is the total length of the strings that will be
3062 # written out in a single SST or CONTINUE block.
3063 #
3064 7 $block_length += $string_length;
3065  
3066  
3067 # We can write the string if it doesn't cross a CONTINUE boundary
3068 7 ▶ if ($block_length < $continue_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_length < $continue_limit) 7 0 7
3069  
3070 # Store location of EXTSST bucket string.
3071 7 ▶ if ($bucket_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bucket_string) 3 4 7
3072 3 my $global_offset = $self->{_datasize};
3073 3 my $local_offset = $self->{_datasize} - $sst_block_start;
3074  
3075 3 push @{$self->{_extsst_offsets}}, [$global_offset, $local_offset];
3076 3 $bucket_string = 0;
3077 }
3078  
3079 7 $self->_append($string);
3080 7 $written += $string_length;
3081 7 next;
3082 }
3083  
3084  
3085 # Deal with the cases where the next string to be written will exceed
3086 # the CONTINUE boundary. If the string is very long it may need to be
3087 # written in more than one CONTINUE record.
3088 #
3089 0 ▶ while ($block_length >= $continue_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_length >= $continue_limit) 0 0 0
3090  
3091 # We need to avoid the case where a string is continued in the first
3092 # n bytes that contain the string header information.
3093 #
3094 0 my $header_length = 3; # Min string + header size -1
3095 0 my $space_remaining = $continue_limit -$written -$continue;
3096  
3097  
3098 # Unicode data should only be split on char (2 byte) boundaries.
3099 # Therefore, in some cases we need to reduce the amount of available
3100 # space by 1 byte to ensure the correct alignment.
3101 0 my $align = 0;
3102  
3103 # Only applies to Unicode strings
3104 0 ▶ if ($encoding == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding == 1) 0 0 0
3105 # Min string + header size -1
3106 0 $header_length = 4;
3107  
3108 0 ▶ if ($space_remaining > $header_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space_remaining > $header_length) 0 0 0
3109 # String contains 3 byte header => split on odd boundary
3110 0 ▶ if (not $split_string and $space_remaining % 2 != 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $split_string and $space_remaining % 2 != 1) { } 0 0 0
elsif ($split_string and $space_remaining % 2 == 1) { } 0 0 0
Condition0%0/3
not $split_string and $space_remaining % 2 != 1
!ll&&!rl&&r
000
MC/DC0%0/2
not $split_string and $space_remaining % 2 != 1
not $split_string $space_remaining % 2 != 1
Truth table0%0/3
not(A) and B
A= not $split_stringB= $space_remaining % 2 != 1
ABresult
0X0
100
111
3111 0 $space_remaining--;
3112 0 $align = 1;
3113 }
3114 # Split section without header => split on even boundary
3115 ▶ elsif ($split_string and $space_remaining % 2 == 1) {
Condition0%0/3
$split_string and $space_remaining % 2 == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$split_string and $space_remaining % 2 == 1
$split_string $space_remaining % 2 == 1
Truth table0%0/3
A and B
A= $split_stringB= $space_remaining % 2 == 1
ABresult
0X0
100
111
3116 0 $space_remaining--;
3117 0 $align = 1;
3118 }
3119  
3120 0 $split_string = 1;
3121 }
3122 }
3123  
3124  
3125 0 ▶ if ($space_remaining > $header_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space_remaining > $header_length) { } 0 0 0
3126 # Write as much as possible of the string in the current block
3127 0 my $tmp = substr $string, 0, $space_remaining;
3128  
3129 # Store location of EXTSST bucket string.
3130 0 ▶ if ($bucket_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bucket_string) 0 0 0
3131 0 my $global_offset = $self->{_datasize};
3132 0 my $local_offset = $self->{_datasize} - $sst_block_start;
3133  
3134 0 push @{$self->{_extsst_offsets}}, [$global_offset, $local_offset];
3135 0 $bucket_string = 0;
3136 }
3137  
3138 0 $self->_append($tmp);
3139  
3140  
3141 # The remainder will be written in the next block(s)
3142 0 $string = substr $string, $space_remaining;
3143  
3144 # Reduce the current block length by the amount written
3145 0 $block_length -= $continue_limit -$continue -$align;
3146  
3147 # If the current string was split then the next CONTINUE block
3148 # should have the string continue flag (grbit) set unless the
3149 # split string fits exactly into the remaining space.
3150 #
3151 0 ▶ if ($block_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_length > 0) { } 0 0 0
3152 0 $continue = 1;
3153 }
3154 else {
3155 0 $continue = 0;
3156 }
3157 }
3158 else {
3159 # Not enough space to start the string in the current block
3160 0 $block_length -= $continue_limit -$space_remaining -$continue;
3161 0 $continue = 0;
3162 }
3163  
3164 # Write the CONTINUE block header
3165 0 ▶ if (@block_sizes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@block_sizes) 0 0 0
3166 0 $sst_block_start= $self->{_datasize}; # Reset EXTSST offset.
3167  
3168 0 $record = 0x003C;
3169 0 $length = shift @block_sizes;
3170  
3171 0 $header = pack("vv", $record, $length);
3172 0 ▶ $header .= pack("C", $encoding) if $continue;
Branch: 1 branch
BranchTrueFalseTotal
if $continue 0 0 0
3173  
3174 0 $self->_append($header);
3175 }
3176  
3177 # If the string (or substr) is small enough we can write it in the
3178 # new CONTINUE block. Else, go through the loop again to write it in
3179 # one or more CONTINUE blocks
3180 #
3181 0 ▶ if ($block_length < $continue_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_length < $continue_limit) { } 0 0 0
3182  
3183 # Store location of EXTSST bucket string.
3184 0 ▶ if ($bucket_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bucket_string) 0 0 0
3185 0 my $global_offset = $self->{_datasize};
3186 0 my $local_offset = $self->{_datasize} - $sst_block_start;
3187  
3188 0 push @{$self->{_extsst_offsets}}, [$global_offset, $local_offset];
3189  
3190 0 $bucket_string = 0;
3191 }
3192 0 $self->_append($string);
3193  
3194 0 $written = $block_length;
3195 }
3196 else {
3197 0 $written = 0;
3198 }
3199 }
3200 }
3201 }
3202  
3203  
3204 ###############################################################################
3205 #
3206 # _calculate_extsst_size
3207 #
3208 # The number of buckets used in the EXTSST is between 0 and 128. The number of
3209 # strings per bucket (bucket size) has a minimum value of 8 and a theoretical
3210 # maximum of 2^16. For "number of strings" < 1024 there is a constant bucket
3211 # size of 8. The following algorithm generates the same size/bucket ratio
3212 # as Excel.
3213 #
3214 89 ▶sub _calculate_extsst_size {
Subroutine _calculate_extsst_size: called CC=2 SCAR=6.9
3215  
3216 89 my $self = shift;
3217  
3218 89 my $unique_strings = $self->{_str_unique};
3219  
3220 89 my $bucket_size;
3221 my $buckets;
3222  
3223 89 ▶ if ($unique_strings < 1024) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique_strings < 1024) { } 75 14 89
3224 75 $bucket_size = 8;
3225 }
3226 else {
3227 14 $bucket_size = 1 + int($unique_strings / 128);
3228 }
3229  
3230 89 $buckets = int(($unique_strings + $bucket_size -1) / $bucket_size);
3231  
3232  
3233 89 $self->{_extsst_buckets} = $buckets ;
3234 89 $self->{_extsst_bucket_size} = $bucket_size;
3235  
3236  
3237 89 return 6 + 8 * $buckets;
3238 }
3239  
3240  
3241 ###############################################################################
3242 #
3243 # _store_extsst
3244 #
3245 # Write EXTSST table using the offsets calculated in _store_shared_strings().
3246 #
3247 61 ▶sub _store_extsst {
Subroutine _store_extsst: called CC=2 SCAR=6.9
3248  
3249 61 my $self = shift;
3250  
3251 61 my @offsets = @{$self->{_extsst_offsets}};
3252 61 my $bucket_size = $self->{_extsst_bucket_size};
3253  
3254 61 my $record = 0x00FF; # Record identifier
3255 61 my $length = 2 + 8 * @offsets; # Bytes to follow
3256  
3257 61 my $header = pack 'vv', $record, $length;
3258 61 my $data = pack 'v', $bucket_size,;
3259  
3260 61 for my $offset (@offsets) {
3261 3 $data .= pack 'Vvv', $offset->[0], $offset->[1], 0;
3262 }
3263  
3264 61 $self->_append($header, $data);
3265  
3266 }
3267  
3268  
3269  
3270  
3271 #
3272 # Methods related to comments and MSO objects.
3273 #
3274  
3275 ###############################################################################
3276 #
3277 # _add_mso_drawing_group()
3278 #
3279 # Write the MSODRAWINGGROUP record that keeps track of the Escher drawing
3280 # objects in the file such as images, comments and filters.
3281 #
3282 78 ▶sub _add_mso_drawing_group {
Subroutine _add_mso_drawing_group: called CC=3 SCAR=11.0
3283  
3284 78 my $self = shift;
3285  
3286 78 ▶ return unless $self->{_mso_size};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_mso_size"} 58 20 78
3287  
3288 20 my $record = 0x00EB; # Record identifier
3289 20 my $length = 0x0000; # Number of bytes to follow
3290  
3291 20 my $data = $self->_store_mso_dgg_container();
3292 20 $data .= $self->_store_mso_dgg(@{$self->{_mso_clusters}});
3293 20 $data .= $self->_store_mso_bstore_container();
3294 20 $data .= $self->_store_mso_images(@$_) for @{$self->{_images_data}};
3295 20 $data .= $self->_store_mso_opt();
3296 20 $data .= $self->_store_mso_split_menu_colors();
3297  
3298 20 $length = length $data;
3299 20 my $header = pack("vv", $record, $length);
3300  
3301 20 $self->_add_mso_drawing_group_continue($header . $data);
3302  
3303 20 return $header . $data; # For testing only.
3304 }
3305  
3306  
3307 ###############################################################################
3308 #
3309 # _add_mso_drawing_group_continue()
3310 #
3311 # See first the Spreadsheet::WriteExcel::BIFFwriter::_add_continue() method.
3312 #
3313 # Add specialised CONTINUE headers to large MSODRAWINGGROUP data block.
3314 # We use the Excel 97 max block size of 8228 - 4 bytes for the header = 8224.
3315 #
3316 # The structure depends on the size of the data block:
3317 #
3318 # Case 1: <= 8224 bytes 1 MSODRAWINGGROUP
3319 # Case 2: <= 2*8224 bytes 1 MSODRAWINGGROUP + 1 CONTINUE
3320 # Case 3: > 2*8224 bytes 2 MSODRAWINGGROUP + n CONTINUE
3321 #
3322 20 ▶sub _add_mso_drawing_group_continue {
Subroutine _add_mso_drawing_group_continue: called CC=4 SCAR=20.1
3323  
3324 20 my $self = shift;
3325  
3326 20 my $data = $_[0];
3327 20 my $limit = 8228 -4;
3328 20 my $mso_group = 0x00EB; # Record identifier
3329 20 my $continue = 0x003C; # Record identifier
3330 20 my $block_count = 1;
3331 20 my $header;
3332 my $tmp;
3333  
3334 # Ignore the base class _add_continue() method.
3335 20 $self->{_ignore_continue} = 1;
3336  
3337 # Case 1 above. Just return the data as it is.
3338 20 ▶ if (length $data <= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data <= $limit) 20 0 20
3339 20 $self->_append($data);
3340 20 return;
3341 }
3342  
3343 # Change length field of the first MSODRAWINGGROUP block. Case 2 and 3.
3344 0 $tmp = substr($data, 0, $limit +4, "");
3345 0 substr($tmp, 2, 2, pack("v", $limit));
3346 0 $self->_append($tmp);
3347  
3348  
3349 # Add MSODRAWINGGROUP and CONTINUE blocks for Case 3 above.
3350 0 ▶ while (length($data) > $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data > $limit) 0 0 0
3351 0 ▶ if ($block_count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_count == 1) { } 0 0 0
3352 # Add extra MSODRAWINGGROUP block header.
3353 0 $header = pack("vv", $mso_group, $limit);
3354 0 $block_count++;
3355 }
3356 else {
3357 # Add normal CONTINUE header.
3358 0 $header = pack("vv", $continue, $limit);
3359 }
3360  
3361 0 $tmp = substr($data, 0, $limit, "");
3362 0 $self->_append($header, $tmp);
3363 }
3364  
3365  
3366 # Last CONTINUE block for remaining data. Case 2 and 3 above.
3367 0 $header = pack("vv", $continue, length($data));
3368 0 $self->_append($header, $data);
3369  
3370  
3371 # Turn the base class _add_continue() method back on.
3372 0 $self->{_ignore_continue} = 0;
3373 }
3374  
3375  
3376 ###############################################################################
3377 #
3378 # _store_mso_dgg_container()
3379 #
3380 # Write the Escher DggContainer record that is part of MSODRAWINGGROUP.
3381 #
3382 21 ▶sub _store_mso_dgg_container {
Subroutine _store_mso_dgg_container: called CC=1 SCAR=0.0
3383  
3384 21 my $self = shift;
3385  
3386 21 my $type = 0xF000;
3387 21 my $version = 15;
3388 21 my $instance = 0;
3389 21 my $data = '';
3390 21 my $length = $self->{_mso_size} -12; # -4 (biff header) -8 (for this).
3391  
3392  
3393 21 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
3394 }
3395  
3396  
3397 ###############################################################################
3398 #
3399 # _store_mso_dgg()
3400 #
3401 # Write the Escher Dgg record that is part of MSODRAWINGGROUP.
3402 #
3403 21 ▶sub _store_mso_dgg {
Subroutine _store_mso_dgg: called CC=2 SCAR=6.9
3404  
3405 21 my $self = shift;
3406  
3407 21 my $type = 0xF006;
3408 21 my $version = 0;
3409 21 my $instance = 0;
3410 21 my $data = '';
3411 21 my $length = undef; # Calculate automatically.
3412  
3413 21 my $max_spid = $_[0];
3414 21 my $num_clusters = $_[1];
3415 21 my $shapes_saved = $_[2];
3416 21 my $drawings_saved = $_[3];
3417 21 my $clusters = $_[4];
3418  
3419 21 $data = pack "VVVV", $max_spid, $num_clusters,
3420 $shapes_saved, $drawings_saved;
3421  
3422 21 for my $aref (@$clusters) {
3423 50 my $drawing_id = $aref->[0];
3424 50 my $shape_ids_used = $aref->[1];
3425  
3426 50 $data .= pack "VV", $drawing_id, $shape_ids_used;
3427 }
3428  
3429  
3430 21 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
3431 }
3432  
3433  
3434 ###############################################################################
3435 #
3436 # _store_mso_bstore_container()
3437 #
3438 # Write the Escher BstoreContainer record that is part of MSODRAWINGGROUP.
3439 #
3440 20 ▶sub _store_mso_bstore_container {
Subroutine _store_mso_bstore_container: called CC=2 SCAR=12.6
3441  
3442 20 my $self = shift;
3443  
3444 20 ▶ return '' unless $self->{_images_size};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_images_size"} 20 0 20
3445  
3446 0 my $type = 0xF001;
3447 0 my $version = 15;
3448 0 my $instance = @{$self->{_images_data}}; # Number of images.
3449 0 my $data = '';
3450 0 my $length = $self->{_images_size} +8 *$instance;
3451  
3452 0 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
3453 }
3454  
3455  
3456  
3457 ###############################################################################
3458 #
3459 # _store_mso_images()
3460 #
3461 # Write the Escher BstoreContainer record that is part of MSODRAWINGGROUP.
3462 #
3463 0 ▶sub _store_mso_images {
Subroutine _store_mso_images: not called CC=1 SCAR=6.9
3464  
3465 0 my $self = shift;
3466  
3467 0 my $ref_count = $_[0];
3468 0 my $image_type = $_[1];
3469 0 my $image = $_[2];
3470 0 my $size = $_[3];
3471 0 my $checksum1 = $_[4];
3472 0 my $checksum2 = $_[5];
3473  
3474 0 my $blip_store_entry = $self->_store_mso_blip_store_entry($ref_count,
3475 $image_type,
3476 $size,
3477 $checksum1);
3478  
3479 0 my $blip = $self->_store_mso_blip($image_type,
3480 $image,
3481 $size,
3482 $checksum1,
3483 $checksum2);
3484  
3485 0 return $blip_store_entry . $blip;
3486 }
3487  
3488  
3489  
3490 ###############################################################################
3491 #
3492 # _store_mso_blip_store_entry()
3493 #
3494 # Write the Escher BlipStoreEntry record that is part of MSODRAWINGGROUP.
3495 #
3496 0 ▶sub _store_mso_blip_store_entry {
Subroutine _store_mso_blip_store_entry: not called CC=1 SCAR=6.9
3497  
3498 0 my $self = shift;
3499  
3500 0 my $ref_count = $_[0];
3501 0 my $image_type = $_[1];
3502 0 my $size = $_[2];
3503 0 my $checksum1 = $_[3];
3504  
3505  
3506 0 my $type = 0xF007;
3507 0 my $version = 2;
3508 0 my $instance = $image_type;
3509 0 my $length = $size +61;
3510 0 my $data = pack('C', $image_type) # Win32
3511 . pack('C', $image_type) # Mac
3512 . pack('H*', $checksum1) # Uid checksum
3513 . pack('v', 0xFF) # Tag
3514 . pack('V', $size +25) # Next Blip size
3515 . pack('V', $ref_count) # Image ref count
3516 . pack('V', 0x00000000) # File offset
3517 . pack('C', 0x00) # Usage
3518 . pack('C', 0x00) # Name length
3519 . pack('C', 0x00) # Unused
3520 . pack('C', 0x00) # Unused
3521 ;
3522  
3523 0 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
3524 }
3525  
3526  
3527 ###############################################################################
3528 #
3529 # _store_mso_blip()
3530 #
3531 # Write the Escher Blip record that is part of MSODRAWINGGROUP.
3532 #
3533 0 ▶sub _store_mso_blip {
Subroutine _store_mso_blip: not called CC=5 SCAR=34.0
3534  
3535 0 my $self = shift;
3536  
3537 0 my $image_type = $_[0];
3538 0 my $image_data = $_[1];
3539 0 my $size = $_[2];
3540 0 my $checksum1 = $_[3];
3541 0 my $checksum2 = $_[4];
3542 0 my $instance;
3543  
3544 0 ▶ $instance = 0x046A if $image_type == 5; # JPG
Branch: 1 branch
BranchTrueFalseTotal
if $image_type == 5 0 0 0
3545 0 ▶ $instance = 0x06E0 if $image_type == 6; # PNG
Branch: 1 branch
BranchTrueFalseTotal
if $image_type == 6 0 0 0
3546 0 ▶ $instance = 0x07A9 if $image_type == 7; # BMP
Branch: 1 branch
BranchTrueFalseTotal
if $image_type == 7 0 0 0
3547  
3548 # BMPs contain an extra checksum for the stripped data.
3549 0 ▶ if ( $image_type == 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($image_type == 7) 0 0 0
3550 0 $checksum1 = $checksum2 . $checksum1;
3551 }
3552  
3553 0 my $type = 0xF018 + $image_type;
3554 0 my $version = 0x0000;
3555 0 my $length = $size +17;
3556 0 my $data = pack('H*', $checksum1) # Uid checksum
3557 . pack('C', 0xFF) # Tag
3558 . $image_data # Image
3559 ;
3560  
3561 0 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
3562 }
3563  
3564  
3565  
3566 ###############################################################################
3567 #
3568 # _store_mso_opt()
3569 #
3570 # Write the Escher Opt record that is part of MSODRAWINGGROUP.
3571 #
3572 21 ▶sub _store_mso_opt {
Subroutine _store_mso_opt: called CC=1 SCAR=0.0
3573  
3574 21 my $self = shift;
3575  
3576 21 my $type = 0xF00B;
3577 21 my $version = 3;
3578 21 my $instance = 3;
3579 21 my $data = '';
3580 21 my $length = 18;
3581  
3582 21 $data = pack "H*", 'BF0008000800810109000008C0014000' .
3583 '0008';
3584  
3585  
3586 21 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
3587 }
3588  
3589  
3590 ###############################################################################
3591 #
3592 # _store_mso_split_menu_colors()
3593 #
3594 # Write the Escher SplitMenuColors record that is part of MSODRAWINGGROUP.
3595 #
3596 21 ▶sub _store_mso_split_menu_colors {
Subroutine _store_mso_split_menu_colors: called CC=1 SCAR=0.0
3597  
3598 21 my $self = shift;
3599  
3600 21 my $type = 0xF11E;
3601 21 my $version = 0;
3602 21 my $instance = 4;
3603 21 my $data = '';
3604 21 my $length = 16;
3605  
3606 21 $data = pack "H*", '0D0000080C00000817000008F7000010';
3607  
3608 21 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
3609 }
3610  
3611  
3612 30 1;
3613  
3614  
3615 __END__
« WriteExcel/Format.pm ↑ Summary WriteExcel/Formula.pm »