WriteExcel/Worksheet.pm

filter: Statementstmt 68.0% 1956 / 2876 Branchbran 52.5% 488 / 928 Conditioncond 44.1% 127 / 288 MC/DCmcdc 30.5% 54 / 177 Subroutinesub 65.9% 122 / 185 Podpod 0.0% 0 / 72 totaltotal 60.6% 2747 / 4526 CC 611 SCAR 33.5
CC
611
Cov
63%
_store_panes
306.0
insert_chart
132.0
write
80.0
CRAP 28.4
« WriteExcel/Formula.pm ↑ Summary WriteExcel/Utility.pm »
1 package Spreadsheet::WriteExcel::Worksheet;
2  
3 ###############################################################################
4 #
5 # Worksheet - A writer class for Excel Worksheets.
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  
15 32 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 32 ▶use Spreadsheet::WriteExcel::BIFFwriter;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 32 ▶use Spreadsheet::WriteExcel::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 32 ▶use Spreadsheet::WriteExcel::Formula;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23  
24 32 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 32 @ISA = qw(Spreadsheet::WriteExcel::BIFFwriter);
26  
27 32 $VERSION = '2.40';
28  
29 ###############################################################################
30 #
31 # new()
32 #
33 # Constructor. Creates a new Worksheet object from a BIFFwriter object
34 #
35 143 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
36  
37 143 my $class = shift;
38 143 my $self = Spreadsheet::WriteExcel::BIFFwriter->new();
39 143 my $rowmax = 65536;
40 143 my $colmax = 256;
41 143 my $strmax = 0;
42  
43 143 $self->{_name} = $_[0];
44 143 $self->{_index} = $_[1];
45 143 $self->{_encoding} = $_[2];
46 143 $self->{_activesheet} = $_[3];
47 143 $self->{_firstsheet} = $_[4];
48 143 $self->{_url_format} = $_[5];
49 143 $self->{_parser} = $_[6];
50 143 $self->{_tempdir} = $_[7];
51  
52 143 $self->{_str_total} = $_[8];
53 143 $self->{_str_unique} = $_[9];
54 143 $self->{_str_table} = $_[10];
55 143 $self->{_1904} = $_[11];
56 143 $self->{_compatibility} = $_[12];
57 143 $self->{_palette} = $_[13];
58  
59 143 $self->{_sheet_type} = 0x0000;
60 143 $self->{_ext_sheets} = [];
61 143 $self->{_using_tmpfile} = 1;
62 143 $self->{_filehandle} = "";
63 143 $self->{_fileclosed} = 0;
64 143 $self->{_offset} = 0;
65 143 $self->{_xls_rowmax} = $rowmax;
66 143 $self->{_xls_colmax} = $colmax;
67 143 $self->{_xls_strmax} = $strmax;
68 143 $self->{_dim_rowmin} = undef;
69 143 $self->{_dim_rowmax} = undef;
70 143 $self->{_dim_colmin} = undef;
71 143 $self->{_dim_colmax} = undef;
72 143 $self->{_colinfo} = [];
73 143 $self->{_selection} = [0, 0];
74 143 $self->{_panes} = [];
75 143 $self->{_active_pane} = 3;
76 143 $self->{_frozen} = 0;
77 143 $self->{_frozen_no_split} = 1;
78 143 $self->{_selected} = 0;
79 143 $self->{_hidden} = 0;
80 143 $self->{_active} = 0;
81 143 $self->{_tab_color} = 0;
82  
83 143 $self->{_first_row} = 0;
84 143 $self->{_first_col} = 0;
85 143 $self->{_display_formulas} = 0;
86 143 $self->{_display_headers} = 1;
87 143 $self->{_display_zeros} = 1;
88 143 $self->{_display_arabic} = 0;
89  
90 143 $self->{_paper_size} = 0x0;
91 143 $self->{_orientation} = 0x1;
92 143 $self->{_header} = '';
93 143 $self->{_footer} = '';
94 143 $self->{_header_encoding} = 0;
95 143 $self->{_footer_encoding} = 0;
96 143 $self->{_hcenter} = 0;
97 143 $self->{_vcenter} = 0;
98 143 $self->{_margin_header} = 0.50;
99 143 $self->{_margin_footer} = 0.50;
100 143 $self->{_margin_left} = 0.75;
101 143 $self->{_margin_right} = 0.75;
102 143 $self->{_margin_top} = 1.00;
103 143 $self->{_margin_bottom} = 1.00;
104  
105 143 $self->{_title_rowmin} = undef;
106 143 $self->{_title_rowmax} = undef;
107 143 $self->{_title_colmin} = undef;
108 143 $self->{_title_colmax} = undef;
109 143 $self->{_print_rowmin} = undef;
110 143 $self->{_print_rowmax} = undef;
111 143 $self->{_print_colmin} = undef;
112 143 $self->{_print_colmax} = undef;
113  
114 143 $self->{_print_gridlines} = 1;
115 143 $self->{_screen_gridlines} = 1;
116 143 $self->{_print_headers} = 0;
117  
118 143 $self->{_page_order} = 0;
119 143 $self->{_black_white} = 0;
120 143 $self->{_draft_quality} = 0;
121 143 $self->{_print_comments} = 0;
122 143 $self->{_page_start} = 1;
123 143 $self->{_custom_start} = 0;
124  
125 143 $self->{_fit_page} = 0;
126 143 $self->{_fit_width} = 0;
127 143 $self->{_fit_height} = 0;
128  
129 143 $self->{_hbreaks} = [];
130 143 $self->{_vbreaks} = [];
131  
132 143 $self->{_protect} = 0;
133 143 $self->{_password} = undef;
134  
135 143 $self->{_col_sizes} = {};
136 143 $self->{_row_sizes} = {};
137  
138 143 $self->{_col_formats} = {};
139 143 $self->{_row_formats} = {};
140  
141 143 $self->{_zoom} = 100;
142 143 $self->{_print_scale} = 100;
143 143 $self->{_page_view} = 0;
144  
145 143 $self->{_leading_zeros} = 0;
146  
147 143 $self->{_outline_row_level} = 0;
148 143 $self->{_outline_style} = 0;
149 143 $self->{_outline_below} = 1;
150 143 $self->{_outline_right} = 1;
151 143 $self->{_outline_on} = 1;
152  
153 143 $self->{_write_match} = [];
154  
155 143 $self->{_object_ids} = [];
156 143 $self->{_images} = {};
157 143 $self->{_images_array} = [];
158 143 $self->{_charts} = {};
159 143 $self->{_charts_array} = [];
160 143 $self->{_comments} = {};
161 143 $self->{_comments_array} = [];
162 143 $self->{_comments_author} = '';
163 143 $self->{_comments_author_enc} = 0;
164 143 $self->{_comments_visible} = 0;
165  
166 143 $self->{_filter_area} = [];
167 143 $self->{_filter_count} = 0;
168 143 $self->{_filter_on} = 0;
169  
170 143 $self->{_writing_url} = 0;
171  
172 143 $self->{_db_indices} = [];
173  
174 143 $self->{_validations} = [];
175  
176 143 bless $self, $class;
177 143 $self->_initialize();
178 143 return $self;
179 }
180  
181  
182 ###############################################################################
183 #
184 # _initialize()
185 #
186 # Open a tmp file to store the majority of the Worksheet data. If this fails,
187 # for example due to write permissions, store the data in memory. This can be
188 # slow for large files.
189 #
190 143 ▶sub _initialize {
Subroutine _initialize: called CC=10 SCAR=38.5
191  
192 143 my $self = shift;
193 143 my $fh;
194 my $tmp_dir;
195  
196 # The following code is complicated by Windows limitations. Porters can
197 # choose a more direct method.
198  
199  
200  
201 # In the default case we use IO::File->new_tmpfile(). This may fail, in
202 # particular with IIS on Windows, so we allow the user to specify a temp
203 # directory via File::Temp.
204 #
205 143 ▶ if (defined $self->{_tempdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_tempdir'}) { } 0 143 143
206  
207 # Delay loading File:Temp to reduce the module dependencies.
208 0 eval { require File::Temp };
209 0 ▶ die "The File::Temp module must be installed in order ".
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
210 "to call set_tempdir().\n" if $@;
211  
212  
213 # Trap but ignore File::Temp errors.
214 0 eval { $fh = File::Temp::tempfile(DIR => $self->{_tempdir}) };
215  
216 # Store the failed tmp dir in case of errors.
217 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
218 }
219 else {
220  
221 143 $fh = IO::File->new_tmpfile();
222  
223 # Store the failed tmp dir in case of errors.
224 143 ▶ $tmp_dir = "POSIX::tmpnam() directory" if not $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 143 143
225 }
226  
227  
228 # Check if the temp file creation was successful. Else store data in memory.
229 143 ▶ if ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) { } 143 0 143
230  
231 # binmode file whether platform requires it or not.
232 143 binmode($fh);
233  
234 # Store filehandle
235 143 $self->{_filehandle} = $fh;
236 }
237 else {
238  
239 # Set flag to store data in memory if XX::tempfile() failed.
240 0 $self->{_using_tmpfile} = 0;
241  
242 0 ▶ if ($self->{_index} == 0 && $^W) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_index"} == 0 and $^W) 0 0 0
Condition0%0/3
$self->{"_index"} == 0 and $^W
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_index"} == 0 and $^W
$self->{"_index"} == 0 $^W
Truth table0%0/3
A and B
A= $self->{"_index"} == 0B= $^W
ABresult
0X0
100
111
243 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
244  
245 0 warn "Unable to create temp files in $tmp_dir. Data will be ".
246 "stored in memory. Refer to set_tempdir() in the ".
247 "Spreadsheet::WriteExcel documentation.\n" ;
248 }
249 }
250 }
251  
252  
253 ###############################################################################
254 #
255 # _close()
256 #
257 # Add data to the beginning of the workbook (note the reverse order)
258 # and to the end of the workbook.
259 #
260 133 ▶sub _close {
Subroutine _close: called CC=4 SCAR=13.9
261  
262 133 my $self = shift;
263  
264 ################################################
265 # Prepend in reverse order!!
266 #
267  
268 # Prepend the sheet dimensions
269 133 $self->_store_dimensions();
270  
271 # Prepend the autofilter filters.
272 133 $self->_store_autofilters;
273  
274 # Prepend the sheet autofilter info.
275 133 $self->_store_autofilterinfo();
276  
277 # Prepend the sheet filtermode record.
278 133 $self->_store_filtermode();
279  
280 # Prepend the COLINFO records if they exist
281 133 ▶ if (@{$self->{_colinfo}}){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_colinfo"};}) 4 129 133
282 4 my @colinfo = @{$self->{_colinfo}};
283 4 ▶ while (@colinfo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@colinfo) 5 4 9
284 5 my $arrayref = pop @colinfo;
285 5 $self->_store_colinfo(@$arrayref);
286 }
287 }
288  
289 # Prepend the DEFCOLWIDTH record
290 133 $self->_store_defcol();
291  
292 # Prepend the sheet password
293 133 $self->_store_password();
294  
295 # Prepend the sheet protection
296 133 $self->_store_protect();
297 133 $self->_store_obj_protect();
298  
299 # Prepend the page setup
300 133 $self->_store_setup();
301  
302 # Prepend the bottom margin
303 133 $self->_store_margin_bottom();
304  
305 # Prepend the top margin
306 133 $self->_store_margin_top();
307  
308 # Prepend the right margin
309 133 $self->_store_margin_right();
310  
311 # Prepend the left margin
312 133 $self->_store_margin_left();
313  
314 # Prepend the page vertical centering
315 133 $self->_store_vcenter();
316  
317 # Prepend the page horizontal centering
318 133 $self->_store_hcenter();
319  
320 # Prepend the page footer
321 133 $self->_store_footer();
322  
323 # Prepend the page header
324 133 $self->_store_header();
325  
326 # Prepend the vertical page breaks
327 133 $self->_store_vbreak();
328  
329 # Prepend the horizontal page breaks
330 133 $self->_store_hbreak();
331  
332 # Prepend WSBOOL
333 133 $self->_store_wsbool();
334  
335 # Prepend the default row height.
336 133 $self->_store_defrow();
337  
338 # Prepend GUTS
339 133 $self->_store_guts();
340  
341 # Prepend GRIDSET
342 133 $self->_store_gridset();
343  
344 # Prepend PRINTGRIDLINES
345 133 $self->_store_print_gridlines();
346  
347 # Prepend PRINTHEADERS
348 133 $self->_store_print_headers();
349  
350 #
351 # End of prepend. Read upwards from here.
352 ################################################
353  
354 # Append
355 133 $self->_store_table();
356 133 $self->_store_images();
357 133 $self->_store_charts();
358 133 $self->_store_filters();
359 133 $self->_store_comments();
360 133 $self->_store_window2();
361 133 $self->_store_page_view();
362 133 $self->_store_zoom();
363 0 ▶ $self->_store_panes(@{$self->{_panes}}) if @{$self->{_panes}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"_panes"};} 0 133 133
364 133 $self->_store_selection(@{$self->{_selection}});
365 133 $self->_store_validation_count();
366 133 $self->_store_validations();
367 133 $self->_store_tab_color();
368 133 $self->_store_eof();
369  
370 # Prepend the BOF and INDEX records
371 133 $self->_store_index();
372 133 $self->_store_bof(0x0010);
373 }
374  
375  
376 ###############################################################################
377 #
378 # _compatibility_mode()
379 #
380 # Set the compatibility mode.
381 #
382 # See the explanation in Workbook::compatibility_mode(). This private method
383 # is mainly used for test purposes.
384 #
385 0 ▶sub _compatibility_mode {
Subroutine _compatibility_mode: not called CC=2 SCAR=17.9
386  
387 0 my $self = shift;
388  
389 0 ▶ if (defined($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
390 0 $self->{_compatibility} = $_[0];
391 }
392 else {
393 0 $self->{_compatibility} = 1;
394 }
395 }
396  
397  
398 ###############################################################################
399 #
400 # get_name().
401 #
402 # Retrieve the worksheet name.
403 #
404 # Note, there is no set_name() method because names are used in formulas and
405 # converted to internal indices. Allowing the user to change sheet names
406 # after they have been set in add_worksheet() is asking for trouble.
407 #
408 0 ▶sub get_name {
Subroutine get_name: not called CC=1 SCAR=6.9
Pod undocumented
409  
410 0 my $self = shift;
411  
412 0 return $self->{_name};
413 }
414  
415  
416 ###############################################################################
417 #
418 # get_data().
419 #
420 # Retrieves data from memory in one chunk, or from disk in $buffer
421 # sized chunks.
422 #
423 402 ▶sub get_data {
Subroutine get_data: called CC=5 SCAR=16.1
Pod undocumented
424  
425 402 my $self = shift;
426 402 my $buffer = 4096;
427 402 my $tmp;
428  
429 # Return data stored in memory
430 402 ▶ if (defined $self->{_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_data"}) 135 267 402
431 135 $tmp = $self->{_data};
432 135 $self->{_data} = undef;
433 135 my $fh = $self->{_filehandle};
434 135 ▶ seek($fh, 0, 0) if $self->{_using_tmpfile};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_using_tmpfile"} 132 3 135
435 135 return $tmp;
436 }
437  
438 # Return data stored on disk
439 267 ▶ if ($self->{_using_tmpfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_using_tmpfile"}) 264 3 267
440 264 ▶ return $tmp if read($self->{_filehandle}, $tmp, $buffer);
Branch: 1 branch
BranchTrueFalseTotal
if read $self->{"_filehandle"}, $tmp, $buffer 132 132 264
441 }
442  
443 # No data to return
444 135 return undef;
445 }
446  
447  
448 ###############################################################################
449 #
450 # select()
451 #
452 # Set this worksheet as a selected worksheet, i.e. the worksheet has its tab
453 # highlighted.
454 #
455 0 ▶sub select {
Subroutine select: not called CC=1 SCAR=6.9
Pod undocumented
456  
457 0 my $self = shift;
458  
459 0 $self->{_hidden} = 0; # Selected worksheet can't be hidden.
460 0 $self->{_selected} = 1;
461 }
462  
463  
464 ###############################################################################
465 #
466 # activate()
467 #
468 # Set this worksheet as the active worksheet, i.e. the worksheet that is
469 # displayed when the workbook is opened. Also set it as selected.
470 #
471 0 ▶sub activate {
Subroutine activate: not called CC=1 SCAR=6.9
Pod undocumented
472  
473 0 my $self = shift;
474  
475 0 $self->{_hidden} = 0; # Active worksheet can't be hidden.
476 0 $self->{_selected} = 1;
477 0 ${$self->{_activesheet}} = $self->{_index};
478 }
479  
480  
481 ###############################################################################
482 #
483 # hide()
484 #
485 # Hide this worksheet.
486 #
487 0 ▶sub hide {
Subroutine hide: not called CC=1 SCAR=6.9
Pod undocumented
488  
489 0 my $self = shift;
490  
491 0 $self->{_hidden} = 1;
492  
493 # A hidden worksheet shouldn't be active or selected.
494 0 $self->{_selected} = 0;
495 0 ${$self->{_activesheet}} = 0;
496 0 ${$self->{_firstsheet}} = 0;
497 }
498  
499  
500 ###############################################################################
501 #
502 # set_first_sheet()
503 #
504 # Set this worksheet as the first visible sheet. This is necessary
505 # when there are a large number of worksheets and the activated
506 # worksheet is not visible on the screen.
507 #
508 0 ▶sub set_first_sheet {
Subroutine set_first_sheet: not called CC=1 SCAR=6.9
Pod undocumented
509  
510 0 my $self = shift;
511  
512 0 $self->{_hidden} = 0; # Active worksheet can't be hidden.
513 0 ${$self->{_firstsheet}} = $self->{_index};
514 }
515  
516  
517 ###############################################################################
518 #
519 # protect($password)
520 #
521 # Set the worksheet protection flag to prevent accidental modification and to
522 # hide formulas if the locked and hidden format properties have been set.
523 #
524 0 ▶sub protect {
Subroutine protect: not called CC=2 SCAR=17.9
Pod undocumented
525  
526 0 my $self = shift;
527  
528 0 $self->{_protect} = 1;
529 0 ▶ $self->{_password} = $self->_encode_password($_[0]) if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 0 0
530  
531 }
532  
533  
534 ###############################################################################
535 #
536 # set_column($firstcol, $lastcol, $width, $format, $hidden, $level)
537 #
538 # Set the width of a single column or a range of columns.
539 # See also: _store_colinfo
540 #
541 5 ▶sub set_column {
Subroutine set_column: called CC=13 SCAR=26.4
Pod undocumented
542  
543 5 my $self = shift;
544 5 my @data = @_;
545 5 my $cell = $data[0];
546  
547 # Check for a cell reference in A1 notation and substitute row and column
548 5 ▶ if ($cell =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell =~ /^\D/) 3 2 5
549 3 @data = $self->_substitute_cellref(@_);
550  
551 # Returned values $row1 and $row2 aren't required here. Remove them.
552 3 shift @data; # $row1
553 3 splice @data, 1, 1; # $row2
554 }
555  
556 5 ▶ return if @data < 3; # Ensure at least $firstcol, $lastcol and $width
Branch: 1 branch
BranchTrueFalseTotal
if @data < 3 0 5 5
557 5 ▶ return if not defined $data[0]; # Columns must be defined.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data[0] 0 5 5
558 5 ▶ return if not defined $data[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data[1] 0 5 5
559  
560 # Assume second column is the same as first if 0. Avoids KB918419 bug.
561 5 ▶ $data[1] = $data[0] if $data[1] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $data[1] == 0 0 5 5
562  
563 # Ensure 2nd col is larger than first. Also for KB918419 bug.
564 5 ▶ ($data[0], $data[1]) = ($data[1], $data[0]) if $data[0] > $data[1];
Branch: 1 branch
BranchTrueFalseTotal
if $data[0] > $data[1] 0 5 5
565  
566 # Limit columns to Excel max of 255.
567 5 ▶ $data[0] = 255 if $data[0] > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $data[0] > 255 0 5 5
568 5 ▶ $data[1] = 255 if $data[1] > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $data[1] > 255 0 5 5
569  
570 5 push @{$self->{_colinfo}}, [ @data ];
571  
572  
573 # Store the col sizes for use when calculating image vertices taking
574 # hidden columns into account. Also store the column formats.
575 #
576 5 ▶ my $width = $data[4] ? 0 : $data[2]; # Set width to zero if col is hidden
Branch: 1 branch
BranchTrueFalseTotal
$data[4] ? : 0 5 5
577 5 ▶ $width ||= 0; # Ensure width isn't undef.
Condition100%2/2
$width ||= 0
l!l
41
MC/DC100%1/1
$width ||= 0
$width
Truth table100%2/2
A
A= $width
Aresult
11
00
578 5 my $format = $data[3];
579  
580 5 my ($firstcol, $lastcol) = @data;
581  
582 5 foreach my $col ($firstcol .. $lastcol) {
583 10 $self->{_col_sizes}->{$col} = $width;
584 10 ▶ $self->{_col_formats}->{$col} = $format if defined $format;
Branch: 1 branch
BranchTrueFalseTotal
if defined $format 1 9 10
585 }
586 }
587  
588  
589 ###############################################################################
590 #
591 # set_selection()
592 #
593 # Set which cell or cells are selected in a worksheet: see also the
594 # sub _store_selection
595 #
596 0 ▶sub set_selection {
Subroutine set_selection: not called CC=2 SCAR=17.9
Pod undocumented
597  
598 0 my $self = shift;
599  
600 # Check for a cell reference in A1 notation and substitute row and column
601 0 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 0 0
602 0 @_ = $self->_substitute_cellref(@_);
603 }
604  
605 0 $self->{_selection} = [ @_ ];
606 }
607  
608  
609 ###############################################################################
610 #
611 # freeze_panes()
612 #
613 # Set panes and mark them as frozen. See also _store_panes().
614 #
615 0 ▶sub freeze_panes {
Subroutine freeze_panes: not called CC=3 SCAR=24.8
Pod undocumented
616  
617 0 my $self = shift;
618  
619 # Check for a cell reference in A1 notation and substitute row and column
620 0 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 0 0
621 0 @_ = $self->_substitute_cellref(@_);
622 }
623  
624 # Extra flag indicated a split and freeze.
625 0 ▶ $self->{_frozen_no_split} = 0 if $_[4];
Branch: 1 branch
BranchTrueFalseTotal
if $_[4] 0 0 0
626  
627 0 $self->{_frozen} = 1;
628 0 $self->{_panes} = [ @_ ];
629 }
630  
631  
632 ###############################################################################
633 #
634 # split_panes()
635 #
636 # Set panes and mark them as split. See also _store_panes().
637 #
638 0 ▶sub split_panes {
Subroutine split_panes: not called CC=1 SCAR=6.9
Pod undocumented
639  
640 0 my $self = shift;
641  
642 0 $self->{_frozen} = 0;
643 0 $self->{_frozen_no_split} = 0;
644 0 $self->{_panes} = [ @_ ];
645 }
646  
647 # Older method name for backwards compatibility.
648 32 *thaw_panes = *split_panes;
649  
650  
651 ###############################################################################
652 #
653 # set_portrait()
654 #
655 # Set the page orientation as portrait.
656 #
657 0 ▶sub set_portrait {
Subroutine set_portrait: not called CC=1 SCAR=6.9
Pod undocumented
658  
659 0 my $self = shift;
660  
661 0 $self->{_orientation} = 1;
662 }
663  
664  
665 ###############################################################################
666 #
667 # set_landscape()
668 #
669 # Set the page orientation as landscape.
670 #
671 0 ▶sub set_landscape {
Subroutine set_landscape: not called CC=1 SCAR=6.9
Pod undocumented
672  
673 0 my $self = shift;
674  
675 0 $self->{_orientation} = 0;
676 }
677  
678  
679 ###############################################################################
680 #
681 # set_page_view()
682 #
683 # Set the page view mode for Mac Excel.
684 #
685 0 ▶sub set_page_view {
Subroutine set_page_view: not called CC=2 SCAR=17.9
Pod undocumented
686  
687 0 my $self = shift;
688  
689 0 ▶ $self->{_page_view} = defined $_[0] ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
690 }
691  
692  
693 ###############################################################################
694 #
695 # set_tab_color()
696 #
697 # Set the colour of the worksheet colour.
698 #
699 0 ▶sub set_tab_color {
Subroutine set_tab_color: not called CC=2 SCAR=17.9
Pod undocumented
700  
701 0 my $self = shift;
702  
703 0 my $color = &Spreadsheet::WriteExcel::Format::_get_color($_[0]);
704 0 ▶ $color = 0 if $color == 0x7FFF; # Default color.
Branch: 1 branch
BranchTrueFalseTotal
if $color == 32767 0 0 0
705  
706 0 $self->{_tab_color} = $color;
707 }
708  
709  
710 ###############################################################################
711 #
712 # set_paper()
713 #
714 # Set the paper type. Ex. 1 = US Letter, 9 = A4
715 #
716 0 ▶sub set_paper {
Subroutine set_paper: not called CC=2 SCAR=17.9
Pod undocumented
717  
718 0 my $self = shift;
719  
720 0 ▶ $self->{_paper_size} = $_[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
721 }
722  
723  
724 ###############################################################################
725 #
726 # set_header()
727 #
728 # Set the page header caption and optional margin.
729 #
730 0 ▶sub set_header {
Subroutine set_header: not called CC=8 SCAR=42.8
Pod undocumented
731  
732 0 my $self = shift;
733 0 ▶ my $string = $_[0] || '';
Condition0%0/2
$_[0] || ''
l!l
00
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
734 0 ▶ my $margin = $_[1] || 0.50;
Condition0%0/2
$_[1] || 0.5
l!l
00
MC/DC0%0/1
$_[1] || 0.5
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
735 0 ▶ my $encoding = $_[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
736  
737 # Handle utf8 strings in perl 5.8.
738 0 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 0 0 0
739 0 require Encode;
740  
741 0 ▶ if (Encode::is_utf8($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($string)) 0 0 0
742 0 $string = Encode::encode("UTF-16BE", $string);
743 0 $encoding = 1;
744 }
745 }
746  
747 0 ▶ my $limit = $encoding ? 255 *2 : 255;
Branch: 1 branch
BranchTrueFalseTotal
$encoding ? : 0 0 0
748  
749 0 ▶ if (length $string >= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string >= $limit) 0 0 0
750 0 carp 'Header string must be less than 255 characters';
751 0 return;
752 }
753  
754 0 $self->{_header} = $string;
755 0 $self->{_margin_header} = $margin;
756 0 $self->{_header_encoding} = $encoding;
757 }
758  
759  
760 ###############################################################################
761 #
762 # set_footer()
763 #
764 # Set the page footer caption and optional margin.
765 #
766 0 ▶sub set_footer {
Subroutine set_footer: not called CC=8 SCAR=42.8
Pod undocumented
767  
768 0 my $self = shift;
769 0 ▶ my $string = $_[0] || '';
Condition0%0/2
$_[0] || ''
l!l
00
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
770 0 ▶ my $margin = $_[1] || 0.50;
Condition0%0/2
$_[1] || 0.5
l!l
00
MC/DC0%0/1
$_[1] || 0.5
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
771 0 ▶ my $encoding = $_[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
772  
773 # Handle utf8 strings in perl 5.8.
774 0 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 0 0 0
775 0 require Encode;
776  
777 0 ▶ if (Encode::is_utf8($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($string)) 0 0 0
778 0 $string = Encode::encode("UTF-16BE", $string);
779 0 $encoding = 1;
780 }
781 }
782  
783 0 ▶ my $limit = $encoding ? 255 *2 : 255;
Branch: 1 branch
BranchTrueFalseTotal
$encoding ? : 0 0 0
784  
785  
786 0 ▶ if (length $string >= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string >= $limit) 0 0 0
787 0 carp 'Footer string must be less than 255 characters';
788 0 return;
789 }
790  
791 0 $self->{_footer} = $string;
792 0 $self->{_margin_footer} = $margin;
793 0 $self->{_footer_encoding} = $encoding;
794 }
795  
796  
797 ###############################################################################
798 #
799 # center_horizontally()
800 #
801 # Center the page horizontally.
802 #
803 0 ▶sub center_horizontally {
Subroutine center_horizontally: not called CC=2 SCAR=17.9
Pod undocumented
804  
805 0 my $self = shift;
806  
807 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
808 0 $self->{_hcenter} = $_[0];
809 }
810 else {
811 0 $self->{_hcenter} = 1;
812 }
813 }
814  
815  
816 ###############################################################################
817 #
818 # center_vertically()
819 #
820 # Center the page horizontally.
821 #
822 0 ▶sub center_vertically {
Subroutine center_vertically: not called CC=2 SCAR=17.9
Pod undocumented
823  
824 0 my $self = shift;
825  
826 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
827 0 $self->{_vcenter} = $_[0];
828 }
829 else {
830 0 $self->{_vcenter} = 1;
831 }
832 }
833  
834  
835 ###############################################################################
836 #
837 # set_margins()
838 #
839 # Set all the page margins to the same value in inches.
840 #
841 0 ▶sub set_margins {
Subroutine set_margins: not called CC=1 SCAR=6.9
Pod undocumented
842  
843 0 my $self = shift;
844  
845 0 $self->set_margin_left($_[0]);
846 0 $self->set_margin_right($_[0]);
847 0 $self->set_margin_top($_[0]);
848 0 $self->set_margin_bottom($_[0]);
849 }
850  
851  
852 ###############################################################################
853 #
854 # set_margins_LR()
855 #
856 # Set the left and right margins to the same value in inches.
857 #
858 0 ▶sub set_margins_LR {
Subroutine set_margins_LR: not called CC=1 SCAR=6.9
Pod undocumented
859  
860 0 my $self = shift;
861  
862 0 $self->set_margin_left($_[0]);
863 0 $self->set_margin_right($_[0]);
864 }
865  
866  
867 ###############################################################################
868 #
869 # set_margins_TB()
870 #
871 # Set the top and bottom margins to the same value in inches.
872 #
873 0 ▶sub set_margins_TB {
Subroutine set_margins_TB: not called CC=1 SCAR=6.9
Pod undocumented
874  
875 0 my $self = shift;
876  
877 0 $self->set_margin_top($_[0]);
878 0 $self->set_margin_bottom($_[0]);
879 }
880  
881  
882 ###############################################################################
883 #
884 # set_margin_left()
885 #
886 # Set the left margin in inches.
887 #
888 0 ▶sub set_margin_left {
Subroutine set_margin_left: not called CC=2 SCAR=17.9
Pod undocumented
889  
890 0 my $self = shift;
891  
892 0 ▶ $self->{_margin_left} = defined $_[0] ? $_[0] : 0.75;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
893 }
894  
895  
896 ###############################################################################
897 #
898 # set_margin_right()
899 #
900 # Set the right margin in inches.
901 #
902 0 ▶sub set_margin_right {
Subroutine set_margin_right: not called CC=2 SCAR=17.9
Pod undocumented
903  
904 0 my $self = shift;
905  
906 0 ▶ $self->{_margin_right} = defined $_[0] ? $_[0] : 0.75;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
907 }
908  
909  
910 ###############################################################################
911 #
912 # set_margin_top()
913 #
914 # Set the top margin in inches.
915 #
916 0 ▶sub set_margin_top {
Subroutine set_margin_top: not called CC=2 SCAR=17.9
Pod undocumented
917  
918 0 my $self = shift;
919  
920 0 ▶ $self->{_margin_top} = defined $_[0] ? $_[0] : 1.00;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
921 }
922  
923  
924 ###############################################################################
925 #
926 # set_margin_bottom()
927 #
928 # Set the bottom margin in inches.
929 #
930 0 ▶sub set_margin_bottom {
Subroutine set_margin_bottom: not called CC=2 SCAR=17.9
Pod undocumented
931  
932 0 my $self = shift;
933  
934 0 ▶ $self->{_margin_bottom} = defined $_[0] ? $_[0] : 1.00;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
935 }
936  
937  
938 ###############################################################################
939 #
940 # repeat_rows($first_row, $last_row)
941 #
942 # Set the rows to repeat at the top of each printed page. See also the
943 # _store_name_xxxx() methods in Workbook.pm.
944 #
945 9 ▶sub repeat_rows {
Subroutine repeat_rows: called CC=2 SCAR=7.0
Pod undocumented
946  
947 9 my $self = shift;
948  
949 9 $self->{_title_rowmin} = $_[0];
950 9 ▶ $self->{_title_rowmax} = $_[1] || $_[0]; # Second row is optional
Condition66%2/3
$_[1] || $_[0]
l!l&&r!l&&!r
801
MC/DC50%1/2
$_[1] || $_[0]
$_[1] $_[0]
Truth table66%2/3
A || B
A= $_[1]B= $_[0]
ABresult
1X1
011
000
951 }
952  
953  
954 ###############################################################################
955 #
956 # repeat_columns($first_col, $last_col)
957 #
958 # Set the columns to repeat at the left hand side of each printed page.
959 # See also the _store_names() methods in Workbook.pm.
960 #
961 5 ▶sub repeat_columns {
Subroutine repeat_columns: called CC=3 SCAR=11.0
Pod undocumented
962  
963 5 my $self = shift;
964  
965 # Check for a cell reference in A1 notation and substitute row and column
966 5 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 2 3 5
967 2 @_ = $self->_substitute_cellref(@_);
968  
969 # Returned values $row1 and $row2 aren't required here. Remove them.
970 2 shift @_; # $row1
971 2 splice @_, 1, 1; # $row2
972 }
973  
974 5 $self->{_title_colmin} = $_[0];
975 5 ▶ $self->{_title_colmax} = $_[1] || $_[0]; # Second col is optional
Condition66%2/3
$_[1] || $_[0]
l!l&&r!l&&!r
302
MC/DC50%1/2
$_[1] || $_[0]
$_[1] $_[0]
Truth table66%2/3
A || B
A= $_[1]B= $_[0]
ABresult
1X1
011
000
976 }
977  
978  
979 ###############################################################################
980 #
981 # print_area($first_row, $first_col, $last_row, $last_col)
982 #
983 # Set the area of each worksheet that will be printed. See also the
984 # _store_names() methods in Workbook.pm.
985 #
986 10 ▶sub print_area {
Subroutine print_area: called CC=3 SCAR=11.1
Pod undocumented
987  
988 10 my $self = shift;
989  
990 # Check for a cell reference in A1 notation and substitute row and column
991 10 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 10 0 10
992 10 @_ = $self->_substitute_cellref(@_);
993 }
994  
995 10 ▶ return if @_ != 4; # Require 4 parameters
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 4 0 10 10
996  
997 10 $self->{_print_rowmin} = $_[0];
998 10 $self->{_print_colmin} = $_[1];
999 10 $self->{_print_rowmax} = $_[2];
1000 10 $self->{_print_colmax} = $_[3];
1001 }
1002  
1003  
1004 ###############################################################################
1005 #
1006 # autofilter($first_row, $first_col, $last_row, $last_col)
1007 #
1008 # Set the autofilter area in the worksheet.
1009 #
1010 3 ▶sub autofilter {
Subroutine autofilter: called CC=5 SCAR=16.7
Pod undocumented
1011  
1012 3 my $self = shift;
1013  
1014 # Check for a cell reference in A1 notation and substitute row and column
1015 3 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 3 0 3
1016 3 @_ = $self->_substitute_cellref(@_);
1017 }
1018  
1019 3 ▶ return if @_ != 4; # Require 4 parameters
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 4 0 3 3
1020  
1021 3 my ($row1, $col1, $row2, $col2) = @_;
1022  
1023 # Reverse max and min values if necessary.
1024 3 ▶ ($row1, $row2) = ($row2, $row1) if $row2 < $row1;
Branch: 1 branch
BranchTrueFalseTotal
if $row2 < $row1 0 3 3
1025 3 ▶ ($col1, $col2) = ($col2, $col1) if $col2 < $col1;
Branch: 1 branch
BranchTrueFalseTotal
if $col2 < $col1 0 3 3
1026  
1027 # Store the Autofilter information
1028 3 $self->{_filter_area} = [$row1, $row2, $col1, $col2];
1029 3 $self->{_filter_count} = 1+ $col2 -$col1;
1030 }
1031  
1032  
1033 ###############################################################################
1034 #
1035 # filter_column($column, $criteria, ...)
1036 #
1037 # Set the column filter criteria.
1038 #
1039 0 ▶sub filter_column {
Subroutine filter_column: not called CC=8 SCAR=42.8
Pod undocumented
1040  
1041 0 my $self = shift;
1042 0 my $col = $_[0];
1043 0 my $expression = $_[1];
1044  
1045  
1046 croak "Must call autofilter() before filter_column()"
1047 0 ▶ unless $self->{_filter_count};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_filter_count"} 0 0 0
1048 0 ▶ croak "Incorrect number of arguments to filter_column()" unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 0 0
1049  
1050  
1051 # Check for a column reference in A1 notation and substitute.
1052 0 ▶ if ($col =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col =~ /^\D/) 0 0 0
1053 # Convert col ref to a cell ref and then to a col number.
1054 0 (undef, $col) = $self->_substitute_cellref($col . '1');
1055 }
1056  
1057 0 my (undef, undef, $col_first, $col_last) = @{$self->{_filter_area}};
1058  
1059 # Reject column if it is outside filter range.
1060 0 ▶ if ($col < $col_first or $col > $col_last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < $col_first or $col > $col_last) 0 0 0
Condition0%0/3
$col < $col_first or $col > $col_last
l!l&&r!l&&!r
000
MC/DC0%0/2
$col < $col_first or $col > $col_last
$col < $col_first $col > $col_last
Truth table0%0/3
A or B
A= $col < $col_firstB= $col > $col_last
ABresult
1X1
011
000
1061 0 croak "Column '$col' outside autofilter() column range " .
1062 "($col_first .. $col_last)";
1063 }
1064  
1065  
1066 0 my @tokens = $self->_extract_filter_tokens($expression);
1067  
1068 0 ▶ croak "Incorrect number of tokens in expression '$expression'"
Branch: 1 branch
BranchTrueFalseTotal
unless @tokens == 3 or @tokens == 7 0 0 0
Condition0%0/3
@tokens == 3 or @tokens == 7
l!l&&r!l&&!r
000
MC/DC0%0/2
@tokens == 3 or @tokens == 7
@tokens == 3 @tokens == 7
Truth table0%0/3
A or B
A= @tokens == 3B= @tokens == 7
ABresult
1X1
011
000
1069 unless (@tokens == 3 or @tokens == 7);
1070  
1071  
1072 0 @tokens = $self->_parse_filter_expression($expression, @tokens);
1073  
1074 0 $self->{_filter_cols}->{$col} = [@tokens];
1075 0 $self->{_filter_on} = 1;
1076 }
1077  
1078  
1079 ###############################################################################
1080 #
1081 # _extract_filter_tokens($expression)
1082 #
1083 # Extract the tokens from the filter expression. The tokens are mainly non-
1084 # whitespace groups. The only tricky part is to extract string tokens that
1085 # contain whitespace and/or quoted double quotes (Excel's escaped quotes).
1086 #
1087 # Examples: 'x < 2000'
1088 # 'x > 2000 and x < 5000'
1089 # 'x = "foo"'
1090 # 'x = "foo bar"'
1091 # 'x = "foo "" bar"'
1092 #
1093 71 ▶sub _extract_filter_tokens {
Subroutine _extract_filter_tokens: called CC=3 SCAR=11.0
1094  
1095 71 my $self = shift;
1096 71 my $expression = $_[0];
1097  
1098 71 ▶ return unless $expression;
Branch: 1 branch
BranchTrueFalseTotal
unless $expression 2 69 71
1099  
1100 69 my @tokens = ($expression =~ /"(?:[^"]|"")*"|\S+/g); #"
1101  
1102 # Remove leading and trailing quotes and unescape other quotes
1103 69 for (@tokens) {
1104 247 s/^"//; #"
1105 247 s/"$//; #"
1106 247 s/""/"/g; #"
1107 }
1108  
1109 69 return @tokens;
1110 }
1111  
1112  
1113 ###############################################################################
1114 #
1115 # _parse_filter_expression(@token)
1116 #
1117 # Converts the tokens of a possibly conditional expression into 1 or 2
1118 # sub expressions for further parsing.
1119 #
1120 # Examples:
1121 # ('x', '==', 2000) -> exp1
1122 # ('x', '>', 2000, 'and', 'x', '<', 5000) -> exp1 and exp2
1123 #
1124 53 ▶sub _parse_filter_expression {
Subroutine _parse_filter_expression: called CC=4 SCAR=13.9
1125  
1126 53 my $self = shift;
1127 53 my $expression = shift;
1128 53 my @tokens = @_;
1129  
1130 # The number of tokens will be either 3 (for 1 expression)
1131 # or 7 (for 2 expressions).
1132 #
1133 53 ▶ if (@tokens == 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens == 7) { } 7 46 53
1134  
1135 7 my $conditional = $tokens[3];
1136  
1137 7 ▶ if ($conditional =~ /^(and|&&)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($conditional =~ /^(and|&&)$/) { } 4 3 7
elsif ($conditional =~ /^(or|\|\|)$/) { } 3 0 3
1138 4 $conditional = 0;
1139 }
1140 elsif ($conditional =~ /^(or|\|\|)$/) {
1141 3 $conditional = 1;
1142 }
1143 else {
1144 0 croak "Token '$conditional' is not a valid conditional " .
1145 "in filter expression '$expression'";
1146 }
1147  
1148 7 my @expression_1 = $self->_parse_filter_tokens($expression,
1149 @tokens[0, 1, 2]);
1150 7 my @expression_2 = $self->_parse_filter_tokens($expression,
1151 @tokens[4, 5, 6]);
1152  
1153 7 return (@expression_1, $conditional, @expression_2);
1154 }
1155 else {
1156 46 return $self->_parse_filter_tokens($expression, @tokens);
1157 }
1158 }
1159  
1160  
1161 ###############################################################################
1162 #
1163 # _parse_filter_tokens(@token)
1164 #
1165 # Parse the 3 tokens of a filter expression and return the operator and token.
1166 #
1167 60 ▶sub _parse_filter_tokens {
Subroutine _parse_filter_tokens: called CC=19 SCAR=31.0
1168  
1169 60 my $self = shift;
1170 60 my $expression = shift;
1171 60 my @tokens = @_;
1172  
1173 60 my %operators = (
1174 '==' => 2,
1175 '=' => 2,
1176 '=~' => 2,
1177 'eq' => 2,
1178  
1179 '!=' => 5,
1180 '!~' => 5,
1181 'ne' => 5,
1182 '<>' => 5,
1183  
1184 '<' => 1,
1185 '<=' => 3,
1186 '>' => 4,
1187 '>=' => 6,
1188 );
1189  
1190 60 my $operator = $operators{$tokens[1]};
1191 60 my $token = $tokens[2];
1192  
1193  
1194 # Special handling of "Top" filter expressions.
1195 60 ▶ if ($tokens[0] =~ /^top|bottom$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens[0] =~ /^top|bottom$/i) 11 49 60
1196  
1197 11 my $value = $tokens[1];
1198  
1199 11 ▶ if ($value =~ /\D/ or
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\D/ or $value < 1 or $value > 500) 0 11 11
Condition33%2/6
$value =~ /\D/ or $value < 1 or $value > 500
l!l&&r!l&&!r
0011
$value =~ /\D/ or $value < 1
l!l&&r!l&&!r
0011
MC/DC0%0/3
$value =~ /\D/ or $value < 1 or $value > 500
$value =~ /\D/ $value < 1 $value > 500
Truth table25%1/4
A or B or C
A= $value =~ /\D/B= $value < 1C= $value > 500
ABCresult
1XX1
01X1
0011
0000
1200 $value < 1 or
1201 $value > 500)
1202 {
1203 0 croak "The value '$value' in expression '$expression' " .
1204 "must be in the range 1 to 500";
1205 }
1206  
1207 11 $token = lc $token;
1208  
1209 11 ▶ if ($token ne 'items' and $token ne '%') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token ne "items" and $token ne "%") 0 11 11
Condition66%2/3
$token ne "items" and $token ne "%"
!ll&&!rl&&r
740
MC/DC0%0/2
$token ne "items" and $token ne "%"
$token ne "items" $token ne "%"
Truth table66%2/3
A and B
A= $token ne "items"B= $token ne "%"
ABresult
0X0
100
111
1210 0 croak "The type '$token' in expression '$expression' " .
1211 "must be either 'items' or '%'";
1212 }
1213  
1214 11 ▶ if ($tokens[0] =~ /^top$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens[0] =~ /^top$/i) { } 7 4 11
1215 7 $operator = 30;
1216 }
1217 else {
1218 4 $operator = 32;
1219 }
1220  
1221 11 ▶ if ($tokens[2] eq '%') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens[2] eq "%") 4 7 11
1222 4 $operator++;
1223 }
1224  
1225 11 $token = $value;
1226 }
1227  
1228  
1229 60 ▶ if (not $operator and $tokens[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $operator and $tokens[0]) 0 60 60
Condition33%1/3
not $operator and $tokens[0]
!ll&&!rl&&r
6000
MC/DC0%0/2
not $operator and $tokens[0]
not $operator $tokens[0]
Truth table33%1/3
not(A) and B
A= not $operatorB= $tokens[0]
ABresult
0X0
100
111
1230 0 croak "Token '$tokens[1]' is not a valid operator " .
1231 "in filter expression '$expression'";
1232 }
1233  
1234  
1235 # Special handling for Blanks/NonBlanks.
1236 60 ▶ if ($token =~ /^blanks|nonblanks$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^blanks|nonblanks$/i) 6 54 60
1237  
1238 # Only allow Equals or NotEqual in this context.
1239 6 ▶ if ($operator != 2 and $operator != 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operator != 2 and $operator != 5) 0 6 6
Condition66%2/3
$operator != 2 and $operator != 5
!ll&&!rl&&r
420
MC/DC0%0/2
$operator != 2 and $operator != 5
$operator != 2 $operator != 5
Truth table66%2/3
A and B
A= $operator != 2B= $operator != 5
ABresult
0X0
100
111
1240 0 croak "The operator '$tokens[1]' in expression '$expression' " .
1241 "is not valid in relation to Blanks/NonBlanks'";
1242 }
1243  
1244 6 $token = lc $token;
1245  
1246 # The operator should always be 2 (=) to flag a "simple" equality in
1247 # the binary record. Therefore we convert <> to =.
1248 6 ▶ if ($token eq 'blanks') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq 'blanks') { } 3 3 6
1249 3 ▶ if ($operator == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operator == 5) 1 2 3
1250 1 $operator = 2;
1251 1 $token = 'nonblanks';
1252 }
1253 }
1254 else {
1255 3 ▶ if ($operator == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operator == 5) 1 2 3
1256 1 $operator = 2;
1257 1 $token = 'blanks';
1258 }
1259 }
1260 }
1261  
1262  
1263 # if the string token contains an Excel match character then change the
1264 # operator type to indicate a non "simple" equality.
1265 60 ▶ if ($operator == 2 and $token =~ /[*?]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operator == 2 and $token =~ /[*?]/) 5 55 60
Condition100%3/3
$operator == 2 and $token =~ /[*?]/
!ll&&!rl&&r
40155
MC/DC100%2/2
$operator == 2 and $token =~ /[*?]/
$operator == 2 $token =~ /[*?]/
Truth table100%3/3
A and B
A= $operator == 2B= $token =~ /[*?]/
ABresult
0X0
100
111
1266 5 $operator = 22;
1267 }
1268  
1269  
1270 60 return ($operator, $token);
1271 }
1272  
1273  
1274 ###############################################################################
1275 #
1276 # hide_gridlines()
1277 #
1278 # Set the option to hide gridlines on the screen and the printed page.
1279 # There are two ways of doing this in the Excel BIFF format: The first is by
1280 # setting the DspGrid field of the WINDOW2 record, this turns off the screen
1281 # and subsequently the print gridline. The second method is to via the
1282 # PRINTGRIDLINES and GRIDSET records, this turns off the printed gridlines
1283 # only. The first method is probably sufficient for most cases. The second
1284 # method is supported for backwards compatibility. Porters take note.
1285 #
1286 0 ▶sub hide_gridlines {
Subroutine hide_gridlines: not called CC=4 SCAR=30.0
Pod undocumented
1287  
1288 0 my $self = shift;
1289 0 my $option = $_[0];
1290  
1291 0 ▶ $option = 1 unless defined $option; # Default to hiding printed gridlines
Branch: 1 branch
BranchTrueFalseTotal
unless defined $option 0 0 0
1292  
1293 0 ▶ if ($option == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($option == 0) { } 0 0 0
elsif ($option == 1) { } 0 0 0
1294 0 $self->{_print_gridlines} = 1; # 1 = display, 0 = hide
1295 0 $self->{_screen_gridlines} = 1;
1296 }
1297 elsif ($option == 1) {
1298 0 $self->{_print_gridlines} = 0;
1299 0 $self->{_screen_gridlines} = 1;
1300 }
1301 else {
1302 0 $self->{_print_gridlines} = 0;
1303 0 $self->{_screen_gridlines} = 0;
1304 }
1305 }
1306  
1307  
1308 ###############################################################################
1309 #
1310 # print_row_col_headers()
1311 #
1312 # Set the option to print the row and column headers on the printed page.
1313 # See also the _store_print_headers() method below.
1314 #
1315 0 ▶sub print_row_col_headers {
Subroutine print_row_col_headers: not called CC=2 SCAR=17.9
Pod undocumented
1316  
1317 0 my $self = shift;
1318  
1319 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
1320 0 $self->{_print_headers} = $_[0];
1321 }
1322 else {
1323 0 $self->{_print_headers} = 1;
1324 }
1325 }
1326  
1327  
1328 ###############################################################################
1329 #
1330 # fit_to_pages($width, $height)
1331 #
1332 # Store the vertical and horizontal number of pages that will define the
1333 # maximum area printed. See also _store_setup() and _store_wsbool() below.
1334 #
1335 0 ▶sub fit_to_pages {
Subroutine fit_to_pages: not called CC=3 SCAR=24.8
Pod undocumented
1336  
1337 0 my $self = shift;
1338  
1339 0 $self->{_fit_page} = 1;
1340 0 ▶ $self->{_fit_width} = $_[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
1341 0 ▶ $self->{_fit_height} = $_[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
1342 }
1343  
1344  
1345 ###############################################################################
1346 #
1347 # set_h_pagebreaks(@breaks)
1348 #
1349 # Store the horizontal page breaks on a worksheet.
1350 #
1351 0 ▶sub set_h_pagebreaks {
Subroutine set_h_pagebreaks: not called CC=1 SCAR=6.9
Pod undocumented
1352  
1353 0 my $self = shift;
1354  
1355 0 push @{$self->{_hbreaks}}, @_;
1356 }
1357  
1358  
1359 ###############################################################################
1360 #
1361 # set_v_pagebreaks(@breaks)
1362 #
1363 # Store the vertical page breaks on a worksheet.
1364 #
1365 0 ▶sub set_v_pagebreaks {
Subroutine set_v_pagebreaks: not called CC=1 SCAR=6.9
Pod undocumented
1366  
1367 0 my $self = shift;
1368  
1369 0 push @{$self->{_vbreaks}}, @_;
1370 }
1371  
1372  
1373 ###############################################################################
1374 #
1375 # set_zoom($scale)
1376 #
1377 # Set the worksheet zoom factor.
1378 #
1379 0 ▶sub set_zoom {
Subroutine set_zoom: not called CC=4 SCAR=30.0
Pod undocumented
1380  
1381 0 my $self = shift;
1382 0 ▶ my $scale = $_[0] || 100;
Condition0%0/2
$_[0] || 100
l!l
00
MC/DC0%0/1
$_[0] || 100
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
1383  
1384 # Confine the scale to Excel's range
1385 0 ▶ if ($scale < 10 or $scale > 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale < 10 or $scale > 400) 0 0 0
Condition0%0/3
$scale < 10 or $scale > 400
l!l&&r!l&&!r
000
MC/DC0%0/2
$scale < 10 or $scale > 400
$scale < 10 $scale > 400
Truth table0%0/3
A or B
A= $scale < 10B= $scale > 400
ABresult
1X1
011
000
1386 0 carp "Zoom factor $scale outside range: 10 <= zoom <= 400";
1387 0 $scale = 100;
1388 }
1389  
1390 0 $self->{_zoom} = int $scale;
1391 }
1392  
1393  
1394 ###############################################################################
1395 #
1396 # set_print_scale($scale)
1397 #
1398 # Set the scale factor for the printed page.
1399 #
1400 0 ▶sub set_print_scale {
Subroutine set_print_scale: not called CC=4 SCAR=30.0
Pod undocumented
1401  
1402 0 my $self = shift;
1403 0 ▶ my $scale = $_[0] || 100;
Condition0%0/2
$_[0] || 100
l!l
00
MC/DC0%0/1
$_[0] || 100
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
1404  
1405 # Confine the scale to Excel's range
1406 0 ▶ if ($scale < 10 or $scale > 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale < 10 or $scale > 400) 0 0 0
Condition0%0/3
$scale < 10 or $scale > 400
l!l&&r!l&&!r
000
MC/DC0%0/2
$scale < 10 or $scale > 400
$scale < 10 $scale > 400
Truth table0%0/3
A or B
A= $scale < 10B= $scale > 400
ABresult
1X1
011
000
1407 0 carp "Print scale $scale outside range: 10 <= zoom <= 400";
1408 0 $scale = 100;
1409 }
1410  
1411 # Turn off "fit to page" option
1412 0 $self->{_fit_page} = 0;
1413  
1414 0 $self->{_print_scale} = int $scale;
1415 }
1416  
1417  
1418 ###############################################################################
1419 #
1420 # keep_leading_zeros()
1421 #
1422 # Causes the write() method to treat integers with a leading zero as a string.
1423 # This ensures that any leading zeros such, as in zip codes, are maintained.
1424 #
1425 0 ▶sub keep_leading_zeros {
Subroutine keep_leading_zeros: not called CC=2 SCAR=17.9
Pod undocumented
1426  
1427 0 my $self = shift;
1428  
1429 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
1430 0 $self->{_leading_zeros} = $_[0];
1431 }
1432 else {
1433 0 $self->{_leading_zeros} = 1;
1434 }
1435 }
1436  
1437  
1438 ###############################################################################
1439 #
1440 # show_comments()
1441 #
1442 # Make any comments in the worksheet visible.
1443 #
1444 0 ▶sub show_comments {
Subroutine show_comments: not called CC=2 SCAR=17.9
Pod undocumented
1445  
1446 0 my $self = shift;
1447  
1448 0 ▶ $self->{_comments_visible} = defined $_[0] ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
1449 }
1450  
1451  
1452 ###############################################################################
1453 #
1454 # set_comments_author()
1455 #
1456 # Set the default author of the cell comments.
1457 #
1458 0 ▶sub set_comments_author {
Subroutine set_comments_author: not called CC=3 SCAR=24.8
Pod undocumented
1459  
1460 0 my $self = shift;
1461  
1462 0 ▶ $self->{_comments_author} = defined $_[0] ? $_[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
1463 0 ▶ $self->{_comments_author_enc} = $_[1] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
1464 }
1465  
1466  
1467 ###############################################################################
1468 #
1469 # right_to_left()
1470 #
1471 # Display the worksheet right to left for some eastern versions of Excel.
1472 #
1473 0 ▶sub right_to_left {
Subroutine right_to_left: not called CC=2 SCAR=17.9
Pod undocumented
1474  
1475 0 my $self = shift;
1476  
1477 0 ▶ $self->{_display_arabic} = defined $_[0] ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
1478 }
1479  
1480  
1481 ###############################################################################
1482 #
1483 # hide_zero()
1484 #
1485 # Hide cell zero values.
1486 #
1487 0 ▶sub hide_zero {
Subroutine hide_zero: not called CC=2 SCAR=17.9
Pod undocumented
1488  
1489 0 my $self = shift;
1490  
1491 0 ▶ $self->{_display_zeros} = defined $_[0] ? not $_[0] : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
1492 }
1493  
1494  
1495 ###############################################################################
1496 #
1497 # print_across()
1498 #
1499 # Set the order in which pages are printed.
1500 #
1501 0 ▶sub print_across {
Subroutine print_across: not called CC=2 SCAR=17.9
Pod undocumented
1502  
1503 0 my $self = shift;
1504  
1505 0 ▶ $self->{_page_order} = defined $_[0] ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
1506 }
1507  
1508  
1509 ###############################################################################
1510 #
1511 # set_start_page()
1512 #
1513 # Set the start page number.
1514 #
1515 0 ▶sub set_start_page {
Subroutine set_start_page: not called CC=2 SCAR=17.9
Pod undocumented
1516  
1517 0 my $self = shift;
1518 0 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
1519  
1520 0 $self->{_page_start} = $_[0];
1521 0 $self->{_custom_start} = 1;
1522 }
1523  
1524  
1525 ###############################################################################
1526 #
1527 # set_first_row_column()
1528 #
1529 # Set the topmost and leftmost visible row and column.
1530 # TODO: Document this when tested fully for interaction with panes.
1531 #
1532 0 ▶sub set_first_row_column {
Subroutine set_first_row_column: not called CC=5 SCAR=34.0
Pod undocumented
1533  
1534 0 my $self = shift;
1535  
1536 0 ▶ my $row = $_[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
1537 0 ▶ my $col = $_[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
1538  
1539 0 ▶ $row = 65535 if $row > 65535;
Branch: 1 branch
BranchTrueFalseTotal
if $row > 65535 0 0 0
1540 0 ▶ $col = 255 if $col > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $col > 255 0 0 0
1541  
1542 0 $self->{_first_row} = $row;
1543 0 $self->{_first_col} = $col;
1544 }
1545  
1546  
1547 ###############################################################################
1548 #
1549 # add_write_handler($re, $code_ref)
1550 #
1551 # Allow the user to add their own matches and handlers to the write() method.
1552 #
1553 0 ▶sub add_write_handler {
Subroutine add_write_handler: not called CC=3 SCAR=24.8
Pod undocumented
1554  
1555 0 my $self = shift;
1556  
1557 0 ▶ return unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 0 0
1558 0 ▶ return unless ref $_[1] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[1] eq "CODE" 0 0 0
1559  
1560 0 push @{$self->{_write_match}}, [ @_ ];
1561 }
1562  
1563  
1564  
1565 ###############################################################################
1566 #
1567 # write($row, $col, $token, $format)
1568 #
1569 # Parse $token and call appropriate write method. $row and $column are zero
1570 # indexed. $format is optional.
1571 #
1572 # The write_url() methods have a flag to prevent recursion when writing a
1573 # string that looks like a url.
1574 #
1575 # Returns: return value of called subroutine
1576 #
1577 36 ▶sub write {
Subroutine write: called CC=18 SCAR=43.8
Pod undocumented
1578  
1579 36 my $self = shift;
1580  
1581 # Check for a cell reference in A1 notation and substitute row and column
1582 36 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 7 29 36
1583 7 @_ = $self->_substitute_cellref(@_);
1584 }
1585  
1586 36 my $token = $_[2];
1587  
1588 # Handle undefs as blanks
1589 36 ▶ $token = '' unless defined $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 36 36
1590  
1591  
1592 # First try user defined matches.
1593 36 for my $aref (@{$self->{_write_match}}) {
1594 0 my $re = $aref->[0];
1595 0 my $sub = $aref->[1];
1596  
1597 0 ▶ if ($token =~ /$re/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /$re/) 0 0 0
1598 0 my $match = &$sub($self, @_);
1599 0 ▶ return $match if defined $match;
Branch: 1 branch
BranchTrueFalseTotal
if defined $match 0 0 0
1600 }
1601 }
1602  
1603  
1604 # Match an array ref.
1605 36 ▶ if (ref $token eq "ARRAY") {
Branch: 8 branches
BranchTrueFalseTotal
if (ref $token eq 'ARRAY') { } 0 36 36
elsif ($self->{'_leading_zeros'} and $token =~ /^0\d+$/) { } 0 36 36
elsif ($token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) { } 0 36 36
elsif ($token =~ m[^[fh]tt?ps?://] and not $self->{'_writing_url'}) { } 0 36 36
elsif ($token =~ /^mailto:/ and not $self->{'_writing_url'}) { } 0 36 36
elsif ($token =~ /^(?:in|ex)ternal:/ and not $self->{'_writing_url'}) { } 0 36 36
elsif ($token =~ /^=/) { } 0 36 36
elsif ($token eq '') { } 0 36 36
1606 0 return $self->write_row(@_);
1607 }
1608 # Match integer with leading zero(s)
1609 ▶ elsif ($self->{_leading_zeros} and $token =~ /^0\d+$/) {
Condition33%1/3
$self->{"_leading_zeros"} and $token =~ /^0\d+$/
!ll&&!rl&&r
3600
MC/DC0%0/2
$self->{"_leading_zeros"} and $token =~ /^0\d+$/
$self->{"_leading_zeros"} $token =~ /^0\d+$/
Truth table33%1/3
A and B
A= $self->{"_leading_zeros"}B= $token =~ /^0\d+$/
ABresult
0X0
100
111
1610 0 return $self->write_string(@_);
1611 }
1612 # Match number
1613 elsif ($token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) {
1614 0 return $self->write_number(@_);
1615 }
1616 # Match http, https or ftp URL
1617 ▶ elsif ($token =~ m|^[fh]tt?ps?://| and not $self->{_writing_url}) {
Condition66%2/3
$token =~ m[^[fh]tt?ps?://] and not $self->{"_writing_url"}
!ll&&!rl&&r
3510
MC/DC0%0/2
$token =~ m[^[fh]tt?ps?://] and not $self->{"_writing_url"}
$token =~ m[^[fh]tt?ps?://] not $self->{"_writing_url"}
Truth table66%2/3
A and not(B)
A= $token =~ m[^[fh]tt?ps?://]B= not $self->{"_writing_url"}
ABresult
0X0
100
111
1618 0 return $self->write_url(@_);
1619 }
1620 # Match mailto:
1621 ▶ elsif ($token =~ m/^mailto:/ and not $self->{_writing_url}) {
Condition33%1/3
$token =~ /^mailto:/ and not $self->{"_writing_url"}
!ll&&!rl&&r
3600
MC/DC0%0/2
$token =~ /^mailto:/ and not $self->{"_writing_url"}
$token =~ /^mailto:/ not $self->{"_writing_url"}
Truth table33%1/3
A and not(B)
A= $token =~ /^mailto:/B= not $self->{"_writing_url"}
ABresult
0X0
100
111
1622 0 return $self->write_url(@_);
1623 }
1624 # Match internal or external sheet link
1625 ▶ elsif ($token =~ m[^(?:in|ex)ternal:] and not $self->{_writing_url}) {
Condition33%1/3
$token =~ /^(?:in|ex)ternal:/ and not $self->{"_writing_url"}
!ll&&!rl&&r
3600
MC/DC0%0/2
$token =~ /^(?:in|ex)ternal:/ and not $self->{"_writing_url"}
$token =~ /^(?:in|ex)ternal:/ not $self->{"_writing_url"}
Truth table33%1/3
A and not(B)
A= $token =~ /^(?:in|ex)ternal:/B= not $self->{"_writing_url"}
ABresult
0X0
100
111
1626 0 return $self->write_url(@_);
1627 }
1628 # Match formula
1629 elsif ($token =~ /^=/) {
1630 0 return $self->write_formula(@_);
1631 }
1632 # Match blank
1633 elsif ($token eq '') {
1634 0 splice @_, 2, 1; # remove the empty string from the parameter list
1635 0 return $self->write_blank(@_);
1636 }
1637 else {
1638 36 return $self->write_string(@_);
1639 }
1640 }
1641  
1642  
1643 ###############################################################################
1644 #
1645 # write_row($row, $col, $array_ref, $format)
1646 #
1647 # Write a row of data starting from ($row, $col). Call write_col() if any of
1648 # the elements of the array ref are in turn array refs. This allows the writing
1649 # of 1D or 2D arrays of data in one go.
1650 #
1651 # Returns: the first encountered error value or zero for no errors
1652 #
1653 0 ▶sub write_row {
Subroutine write_row: not called CC=6 SCAR=37.4
Pod undocumented
1654  
1655 0 my $self = shift;
1656  
1657  
1658 # Check for a cell reference in A1 notation and substitute row and column
1659 0 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 0 0
1660 0 @_ = $self->_substitute_cellref(@_);
1661 }
1662  
1663 # Catch non array refs passed by user.
1664 0 ▶ if (ref $_[2] ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[2] ne "ARRAY") 0 0 0
1665 0 croak "Not an array ref in call to write_row()$!";
1666 }
1667  
1668 0 my $row = shift;
1669 0 my $col = shift;
1670 0 my $tokens = shift;
1671 0 my @options = @_;
1672 0 my $error = 0;
1673 0 my $ret;
1674  
1675 0 foreach my $token (@$tokens) {
1676  
1677 # Check for nested arrays
1678 0 ▶ if (ref $token eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $token eq 'ARRAY') { } 0 0 0
1679 0 $ret = $self->write_col($row, $col, $token, @options);
1680 } else {
1681 0 $ret = $self->write ($row, $col, $token, @options);
1682 }
1683  
1684 # Return only the first error encountered, if any.
1685 0 ▶ $error ||= $ret;
Condition0%0/3
$error ||= $ret
l!l&&r!l&&!r
000
MC/DC0%0/2
$error ||= $ret
$error $ret
Truth table0%0/3
A ||= B
A= $errorB= $ret
ABresult
1X1
011
000
1686 0 $col++;
1687 }
1688  
1689 0 return $error;
1690 }
1691  
1692  
1693 ###############################################################################
1694 #
1695 # write_col($row, $col, $array_ref, $format)
1696 #
1697 # Write a column of data starting from ($row, $col). Call write_row() if any of
1698 # the elements of the array ref are in turn array refs. This allows the writing
1699 # of 1D or 2D arrays of data in one go.
1700 #
1701 # Returns: the first encountered error value or zero for no errors
1702 #
1703 0 ▶sub write_col {
Subroutine write_col: not called CC=5 SCAR=34.0
Pod undocumented
1704  
1705 0 my $self = shift;
1706  
1707  
1708 # Check for a cell reference in A1 notation and substitute row and column
1709 0 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 0 0
1710 0 @_ = $self->_substitute_cellref(@_);
1711 }
1712  
1713 # Catch non array refs passed by user.
1714 0 ▶ if (ref $_[2] ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[2] ne "ARRAY") 0 0 0
1715 0 croak "Not an array ref in call to write_col()$!";
1716 }
1717  
1718 0 my $row = shift;
1719 0 my $col = shift;
1720 0 my $tokens = shift;
1721 0 my @options = @_;
1722 0 my $error = 0;
1723 0 my $ret;
1724  
1725 0 foreach my $token (@$tokens) {
1726  
1727 # write() will deal with any nested arrays
1728 0 $ret = $self->write($row, $col, $token, @options);
1729  
1730 # Return only the first error encountered, if any.
1731 0 ▶ $error ||= $ret;
Condition0%0/3
$error ||= $ret
l!l&&r!l&&!r
000
MC/DC0%0/2
$error ||= $ret
$error $ret
Truth table0%0/3
A ||= B
A= $errorB= $ret
ABresult
1X1
011
000
1732 0 $row++;
1733 }
1734  
1735 0 return $error;
1736 }
1737  
1738  
1739 ###############################################################################
1740 #
1741 # write_comment($row, $col, $comment)
1742 #
1743 # Write a comment to the specified row and column (zero indexed).
1744 #
1745 # Returns 0 : normal termination
1746 # -1 : insufficient number of arguments
1747 # -2 : row or column out of range
1748 #
1749 22541 ▶sub write_comment {
Subroutine write_comment: called CC=5 SCAR=17.8
Pod undocumented
1750  
1751 22541 my $self = shift;
1752  
1753  
1754 # Check for a cell reference in A1 notation and substitute row and column
1755 22541 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 22541 22541
1756 0 @_ = $self->_substitute_cellref(@_);
1757 }
1758  
1759 0 ▶ if (@_ < 3) { return -1 } # Check the number of args
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3) 0 22541 22541
1760  
1761  
1762 22541 my $row = $_[0];
1763 22541 my $col = $_[1];
1764  
1765 # Check for pairs of optional arguments, i.e. an odd number of args.
1766 22541 ▶ croak "Uneven number of additional arguments" unless @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 0 22541 22541
1767  
1768  
1769 # Check that row and col are valid and store max and min values
1770 22541 ▶ return -2 if $self->_check_dimensions($row, $col);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_dimensions($row, $col) 0 22541 22541
1771  
1772  
1773 # We have to avoid duplicate comments in cells or else Excel will complain.
1774 22541 $self->{_comments}->{$row}->{$col} = [ $self->_comment_params(@_) ];
1775  
1776 }
1777  
1778  
1779 ###############################################################################
1780 #
1781 # _XF()
1782 #
1783 # Returns an index to the XF record in the workbook.
1784 #
1785 # Note: this is a function, not a method.
1786 #
1787 59 ▶sub _XF {
Subroutine _XF: called CC=7 SCAR=20.1
1788  
1789 59 my $self = $_[0];
1790 59 my $row = $_[1];
1791 59 my $col = $_[2];
1792 59 my $format = $_[3];
1793  
1794 59 my $error = "Error: refer to merge_range() in the documentation. " .
1795 "Can't use previously merged format in non-merged cell";
1796  
1797 59 ▶ if (ref($format)) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $format) { } 22 37 59
elsif (exists $self->{'_row_formats'}{$row}) { } 2 35 37
elsif (exists $self->{'_col_formats'}{$col}) { } 1 34 35
1798 # Temp code to prevent merged formats in non-merged cells.
1799 22 ▶ croak $error if $format->{_used_merge} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $format->{"_used_merge"} == 1 1 21 22
1800 21 $format->{_used_merge} = -1;
1801  
1802 21 return $format->get_xf_index();
1803 }
1804 elsif (exists $self->{_row_formats}->{$row}) {
1805 # Temp code to prevent merged formats in non-merged cells.
1806 2 ▶ croak $error if $self->{_row_formats}->{$row}->{_used_merge} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_row_formats"}{$row}{"_used_merge"} == 1 2 0 2
1807 0 $self->{_row_formats}->{$row}->{_used_merge} = -1;
1808  
1809 0 return $self->{_row_formats}->{$row}->get_xf_index();
1810 }
1811 elsif (exists $self->{_col_formats}->{$col}) {
1812 # Temp code to prevent merged formats in non-merged cells.
1813 1 ▶ croak $error if $self->{_col_formats}->{$col}->{_used_merge} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_col_formats"}{$col}{"_used_merge"} == 1 1 0 1
1814 0 $self->{_col_formats}->{$col}->{_used_merge} = -1;
1815  
1816 0 return $self->{_col_formats}->{$col}->get_xf_index();
1817 }
1818 else {
1819 34 return 0x0F;
1820 }
1821 }
1822  
1823  
1824 ###############################################################################
1825 ###############################################################################
1826 #
1827 # Internal methods
1828 #
1829  
1830  
1831 ###############################################################################
1832 #
1833 # _append(), overridden.
1834 #
1835 # Store Worksheet data in memory using the base class _append() or to a
1836 # temporary file, the default.
1837 #
1838 885 ▶sub _append {
Subroutine _append: called CC=3 SCAR=11.0
1839  
1840 885 my $self = shift;
1841 885 my $data = '';
1842  
1843 885 ▶ if ($self->{_using_tmpfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_using_tmpfile'}) { } 562 323 885
1844 562 $data = join('', @_);
1845  
1846 # Add CONTINUE records if necessary
1847 562 ▶ $data = $self->_add_continue($data) if length($data) > $self->{_limit};
Branch: 1 branch
BranchTrueFalseTotal
if length $data > $self->{"_limit"} 0 562 562
1848  
1849 # Protect print() from -l on the command line.
1850 562 local $\ = undef;
1851  
1852 562 print {$self->{_filehandle}} $data;
1853 562 $self->{_datasize} += length($data);
1854 }
1855 else {
1856 323 $data = $self->SUPER::_append(@_);
1857 }
1858  
1859 885 return $data;
1860 }
1861  
1862  
1863 ###############################################################################
1864 #
1865 # _substitute_cellref()
1866 #
1867 # Substitute an Excel cell reference in A1 notation for zero based row and
1868 # column values in an argument list.
1869 #
1870 # Ex: ("A4", "Hello") is converted to (3, 0, "Hello").
1871 #
1872 83 ▶sub _substitute_cellref {
Subroutine _substitute_cellref: called CC=4 SCAR=13.9
1873  
1874 83 my $self = shift;
1875 83 my $cell = uc(shift);
1876  
1877 # Convert a column range: 'A:A' or 'B:G'.
1878 # A range such as A:A is equivalent to A1:65536, so add rows as required
1879 83 ▶ if ($cell =~ /\$?([A-I]?[A-Z]):\$?([A-I]?[A-Z])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell =~ /\$?([A-I]?[A-Z]):\$?([A-I]?[A-Z])/) 6 77 83
1880 6 my ($row1, $col1) = $self->_cell_to_rowcol($1 .'1');
1881 6 my ($row2, $col2) = $self->_cell_to_rowcol($2 .'65536');
1882 6 return $row1, $col1, $row2, $col2, @_;
1883 }
1884  
1885 # Convert a cell range: 'A1:B7'
1886 77 ▶ if ($cell =~ /\$?([A-I]?[A-Z]\$?\d+):\$?([A-I]?[A-Z]\$?\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell =~ /\$?([A-I]?[A-Z]\$?\d+):\$?([A-I]?[A-Z]\$?\d+)/) 19 58 77
1887 19 my ($row1, $col1) = $self->_cell_to_rowcol($1);
1888 19 my ($row2, $col2) = $self->_cell_to_rowcol($2);
1889 19 return $row1, $col1, $row2, $col2, @_;
1890 }
1891  
1892 # Convert a cell reference: 'A1' or 'AD2000'
1893 58 ▶ if ($cell =~ /\$?([A-I]?[A-Z]\$?\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell =~ /\$?([A-I]?[A-Z]\$?\d+)/) 58 0 58
1894 58 my ($row1, $col1) = $self->_cell_to_rowcol($1);
1895 58 return $row1, $col1, @_;
1896  
1897 }
1898  
1899 0 croak("Unknown cell reference $cell");
1900 }
1901  
1902  
1903 ###############################################################################
1904 #
1905 # _cell_to_rowcol($cell_ref)
1906 #
1907 # Convert an Excel cell reference in A1 notation to a zero based row and column
1908 # reference; converts C1 to (0, 2).
1909 #
1910 # Returns: row, column
1911 #
1912 108 ▶sub _cell_to_rowcol {
Subroutine _cell_to_rowcol: called CC=2 SCAR=6.9
1913  
1914 108 my $self = shift;
1915 108 my $cell = shift;
1916  
1917 108 $cell =~ /\$?([A-I]?[A-Z])\$?(\d+)/;
1918  
1919 108 my $col = $1;
1920 108 my $row = $2;
1921  
1922 # Convert base26 column string to number
1923 # All your Base are belong to us.
1924 108 my @chars = split //, $col;
1925 108 my $expn = 0;
1926 108 $col = 0;
1927  
1928 108 ▶ while (@chars) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chars) 112 108 220
1929 112 my $char = pop(@chars); # LS char first
1930 112 $col += (ord($char) -ord('A') +1) * (26**$expn);
1931 112 $expn++;
1932 }
1933  
1934 # Convert 1-index to zero-index
1935 108 $row--;
1936 108 $col--;
1937  
1938 108 return $row, $col;
1939 }
1940  
1941  
1942 ###############################################################################
1943 #
1944 # _sort_pagebreaks()
1945 #
1946 #
1947 # This is an internal method that is used to filter elements of the array of
1948 # pagebreaks used in the _store_hbreak() and _store_vbreak() methods. It:
1949 # 1. Removes duplicate entries from the list.
1950 # 2. Sorts the list.
1951 # 3. Removes 0 from the list if present.
1952 #
1953 0 ▶sub _sort_pagebreaks {
Subroutine _sort_pagebreaks: not called CC=3 SCAR=24.8
1954  
1955 0 my $self= shift;
1956  
1957 0 my %hash;
1958 my @array;
1959  
1960 0 @hash{@_} = undef; # Hash slice to remove duplicates
1961 0 @array = sort {$a <=> $b} keys %hash; # Numerical sort
1962 0 ▶ shift @array if $array[0] == 0; # Remove zero
Branch: 1 branch
BranchTrueFalseTotal
if $array[0] == 0 0 0 0
1963  
1964 # 1000 vertical pagebreaks appears to be an internal Excel 5 limit.
1965 # It is slightly higher in Excel 97/200, approx. 1026
1966 0 ▶ splice(@array, 1000) if (@array > 1000);
Branch: 1 branch
BranchTrueFalseTotal
if @array > 1000 0 0 0
1967  
1968 return @array
1969 0 }
1970  
1971  
1972 ###############################################################################
1973 #
1974 # _encode_password($password)
1975 #
1976 # Based on the algorithm provided by Daniel Rentz of OpenOffice.
1977 #
1978 #
1979 0 ▶sub _encode_password {
Subroutine _encode_password: not called CC=3 SCAR=21.6
1980  
1981 32 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
1982  
1983 0 my $self = shift;
1984 0 my $plaintext = $_[0];
1985 0 my $password;
1986 my $count;
1987 0 my @chars;
1988 0 my $i = 0;
1989  
1990 0 $count = @chars = split //, $plaintext;
1991  
1992 0 foreach my $char (@chars) {
1993 0 my $low_15;
1994 my $high_15;
1995 0 $char = ord($char) << ++$i;
1996 0 $low_15 = $char & 0x7fff;
1997 0 $high_15 = $char & 0x7fff << 15;
1998 0 $high_15 = $high_15 >> 15;
1999 0 $char = $low_15 | $high_15;
2000 }
2001  
2002 0 $password = 0x0000;
2003 0 $password ^= $_ for @chars;
2004 0 $password ^= $count;
2005 0 $password ^= 0xCE4B;
2006  
2007 0 return $password;
2008 }
2009  
2010  
2011 ###############################################################################
2012 #
2013 # outline_settings($visible, $symbols_below, $symbols_right, $auto_style)
2014 #
2015 # This method sets the properties for outlining and grouping. The defaults
2016 # correspond to Excel's defaults.
2017 #
2018 0 ▶sub outline_settings {
Subroutine outline_settings: not called CC=6 SCAR=37.4
Pod undocumented
2019  
2020 0 my $self = shift;
2021  
2022 0 ▶ $self->{_outline_on} = defined $_[0] ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
2023 0 ▶ $self->{_outline_below} = defined $_[1] ? $_[1] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
2024 0 ▶ $self->{_outline_right} = defined $_[2] ? $_[2] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
2025 0 ▶ $self->{_outline_style} = $_[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
2026  
2027 # Ensure this is a boolean vale for Window2
2028 0 ▶ $self->{_outline_on} = 1 if $self->{_outline_on};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_outline_on"} 0 0 0
2029 }
2030  
2031  
2032  
2033  
2034 ###############################################################################
2035 ###############################################################################
2036 #
2037 # BIFF RECORDS
2038 #
2039  
2040  
2041 ###############################################################################
2042 #
2043 # write_number($row, $col, $num, $format)
2044 #
2045 # Write a double to the specified row and column (zero indexed).
2046 # An integer can be written as a double. Excel will display an
2047 # integer. $format is optional.
2048 #
2049 # Returns 0 : normal termination
2050 # -1 : insufficient number of arguments
2051 # -2 : row or column out of range
2052 #
2053 1 ▶sub write_number {
Subroutine write_number: called CC=6 SCAR=19.3
Pod undocumented
2054  
2055 1 my $self = shift;
2056  
2057 # Check for a cell reference in A1 notation and substitute row and column
2058 1 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 1 1
2059 0 @_ = $self->_substitute_cellref(@_);
2060 }
2061  
2062 0 ▶ if (@_ < 3) { return -1 } # Check the number of args
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3) 0 1 1
2063  
2064 1 my $record = 0x0203; # Record identifier
2065 1 my $length = 0x000E; # Number of bytes to follow
2066  
2067 1 my $row = $_[0]; # Zero indexed row
2068 1 my $col = $_[1]; # Zero indexed column
2069 1 my $num = $_[2];
2070 1 my $xf = _XF($self, $row, $col, $_[3]); # The cell format
2071  
2072 # Check that row and col are valid and store max and min values
2073 1 ▶ return -2 if $self->_check_dimensions($row, $col);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_dimensions($row, $col) 0 1 1
2074  
2075 1 my $header = pack("vv", $record, $length);
2076 1 my $data = pack("vvv", $row, $col, $xf);
2077 1 my $xl_double = pack("d", $num);
2078  
2079 0 ▶ if ($self->{_byte_order}) { $xl_double = reverse $xl_double }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_byte_order"}) 0 1 1
2080  
2081 # Store the data or write immediately depending on the compatibility mode.
2082 1 ▶ if ($self->{_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_compatibility'}) { } 0 1 1
2083 0 $self->{_table}->[$row]->[$col] = $header . $data . $xl_double;
2084 }
2085 else {
2086 1 $self->_append($header, $data, $xl_double);
2087 }
2088  
2089 1 return 0;
2090 }
2091  
2092  
2093 ###############################################################################
2094 #
2095 # write_string ($row, $col, $string, $format)
2096 #
2097 # Write a string to the specified row and column (zero indexed).
2098 # $format is optional.
2099 # Returns 0 : normal termination
2100 # -1 : insufficient number of arguments
2101 # -2 : row or column out of range
2102 # -3 : long string truncated to max chars
2103 #
2104 41 ▶sub write_string {
Subroutine write_string: called CC=9 SCAR=22.4
Pod undocumented
2105  
2106 41 my $self = shift;
2107  
2108 # Check for a cell reference in A1 notation and substitute row and column
2109 41 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 41 41
2110 0 @_ = $self->_substitute_cellref(@_);
2111 }
2112  
2113 41 ▶ if (@_ < 3) { return -1 } # Check the number of args
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3) 1 40 41
2114  
2115 40 my $record = 0x00FD; # Record identifier
2116 40 my $length = 0x000A; # Bytes to follow
2117  
2118 40 my $row = $_[0]; # Zero indexed row
2119 40 my $col = $_[1]; # Zero indexed column
2120 40 my $strlen = length($_[2]);
2121 40 my $str = $_[2];
2122 40 my $xf = _XF($self, $row, $col, $_[3]); # The cell format
2123 36 my $encoding = 0x0;
2124 36 my $str_error = 0;
2125  
2126  
2127 # Handle utf8 strings in perl 5.8.
2128 36 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 36 0 36
2129 36 require Encode;
2130  
2131 36 ▶ if (Encode::is_utf8($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($str)) 0 36 36
2132 0 my $tmp = Encode::encode("UTF-16LE", $str);
2133 0 return $self->write_utf16le_string($row, $col, $tmp, $_[3]);
2134 }
2135 }
2136  
2137  
2138 # Check that row and col are valid and store max and min values
2139 36 ▶ return -2 if $self->_check_dimensions($row, $col);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_dimensions($row, $col) 2 34 36
2140  
2141 # Limit the string to the max number of chars.
2142 34 ▶ if ($strlen > 32767) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strlen > 32767) 0 34 34
2143 0 $str = substr($str, 0, 32767);
2144 0 $str_error = -3;
2145 }
2146  
2147  
2148 # Prepend the string with the type.
2149 34 my $str_header = pack("vC", length($str), $encoding);
2150 34 $str = $str_header . $str;
2151  
2152  
2153 34 ▶ if (not exists ${$self->{_str_table}}->{$str}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists ${$$self{"_str_table"};}->{$str}) 5 29 34
2154 5 ${$self->{_str_table}}->{$str} = ${$self->{_str_unique}}++;
2155 }
2156  
2157  
2158 34 ${$self->{_str_total}}++;
2159  
2160  
2161 34 my $header = pack("vv", $record, $length);
2162 34 my $data = pack("vvvV", $row, $col, $xf, ${$self->{_str_table}}->{$str});
2163  
2164  
2165 # Store the data or write immediately depending on the compatibility mode.
2166 34 ▶ if ($self->{_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_compatibility'}) { } 12 22 34
2167 12 $self->{_table}->[$row]->[$col] = $header . $data;
2168 }
2169 else {
2170 22 $self->_append($header, $data);
2171 }
2172  
2173 34 return $str_error;
2174 }
2175  
2176  
2177 ###############################################################################
2178 #
2179 # write_blank($row, $col, $format)
2180 #
2181 # Write a blank cell to the specified row and column (zero indexed).
2182 # A blank cell is used to specify formatting without adding a string
2183 # or a number.
2184 #
2185 # A blank cell without a format serves no purpose. Therefore, we don't write
2186 # a BLANK record unless a format is specified. This is mainly an optimisation
2187 # for the write_row() and write_col() methods.
2188 #
2189 # Returns 0 : normal termination (including no format)
2190 # -1 : insufficient number of arguments
2191 # -2 : row or column out of range
2192 #
2193 14 ▶sub write_blank {
Subroutine write_blank: called CC=6 SCAR=18.9
Pod undocumented
2194  
2195 14 my $self = shift;
2196  
2197 # Check for a cell reference in A1 notation and substitute row and column
2198 14 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 14 14
2199 0 @_ = $self->_substitute_cellref(@_);
2200 }
2201  
2202 # Check the number of args
2203 14 ▶ return -1 if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 14 14
2204  
2205 # Don't write a blank cell unless it has a format
2206 14 ▶ return 0 if not defined $_[2];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[2] 0 14 14
2207  
2208  
2209 14 my $record = 0x0201; # Record identifier
2210 14 my $length = 0x0006; # Number of bytes to follow
2211  
2212 14 my $row = $_[0]; # Zero indexed row
2213 14 my $col = $_[1]; # Zero indexed column
2214 14 my $xf = _XF($self, $row, $col, $_[2]); # The cell format
2215  
2216 # Check that row and col are valid and store max and min values
2217 14 ▶ return -2 if $self->_check_dimensions($row, $col);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_dimensions($row, $col) 0 14 14
2218  
2219 14 my $header = pack("vv", $record, $length);
2220 14 my $data = pack("vvv", $row, $col, $xf);
2221  
2222 # Store the data or write immediately depending on the compatibility mode.
2223 14 ▶ if ($self->{_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_compatibility'}) { } 0 14 14
2224 0 $self->{_table}->[$row]->[$col] = $header . $data;
2225 }
2226 else {
2227 14 $self->_append($header, $data);
2228 }
2229  
2230 14 return 0;
2231 }
2232  
2233  
2234 ###############################################################################
2235 #
2236 # write_formula($row, $col, $formula, $format, $value)
2237 #
2238 # Write a formula to the specified row and column (zero indexed).
2239 # The textual representation of the formula is passed to the parser in
2240 # Formula.pm which returns a packed binary string.
2241 #
2242 # $format is optional.
2243 #
2244 # $value is an optional result of the formula that can be supplied by the user.
2245 #
2246 # Returns 0 : normal termination
2247 # -1 : insufficient number of arguments
2248 # -2 : row or column out of range
2249 #
2250 1 ▶sub write_formula {
Subroutine write_formula: called CC=8 SCAR=21.7
Pod undocumented
2251  
2252 1 my $self = shift;
2253  
2254 # Check for a cell reference in A1 notation and substitute row and column
2255 1 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 1 1
2256 0 @_ = $self->_substitute_cellref(@_);
2257 }
2258  
2259 0 ▶ if (@_ < 3) { return -1 } # Check the number of args
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3) 0 1 1
2260  
2261 1 ▶ return if ! defined $_[2];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[2] 0 1 1
2262  
2263 1 my $record = 0x0006; # Record identifier
2264 1 my $length; # Bytes to follow
2265  
2266 1 my $row = $_[0]; # Zero indexed row
2267 1 my $col = $_[1]; # Zero indexed column
2268 1 my $formula = $_[2]; # The formula text string
2269 1 my $value = $_[4]; # The formula value.
2270  
2271  
2272 1 my $xf = _XF($self, $row, $col, $_[3]); # The cell format
2273 1 my $chn = 0x0000; # Must be zero
2274 1 my $is_string = 0; # Formula evaluates to str
2275 1 my $num; # Current value of formula
2276 my $grbit; # Option flags
2277  
2278  
2279 # Excel normally stores the last calculated value of the formula in $num.
2280 # Clearly we are not in a position to calculate this "a priori". Instead
2281 # we set $num to zero and set the option flags in $grbit to ensure
2282 # automatic calculation of the formula when the file is opened.
2283 # As a workaround for some non-Excel apps we also allow the user to
2284 # specify the result of the formula.
2285 #
2286 1 ($num, $grbit, $is_string) = $self->_encode_formula_result($value);
2287  
2288  
2289 # Check that row and col are valid and store max and min values
2290 1 ▶ return -2 if $self->_check_dimensions($row, $col);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_dimensions($row, $col) 0 1 1
2291  
2292 # Strip the = sign at the beginning of the formula string
2293 1 $formula =~ s(^=)();
2294  
2295 1 my $tmp = $formula;
2296  
2297 # Parse the formula using the parser in Formula.pm
2298 1 my $parser = $self->{_parser};
2299  
2300 # In order to raise formula errors from the point of view of the calling
2301 # program we use an eval block and re-raise the error from here.
2302 #
2303 1 eval { $formula = $parser->parse_formula($formula) };
2304  
2305 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
2306 0 $@ =~ s/\n$//; # Strip the \n used in the Formula.pm die()
2307 0 croak $@; # Re-raise the error
2308 }
2309  
2310  
2311 1 my $formlen = length($formula); # Length of the binary string
2312 1 $length = 0x16 + $formlen; # Length of the record data
2313  
2314 1 my $header = pack("vv", $record, $length);
2315 1 my $data = pack("vvv", $row, $col, $xf);
2316 1 $data .= $num;
2317 1 $data .= pack("vVv", $grbit, $chn, $formlen);
2318  
2319 # The STRING record if the formula evaluates to a string.
2320 1 my $string = '';
2321 1 ▶ $string = $self->_get_formula_string($value) if $is_string;
Branch: 1 branch
BranchTrueFalseTotal
if $is_string 0 1 1
2322  
2323  
2324 # Store the data or write immediately depending on the compatibility mode.
2325 1 ▶ if ($self->{_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_compatibility'}) { } 0 1 1
2326 0 $self->{_table}->[$row]->[$col] = $header . $data . $formula . $string;
2327 }
2328 else {
2329 1 $self->_append($header, $data, $formula, $string);
2330 }
2331  
2332 1 return 0;
2333 }
2334  
2335  
2336 ###############################################################################
2337 #
2338 # _encode_formula_result()
2339 #
2340 # Encode the user supplied result for a formula.
2341 #
2342 2 ▶sub _encode_formula_result {
Subroutine _encode_formula_result: called CC=4 SCAR=19.9
2343  
2344 2 my $self = shift;
2345  
2346 2 my $value = $_[0]; # Result to be encoded.
2347 2 my $is_string = 0; # Formula evaluates to str.
2348 2 my $num; # Current value of formula.
2349 my $grbit; # Option flags.
2350  
2351 2 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value) { } 2 0 2
2352 2 $grbit = 0x03;
2353 2 $num = pack "d", 0;
2354 }
2355 else {
2356 # The user specified the result of the formula. We turn off the recalc
2357 # flag and check the result type.
2358 0 $grbit = 0x00;
2359  
2360 0 ▶ if ($value =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) { } 0 0 0
2361 # Value is a number.
2362 0 $num = pack "d", $value;
2363 }
2364 else {
2365  
2366 0 my %bools = (
2367 'TRUE' => [1, 1],
2368 'FALSE' => [1, 0],
2369 '#NULL!' => [2, 0],
2370 '#DIV/0!' => [2, 7],
2371 '#VALUE!' => [2, 15],
2372 '#REF!' => [2, 23],
2373 '#NAME?' => [2, 29],
2374 '#NUM!' => [2, 36],
2375 '#N/A' => [2, 42],
2376 );
2377  
2378 0 ▶ if (exists $bools{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $bools{$value}) { } 0 0 0
2379 # Value is a boolean.
2380 $num = pack "vvvv", $bools{$value}->[0],
2381 0 $bools{$value}->[1],
2382 0,
2383 0xFFFF;
2384 }
2385 else {
2386 # Value is a string.
2387 0 $num = pack "vvvv", 0,
2388 0,
2389 0,
2390 0xFFFF;
2391 0 $is_string = 1;
2392 }
2393 }
2394 }
2395  
2396 2 return ($num, $grbit, $is_string);
2397 }
2398  
2399  
2400 ###############################################################################
2401 #
2402 # _get_formula_string()
2403 #
2404 # Pack the string value when a formula evaluates to a string. The value cannot
2405 # be calculated by the module and thus must be supplied by the user.
2406 #
2407 0 ▶sub _get_formula_string {
Subroutine _get_formula_string: not called CC=3 SCAR=24.8
2408  
2409 0 my $self = shift;
2410  
2411 0 my $record = 0x0207; # Record identifier
2412 0 my $length = 0x00; # Bytes to follow
2413 0 my $string = $_[0]; # Formula string.
2414 0 my $strlen = length $_[0]; # Length of the formula string (chars).
2415 0 my $encoding = 0; # String encoding.
2416  
2417  
2418 # Handle utf8 strings in perl 5.8.
2419 0 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 0 0 0
2420 0 require Encode;
2421  
2422 0 ▶ if (Encode::is_utf8($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($string)) 0 0 0
2423 0 $string = Encode::encode("UTF-16BE", $string);
2424 0 $encoding = 1;
2425 }
2426 }
2427  
2428  
2429 0 $length = 0x03 + length $string; # Length of the record data
2430  
2431 0 my $header = pack("vv", $record, $length);
2432 0 my $data = pack("vC", $strlen, $encoding);
2433  
2434 0 return $header . $data . $string;
2435 }
2436  
2437  
2438 ###############################################################################
2439 #
2440 # store_formula($formula)
2441 #
2442 # Pre-parse a formula. This is used in conjunction with repeat_formula()
2443 # to repetitively rewrite a formula without re-parsing it.
2444 #
2445 4 ▶sub store_formula {
Subroutine store_formula: called CC=2 SCAR=7.2
Pod undocumented
2446  
2447 4 my $self = shift;
2448 4 my $formula = $_[0]; # The formula text string
2449  
2450 # Strip the = sign at the beginning of the formula string
2451 4 $formula =~ s(^=)();
2452  
2453 # Parse the formula using the parser in Formula.pm
2454 4 my $parser = $self->{_parser};
2455  
2456 # In order to raise formula errors from the point of view of the calling
2457 # program we use an eval block and re-raise the error from here.
2458 #
2459 4 my @tokens;
2460 4 eval { @tokens = $parser->parse_formula($formula) };
2461  
2462 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
2463 0 $@ =~ s/\n$//; # Strip the \n used in the Formula.pm die()
2464 0 croak $@; # Re-raise the error
2465 }
2466  
2467  
2468 # Return the parsed tokens in an anonymous array
2469 4 return [@tokens];
2470 }
2471  
2472  
2473 ###############################################################################
2474 #
2475 # repeat_formula($row, $col, $formula, $format, ($pattern => $replacement,...))
2476 #
2477 # Write a formula to the specified row and column (zero indexed) by
2478 # substituting $pattern $replacement pairs in the $formula created via
2479 # store_formula(). This allows the user to repetitively rewrite a formula
2480 # without the significant overhead of parsing.
2481 #
2482 # Returns 0 : normal termination
2483 # -1 : insufficient number of arguments
2484 # -2 : row or column out of range
2485 #
2486 1 ▶sub repeat_formula {
Subroutine repeat_formula: called CC=15 SCAR=28.8
Pod undocumented
2487  
2488 1 my $self = shift;
2489  
2490 # Check for a cell reference in A1 notation and substitute row and column
2491 1 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 1 1
2492 0 @_ = $self->_substitute_cellref(@_);
2493 }
2494  
2495 0 ▶ if (@_ < 2) { return -1 } # Check the number of args
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) 0 1 1
2496  
2497 1 my $record = 0x0006; # Record identifier
2498 1 my $length; # Bytes to follow
2499  
2500 1 my $row = shift; # Zero indexed row
2501 1 my $col = shift; # Zero indexed column
2502 1 my $formula_ref = shift; # Array ref with formula tokens
2503 1 my $format = shift; # XF format
2504 1 my @pairs = @_; # Pattern/replacement pairs
2505  
2506  
2507 # Enforce an even number of arguments in the pattern/replacement list
2508 1 ▶ croak "Odd number of elements in pattern/replacement list" if @pairs %2;
Branch: 1 branch
BranchTrueFalseTotal
if @pairs % 2 0 1 1
2509  
2510 # Check that $formula is an array ref
2511 1 ▶ croak "Not a valid formula" if ref $formula_ref ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $formula_ref ne "ARRAY" 0 1 1
2512  
2513 1 my @tokens = @$formula_ref;
2514  
2515 # Ensure that there are tokens to substitute
2516 1 ▶ croak "No tokens in formula" unless @tokens;
Branch: 1 branch
BranchTrueFalseTotal
unless @tokens 0 1 1
2517  
2518  
2519 # As a temporary and undocumented measure we allow the user to specify the
2520 # result of the formula by appending a result => $value pair to the end
2521 # of the arguments.
2522 1 my $value = undef;
2523 1 ▶ if (@pairs && $pairs[-2] eq 'result') {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs and $pairs[-2] eq "result") 0 1 1
Condition33%1/3
@pairs and $pairs[-2] eq "result"
!ll&&!rl&&r
010
MC/DC0%0/2
@pairs and $pairs[-2] eq "result"
@pairs $pairs[-2] eq "result"
Truth table33%1/3
A and B
A= @pairsB= $pairs[-2] eq "result"
ABresult
0X0
100
111
2524 0 $value = pop @pairs;
2525 0 pop @pairs;
2526 }
2527  
2528  
2529 1 ▶ while (@pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs) 1 1 2
2530 1 my $pattern = shift @pairs;
2531 1 my $replace = shift @pairs;
2532  
2533 1 foreach my $token (@tokens) {
2534 2 ▶ last if $token =~ s/$pattern/$replace/;
Branch: 1 branch
BranchTrueFalseTotal
if $token =~ s/$pattern/$replace/ 1 1 2
2535 }
2536 }
2537  
2538  
2539 # Change the parameters in the formula cached by the Formula.pm object
2540 1 my $parser = $self->{_parser};
2541 1 my $formula = $parser->parse_tokens(@tokens);
2542  
2543 1 ▶ croak "Unrecognised token in formula" unless defined $formula;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $formula 0 1 1
2544  
2545  
2546 1 my $xf = _XF($self, $row, $col, $format); # The cell format
2547 1 my $chn = 0x0000; # Must be zero
2548 1 my $is_string = 0; # Formula evaluates to str
2549 1 my $num; # Current value of formula
2550 my $grbit; # Option flags
2551  
2552 # Excel normally stores the last calculated value of the formula in $num.
2553 # Clearly we are not in a position to calculate this "a priori". Instead
2554 # we set $num to zero and set the option flags in $grbit to ensure
2555 # automatic calculation of the formula when the file is opened.
2556 # As a workaround for some non-Excel apps we also allow the user to
2557 # specify the result of the formula.
2558 #
2559 1 ($num, $grbit, $is_string) = $self->_encode_formula_result($value);
2560  
2561 # Check that row and col are valid and store max and min values
2562 1 ▶ return -2 if $self->_check_dimensions($row, $col);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_dimensions($row, $col) 0 1 1
2563  
2564  
2565 1 my $formlen = length($formula); # Length of the binary string
2566 1 $length = 0x16 + $formlen; # Length of the record data
2567  
2568 1 my $header = pack("vv", $record, $length);
2569 1 my $data = pack("vvv", $row, $col, $xf);
2570 1 $data .= $num;
2571 1 $data .= pack("vVv", $grbit, $chn, $formlen);
2572  
2573  
2574 # The STRING record if the formula evaluates to a string.
2575 1 my $string = '';
2576 1 ▶ $string = $self->_get_formula_string($value) if $is_string;
Branch: 1 branch
BranchTrueFalseTotal
if $is_string 0 1 1
2577  
2578  
2579 # Store the data or write immediately depending on the compatibility mode.
2580 1 ▶ if ($self->{_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_compatibility'}) { } 0 1 1
2581 0 $self->{_table}->[$row]->[$col] = $header . $data . $formula . $string;
2582 }
2583 else {
2584 1 $self->_append($header, $data, $formula, $string);
2585 }
2586  
2587 1 return 0;
2588 }
2589  
2590  
2591 ###############################################################################
2592 #
2593 # write_url($row, $col, $url, $string, $format)
2594 #
2595 # Write a hyperlink. This is comprised of two elements: the visible label and
2596 # the invisible link. The visible label is the same as the link unless an
2597 # alternative string is specified.
2598 #
2599 # The parameters $string and $format are optional and their order is
2600 # interchangeable for backward compatibility reasons.
2601 #
2602 # The hyperlink can be to a http, ftp, mail, internal sheet, or external
2603 # directory url.
2604 #
2605 # Returns 0 : normal termination
2606 # -1 : insufficient number of arguments
2607 # -2 : row or column out of range
2608 # -3 : long string truncated to 255 chars
2609 #
2610 1 ▶sub write_url {
Subroutine write_url: called CC=3 SCAR=12.0
Pod undocumented
2611  
2612 1 my $self = shift;
2613  
2614 # Check for a cell reference in A1 notation and substitute row and column
2615 1 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 1 1
2616 0 @_ = $self->_substitute_cellref(@_);
2617 }
2618  
2619 # Check the number of args
2620 1 ▶ return -1 if @_ < 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 3 0 1 1
2621  
2622 # Add start row and col to arg list
2623 1 return $self->write_url_range($_[0], $_[1], @_);
2624 }
2625  
2626  
2627 ###############################################################################
2628 #
2629 # write_url_range($row1, $col1, $row2, $col2, $url, $string, $format)
2630 #
2631 # This is the more general form of write_url(). It allows a hyperlink to be
2632 # written to a range of cells. This function also decides the type of hyperlink
2633 # to be written. These are either, Web (http, ftp, mailto), Internal
2634 # (Sheet1!A1) or external ('c:\temp\foo.xls#Sheet1!A1').
2635 #
2636 # See also write_url() above for a general description and return values.
2637 #
2638 1 ▶sub write_url_range {
Subroutine write_url_range: called CC=6 SCAR=19.4
Pod undocumented
2639  
2640 1 my $self = shift;
2641  
2642 # Check for a cell reference in A1 notation and substitute row and column
2643 1 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 1 1
2644 0 @_ = $self->_substitute_cellref(@_);
2645 }
2646  
2647 # Check the number of args
2648 1 ▶ return -1 if @_ < 5;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 5 0 1 1
2649  
2650  
2651 # Reverse the order of $string and $format if necessary. We work on a copy
2652 # in order to protect the callers args. We don't use "local @_" in case of
2653 # perl50005 threads.
2654 #
2655 1 my @args = @_;
2656  
2657 1 ▶ ($args[5], $args[6]) = ($args[6], $args[5]) if ref $args[5];
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[5] 0 1 1
2658  
2659 1 my $url = $args[4];
2660  
2661  
2662 # Check for internal/external sheet links or default to web link
2663 1 ▶ return $self->_write_url_internal(@args) if $url =~ m[^internal:];
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /^internal:/ 0 1 1
2664 1 ▶ return $self->_write_url_external(@args) if $url =~ m[^external:];
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /^external:/ 0 1 1
2665 1 return $self->_write_url_web(@args);
2666 }
2667  
2668  
2669 ###############################################################################
2670 #
2671 # _write_url_web($row1, $col1, $row2, $col2, $url, $string, $format)
2672 #
2673 # Used to write http, ftp and mailto hyperlinks.
2674 # The link type ($options) is 0x03 is the same as absolute dir ref without
2675 # sheet. However it is differentiated by the $unknown2 data stream.
2676 #
2677 # See also write_url() above for a general description and return values.
2678 #
2679 1 ▶sub _write_url_web {
Subroutine _write_url_web: called CC=7 SCAR=20.1
2680  
2681 1 my $self = shift;
2682  
2683 1 my $record = 0x01B8; # Record identifier
2684 1 my $length = 0x00000; # Bytes to follow
2685  
2686 1 my $row1 = $_[0]; # Start row
2687 1 my $col1 = $_[1]; # Start column
2688 1 my $row2 = $_[2]; # End row
2689 1 my $col2 = $_[3]; # End column
2690 1 my $url = $_[4]; # URL string
2691 1 my $str = $_[5]; # Alternative label
2692 1 ▶ my $xf = $_[6] || $self->{_url_format};# The cell format
Condition33%1/3
$_[6] || $self->{'_url_format'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$_[6] || $self->{'_url_format'}
$_[6] $self->{'_url_format'}
Truth table33%1/3
A || B
A= $_[6]B= $self->{'_url_format'}
ABresult
1X1
011
000
2693  
2694  
2695 # Write the visible label but protect against url recursion in write().
2696 1 ▶ $str = $url unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 1 0 1
2697 1 $self->{_writing_url} = 1;
2698 1 my $error = $self->write($row1, $col1, $str, $xf);
2699 1 $self->{_writing_url} = 0;
2700 1 ▶ return $error if $error == -2;
Branch: 1 branch
BranchTrueFalseTotal
if $error == -2 0 1 1
2701  
2702  
2703 # Pack the undocumented parts of the hyperlink stream
2704 1 my $unknown1 = pack("H*", "D0C9EA79F9BACE118C8200AA004BA90B02000000");
2705 1 my $unknown2 = pack("H*", "E0C9EA79F9BACE118C8200AA004BA90B");
2706  
2707  
2708 # Pack the option flags
2709 1 my $options = pack("V", 0x03);
2710  
2711  
2712 # URL encoding.
2713 1 my $encoding = 0;
2714  
2715 # Convert an Utf8 URL type and to a null terminated wchar string.
2716 1 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 1 0 1
2717 1 require Encode;
2718  
2719 1 ▶ if (Encode::is_utf8($url)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($url)) 0 1 1
2720 0 $url = Encode::encode("UTF-16LE", $url);
2721 0 $url .= "\0\0"; # URL is null terminated.
2722 0 $encoding = 1;
2723 }
2724 }
2725  
2726 # Convert an Ascii URL type and to a null terminated wchar string.
2727 1 ▶ if ($encoding == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding == 0) 1 0 1
2728 1 $url .= "\0";
2729 1 $url = pack 'v*', unpack 'c*', $url;
2730 }
2731  
2732  
2733 # Pack the length of the URL
2734 1 my $url_len = pack("V", length($url));
2735  
2736  
2737 # Calculate the data length
2738 1 $length = 0x34 + length($url);
2739  
2740  
2741 # Pack the header data
2742 1 my $header = pack("vv", $record, $length);
2743 1 my $data = pack("vvvv", $row1, $row2, $col1, $col2);
2744  
2745  
2746 # Write the packed data
2747 1 $self->_append( $header,
2748 $data,
2749 $unknown1,
2750 $options,
2751 $unknown2,
2752 $url_len,
2753 $url);
2754  
2755 1 return $error;
2756 }
2757  
2758  
2759 ###############################################################################
2760 #
2761 # _write_url_internal($row1, $col1, $row2, $col2, $url, $string, $format)
2762 #
2763 # Used to write internal reference hyperlinks such as "Sheet1!A1".
2764 #
2765 # See also write_url() above for a general description and return values.
2766 #
2767 0 ▶sub _write_url_internal {
Subroutine _write_url_internal: not called CC=8 SCAR=42.8
2768  
2769 0 my $self = shift;
2770  
2771 0 my $record = 0x01B8; # Record identifier
2772 0 my $length = 0x00000; # Bytes to follow
2773  
2774 0 my $row1 = $_[0]; # Start row
2775 0 my $col1 = $_[1]; # Start column
2776 0 my $row2 = $_[2]; # End row
2777 0 my $col2 = $_[3]; # End column
2778 0 my $url = $_[4]; # URL string
2779 0 my $str = $_[5]; # Alternative label
2780 0 ▶ my $xf = $_[6] || $self->{_url_format};# The cell format
Condition0%0/3
$_[6] || $self->{'_url_format'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[6] || $self->{'_url_format'}
$_[6] $self->{'_url_format'}
Truth table0%0/3
A || B
A= $_[6]B= $self->{'_url_format'}
ABresult
1X1
011
000
2781  
2782 # Strip URL type
2783 0 $url =~ s[^internal:][];
2784  
2785  
2786 # Write the visible label but protect against url recursion in write().
2787 0 ▶ $str = $url unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
2788 0 $self->{_writing_url} = 1;
2789 0 my $error = $self->write($row1, $col1, $str, $xf);
2790 0 $self->{_writing_url} = 0;
2791 0 ▶ return $error if $error == -2;
Branch: 1 branch
BranchTrueFalseTotal
if $error == -2 0 0 0
2792  
2793  
2794 # Pack the undocumented parts of the hyperlink stream
2795 0 my $unknown1 = pack("H*", "D0C9EA79F9BACE118C8200AA004BA90B02000000");
2796  
2797  
2798 # Pack the option flags
2799 0 my $options = pack("V", 0x08);
2800  
2801  
2802 # URL encoding.
2803 0 my $encoding = 0;
2804  
2805  
2806 # Convert an Utf8 URL type and to a null terminated wchar string.
2807 0 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 0 0 0
2808 0 require Encode;
2809  
2810 0 ▶ if (Encode::is_utf8($url)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($url)) 0 0 0
2811 # Quote sheet name if not already, i.e., Sheet!A1 to 'Sheet!A1'.
2812 0 ▶ $url =~ s/^(.+)!/'$1'!/ if not $url =~ /^'/;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /^'/ 0 0 0
2813  
2814 0 $url = Encode::encode("UTF-16LE", $url);
2815 0 $url .= "\0\0"; # URL is null terminated.
2816 0 $encoding = 1;
2817 }
2818 }
2819  
2820  
2821 # Convert an Ascii URL type and to a null terminated wchar string.
2822 0 ▶ if ($encoding == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding == 0) 0 0 0
2823 0 $url .= "\0";
2824 0 $url = pack 'v*', unpack 'c*', $url;
2825 }
2826  
2827  
2828 # Pack the length of the URL as chars (not wchars)
2829 0 my $url_len = pack("V", int(length($url)/2));
2830  
2831  
2832 # Calculate the data length
2833 0 $length = 0x24 + length($url);
2834  
2835  
2836 # Pack the header data
2837 0 my $header = pack("vv", $record, $length);
2838 0 my $data = pack("vvvv", $row1, $row2, $col1, $col2);
2839  
2840  
2841 # Write the packed data
2842 0 $self->_append( $header,
2843 $data,
2844 $unknown1,
2845 $options,
2846 $url_len,
2847 $url);
2848  
2849 0 return $error;
2850 }
2851  
2852  
2853 ###############################################################################
2854 #
2855 # _write_url_external($row1, $col1, $row2, $col2, $url, $string, $format)
2856 #
2857 # Write links to external directory names such as 'c:\foo.xls',
2858 # c:\foo.xls#Sheet1!A1', '../../foo.xls'. and '../../foo.xls#Sheet1!A1'.
2859 #
2860 # Note: Excel writes some relative links with the $dir_long string. We ignore
2861 # these cases for the sake of simpler code.
2862 #
2863 # See also write_url() above for a general description and return values.
2864 #
2865 0 ▶sub _write_url_external {
Subroutine _write_url_external: not called CC=8 SCAR=42.8
2866  
2867 0 my $self = shift;
2868  
2869 # Network drives are different. We will handle them separately
2870 # MS/Novell network drives and shares start with \\
2871 0 ▶ return $self->_write_url_external_net(@_) if $_[4] =~ m[^external:\\\\];
Branch: 1 branch
BranchTrueFalseTotal
if $_[4] =~ /^external:\\\\/ 0 0 0
2872  
2873  
2874 0 my $record = 0x01B8; # Record identifier
2875 0 my $length = 0x00000; # Bytes to follow
2876  
2877 0 my $row1 = $_[0]; # Start row
2878 0 my $col1 = $_[1]; # Start column
2879 0 my $row2 = $_[2]; # End row
2880 0 my $col2 = $_[3]; # End column
2881 0 my $url = $_[4]; # URL string
2882 0 my $str = $_[5]; # Alternative label
2883 0 ▶ my $xf = $_[6] || $self->{_url_format};# The cell format
Condition0%0/3
$_[6] || $self->{'_url_format'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[6] || $self->{'_url_format'}
$_[6] $self->{'_url_format'}
Truth table0%0/3
A || B
A= $_[6]B= $self->{'_url_format'}
ABresult
1X1
011
000
2884  
2885  
2886 # Strip URL type and change Unix dir separator to Dos style (if needed)
2887 #
2888 0 $url =~ s[^external:][];
2889 0 $url =~ s[/][\\]g;
2890  
2891  
2892 # Write the visible label but protect against url recursion in write().
2893 0 ▶ ($str = $url) =~ s[\#][ - ] unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
2894 0 $self->{_writing_url} = 1;
2895 0 my $error = $self->write($row1, $col1, $str, $xf);
2896 0 $self->{_writing_url} = 0;
2897 0 ▶ return $error if $error == -2;
Branch: 1 branch
BranchTrueFalseTotal
if $error == -2 0 0 0
2898  
2899  
2900 # Determine if the link is relative or absolute:
2901 # Absolute if link starts with DOS drive specifier like C:
2902 # Otherwise default to 0x00 for relative link.
2903 #
2904 0 my $absolute = 0x00;
2905 0 ▶ $absolute = 0x02 if $url =~ m/^[A-Za-z]:/;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /^[A-Za-z]:/ 0 0 0
2906  
2907  
2908 # Determine if the link contains a sheet reference and change some of the
2909 # parameters accordingly.
2910 # Split the dir name and sheet name (if it exists)
2911 #
2912 0 my ($dir_long , $sheet) = split /\#/, $url;
2913 0 my $link_type = 0x01 | $absolute;
2914 0 my $sheet_len;
2915  
2916 0 ▶ if (defined $sheet) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sheet) { } 0 0 0
2917 0 $link_type |= 0x08;
2918 0 $sheet_len = pack("V", length($sheet) + 0x01);
2919 0 $sheet = join("\0", split('', $sheet));
2920 0 $sheet .= "\0\0\0";
2921 }
2922 else {
2923 0 $sheet_len = '';
2924 0 $sheet = '';
2925 }
2926  
2927  
2928 # Pack the link type
2929 0 $link_type = pack("V", $link_type);
2930  
2931  
2932 # Calculate the up-level dir count e.g. (..\..\..\ == 3)
2933 0 my $up_count = 0;
2934 0 ▶ $up_count++ while $dir_long =~ s[^\.\.\\][];
Branch: 1 branch
BranchTrueFalseTotal
$dir_long =~ s/^\.\.\\// and do { ++$up_count } 0 0 0
2935 0 $up_count = pack("v", $up_count);
2936  
2937  
2938 # Store the short dos dir name (null terminated)
2939 0 my $dir_short = $dir_long . "\0";
2940  
2941  
2942 # Store the long dir name as a wchar string (non-null terminated)
2943 0 $dir_long = join("\0", split('', $dir_long));
2944 0 $dir_long = $dir_long . "\0";
2945  
2946  
2947 # Pack the lengths of the dir strings
2948 0 my $dir_short_len = pack("V", length $dir_short );
2949 0 my $dir_long_len = pack("V", length $dir_long );
2950 0 my $stream_len = pack("V", length($dir_long) + 0x06);
2951  
2952  
2953 # Pack the undocumented parts of the hyperlink stream
2954 0 my $unknown1 =pack("H*",'D0C9EA79F9BACE118C8200AA004BA90B02000000' );
2955 0 my $unknown2 =pack("H*",'0303000000000000C000000000000046' );
2956 0 my $unknown3 =pack("H*",'FFFFADDE000000000000000000000000000000000000000');
2957 0 my $unknown4 =pack("v", 0x03 );
2958  
2959  
2960 # Pack the main data stream
2961 0 my $data = pack("vvvv", $row1, $row2, $col1, $col2) .
2962 $unknown1 .
2963 $link_type .
2964 $unknown2 .
2965 $up_count .
2966 $dir_short_len.
2967 $dir_short .
2968 $unknown3 .
2969 $stream_len .
2970 $dir_long_len .
2971 $unknown4 .
2972 $dir_long .
2973 $sheet_len .
2974 $sheet ;
2975  
2976  
2977 # Pack the header data
2978 0 $length = length $data;
2979 0 my $header = pack("vv", $record, $length);
2980  
2981  
2982 # Write the packed data
2983 0 $self->_append($header, $data);
2984  
2985 0 return $error;
2986 }
2987  
2988  
2989  
2990  
2991 ###############################################################################
2992 #
2993 # _write_url_external_net($row1, $col1, $row2, $col2, $url, $string, $format)
2994 #
2995 # Write links to external MS/Novell network drives and shares such as
2996 # '//NETWORK/share/foo.xls' and '//NETWORK/share/foo.xls#Sheet1!A1'.
2997 #
2998 # See also write_url() above for a general description and return values.
2999 #
3000 0 ▶sub _write_url_external_net {
Subroutine _write_url_external_net: not called CC=5 SCAR=34.0
3001  
3002 0 my $self = shift;
3003  
3004 0 my $record = 0x01B8; # Record identifier
3005 0 my $length = 0x00000; # Bytes to follow
3006  
3007 0 my $row1 = $_[0]; # Start row
3008 0 my $col1 = $_[1]; # Start column
3009 0 my $row2 = $_[2]; # End row
3010 0 my $col2 = $_[3]; # End column
3011 0 my $url = $_[4]; # URL string
3012 0 my $str = $_[5]; # Alternative label
3013 0 ▶ my $xf = $_[6] || $self->{_url_format};# The cell format
Condition0%0/3
$_[6] || $self->{'_url_format'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[6] || $self->{'_url_format'}
$_[6] $self->{'_url_format'}
Truth table0%0/3
A || B
A= $_[6]B= $self->{'_url_format'}
ABresult
1X1
011
000
3014  
3015  
3016 # Strip URL type and change Unix dir separator to Dos style (if needed)
3017 #
3018 0 $url =~ s[^external:][];
3019 0 $url =~ s[/][\\]g;
3020  
3021  
3022 # Write the visible label but protect against url recursion in write().
3023 0 ▶ ($str = $url) =~ s[\#][ - ] unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
3024 0 $self->{_writing_url} = 1;
3025 0 my $error = $self->write($row1, $col1, $str, $xf);
3026 0 $self->{_writing_url} = 0;
3027 0 ▶ return $error if $error == -2;
Branch: 1 branch
BranchTrueFalseTotal
if $error == -2 0 0 0
3028  
3029  
3030 # Determine if the link contains a sheet reference and change some of the
3031 # parameters accordingly.
3032 # Split the dir name and sheet name (if it exists)
3033 #
3034 0 my ($dir_long , $sheet) = split /\#/, $url;
3035 0 my $link_type = 0x0103; # Always absolute
3036 0 my $sheet_len;
3037  
3038 0 ▶ if (defined $sheet) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sheet) { } 0 0 0
3039 0 $link_type |= 0x08;
3040 0 $sheet_len = pack("V", length($sheet) + 0x01);
3041 0 $sheet = join("\0", split('', $sheet));
3042 0 $sheet .= "\0\0\0";
3043 }
3044 else {
3045 0 $sheet_len = '';
3046 0 $sheet = '';
3047 }
3048  
3049 # Pack the link type
3050 0 $link_type = pack("V", $link_type);
3051  
3052  
3053 # Make the string null terminated
3054 0 $dir_long = $dir_long . "\0";
3055  
3056  
3057 # Pack the lengths of the dir string
3058 0 my $dir_long_len = pack("V", length $dir_long);
3059  
3060  
3061 # Store the long dir name as a wchar string (non-null terminated)
3062 0 $dir_long = join("\0", split('', $dir_long));
3063 0 $dir_long = $dir_long . "\0";
3064  
3065  
3066 # Pack the undocumented part of the hyperlink stream
3067 0 my $unknown1 = pack("H*",'D0C9EA79F9BACE118C8200AA004BA90B02000000');
3068  
3069  
3070 # Pack the main data stream
3071 0 my $data = pack("vvvv", $row1, $row2, $col1, $col2) .
3072 $unknown1 .
3073 $link_type .
3074 $dir_long_len .
3075 $dir_long .
3076 $sheet_len .
3077 $sheet ;
3078  
3079  
3080 # Pack the header data
3081 0 $length = length $data;
3082 0 my $header = pack("vv", $record, $length);
3083  
3084  
3085 # Write the packed data
3086 0 $self->_append($header, $data);
3087  
3088 0 return $error;
3089 }
3090  
3091  
3092 ###############################################################################
3093 #
3094 # write_date_time ($row, $col, $string, $format)
3095 #
3096 # Write a datetime string in ISO8601 "yyyy-mm-ddThh:mm:ss.ss" format as a
3097 # number representing an Excel date. $format is optional.
3098 #
3099 # Returns 0 : normal termination
3100 # -1 : insufficient number of arguments
3101 # -2 : row or column out of range
3102 # -3 : Invalid date_time, written as string
3103 #
3104 0 ▶sub write_date_time {
Subroutine write_date_time: not called CC=5 SCAR=34.0
Pod undocumented
3105  
3106 0 my $self = shift;
3107  
3108 # Check for a cell reference in A1 notation and substitute row and column
3109 0 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 0 0
3110 0 @_ = $self->_substitute_cellref(@_);
3111 }
3112  
3113 0 ▶ if (@_ < 3) { return -1 } # Check the number of args
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3) 0 0 0
3114  
3115 0 my $row = $_[0]; # Zero indexed row
3116 0 my $col = $_[1]; # Zero indexed column
3117 0 my $str = $_[2];
3118  
3119  
3120 # Check that row and col are valid and store max and min values
3121 0 ▶ return -2 if $self->_check_dimensions($row, $col);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_dimensions($row, $col) 0 0 0
3122  
3123 0 my $error = 0;
3124 0 my $date_time = $self->convert_date_time($str);
3125  
3126 0 ▶ if (defined $date_time) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $date_time) { } 0 0 0
3127 0 $error = $self->write_number($row, $col, $date_time, $_[3]);
3128 }
3129 else {
3130 # The date isn't valid so write it as a string.
3131 0 $self->write_string($row, $col, $str, $_[3]);
3132 0 $error = -3;
3133 }
3134 0 return $error;
3135 }
3136  
3137  
3138  
3139 ###############################################################################
3140 #
3141 # convert_date_time($date_time_string)
3142 #
3143 # The function takes a date and time in ISO8601 "yyyy-mm-ddThh:mm:ss.ss" format
3144 # and converts it to a decimal number representing a valid Excel date.
3145 #
3146 # Dates and times in Excel are represented by real numbers. The integer part of
3147 # the number stores the number of days since the epoch and the fractional part
3148 # stores the percentage of the day in seconds. The epoch can be either 1900 or
3149 # 1904.
3150 #
3151 # Parameter: Date and time string in one of the following formats:
3152 # yyyy-mm-ddThh:mm:ss.ss # Standard
3153 # yyyy-mm-ddT # Date only
3154 # Thh:mm:ss.ss # Time only
3155 #
3156 # Returns:
3157 # A decimal number representing a valid Excel date, or
3158 # undef if the date is invalid.
3159 #
3160 630 ▶sub convert_date_time {
Subroutine convert_date_time: called CC=30 SCAR=34.0
Pod undocumented
3161  
3162 630 my $self = shift;
3163 630 my $date_time = $_[0];
3164  
3165 630 my $days = 0; # Number of days since epoch
3166 630 my $seconds = 0; # Time expressed as fraction of 24h hours in seconds
3167  
3168 630 my ($year, $month, $day);
3169 630 my ($hour, $min, $sec);
3170  
3171  
3172 # Strip leading and trailing whitespace.
3173 630 $date_time =~ s/^\s+//;
3174 630 $date_time =~ s/\s+$//;
3175  
3176 # Check for invalid date char.
3177 630 ▶ return if $date_time =~ /[^0-9T:\-\.Z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $date_time =~ /[^0-9T:\-\.Z]/ 0 630 630
3178  
3179 # Check for "T" after date or before time.
3180 630 ▶ return unless $date_time =~ /\dT|T\d/;
Branch: 1 branch
BranchTrueFalseTotal
unless $date_time =~ /\dT|T\d/ 0 630 630
3181  
3182 # Strip trailing Z in ISO8601 date.
3183 630 $date_time =~ s/Z$//;
3184  
3185  
3186 # Split into date and time.
3187 630 my ($date, $time) = split /T/, $date_time;
3188  
3189  
3190 # We allow the time portion of the input DateTime to be optional.
3191 630 ▶ if ($time ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($time ne "") 206 424 630
3192 # Match hh:mm:ss.sss+ where the seconds are optional
3193 206 ▶ if ($time =~ /^(\d\d):(\d\d)(:(\d\d(\.\d+)?))?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time =~ /^(\d\d):(\d\d)(:(\d\d(\.\d+)?))?/) { } 206 0 206
3194 206 $hour = $1;
3195 206 $min = $2;
3196 206 ▶ $sec = $4 || 0;
Condition100%2/2
$4 || 0
l!l
2051
MC/DC100%1/1
$4 || 0
$4
Truth table100%2/2
A
A= $4
Aresult
11
00
3197 }
3198 else {
3199 0 return undef; # Not a valid time format.
3200 }
3201  
3202 # Some boundary checks
3203 206 ▶ return if $hour >= 24;
Branch: 1 branch
BranchTrueFalseTotal
if $hour >= 24 1 205 206
3204 205 ▶ return if $min >= 60;
Branch: 1 branch
BranchTrueFalseTotal
if $min >= 60 1 204 205
3205 204 ▶ return if $sec >= 60;
Branch: 1 branch
BranchTrueFalseTotal
if $sec >= 60 2 202 204
3206  
3207 # Excel expresses seconds as a fraction of the number in 24 hours.
3208 202 $seconds = ($hour *60*60 + $min *60 + $sec) / (24 *60 *60);
3209 }
3210  
3211  
3212 # We allow the date portion of the input DateTime to be optional.
3213 626 ▶ return $seconds if $date eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $date eq "" 2 624 626
3214  
3215  
3216 # Match date as yyyy-mm-dd.
3217 624 ▶ if ($date =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/) { } 622 2 624
3218 622 $year = $1;
3219 622 $month = $2;
3220 622 $day = $3;
3221 }
3222 else {
3223 2 return undef; # Not a valid date format.
3224 }
3225  
3226 # Set the epoch as 1900 or 1904. Defaults to 1900.
3227 622 my $date_1904 = $self->{_1904};
3228  
3229  
3230 # Special cases for Excel.
3231 622 ▶ if (not $date_1904) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($date_1904) 413 209 622
3232 413 ▶ return $seconds if $date eq '1899-12-31'; # Excel 1900 epoch
Branch: 1 branch
BranchTrueFalseTotal
if $date eq "1899-12-31" 104 309 413
3233 309 ▶ return $seconds if $date eq '1900-01-00'; # Excel 1900 epoch
Branch: 1 branch
BranchTrueFalseTotal
if $date eq "1900-01-00" 1 308 309
3234 308 ▶ return 60 + $seconds if $date eq '1900-02-29'; # Excel false leapday
Branch: 1 branch
BranchTrueFalseTotal
if $date eq "1900-02-29" 1 307 308
3235 }
3236  
3237  
3238 # We calculate the date by calculating the number of days since the epoch
3239 # and adjust for the number of leap days. We calculate the number of leap
3240 # days by normalising the year in relation to the epoch. Thus the year 2000
3241 # becomes 100 for 4 and 100 year leapdays and 400 for 400 year leapdays.
3242 #
3243 516 ▶ my $epoch = $date_1904 ? 1904 : 1900;
Branch: 1 branch
BranchTrueFalseTotal
$date_1904 ? : 209 307 516
3244 516 ▶ my $offset = $date_1904 ? 4 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$date_1904 ? : 209 307 516
3245 516 my $norm = 300;
3246 516 my $range = $year -$epoch;
3247  
3248  
3249 # Set month days and check for leap year.
3250 516 my @mdays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
3251 516 my $leap = 0;
3252 516 ▶ $leap = 1 if $year % 4 == 0 and $year % 100 or $year % 400 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $year % 4 == 0 and $year % 100 or $year % 400 == 0 211 305 516
Condition100%6/6
$year % 4 == 0 and $year % 100 or $year % 400 == 0
l!l&&r!l&&!r
56155305
$year % 4 == 0 and $year % 100
!ll&&!rl&&r
29516556
MC/DC100%3/3
$year % 4 == 0 and $year % 100 or $year % 400 == 0
$year % 4 == 0 $year % 100 $year % 400 == 0
Truth table80%4/5
A and B or C
A= $year % 4 == 0B= $year % 100C= $year % 400 == 0
ABCresult
11X1
0X11
1011
0X00
1000
3253 516 ▶ $mdays[1] = 29 if $leap;
Branch: 1 branch
BranchTrueFalseTotal
if $leap 211 305 516
3254  
3255  
3256 # Some boundary checks
3257 516 ▶ return if $year < $epoch or $year > 9999;
Branch: 1 branch
BranchTrueFalseTotal
if $year < $epoch or $year > 9999 6 510 516
Condition66%2/3
$year < $epoch or $year > 9999
l!l&&r!l&&!r
60510
MC/DC50%1/2
$year < $epoch or $year > 9999
$year < $epoch $year > 9999
Truth table66%2/3
A or B
A= $year < $epochB= $year > 9999
ABresult
1X1
011
000
3258 510 ▶ return if $month < 1 or $month > 12;
Branch: 1 branch
BranchTrueFalseTotal
if $month < 1 or $month > 12 6 504 510
Condition100%3/3
$month < 1 or $month > 12
l!l&&r!l&&!r
42504
MC/DC100%2/2
$month < 1 or $month > 12
$month < 1 $month > 12
Truth table100%3/3
A or B
A= $month < 1B= $month > 12
ABresult
1X1
011
000
3259 504 ▶ return if $day < 1 or $day > $mdays[$month -1];
Branch: 1 branch
BranchTrueFalseTotal
if $day < 1 or $day > $mdays[$month - 1] 6 498 504
Condition100%3/3
$day < 1 or $day > $mdays[$month - 1]
l!l&&r!l&&!r
24498
MC/DC100%2/2
$day < 1 or $day > $mdays[$month - 1]
$day < 1 $day > $mdays[$month - 1]
Truth table100%3/3
A or B
A= $day < 1B= $day > $mdays[$month - 1]
ABresult
1X1
011
000
3260  
3261 # Accumulate the number of days since the epoch.
3262 498 $days = $day; # Add days for current month
3263 498 $days += $mdays[$_] for 0 .. $month -2; # Add days for past months
3264 498 $days += $range *365; # Add days for past years
3265 498 $days += int(($range) / 4); # Add leapdays
3266 498 $days -= int(($range +$offset) /100); # Subtract 100 year leapdays
3267 498 $days += int(($range +$offset +$norm)/400); # Add 400 year leapdays
3268 498 $days -= $leap; # Already counted above
3269  
3270  
3271 # Adjust for Excel erroneously treating 1900 as a leap year.
3272 498 ▶ $days++ if $date_1904 == 0 and $days > 59;
Branch: 1 branch
BranchTrueFalseTotal
if $date_1904 == 0 and $days > 59 295 203 498
Condition100%3/3
$date_1904 == 0 and $days > 59
!ll&&!rl&&r
2003295
MC/DC100%2/2
$date_1904 == 0 and $days > 59
$date_1904 == 0 $days > 59
Truth table100%3/3
A and B
A= $date_1904 == 0B= $days > 59
ABresult
0X0
100
111
3273  
3274 498 return $days + $seconds;
3275 }
3276  
3277  
3278  
3279  
3280  
3281 ###############################################################################
3282 #
3283 # set_row($row, $height, $XF, $hidden, $level)
3284 #
3285 # This method is used to set the height and XF format for a row.
3286 # Writes the BIFF record ROW.
3287 #
3288 15 ▶sub set_row {
Subroutine set_row: called CC=16 SCAR=28.2
Pod undocumented
3289  
3290 15 my $self = shift;
3291 15 my $record = 0x0208; # Record identifier
3292 15 my $length = 0x0010; # Number of bytes to follow
3293  
3294 15 my $row = $_[0]; # Row Number
3295 15 my $colMic = 0x0000; # First defined column
3296 15 my $colMac = 0x0000; # Last defined column
3297 15 my $miyRw; # Row height
3298 15 my $irwMac = 0x0000; # Used by Excel to optimise loading
3299 15 my $reserved = 0x0000; # Reserved
3300 15 my $grbit = 0x0000; # Option flags
3301 15 my $ixfe; # XF index
3302 15 my $height = $_[1]; # Row height
3303 15 my $format = $_[2]; # Format object
3304 15 ▶ my $hidden = $_[3] || 0; # Hidden flag
Condition50%1/2
$_[3] || 0
l!l
015
MC/DC0%0/1
$_[3] || 0
$_[3]
Truth table50%1/2
A
A= $_[3]
Aresult
11
00
3305 15 ▶ my $level = $_[4] || 0; # Outline level
Condition50%1/2
$_[4] || 0
l!l
015
MC/DC0%0/1
$_[4] || 0
$_[4]
Truth table50%1/2
A
A= $_[4]
Aresult
11
00
3306 15 ▶ my $collapsed = $_[5] || 0; # Collapsed row
Condition50%1/2
$_[5] || 0
l!l
015
MC/DC0%0/1
$_[5] || 0
$_[5]
Truth table50%1/2
A
A= $_[5]
Aresult
11
00
3307  
3308  
3309 15 ▶ return unless defined $row; # Ensure at least $row is specified.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 15 15
3310  
3311 # Check that row and col are valid and store max and min values
3312 15 ▶ return -2 if $self->_check_dimensions($row, 0, 0, 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_dimensions($row, 0, 0, 1) 0 15 15
3313  
3314 # Check for a format object
3315 15 ▶ if (ref $format) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $format) { } 1 14 15
3316 1 $ixfe = $format->get_xf_index();
3317 }
3318 else {
3319 14 $ixfe = 0x0F;
3320 }
3321  
3322  
3323 # Set the row height in units of 1/20 of a point. Note, some heights may
3324 # not be obtained exactly due to rounding in Excel.
3325 #
3326 15 ▶ if (defined $height) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $height) { } 14 1 15
3327 14 $miyRw = $height *20;
3328 }
3329 else {
3330 1 $miyRw = 0xff; # The default row height
3331 1 $height = 0;
3332 }
3333  
3334  
3335 # Set the limits for the outline levels (0 <= x <= 7).
3336 15 ▶ $level = 0 if $level < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $level < 0 0 15 15
3337 15 ▶ $level = 7 if $level > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $level > 7 0 15 15
3338  
3339 15 ▶ $self->{_outline_row_level} = $level if $level >$self->{_outline_row_level};
Branch: 1 branch
BranchTrueFalseTotal
if $level > $self->{"_outline_row_level"} 0 15 15
3340  
3341  
3342 # Set the options flags.
3343 # 0x10: The fCollapsed flag indicates that the row contains the "+"
3344 # when an outline group is collapsed.
3345 # 0x20: The fDyZero height flag indicates a collapsed or hidden row.
3346 # 0x40: The fUnsynced flag is used to show that the font and row heights
3347 # are not compatible. This is usually the case for WriteExcel.
3348 # 0x80: The fGhostDirty flag indicates that the row has been formatted.
3349 #
3350 15 $grbit |= $level;
3351 15 ▶ $grbit |= 0x0010 if $collapsed;
Branch: 1 branch
BranchTrueFalseTotal
if $collapsed 0 15 15
3352 15 ▶ $grbit |= 0x0020 if $hidden;
Branch: 1 branch
BranchTrueFalseTotal
if $hidden 0 15 15
3353 15 $grbit |= 0x0040;
3354 15 ▶ $grbit |= 0x0080 if $format;
Branch: 1 branch
BranchTrueFalseTotal
if $format 1 14 15
3355 15 $grbit |= 0x0100;
3356  
3357  
3358 15 my $header = pack("vv", $record, $length);
3359 15 my $data = pack("vvvvvvvv", $row, $colMic, $colMac, $miyRw,
3360 $irwMac,$reserved, $grbit, $ixfe);
3361  
3362  
3363 # Store the data or write immediately depending on the compatibility mode.
3364 15 ▶ if ($self->{_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_compatibility'}) { } 2 13 15
3365 2 $self->{_row_data}->{$_[0]} = $header . $data;
3366 }
3367 else {
3368 13 $self->_append($header, $data);
3369 }
3370  
3371  
3372 # Store the row sizes for use when calculating image vertices.
3373 # Also store the row formats.
3374 15 $self->{_row_sizes}->{$_[0]} = $height;
3375 15 ▶ $self->{_row_formats}->{$_[0]} = $format if defined $format;
Branch: 1 branch
BranchTrueFalseTotal
if defined $format 1 14 15
3376 }
3377  
3378  
3379  
3380 ###############################################################################
3381 #
3382 # _write_row_default()
3383 #
3384 # Write a default row record, in compatibility mode, for rows that don't have
3385 # user specified values..
3386 #
3387 5 ▶sub _write_row_default {
Subroutine _write_row_default: called CC=1 SCAR=0.0
3388  
3389 5 my $self = shift;
3390 5 my $record = 0x0208; # Record identifier
3391 5 my $length = 0x0010; # Number of bytes to follow
3392  
3393 5 my $row = $_[0]; # Row Number
3394 5 my $colMic = $_[1]; # First defined column
3395 5 my $colMac = $_[2]; # Last defined column
3396 5 my $miyRw = 0xFF; # Row height
3397 5 my $irwMac = 0x0000; # Used by Excel to optimise loading
3398 5 my $reserved = 0x0000; # Reserved
3399 5 my $grbit = 0x0100; # Option flags
3400 5 my $ixfe = 0x0F; # XF index
3401  
3402 5 my $header = pack("vv", $record, $length);
3403 5 my $data = pack("vvvvvvvv", $row, $colMic, $colMac, $miyRw,
3404 $irwMac,$reserved, $grbit, $ixfe);
3405  
3406 5 $self->_append($header, $data);
3407 }
3408  
3409  
3410 ###############################################################################
3411 #
3412 # _check_dimensions($row, $col, $ignore_row, $ignore_col)
3413 #
3414 # Check that $row and $col are valid and store max and min values for use in
3415 # DIMENSIONS record. See, _store_dimensions().
3416 #
3417 # The $ignore_row/$ignore_col flags is used to indicate that we wish to
3418 # perform the dimension check without storing the value.
3419 #
3420 # The ignore flags are use by set_row() and data_validate.
3421 #
3422 22697 ▶sub _check_dimensions {
Subroutine _check_dimensions: called CC=15 SCAR=27.1
3423  
3424 22697 my $self = shift;
3425 22697 my $row = $_[0];
3426 22697 my $col = $_[1];
3427 22697 my $ignore_row = $_[2];
3428 22697 my $ignore_col = $_[3];
3429  
3430  
3431 22697 ▶ return -2 if not defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 1 22696 22697
3432 22696 ▶ return -2 if $row >= $self->{_xls_rowmax};
Branch: 1 branch
BranchTrueFalseTotal
if $row >= $self->{"_xls_rowmax"} 0 22696 22696
3433  
3434 22696 ▶ return -2 if not defined $col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col 1 22695 22696
3435 22695 ▶ return -2 if $col >= $self->{_xls_colmax};
Branch: 1 branch
BranchTrueFalseTotal
if $col >= $self->{"_xls_colmax"} 0 22695 22695
3436  
3437  
3438 22695 ▶ if (not $ignore_row) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore_row) 22609 86 22695
3439  
3440 22609 ▶ if (not defined $self->{_dim_rowmin} or $row < $self->{_dim_rowmin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"_dim_rowmin"} or $row < $self->{"_dim_rowmin"}) 68 22541 22609
Condition100%3/3
not defined $self->{"_dim_rowmin"} or $row < $self->{"_dim_rowmin"}
l!l&&r!l&&!r
65322541
MC/DC100%2/2
not defined $self->{"_dim_rowmin"} or $row < $self->{"_dim_rowmin"}
not defined $self->{"_dim_rowmin"} $row < $self->{"_dim_rowmin"}
Truth table100%3/3
not(A) or B
A= not defined $self->{"_dim_rowmin"}B= $row < $self->{"_dim_rowmin"}
ABresult
1X1
011
000
3441 68 $self->{_dim_rowmin} = $row;
3442 }
3443  
3444 22609 ▶ if (not defined $self->{_dim_rowmax} or $row > $self->{_dim_rowmax}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"_dim_rowmax"} or $row > $self->{"_dim_rowmax"}) 20535 2074 22609
Condition100%3/3
not defined $self->{"_dim_rowmax"} or $row > $self->{"_dim_rowmax"}
l!l&&r!l&&!r
65204702074
MC/DC100%2/2
not defined $self->{"_dim_rowmax"} or $row > $self->{"_dim_rowmax"}
not defined $self->{"_dim_rowmax"} $row > $self->{"_dim_rowmax"}
Truth table100%3/3
not(A) or B
A= not defined $self->{"_dim_rowmax"}B= $row > $self->{"_dim_rowmax"}
ABresult
1X1
011
000
3445 20535 $self->{_dim_rowmax} = $row;
3446 }
3447 }
3448  
3449 22695 ▶ if (not $ignore_col) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore_col) 22594 101 22695
3450  
3451 22594 ▶ if (not defined $self->{_dim_colmin} or $col < $self->{_dim_colmin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"_dim_colmin"} or $col < $self->{"_dim_colmin"}) 62 22532 22594
Condition100%3/3
not defined $self->{"_dim_colmin"} or $col < $self->{"_dim_colmin"}
l!l&&r!l&&!r
61122532
MC/DC100%2/2
not defined $self->{"_dim_colmin"} or $col < $self->{"_dim_colmin"}
not defined $self->{"_dim_colmin"} $col < $self->{"_dim_colmin"}
Truth table100%3/3
not(A) or B
A= not defined $self->{"_dim_colmin"}B= $col < $self->{"_dim_colmin"}
ABresult
1X1
011
000
3452 62 $self->{_dim_colmin} = $col;
3453 }
3454  
3455 22594 ▶ if (not defined $self->{_dim_colmax} or $col > $self->{_dim_colmax}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"_dim_colmax"} or $col > $self->{"_dim_colmax"}) 69 22525 22594
Condition100%3/3
not defined $self->{"_dim_colmax"} or $col > $self->{"_dim_colmax"}
l!l&&r!l&&!r
61822525
MC/DC100%2/2
not defined $self->{"_dim_colmax"} or $col > $self->{"_dim_colmax"}
not defined $self->{"_dim_colmax"} $col > $self->{"_dim_colmax"}
Truth table100%3/3
not(A) or B
A= not defined $self->{"_dim_colmax"}B= $col > $self->{"_dim_colmax"}
ABresult
1X1
011
000
3456 69 $self->{_dim_colmax} = $col;
3457 }
3458 }
3459  
3460 22695 return 0;
3461 }
3462  
3463  
3464 ###############################################################################
3465 #
3466 # _store_dimensions()
3467 #
3468 # Writes Excel DIMENSIONS to define the area in which there is cell data.
3469 #
3470 # Notes:
3471 # Excel stores the max row/col as row/col +1.
3472 # Max and min values of 0 are used to indicate that no cell data.
3473 # We set the undef member data to 0 since it is used by _store_table().
3474 # Inserting images or charts doesn't change the DIMENSION data.
3475 #
3476 164 ▶sub _store_dimensions {
Subroutine _store_dimensions: called CC=5 SCAR=16.1
3477  
3478 164 my $self = shift;
3479 164 my $record = 0x0200; # Record identifier
3480 164 my $length = 0x000E; # Number of bytes to follow
3481 164 my $row_min; # First row
3482 my $row_max; # Last row plus 1
3483 164 my $col_min; # First column
3484 164 my $col_max; # Last column plus 1
3485 164 my $reserved = 0x0000; # Reserved by Excel
3486  
3487 164 ▶ if (defined $self->{_dim_rowmin}) {$row_min = $self->{_dim_rowmin} }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_dim_rowmin'}) { } 94 70 164
3488 70 else {$row_min = 0 }
3489  
3490 164 ▶ if (defined $self->{_dim_rowmax}) {$row_max = $self->{_dim_rowmax} + 1}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_dim_rowmax'}) { } 94 70 164
3491 70 else {$row_max = 0 }
3492  
3493 164 ▶ if (defined $self->{_dim_colmin}) {$col_min = $self->{_dim_colmin} }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_dim_colmin'}) { } 90 74 164
3494 74 else {$col_min = 0 }
3495  
3496 164 ▶ if (defined $self->{_dim_colmax}) {$col_max = $self->{_dim_colmax} + 1}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_dim_colmax'}) { } 90 74 164
3497 74 else {$col_max = 0 }
3498  
3499  
3500 # Set member data to the new max/min value for use by _store_table().
3501 164 $self->{_dim_rowmin} = $row_min;
3502 164 $self->{_dim_rowmax} = $row_max;
3503 164 $self->{_dim_colmin} = $col_min;
3504 164 $self->{_dim_colmax} = $col_max;
3505  
3506  
3507 164 my $header = pack("vv", $record, $length);
3508 164 my $data = pack("VVvvv", $row_min, $row_max,
3509 $col_min, $col_max, $reserved);
3510 164 $self->_prepend($header, $data);
3511 }
3512  
3513  
3514 ###############################################################################
3515 #
3516 # _store_window2()
3517 #
3518 # Write BIFF record Window2.
3519 #
3520 133 ▶sub _store_window2 {
Subroutine _store_window2: called CC=1 SCAR=0.0
3521  
3522 32 ▶ use integer; # Avoid << shift bug in Perl 5.6.0 on HP-UX
Subroutine BEGIN: called CC=1 SCAR=0.0
3523  
3524 133 my $self = shift;
3525 133 my $record = 0x023E; # Record identifier
3526 133 my $length = 0x0012; # Number of bytes to follow
3527  
3528 133 my $grbit = 0x00B6; # Option flags
3529 133 my $rwTop = $self->{_first_row}; # Top visible row
3530 133 my $colLeft = $self->{_first_col}; # Leftmost visible column
3531 133 my $rgbHdr = 0x00000040; # Row/col heading, grid color
3532  
3533 133 my $wScaleSLV = 0x0000; # Zoom in page break preview
3534 133 my $wScaleNormal = 0x0000; # Zoom in normal view
3535 133 my $reserved = 0x00000000;
3536  
3537  
3538 # The options flags that comprise $grbit
3539 133 my $fDspFmla = $self->{_display_formulas}; # 0 - bit
3540 133 my $fDspGrid = $self->{_screen_gridlines}; # 1
3541 133 my $fDspRwCol = $self->{_display_headers}; # 2
3542 133 my $fFrozen = $self->{_frozen}; # 3
3543 133 my $fDspZeros = $self->{_display_zeros}; # 4
3544 133 my $fDefaultHdr = 1; # 5
3545 133 my $fArabic = $self->{_display_arabic}; # 6
3546 133 my $fDspGuts = $self->{_outline_on}; # 7
3547 133 my $fFrozenNoSplit = $self->{_frozen_no_split}; # 0 - bit
3548 133 my $fSelected = $self->{_selected}; # 1
3549 133 my $fPaged = $self->{_active}; # 2
3550 133 my $fBreakPreview = 0; # 3
3551  
3552 133 $grbit = $fDspFmla;
3553 133 $grbit |= $fDspGrid << 1;
3554 133 $grbit |= $fDspRwCol << 2;
3555 133 $grbit |= $fFrozen << 3;
3556 133 $grbit |= $fDspZeros << 4;
3557 133 $grbit |= $fDefaultHdr << 5;
3558 133 $grbit |= $fArabic << 6;
3559 133 $grbit |= $fDspGuts << 7;
3560 133 $grbit |= $fFrozenNoSplit << 8;
3561 133 $grbit |= $fSelected << 9;
3562 133 $grbit |= $fPaged << 10;
3563 133 $grbit |= $fBreakPreview << 11;
3564  
3565 133 my $header = pack("vv", $record, $length);
3566 133 my $data = pack("vvvVvvV", $grbit, $rwTop, $colLeft, $rgbHdr,
3567 $wScaleSLV, $wScaleNormal, $reserved );
3568  
3569 133 $self->_append($header, $data);
3570 }
3571  
3572  
3573 ###############################################################################
3574 #
3575 # _store_page_view()
3576 #
3577 # Set page view mode. Only applicable to Mac Excel.
3578 #
3579 133 ▶sub _store_page_view {
Subroutine _store_page_view: called CC=2 SCAR=9.2
3580  
3581 133 my $self = shift;
3582  
3583 133 ▶ return unless $self->{_page_view};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_page_view"} 133 0 133
3584  
3585 0 my $data = pack "H*", 'C8081100C808000000000040000000000900000000';
3586  
3587 0 $self->_append($data);
3588 }
3589  
3590  
3591 ###############################################################################
3592 #
3593 # _store_tab_color()
3594 #
3595 # Write the Tab Color BIFF record.
3596 #
3597 135 ▶sub _store_tab_color {
Subroutine _store_tab_color: called CC=2 SCAR=11.6
3598  
3599 135 my $self = shift;
3600 135 my $color = $self->{_tab_color};
3601  
3602 135 ▶ return unless $color;
Branch: 1 branch
BranchTrueFalseTotal
unless $color 135 0 135
3603  
3604 0 my $record = 0x0862; # Record identifier
3605 0 my $length = 0x0014; # Number of bytes to follow
3606  
3607 0 my $zero = 0x0000;
3608 0 my $unknown = 0x0014;
3609  
3610 0 my $header = pack("vv", $record, $length);
3611 0 my $data = pack("vvvvvvvvvv", $record, $zero, $zero, $zero, $zero,
3612 $zero, $unknown, $zero, $color, $zero);
3613  
3614 0 $self->_append($header, $data);
3615 }
3616  
3617  
3618 ###############################################################################
3619 #
3620 # _store_defrow()
3621 #
3622 # Write BIFF record DEFROWHEIGHT.
3623 #
3624 133 ▶sub _store_defrow {
Subroutine _store_defrow: called CC=1 SCAR=0.0
3625  
3626 133 my $self = shift;
3627 133 my $record = 0x0225; # Record identifier
3628 133 my $length = 0x0004; # Number of bytes to follow
3629  
3630 133 my $grbit = 0x0000; # Options.
3631 133 my $height = 0x00FF; # Default row height
3632  
3633 133 my $header = pack("vv", $record, $length);
3634 133 my $data = pack("vv", $grbit, $height);
3635  
3636 133 $self->_prepend($header, $data);
3637 }
3638  
3639  
3640 ###############################################################################
3641 #
3642 # _store_defcol()
3643 #
3644 # Write BIFF record DEFCOLWIDTH.
3645 #
3646 133 ▶sub _store_defcol {
Subroutine _store_defcol: called CC=1 SCAR=0.0
3647  
3648 133 my $self = shift;
3649 133 my $record = 0x0055; # Record identifier
3650 133 my $length = 0x0002; # Number of bytes to follow
3651  
3652 133 my $colwidth = 0x0008; # Default column width
3653  
3654 133 my $header = pack("vv", $record, $length);
3655 133 my $data = pack("v", $colwidth);
3656  
3657 133 $self->_prepend($header, $data);
3658 }
3659  
3660  
3661 ###############################################################################
3662 #
3663 # _store_colinfo($firstcol, $lastcol, $width, $format, $hidden)
3664 #
3665 # Write BIFF record COLINFO to define column widths
3666 #
3667 # Note: The SDK says the record length is 0x0B but Excel writes a 0x0C
3668 # length record.
3669 #
3670 5 ▶sub _store_colinfo {
Subroutine _store_colinfo: called CC=13 SCAR=26.7
3671  
3672 5 my $self = shift;
3673 5 my $record = 0x007D; # Record identifier
3674 5 my $length = 0x000B; # Number of bytes to follow
3675  
3676 5 ▶ my $colFirst = $_[0] || 0; # First formatted column
Condition50%1/2
$_[0] || 0
l!l
50
MC/DC0%0/1
$_[0] || 0
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
3677 5 ▶ my $colLast = $_[1] || 0; # Last formatted column
Condition50%1/2
$_[1] || 0
l!l
50
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
3678 5 ▶ my $width = $_[2] || 8.43; # Col width in user units, 8.43 is default
Condition100%2/2
$_[2] || 8.43
l!l
41
MC/DC100%1/1
$_[2] || 8.43
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
3679 5 my $coldx; # Col width in internal units
3680 my $pixels; # Col width in pixels
3681  
3682 # Excel rounds the column width to the nearest pixel. Therefore we first
3683 # convert to pixels and then to the internal units. The pixel to users-units
3684 # relationship is different for values less than 1.
3685 #
3686 5 ▶ if ($width < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width < 1) { } 0 5 5
3687 0 $pixels = int($width *12);
3688 }
3689 else {
3690 5 $pixels = int($width *7 ) +5;
3691 }
3692  
3693 5 $coldx = int($pixels *256/7);
3694  
3695  
3696 5 my $ixfe; # XF index
3697 5 my $grbit = 0x0000; # Option flags
3698 5 my $reserved = 0x00; # Reserved
3699 5 my $format = $_[3]; # Format object
3700 5 ▶ my $hidden = $_[4] || 0; # Hidden flag
Condition50%1/2
$_[4] || 0
l!l
05
MC/DC0%0/1
$_[4] || 0
$_[4]
Truth table50%1/2
A
A= $_[4]
Aresult
11
00
3701 5 ▶ my $level = $_[5] || 0; # Outline level
Condition50%1/2
$_[5] || 0
l!l
05
MC/DC0%0/1
$_[5] || 0
$_[5]
Truth table50%1/2
A
A= $_[5]
Aresult
11
00
3702 5 ▶ my $collapsed = $_[6] || 0; # Outline level
Condition50%1/2
$_[6] || 0
l!l
05
MC/DC0%0/1
$_[6] || 0
$_[6]
Truth table50%1/2
A
A= $_[6]
Aresult
11
00
3703  
3704  
3705 # Check for a format object
3706 5 ▶ if (ref $format) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $format) { } 1 4 5
3707 1 $ixfe = $format->get_xf_index();
3708 }
3709 else {
3710 4 $ixfe = 0x0F;
3711 }
3712  
3713  
3714 # Set the limits for the outline levels (0 <= x <= 7).
3715 5 ▶ $level = 0 if $level < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $level < 0 0 5 5
3716 5 ▶ $level = 7 if $level > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $level > 7 0 5 5
3717  
3718  
3719 # Set the options flags. (See set_row() for more details).
3720 5 ▶ $grbit |= 0x0001 if $hidden;
Branch: 1 branch
BranchTrueFalseTotal
if $hidden 0 5 5
3721 5 $grbit |= $level << 8;
3722 5 ▶ $grbit |= 0x1000 if $collapsed;
Branch: 1 branch
BranchTrueFalseTotal
if $collapsed 0 5 5
3723  
3724  
3725 5 my $header = pack("vv", $record, $length);
3726 5 my $data = pack("vvvvvC", $colFirst, $colLast, $coldx,
3727 $ixfe, $grbit, $reserved);
3728  
3729 5 $self->_prepend($header, $data);
3730 }
3731  
3732  
3733 ###############################################################################
3734 #
3735 # _store_filtermode()
3736 #
3737 # Write BIFF record FILTERMODE to indicate that the worksheet contains
3738 # AUTOFILTER record, ie. autofilters with a filter set.
3739 #
3740 133 ▶sub _store_filtermode {
Subroutine _store_filtermode: called CC=2 SCAR=7.9
3741  
3742 133 my $self = shift;
3743  
3744 133 my $record = 0x009B; # Record identifier
3745 133 my $length = 0x0000; # Number of bytes to follow
3746  
3747 # Only write the record if the worksheet contains a filtered autofilter.
3748 133 ▶ return unless $self->{_filter_on};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_filter_on"} 133 0 133
3749  
3750 0 my $header = pack("vv", $record, $length);
3751  
3752 0 $self->_prepend($header);
3753 }
3754  
3755  
3756 ###############################################################################
3757 #
3758 # _store_autofilterinfo()
3759 #
3760 # Write BIFF record AUTOFILTERINFO.
3761 #
3762 133 ▶sub _store_autofilterinfo {
Subroutine _store_autofilterinfo: called CC=2 SCAR=6.9
3763  
3764 133 my $self = shift;
3765  
3766 133 my $record = 0x009D; # Record identifier
3767 133 my $length = 0x0002; # Number of bytes to follow
3768 133 my $num_filters = $self->{_filter_count};
3769  
3770 # Only write the record if the worksheet contains an autofilter.
3771 133 ▶ return unless $self->{_filter_count};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_filter_count"} 130 3 133
3772  
3773 3 my $header = pack("vv", $record, $length);
3774 3 my $data = pack("v", $num_filters);
3775  
3776 3 $self->_prepend($header, $data);
3777 }
3778  
3779  
3780 ###############################################################################
3781 #
3782 # _store_selection($first_row, $first_col, $last_row, $last_col)
3783 #
3784 # Write BIFF record SELECTION.
3785 #
3786 133 ▶sub _store_selection {
Subroutine _store_selection: called CC=5 SCAR=17.1
3787  
3788 133 my $self = shift;
3789 133 my $record = 0x001D; # Record identifier
3790 133 my $length = 0x000F; # Number of bytes to follow
3791  
3792 133 my $pnn = $self->{_active_pane}; # Pane position
3793 133 my $rwAct = $_[0]; # Active row
3794 133 my $colAct = $_[1]; # Active column
3795 133 my $irefAct = 0; # Active cell ref
3796 133 my $cref = 1; # Number of refs
3797  
3798 133 my $rwFirst = $_[0]; # First row in reference
3799 133 my $colFirst = $_[1]; # First col in reference
3800 133 ▶ my $rwLast = $_[2] || $rwFirst; # Last row in reference
Condition33%1/3
$_[2] || $rwFirst
l!l&&r!l&&!r
00133
MC/DC0%0/2
$_[2] || $rwFirst
$_[2] $rwFirst
Truth table33%1/3
A || B
A= $_[2]B= $rwFirst
ABresult
1X1
011
000
3801 133 ▶ my $colLast = $_[3] || $colFirst; # Last col in reference
Condition33%1/3
$_[3] || $colFirst
l!l&&r!l&&!r
00133
MC/DC0%0/2
$_[3] || $colFirst
$_[3] $colFirst
Truth table33%1/3
A || B
A= $_[3]B= $colFirst
ABresult
1X1
011
000
3802  
3803 # Swap last row/col for first row/col as necessary
3804 133 ▶ if ($rwFirst > $rwLast) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rwFirst > $rwLast) 0 133 133
3805 0 ($rwFirst, $rwLast) = ($rwLast, $rwFirst);
3806 }
3807  
3808 133 ▶ if ($colFirst > $colLast) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colFirst > $colLast) 0 133 133
3809 0 ($colFirst, $colLast) = ($colLast, $colFirst);
3810 }
3811  
3812  
3813 133 my $header = pack("vv", $record, $length);
3814 133 my $data = pack("CvvvvvvCC", $pnn, $rwAct, $colAct,
3815 $irefAct, $cref,
3816 $rwFirst, $rwLast,
3817 $colFirst, $colLast);
3818  
3819 133 $self->_append($header, $data);
3820 }
3821  
3822  
3823 ###############################################################################
3824 #
3825 # _store_externcount($count)
3826 #
3827 # Write BIFF record EXTERNCOUNT to indicate the number of external sheet
3828 # references in a worksheet.
3829 #
3830 # Excel only stores references to external sheets that are used in formulas.
3831 # For simplicity we store references to all the sheets in the workbook
3832 # regardless of whether they are used or not. This reduces the overall
3833 # complexity and eliminates the need for a two way dialogue between the formula
3834 # parser the worksheet objects.
3835 #
3836 0 ▶sub _store_externcount {
Subroutine _store_externcount: not called CC=1 SCAR=6.9
3837  
3838 0 my $self = shift;
3839 0 my $record = 0x0016; # Record identifier
3840 0 my $length = 0x0002; # Number of bytes to follow
3841  
3842 0 my $cxals = $_[0]; # Number of external references
3843  
3844 0 my $header = pack("vv", $record, $length);
3845 0 my $data = pack("v", $cxals);
3846  
3847 0 $self->_prepend($header, $data);
3848 }
3849  
3850  
3851 ###############################################################################
3852 #
3853 # _store_externsheet($sheetname)
3854 #
3855 #
3856 # Writes the Excel BIFF EXTERNSHEET record. These references are used by
3857 # formulas. A formula references a sheet name via an index. Since we store a
3858 # reference to all of the external worksheets the EXTERNSHEET index is the same
3859 # as the worksheet index.
3860 #
3861 0 ▶sub _store_externsheet {
Subroutine _store_externsheet: not called CC=2 SCAR=17.9
3862  
3863 0 my $self = shift;
3864  
3865 0 my $record = 0x0017; # Record identifier
3866 0 my $length; # Number of bytes to follow
3867  
3868 0 my $sheetname = $_[0]; # Worksheet name
3869 0 my $cch; # Length of sheet name
3870 my $rgch; # Filename encoding
3871  
3872 # References to the current sheet are encoded differently to references to
3873 # external sheets.
3874 #
3875 0 ▶ if ($self->{_name} eq $sheetname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_name'} eq $sheetname) { } 0 0 0
3876 0 $sheetname = '';
3877 0 $length = 0x02; # The following 2 bytes
3878 0 $cch = 1; # The following byte
3879 0 $rgch = 0x02; # Self reference
3880 }
3881 else {
3882 0 $length = 0x02 + length($_[0]);
3883 0 $cch = length($sheetname);
3884 0 $rgch = 0x03; # Reference to a sheet in the current workbook
3885 }
3886  
3887 0 my $header = pack("vv", $record, $length);
3888 0 my $data = pack("CC", $cch, $rgch);
3889  
3890 0 $self->_prepend($header, $data, $sheetname);
3891 }
3892  
3893  
3894 ###############################################################################
3895 #
3896 # _store_panes()
3897 #
3898 #
3899 # Writes the Excel BIFF PANE record.
3900 # The panes can either be frozen or thawed (unfrozen).
3901 # Frozen panes are specified in terms of a integer number of rows and columns.
3902 # Thawed panes are specified in terms of Excel's units for rows and columns.
3903 #
3904 0 ▶sub _store_panes {
Subroutine _store_panes: not called CC=17 SCAR=57.2
3905  
3906 0 my $self = shift;
3907 0 my $record = 0x0041; # Record identifier
3908 0 my $length = 0x000A; # Number of bytes to follow
3909  
3910 0 ▶ my $y = $_[0] || 0; # Vertical split position
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
3911 0 ▶ my $x = $_[1] || 0; # Horizontal split position
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
3912 0 my $rwTop = $_[2]; # Top row visible
3913 0 my $colLeft = $_[3]; # Leftmost column visible
3914 0 my $no_split = $_[4]; # No used here.
3915 0 my $pnnAct = $_[5]; # Active pane
3916  
3917  
3918 # Code specific to frozen or thawed panes.
3919 0 ▶ if ($self->{_frozen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_frozen'}) { } 0 0 0
3920 # Set default values for $rwTop and $colLeft
3921 0 ▶ $rwTop = $y unless defined $rwTop;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rwTop 0 0 0
3922 0 ▶ $colLeft = $x unless defined $colLeft;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $colLeft 0 0 0
3923 }
3924 else {
3925 # Set default values for $rwTop and $colLeft
3926 0 ▶ $rwTop = 0 unless defined $rwTop;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rwTop 0 0 0
3927 0 ▶ $colLeft = 0 unless defined $colLeft;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $colLeft 0 0 0
3928  
3929 # Convert Excel's row and column units to the internal units.
3930 # The default row height is 12.75
3931 # The default column width is 8.43
3932 # The following slope and intersection values were interpolated.
3933 #
3934 0 $y = 20*$y + 255;
3935 0 $x = 113.879*$x + 390;
3936 }
3937  
3938  
3939 # Determine which pane should be active. There is also the undocumented
3940 # option to override this should it be necessary: may be removed later.
3941 #
3942 0 ▶ if (not defined $pnnAct) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pnnAct) 0 0 0
3943 0 ▶ $pnnAct = 0 if ($x != 0 && $y != 0); # Bottom right
Branch: 1 branch
BranchTrueFalseTotal
if $x != 0 and $y != 0 0 0 0
Condition0%0/3
$x != 0 and $y != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$x != 0 and $y != 0
$x != 0 $y != 0
Truth table0%0/3
A and B
A= $x != 0B= $y != 0
ABresult
0X0
100
111
3944 0 ▶ $pnnAct = 1 if ($x != 0 && $y == 0); # Top right
Branch: 1 branch
BranchTrueFalseTotal
if $x != 0 and $y == 0 0 0 0
Condition0%0/3
$x != 0 and $y == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$x != 0 and $y == 0
$x != 0 $y == 0
Truth table0%0/3
A and B
A= $x != 0B= $y == 0
ABresult
0X0
100
111
3945 0 ▶ $pnnAct = 2 if ($x == 0 && $y != 0); # Bottom left
Branch: 1 branch
BranchTrueFalseTotal
if $x == 0 and $y != 0 0 0 0
Condition0%0/3
$x == 0 and $y != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$x == 0 and $y != 0
$x == 0 $y != 0
Truth table0%0/3
A and B
A= $x == 0B= $y != 0
ABresult
0X0
100
111
3946 0 ▶ $pnnAct = 3 if ($x == 0 && $y == 0); # Top left
Branch: 1 branch
BranchTrueFalseTotal
if $x == 0 and $y == 0 0 0 0
Condition0%0/3
$x == 0 and $y == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$x == 0 and $y == 0
$x == 0 $y == 0
Truth table0%0/3
A and B
A= $x == 0B= $y == 0
ABresult
0X0
100
111
3947 }
3948  
3949 0 $self->{_active_pane} = $pnnAct; # Used in _store_selection
3950  
3951 0 my $header = pack("vv", $record, $length);
3952 0 my $data = pack("vvvvv", $x, $y, $rwTop, $colLeft, $pnnAct);
3953  
3954 0 $self->_append($header, $data);
3955 }
3956  
3957  
3958 ###############################################################################
3959 #
3960 # _store_setup()
3961 #
3962 # Store the page setup SETUP BIFF record.
3963 #
3964 135 ▶sub _store_setup {
Subroutine _store_setup: called CC=2 SCAR=6.9
3965  
3966 32 ▶ use integer; # Avoid << shift bug in Perl 5.6.0 on HP-UX
Subroutine BEGIN: called CC=1 SCAR=0.0
3967  
3968 135 my $self = shift;
3969 135 my $record = 0x00A1; # Record identifier
3970 135 my $length = 0x0022; # Number of bytes to follow
3971  
3972  
3973 135 my $iPaperSize = $self->{_paper_size}; # Paper size
3974 135 my $iScale = $self->{_print_scale}; # Print scaling factor
3975 135 my $iPageStart = $self->{_page_start}; # Starting page number
3976 135 my $iFitWidth = $self->{_fit_width}; # Fit to number of pages wide
3977 135 my $iFitHeight = $self->{_fit_height}; # Fit to number of pages high
3978 135 my $grbit = 0x00; # Option flags
3979 135 my $iRes = 0x0258; # Print resolution
3980 135 my $iVRes = 0x0258; # Vertical print resolution
3981 135 my $numHdr = $self->{_margin_header}; # Header Margin
3982 135 my $numFtr = $self->{_margin_footer}; # Footer Margin
3983 135 my $iCopies = 0x01; # Number of copies
3984  
3985  
3986 135 my $fLeftToRight = $self->{_page_order}; # Print over then down
3987 135 my $fLandscape = $self->{_orientation}; # Page orientation
3988 135 my $fNoPls = 0x0; # Setup not read from printer
3989 135 my $fNoColor = $self->{_black_white}; # Print black and white
3990 135 my $fDraft = $self->{_draft_quality}; # Print draft quality
3991 135 my $fNotes = $self->{_print_comments};# Print notes
3992 135 my $fNoOrient = 0x0; # Orientation not set
3993 135 my $fUsePage = $self->{_custom_start}; # Use custom starting page
3994  
3995  
3996 135 $grbit = $fLeftToRight;
3997 135 $grbit |= $fLandscape << 1;
3998 135 $grbit |= $fNoPls << 2;
3999 135 $grbit |= $fNoColor << 3;
4000 135 $grbit |= $fDraft << 4;
4001 135 $grbit |= $fNotes << 5;
4002 135 $grbit |= $fNoOrient << 6;
4003 135 $grbit |= $fUsePage << 7;
4004  
4005  
4006 135 $numHdr = pack("d", $numHdr);
4007 135 $numFtr = pack("d", $numFtr);
4008  
4009 135 ▶ if ($self->{_byte_order}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_byte_order"}) 0 135 135
4010 0 $numHdr = reverse $numHdr;
4011 0 $numFtr = reverse $numFtr;
4012 }
4013  
4014 135 my $header = pack("vv", $record, $length);
4015 135 my $data1 = pack("vvvvvvvv", $iPaperSize,
4016 $iScale,
4017 $iPageStart,
4018 $iFitWidth,
4019 $iFitHeight,
4020 $grbit,
4021 $iRes,
4022 $iVRes);
4023 135 my $data2 = $numHdr .$numFtr;
4024 135 my $data3 = pack("v", $iCopies);
4025  
4026 135 $self->_prepend($header, $data1, $data2, $data3);
4027  
4028 }
4029  
4030 ###############################################################################
4031 #
4032 # _store_header()
4033 #
4034 # Store the header caption BIFF record.
4035 #
4036 135 ▶sub _store_header {
Subroutine _store_header: called CC=3 SCAR=11.0
4037  
4038 135 my $self = shift;
4039  
4040 135 my $record = 0x0014; # Record identifier
4041 135 my $length; # Bytes to follow
4042  
4043 135 my $str = $self->{_header}; # header string
4044 135 my $cch = length($str); # Length of header string
4045 135 my $encoding = $self->{_header_encoding}; # Character encoding
4046  
4047  
4048 # Character length is num of chars not num of bytes
4049 135 ▶ $cch /= 2 if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 0 135 135
4050  
4051 # Change the UTF-16 name from BE to LE
4052 135 ▶ $str = pack 'n*', unpack 'v*', $str if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 0 135 135
4053  
4054 135 $length = 3 + length($str);
4055  
4056 135 my $header = pack("vv", $record, $length);
4057 135 my $data = pack("vC", $cch, $encoding);
4058  
4059 135 $self->_prepend($header, $data, $str);
4060 }
4061  
4062  
4063 ###############################################################################
4064 #
4065 # _store_footer()
4066 #
4067 # Store the footer caption BIFF record.
4068 #
4069 135 ▶sub _store_footer {
Subroutine _store_footer: called CC=3 SCAR=11.0
4070  
4071 135 my $self = shift;
4072  
4073 135 my $record = 0x0015; # Record identifier
4074 135 my $length; # Bytes to follow
4075  
4076 135 my $str = $self->{_footer}; # footer string
4077 135 my $cch = length($str); # Length of footer string
4078 135 my $encoding = $self->{_footer_encoding}; # Character encoding
4079  
4080  
4081 # Character length is num of chars not num of bytes
4082 135 ▶ $cch /= 2 if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 0 135 135
4083  
4084 # Change the UTF-16 name from BE to LE
4085 135 ▶ $str = pack 'n*', unpack 'v*', $str if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 0 135 135
4086  
4087 135 $length = 3 + length($str);
4088  
4089 135 my $header = pack("vv", $record, $length);
4090 135 my $data = pack("vC", $cch, $encoding);
4091  
4092 135 $self->_prepend($header, $data, $str);
4093 }
4094  
4095  
4096 ###############################################################################
4097 #
4098 # _store_hcenter()
4099 #
4100 # Store the horizontal centering HCENTER BIFF record.
4101 #
4102 135 ▶sub _store_hcenter {
Subroutine _store_hcenter: called CC=1 SCAR=0.0
4103  
4104 135 my $self = shift;
4105  
4106 135 my $record = 0x0083; # Record identifier
4107 135 my $length = 0x0002; # Bytes to follow
4108  
4109 135 my $fHCenter = $self->{_hcenter}; # Horizontal centering
4110  
4111 135 my $header = pack("vv", $record, $length);
4112 135 my $data = pack("v", $fHCenter);
4113  
4114 135 $self->_prepend($header, $data);
4115 }
4116  
4117  
4118 ###############################################################################
4119 #
4120 # _store_vcenter()
4121 #
4122 # Store the vertical centering VCENTER BIFF record.
4123 #
4124 135 ▶sub _store_vcenter {
Subroutine _store_vcenter: called CC=1 SCAR=0.0
4125  
4126 135 my $self = shift;
4127  
4128 135 my $record = 0x0084; # Record identifier
4129 135 my $length = 0x0002; # Bytes to follow
4130  
4131 135 my $fVCenter = $self->{_vcenter}; # Horizontal centering
4132  
4133 135 my $header = pack("vv", $record, $length);
4134 135 my $data = pack("v", $fVCenter);
4135  
4136 135 $self->_prepend($header, $data);
4137 }
4138  
4139  
4140 ###############################################################################
4141 #
4142 # _store_margin_left()
4143 #
4144 # Store the LEFTMARGIN BIFF record.
4145 #
4146 135 ▶sub _store_margin_left {
Subroutine _store_margin_left: called CC=2 SCAR=7.1
4147  
4148 135 my $self = shift;
4149  
4150 135 my $record = 0x0026; # Record identifier
4151 135 my $length = 0x0008; # Bytes to follow
4152  
4153 135 my $margin = $self->{_margin_left}; # Margin in inches
4154  
4155 135 my $header = pack("vv", $record, $length);
4156 135 my $data = pack("d", $margin);
4157  
4158 0 ▶ if ($self->{_byte_order}) { $data = reverse $data }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_byte_order"}) 0 135 135
4159  
4160 135 $self->_prepend($header, $data);
4161 }
4162  
4163  
4164 ###############################################################################
4165 #
4166 # _store_margin_right()
4167 #
4168 # Store the RIGHTMARGIN BIFF record.
4169 #
4170 135 ▶sub _store_margin_right {
Subroutine _store_margin_right: called CC=2 SCAR=7.1
4171  
4172 135 my $self = shift;
4173  
4174 135 my $record = 0x0027; # Record identifier
4175 135 my $length = 0x0008; # Bytes to follow
4176  
4177 135 my $margin = $self->{_margin_right}; # Margin in inches
4178  
4179 135 my $header = pack("vv", $record, $length);
4180 135 my $data = pack("d", $margin);
4181  
4182 0 ▶ if ($self->{_byte_order}) { $data = reverse $data }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_byte_order"}) 0 135 135
4183  
4184 135 $self->_prepend($header, $data);
4185 }
4186  
4187  
4188 ###############################################################################
4189 #
4190 # _store_margin_top()
4191 #
4192 # Store the TOPMARGIN BIFF record.
4193 #
4194 135 ▶sub _store_margin_top {
Subroutine _store_margin_top: called CC=2 SCAR=7.1
4195  
4196 135 my $self = shift;
4197  
4198 135 my $record = 0x0028; # Record identifier
4199 135 my $length = 0x0008; # Bytes to follow
4200  
4201 135 my $margin = $self->{_margin_top}; # Margin in inches
4202  
4203 135 my $header = pack("vv", $record, $length);
4204 135 my $data = pack("d", $margin);
4205  
4206 0 ▶ if ($self->{_byte_order}) { $data = reverse $data }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_byte_order"}) 0 135 135
4207  
4208 135 $self->_prepend($header, $data);
4209 }
4210  
4211  
4212 ###############################################################################
4213 #
4214 # _store_margin_bottom()
4215 #
4216 # Store the BOTTOMMARGIN BIFF record.
4217 #
4218 135 ▶sub _store_margin_bottom {
Subroutine _store_margin_bottom: called CC=2 SCAR=7.1
4219  
4220 135 my $self = shift;
4221  
4222 135 my $record = 0x0029; # Record identifier
4223 135 my $length = 0x0008; # Bytes to follow
4224  
4225 135 my $margin = $self->{_margin_bottom}; # Margin in inches
4226  
4227 135 my $header = pack("vv", $record, $length);
4228 135 my $data = pack("d", $margin);
4229  
4230 0 ▶ if ($self->{_byte_order}) { $data = reverse $data }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_byte_order"}) 0 135 135
4231  
4232 135 $self->_prepend($header, $data);
4233 }
4234  
4235  
4236 ###############################################################################
4237 #
4238 # merge_cells($first_row, $first_col, $last_row, $last_col)
4239 #
4240 # This is an Excel97/2000 method. It is required to perform more complicated
4241 # merging than the normal align merge in Format.pm
4242 #
4243 3 ▶sub merge_cells {
Subroutine merge_cells: called CC=8 SCAR=23.4
Pod undocumented
4244  
4245 3 my $self = shift;
4246  
4247 # Check for a cell reference in A1 notation and substitute row and column
4248 3 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 3 3
4249 0 @_ = $self->_substitute_cellref(@_);
4250 }
4251  
4252 3 my $record = 0x00E5; # Record identifier
4253 3 my $length = 0x000A; # Bytes to follow
4254  
4255 3 my $cref = 1; # Number of refs
4256 3 my $rwFirst = $_[0]; # First row in reference
4257 3 my $colFirst = $_[1]; # First col in reference
4258 3 ▶ my $rwLast = $_[2] || $rwFirst; # Last row in reference
Condition33%1/3
$_[2] || $rwFirst
l!l&&r!l&&!r
300
MC/DC0%0/2
$_[2] || $rwFirst
$_[2] $rwFirst
Truth table33%1/3
A || B
A= $_[2]B= $rwFirst
ABresult
1X1
011
000
4259 3 ▶ my $colLast = $_[3] || $colFirst; # Last col in reference
Condition33%1/3
$_[3] || $colFirst
l!l&&r!l&&!r
300
MC/DC0%0/2
$_[3] || $colFirst
$_[3] $colFirst
Truth table33%1/3
A || B
A= $_[3]B= $colFirst
ABresult
1X1
011
000
4260  
4261  
4262 # Excel doesn't allow a single cell to be merged
4263 3 ▶ return if $rwFirst == $rwLast and $colFirst == $colLast;
Branch: 1 branch
BranchTrueFalseTotal
if $rwFirst == $rwLast and $colFirst == $colLast 0 3 3
Condition33%1/3
$rwFirst == $rwLast and $colFirst == $colLast
!ll&&!rl&&r
300
MC/DC0%0/2
$rwFirst == $rwLast and $colFirst == $colLast
$rwFirst == $rwLast $colFirst == $colLast
Truth table33%1/3
A and B
A= $rwFirst == $rwLastB= $colFirst == $colLast
ABresult
0X0
100
111
4264  
4265 # Swap last row/col with first row/col as necessary
4266 3 ▶ ($rwFirst, $rwLast ) = ($rwLast, $rwFirst ) if $rwFirst > $rwLast;
Branch: 1 branch
BranchTrueFalseTotal
if $rwFirst > $rwLast 0 3 3
4267 3 ▶ ($colFirst, $colLast) = ($colLast, $colFirst) if $colFirst > $colLast;
Branch: 1 branch
BranchTrueFalseTotal
if $colFirst > $colLast 0 3 3
4268  
4269 3 my $header = pack("vv", $record, $length);
4270 3 my $data = pack("vvvvv", $cref,
4271 $rwFirst, $rwLast,
4272 $colFirst, $colLast);
4273  
4274 3 $self->_append($header, $data);
4275 }
4276  
4277  
4278 ###############################################################################
4279 #
4280 # merge_range($row1, $col1, $row2, $col2, $string, $format, $encoding)
4281 #
4282 # This is a wrapper to ensure correct use of the merge_cells method, i.e., write
4283 # the first cell of the range, write the formatted blank cells in the range and
4284 # then call the merge_cells record. Failing to do the steps in this order will
4285 # cause Excel 97 to crash.
4286 #
4287 4 ▶sub merge_range {
Subroutine merge_range: called CC=16 SCAR=29.5
Pod undocumented
4288  
4289 4 my $self = shift;
4290  
4291 # Check for a cell reference in A1 notation and substitute row and column
4292 4 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 4 0 4
4293 4 @_ = $self->_substitute_cellref(@_);
4294 }
4295 4 ▶ croak "Incorrect number of arguments" if @_ != 6 and @_ != 7;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 6 and @_ != 7 0 4 4
Condition33%1/3
@_ != 6 and @_ != 7
!ll&&!rl&&r
400
MC/DC0%0/2
@_ != 6 and @_ != 7
@_ != 6 @_ != 7
Truth table33%1/3
A and B
A= @_ != 6B= @_ != 7
ABresult
0X0
100
111
4296 4 ▶ croak "Format argument is not a format object" unless ref $_[5];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[5] 0 4 4
4297  
4298 4 my $rwFirst = $_[0];
4299 4 my $colFirst = $_[1];
4300 4 my $rwLast = $_[2];
4301 4 my $colLast = $_[3];
4302 4 my $string = $_[4];
4303 4 my $format = $_[5];
4304 4 ▶ my $encoding = $_[6] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[6] ? : 0 4 4
4305  
4306  
4307 # Temp code to prevent merged formats in non-merged cells.
4308 4 my $error = "Error: refer to merge_range() in the documentation. " .
4309 "Can't use previously non-merged format in merged cells";
4310  
4311 4 ▶ croak $error if $format->{_used_merge} == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $format->{"_used_merge"} == -1 1 3 4
4312 3 $format->{_used_merge} = 0; # Until the end of this function.
4313  
4314  
4315 # Set the merge_range property of the format object. For BIFF8+.
4316 3 $format->set_merge_range();
4317  
4318 # Excel doesn't allow a single cell to be merged
4319 3 ▶ croak "Can't merge single cell" if $rwFirst == $rwLast and
Branch: 1 branch
BranchTrueFalseTotal
if $rwFirst == $rwLast and $colFirst == $colLast 0 3 3
Condition33%1/3
$rwFirst == $rwLast and $colFirst == $colLast
!ll&&!rl&&r
300
MC/DC0%0/2
$rwFirst == $rwLast and $colFirst == $colLast
$rwFirst == $rwLast $colFirst == $colLast
Truth table33%1/3
A and B
A= $rwFirst == $rwLastB= $colFirst == $colLast
ABresult
0X0
100
111
4320 $colFirst == $colLast;
4321  
4322 # Swap last row/col with first row/col as necessary
4323 3 ▶ ($rwFirst, $rwLast ) = ($rwLast, $rwFirst ) if $rwFirst > $rwLast;
Branch: 1 branch
BranchTrueFalseTotal
if $rwFirst > $rwLast 0 3 3
4324 3 ▶ ($colFirst, $colLast) = ($colLast, $colFirst) if $colFirst > $colLast;
Branch: 1 branch
BranchTrueFalseTotal
if $colFirst > $colLast 0 3 3
4325  
4326 # Write the first cell
4327 3 ▶ if ($encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding) { } 0 3 3
4328 0 $self->write_utf16be_string($rwFirst, $colFirst, $string, $format);
4329 }
4330 else {
4331 3 $self->write ($rwFirst, $colFirst, $string, $format);
4332 }
4333  
4334 # Pad out the rest of the area with formatted blank cells.
4335 3 for my $row ($rwFirst .. $rwLast) {
4336 7 for my $col ($colFirst .. $colLast) {
4337 17 ▶ next if $row == $rwFirst and $col == $colFirst;
Branch: 1 branch
BranchTrueFalseTotal
if $row == $rwFirst and $col == $colFirst 3 14 17
Condition100%3/3
$row == $rwFirst and $col == $colFirst
!ll&&!rl&&r
1043
MC/DC100%2/2
$row == $rwFirst and $col == $colFirst
$row == $rwFirst $col == $colFirst
Truth table100%3/3
A and B
A= $row == $rwFirstB= $col == $colFirst
ABresult
0X0
100
111
4338 14 $self->write_blank($row, $col, $format);
4339 }
4340 }
4341  
4342 3 $self->merge_cells($rwFirst, $colFirst, $rwLast, $colLast);
4343  
4344 # Temp code to prevent merged formats in non-merged cells.
4345 3 $format->{_used_merge} = 1;
4346  
4347 }
4348  
4349  
4350 ###############################################################################
4351 #
4352 # _store_print_headers()
4353 #
4354 # Write the PRINTHEADERS BIFF record.
4355 #
4356 133 ▶sub _store_print_headers {
Subroutine _store_print_headers: called CC=1 SCAR=0.0
4357  
4358 133 my $self = shift;
4359  
4360 133 my $record = 0x002a; # Record identifier
4361 133 my $length = 0x0002; # Bytes to follow
4362  
4363 133 my $fPrintRwCol = $self->{_print_headers}; # Boolean flag
4364  
4365 133 my $header = pack("vv", $record, $length);
4366 133 my $data = pack("v", $fPrintRwCol);
4367  
4368 133 $self->_prepend($header, $data);
4369 }
4370  
4371  
4372 ###############################################################################
4373 #
4374 # _store_print_gridlines()
4375 #
4376 # Write the PRINTGRIDLINES BIFF record. Must be used in conjunction with the
4377 # GRIDSET record.
4378 #
4379 133 ▶sub _store_print_gridlines {
Subroutine _store_print_gridlines: called CC=1 SCAR=0.0
4380  
4381 133 my $self = shift;
4382  
4383 133 my $record = 0x002b; # Record identifier
4384 133 my $length = 0x0002; # Bytes to follow
4385  
4386 133 my $fPrintGrid = $self->{_print_gridlines}; # Boolean flag
4387  
4388 133 my $header = pack("vv", $record, $length);
4389 133 my $data = pack("v", $fPrintGrid);
4390  
4391 133 $self->_prepend($header, $data);
4392 }
4393  
4394  
4395 ###############################################################################
4396 #
4397 # _store_gridset()
4398 #
4399 # Write the GRIDSET BIFF record. Must be used in conjunction with the
4400 # PRINTGRIDLINES record.
4401 #
4402 133 ▶sub _store_gridset {
Subroutine _store_gridset: called CC=1 SCAR=0.0
4403  
4404 133 my $self = shift;
4405  
4406 133 my $record = 0x0082; # Record identifier
4407 133 my $length = 0x0002; # Bytes to follow
4408  
4409 133 my $fGridSet = not $self->{_print_gridlines}; # Boolean flag
4410  
4411 133 my $header = pack("vv", $record, $length);
4412 133 my $data = pack("v", $fGridSet);
4413  
4414 133 $self->_prepend($header, $data);
4415  
4416 }
4417  
4418  
4419 ###############################################################################
4420 #
4421 # _store_guts()
4422 #
4423 # Write the GUTS BIFF record. This is used to configure the gutter margins
4424 # where Excel outline symbols are displayed. The visibility of the gutters is
4425 # controlled by a flag in WSBOOL. See also _store_wsbool().
4426 #
4427 # We are all in the gutter but some of us are looking at the stars.
4428 #
4429 133 ▶sub _store_guts {
Subroutine _store_guts: called CC=8 SCAR=22.8
4430  
4431 133 my $self = shift;
4432  
4433 133 my $record = 0x0080; # Record identifier
4434 133 my $length = 0x0008; # Bytes to follow
4435  
4436 133 my $dxRwGut = 0x0000; # Size of row gutter
4437 133 my $dxColGut = 0x0000; # Size of col gutter
4438  
4439 133 my $row_level = $self->{_outline_row_level};
4440 133 my $col_level = 0;
4441  
4442  
4443 # Calculate the maximum column outline level. The equivalent calculation
4444 # for the row outline level is carried out in set_row().
4445 #
4446 133 foreach my $colinfo (@{$self->{_colinfo}}) {
4447 # Skip cols without outline level info.
4448 5 ▶ next if @{$colinfo} < 6;
Branch: 1 branch
BranchTrueFalseTotal
if @{$colinfo;} < 6 5 0 5
4449 0 ▶ $col_level = @{$colinfo}[5] if @{$colinfo}[5] > $col_level;
Branch: 1 branch
BranchTrueFalseTotal
if @{$colinfo;}[5] > $col_level 0 0 0
4450 }
4451  
4452  
4453 # Set the limits for the outline levels (0 <= x <= 7).
4454 133 ▶ $col_level = 0 if $col_level < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $col_level < 0 0 133 133
4455 133 ▶ $col_level = 7 if $col_level > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $col_level > 7 0 133 133
4456  
4457  
4458 # The displayed level is one greater than the max outline levels
4459 133 ▶ $row_level++ if $row_level > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $row_level > 0 0 133 133
4460 133 ▶ $col_level++ if $col_level > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $col_level > 0 0 133 133
4461  
4462 133 my $header = pack("vv", $record, $length);
4463 133 my $data = pack("vvvv", $dxRwGut, $dxColGut, $row_level, $col_level);
4464  
4465 133 $self->_prepend($header, $data);
4466  
4467 }
4468  
4469  
4470 ###############################################################################
4471 #
4472 # _store_wsbool()
4473 #
4474 # Write the WSBOOL BIFF record, mainly for fit-to-page. Used in conjunction
4475 # with the SETUP record.
4476 #
4477 133 ▶sub _store_wsbool {
Subroutine _store_wsbool: called CC=6 SCAR=18.5
4478  
4479 133 my $self = shift;
4480  
4481 133 my $record = 0x0081; # Record identifier
4482 133 my $length = 0x0002; # Bytes to follow
4483  
4484 133 my $grbit = 0x0000; # Option flags
4485  
4486 # Set the option flags
4487 133 $grbit |= 0x0001; # Auto page breaks visible
4488 133 ▶ $grbit |= 0x0020 if $self->{_outline_style}; # Auto outline styles
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_outline_style"} 0 133 133
4489 133 ▶ $grbit |= 0x0040 if $self->{_outline_below}; # Outline summary below
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_outline_below"} 133 0 133
4490 133 ▶ $grbit |= 0x0080 if $self->{_outline_right}; # Outline summary right
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_outline_right"} 133 0 133
4491 133 ▶ $grbit |= 0x0100 if $self->{_fit_page}; # Page setup fit to page
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_fit_page"} 0 133 133
4492 133 ▶ $grbit |= 0x0400 if $self->{_outline_on}; # Outline symbols displayed
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_outline_on"} 133 0 133
4493  
4494  
4495 133 my $header = pack("vv", $record, $length);
4496 133 my $data = pack("v", $grbit);
4497  
4498 133 $self->_prepend($header, $data);
4499 }
4500  
4501  
4502 ###############################################################################
4503 #
4504 # _store_hbreak()
4505 #
4506 # Write the HORIZONTALPAGEBREAKS BIFF record.
4507 #
4508 133 ▶sub _store_hbreak {
Subroutine _store_hbreak: called CC=3 SCAR=18.8
4509  
4510 133 my $self = shift;
4511  
4512 # Return if the user hasn't specified pagebreaks
4513 133 ▶ return unless @{$self->{_hbreaks}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"_hbreaks"};} 133 0 133
4514  
4515 # Sort and filter array of page breaks
4516 0 my @breaks = $self->_sort_pagebreaks(@{$self->{_hbreaks}});
4517  
4518 0 my $record = 0x001b; # Record identifier
4519 0 my $cbrk = scalar @breaks; # Number of page breaks
4520 0 my $length = 2 + 6*$cbrk; # Bytes to follow
4521  
4522  
4523 0 my $header = pack("vv", $record, $length);
4524 0 my $data = pack("v", $cbrk);
4525  
4526 # Append each page break
4527 0 foreach my $break (@breaks) {
4528 0 $data .= pack("vvv", $break, 0x0000, 0x00ff);
4529 }
4530  
4531 0 $self->_prepend($header, $data);
4532 }
4533  
4534  
4535 ###############################################################################
4536 #
4537 # _store_vbreak()
4538 #
4539 # Write the VERTICALPAGEBREAKS BIFF record.
4540 #
4541 133 ▶sub _store_vbreak {
Subroutine _store_vbreak: called CC=3 SCAR=18.8
4542  
4543 133 my $self = shift;
4544  
4545 # Return if the user hasn't specified pagebreaks
4546 133 ▶ return unless @{$self->{_vbreaks}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"_vbreaks"};} 133 0 133
4547  
4548 # Sort and filter array of page breaks
4549 0 my @breaks = $self->_sort_pagebreaks(@{$self->{_vbreaks}});
4550  
4551 0 my $record = 0x001a; # Record identifier
4552 0 my $cbrk = scalar @breaks; # Number of page breaks
4553 0 my $length = 2 + 6*$cbrk; # Bytes to follow
4554  
4555  
4556 0 my $header = pack("vv", $record, $length);
4557 0 my $data = pack("v", $cbrk);
4558  
4559 # Append each page break
4560 0 foreach my $break (@breaks) {
4561 0 $data .= pack("vvv", $break, 0x0000, 0xffff);
4562 }
4563  
4564 0 $self->_prepend($header, $data);
4565 }
4566  
4567  
4568 ###############################################################################
4569 #
4570 # _store_protect()
4571 #
4572 # Set the Biff PROTECT record to indicate that the worksheet is protected.
4573 #
4574 133 ▶sub _store_protect {
Subroutine _store_protect: called CC=2 SCAR=12.2
4575  
4576 133 my $self = shift;
4577  
4578 # Exit unless sheet protection has been specified
4579 133 ▶ return unless $self->{_protect};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_protect"} 133 0 133
4580  
4581 0 my $record = 0x0012; # Record identifier
4582 0 my $length = 0x0002; # Bytes to follow
4583  
4584 0 my $fLock = $self->{_protect}; # Worksheet is protected
4585  
4586 0 my $header = pack("vv", $record, $length);
4587 0 my $data = pack("v", $fLock);
4588  
4589 0 $self->_prepend($header, $data);
4590 }
4591  
4592  
4593 ###############################################################################
4594 #
4595 # _store_obj_protect()
4596 #
4597 # Set the Biff OBJPROTECT record to indicate that objects are protected.
4598 #
4599 133 ▶sub _store_obj_protect {
Subroutine _store_obj_protect: called CC=2 SCAR=12.2
4600  
4601 133 my $self = shift;
4602  
4603 # Exit unless sheet protection has been specified
4604 133 ▶ return unless $self->{_protect};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_protect"} 133 0 133
4605  
4606 0 my $record = 0x0063; # Record identifier
4607 0 my $length = 0x0002; # Bytes to follow
4608  
4609 0 my $fLock = $self->{_protect}; # Worksheet is protected
4610  
4611 0 my $header = pack("vv", $record, $length);
4612 0 my $data = pack("v", $fLock);
4613  
4614 0 $self->_prepend($header, $data);
4615 }
4616  
4617  
4618 ###############################################################################
4619 #
4620 # _store_password()
4621 #
4622 # Write the worksheet PASSWORD record.
4623 #
4624 135 ▶sub _store_password {
Subroutine _store_password: called CC=3 SCAR=17.9
4625  
4626 135 my $self = shift;
4627  
4628 # Exit unless sheet protection and password have been specified
4629 135 ▶ return unless $self->{_protect} and defined $self->{_password};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_protect"} and defined $self->{"_password"} 135 0 135
Condition33%1/3
$self->{"_protect"} and defined $self->{"_password"}
!ll&&!rl&&r
13500
MC/DC0%0/2
$self->{"_protect"} and defined $self->{"_password"}
$self->{"_protect"} defined $self->{"_password"}
Truth table33%1/3
A and B
A= $self->{"_protect"}B= defined $self->{"_password"}
ABresult
0X0
100
111
4630  
4631 0 my $record = 0x0013; # Record identifier
4632 0 my $length = 0x0002; # Bytes to follow
4633  
4634 0 my $wPassword = $self->{_password}; # Encoded password
4635  
4636 0 my $header = pack("vv", $record, $length);
4637 0 my $data = pack("v", $wPassword);
4638  
4639 0 $self->_prepend($header, $data);
4640 }
4641  
4642  
4643 #
4644 # Note about compatibility mode.
4645 #
4646 # Excel doesn't require every possible Biff record to be present in a file.
4647 # In particular if the indexing records INDEX, ROW and DBCELL aren't present
4648 # it just ignores the fact and reads the cells anyway. This is also true of
4649 # the EXTSST record. Gnumeric and OOo also take this approach. This allows
4650 # WriteExcel to ignore these records in order to minimise the amount of data
4651 # stored in memory. However, other third party applications that read Excel
4652 # files often expect these records to be present. In "compatibility mode"
4653 # WriteExcel writes these records and tries to be as close to an Excel
4654 # generated file as possible.
4655 #
4656 # This requires additional data to be stored in memory until the file is
4657 # about to be written. This incurs a memory and speed penalty and may not be
4658 # suitable for very large files.
4659 #
4660  
4661  
4662  
4663 ###############################################################################
4664 #
4665 # _store_table()
4666 #
4667 # Write cell data stored in the worksheet row/col table.
4668 #
4669 # This is only used when compatibity_mode() is in operation.
4670 #
4671 # This method writes ROW data, then cell data (NUMBER, LABELSST, etc) and then
4672 # DBCELL records in blocks of 32 rows. This is explained in detail (for a
4673 # change) in the Excel SDK and in the OOo Excel file format doc.
4674 #
4675 133 ▶sub _store_table {
Subroutine _store_table: called CC=11 SCAR=24.0
4676  
4677 133 my $self = shift;
4678  
4679 133 ▶ return unless $self->{_compatibility};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_compatibility"} 126 7 133
4680  
4681 # Offset from the DBCELL record back to the first ROW of the 32 row block.
4682 7 my $row_offset = 0;
4683  
4684 # Track rows that have cell data or modified by set_row().
4685 7 my @written_rows;
4686  
4687  
4688 # Write the ROW records with updated max/min col fields.
4689 #
4690 7 for my $row (0 .. $self->{_dim_rowmax} -1) {
4691 # Skip unless there is cell data in row or the row has been modified.
4692 35 ▶ next unless $self->{_table}->[$row] or $self->{_row_data}->{$row};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_table"}[$row] or $self->{"_row_data"}{$row} 28 7 35
Condition100%3/3
$self->{"_table"}[$row] or $self->{"_row_data"}{$row}
l!l&&r!l&&!r
6128
MC/DC100%2/2
$self->{"_table"}[$row] or $self->{"_row_data"}{$row}
$self->{"_table"}[$row] $self->{"_row_data"}{$row}
Truth table100%3/3
A or B
A= $self->{"_table"}[$row]B= $self->{"_row_data"}{$row}
ABresult
1X1
011
000
4693  
4694 # Store the rows with data.
4695 7 push @written_rows, $row;
4696  
4697 # Increase the row offset by the length of a ROW record;
4698 7 $row_offset += 20;
4699  
4700 # The max/min cols in the ROW records are the same as in DIMENSIONS.
4701 7 my $col_min = $self->{_dim_colmin};
4702 7 my $col_max = $self->{_dim_colmax};
4703  
4704 # Write a user specified ROW record (modified by set_row()).
4705 7 ▶ if ($self->{_row_data}->{$row}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_row_data'}{$row}) { } 2 5 7
4706 # Rewrite the min and max cols for user defined row record.
4707 2 my $packed_row = $self->{_row_data}->{$row};
4708 2 substr $packed_row, 6, 4, pack('vv', $col_min, $col_max);
4709 2 $self->_append($packed_row);
4710 }
4711 else {
4712 # Write a default Row record if there isn't a user defined ROW.
4713 5 $self->_write_row_default($row, $col_min, $col_max);
4714 }
4715  
4716  
4717  
4718 # If 32 rows have been written or we are at the last row in the
4719 # worksheet then write the cell data and the DBCELL record.
4720 #
4721 7 ▶ if (@written_rows == 32 or $row == $self->{_dim_rowmax} -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@written_rows == 32 or $row == $self->{"_dim_rowmax"} - 1) 7 0 7
Condition33%1/3
@written_rows == 32 or $row == $self->{"_dim_rowmax"} - 1
l!l&&r!l&&!r
070
MC/DC0%0/2
@written_rows == 32 or $row == $self->{"_dim_rowmax"} - 1
@written_rows == 32 $row == $self->{"_dim_rowmax"} - 1
Truth table33%1/3
A or B
A= @written_rows == 32B= $row == $self->{"_dim_rowmax"} - 1
ABresult
1X1
011
000
4722  
4723 # Offsets to the first cell of each row.
4724 7 my @cell_offsets;
4725 7 push @cell_offsets, $row_offset - 20;
4726  
4727 # Write the cell data in each row and sum their lengths for the
4728 # cell offsets.
4729 #
4730 7 for my $row (@written_rows) {
4731 7 my $cell_offset = 0;
4732  
4733 7 for my $col (@{$self->{_table}->[$row]}) {
4734 527 ▶ next unless $col;
Branch: 1 branch
BranchTrueFalseTotal
unless $col 518 9 527
4735 9 $self->_append($col);
4736 9 my $length = length $col;
4737 9 $row_offset += $length;
4738 9 $cell_offset += $length;
4739 }
4740 7 push @cell_offsets, $cell_offset;
4741 }
4742  
4743 # The last offset isn't required.
4744 7 pop @cell_offsets;
4745  
4746 # Stores the DBCELL offset for use in the INDEX record.
4747 7 push @{$self->{_db_indices}}, $self->{_datasize};
4748  
4749 # Write the DBCELL record.
4750 7 $self->_store_dbcell($row_offset, @cell_offsets);
4751  
4752 # Clear the variable for the next block of rows.
4753 7 @written_rows = ();
4754 7 @cell_offsets = ();
4755 7 $row_offset = 0;
4756 }
4757 }
4758 }
4759  
4760  
4761 ###############################################################################
4762 #
4763 # _store_dbcell()
4764 #
4765 # Store the DBCELL record using the offset calculated in _store_table().
4766 #
4767 # This is only used when compatibity_mode() is in operation.
4768 #
4769 7 ▶sub _store_dbcell {
Subroutine _store_dbcell: called CC=2 SCAR=6.9
4770  
4771 7 my $self = shift;
4772 7 my $row_offset = shift;
4773 7 my @cell_offsets = @_;
4774  
4775  
4776 7 my $record = 0x00D7; # Record identifier
4777 7 my $length = 4 + 2 * @cell_offsets; # Bytes to follow
4778  
4779  
4780 7 my $header = pack 'vv', $record, $length;
4781 7 my $data = pack 'V', $row_offset;
4782 7 $data .= pack 'v', $_ for @cell_offsets;
4783  
4784 7 $self->_append($header, $data);
4785 }
4786  
4787  
4788 ###############################################################################
4789 #
4790 # _store_index()
4791 #
4792 # Store the INDEX record using the DBCELL offsets calculated in _store_table().
4793 #
4794 # This is only used when compatibity_mode() is in operation.
4795 #
4796 133 ▶sub _store_index {
Subroutine _store_index: called CC=3 SCAR=11.0
4797  
4798 133 my $self = shift;
4799  
4800 133 ▶ return unless $self->{_compatibility};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_compatibility"} 126 7 133
4801  
4802 7 my @indices = @{$self->{_db_indices}};
4803 7 my $reserved = 0x00000000;
4804 7 my $row_min = $self->{_dim_rowmin};
4805 7 my $row_max = $self->{_dim_rowmax};
4806  
4807 7 my $record = 0x020B; # Record identifier
4808 7 my $length = 16 + 4 * @indices; # Bytes to follow
4809  
4810 7 my $header = pack 'vv', $record, $length;
4811 7 my $data = pack 'VVVV', $reserved,
4812 $row_min,
4813 $row_max,
4814 $reserved;
4815  
4816 7 for my $index (@indices) {
4817 7 $data .= pack 'V', $index + $self->{_offset} + 20 + $length +4;
4818 }
4819  
4820 7 $self->_prepend($header, $data);
4821  
4822 }
4823  
4824  
4825 ###############################################################################
4826 #
4827 # insert_chart($row, $col, $chart, $x, $y, $scale_x, $scale_y)
4828 #
4829 # Insert a chart into a worksheet. The $chart argument should be a Chart
4830 # object or else it is assumed to be a filename of an external binary file.
4831 # The latter is for backwards compatibility.
4832 #
4833 0 ▶sub insert_chart {
Subroutine insert_chart: not called CC=11 SCAR=48.8
Pod undocumented
4834  
4835 0 my $self = shift;
4836  
4837 # Check for a cell reference in A1 notation and substitute row and column
4838 0 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 0 0
4839 0 @_ = $self->_substitute_cellref(@_);
4840 }
4841  
4842 0 my $row = $_[0];
4843 0 my $col = $_[1];
4844 0 my $chart = $_[2];
4845 0 ▶ my $x_offset = $_[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
4846 0 ▶ my $y_offset = $_[4] || 0;
Condition0%0/2
$_[4] || 0
l!l
00
MC/DC0%0/1
$_[4] || 0
$_[4]
Truth table0%0/2
A
A= $_[4]
Aresult
11
00
4847 0 ▶ my $scale_x = $_[5] || 1;
Condition0%0/2
$_[5] || 1
l!l
00
MC/DC0%0/1
$_[5] || 1
$_[5]
Truth table0%0/2
A
A= $_[5]
Aresult
11
00
4848 0 ▶ my $scale_y = $_[6] || 1;
Condition0%0/2
$_[6] || 1
l!l
00
MC/DC0%0/1
$_[6] || 1
$_[6]
Truth table0%0/2
A
A= $_[6]
Aresult
11
00
4849  
4850 0 ▶ croak "Insufficient arguments in insert_chart()" unless @_ >= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 3 0 0 0
4851  
4852 0 ▶ if ( ref $chart ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $chart) { } 0 0 0
4853 # Check for a Chart object.
4854 0 ▶ croak "Not a Chart object in insert_chart()"
Branch: 1 branch
BranchTrueFalseTotal
unless $chart->isa("Spreadsheet::WriteExcel::Chart") 0 0 0
4855 unless $chart->isa( 'Spreadsheet::WriteExcel::Chart' );
4856  
4857 # Check that the chart is an embedded style chart.
4858 croak "Not a embedded style Chart object in insert_chart()"
4859 0 ▶ unless $chart->{_embedded};
Branch: 1 branch
BranchTrueFalseTotal
unless $chart->{"_embedded"} 0 0 0
4860  
4861 }
4862 else {
4863  
4864 # Assume an external bin filename.
4865 0 ▶ croak "Couldn't locate $chart in insert_chart(): $!" unless -e $chart;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $chart 0 0 0
4866 }
4867  
4868 0 $self->{_charts}->{$row}->{$col} = [
4869 $row,
4870 $col,
4871 $chart,
4872 $x_offset,
4873 $y_offset,
4874 $scale_x,
4875 $scale_y,
4876 ];
4877  
4878 }
4879  
4880 # Older method name for backwards compatibility.
4881 32 *embed_chart = *insert_chart;
4882  
4883 ###############################################################################
4884 #
4885 # insert_image($row, $col, $filename, $x, $y, $scale_x, $scale_y)
4886 #
4887 # Insert an image into the worksheet.
4888 #
4889 0 ▶sub insert_image {
Subroutine insert_image: not called CC=8 SCAR=42.8
Pod undocumented
4890  
4891 0 my $self = shift;
4892  
4893 # Check for a cell reference in A1 notation and substitute row and column
4894 0 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 0 0
4895 0 @_ = $self->_substitute_cellref(@_);
4896 }
4897  
4898 0 my $row = $_[0];
4899 0 my $col = $_[1];
4900 0 my $image = $_[2];
4901 0 ▶ my $x_offset = $_[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
4902 0 ▶ my $y_offset = $_[4] || 0;
Condition0%0/2
$_[4] || 0
l!l
00
MC/DC0%0/1
$_[4] || 0
$_[4]
Truth table0%0/2
A
A= $_[4]
Aresult
11
00
4903 0 ▶ my $scale_x = $_[5] || 1;
Condition0%0/2
$_[5] || 1
l!l
00
MC/DC0%0/1
$_[5] || 1
$_[5]
Truth table0%0/2
A
A= $_[5]
Aresult
11
00
4904 0 ▶ my $scale_y = $_[6] || 1;
Condition0%0/2
$_[6] || 1
l!l
00
MC/DC0%0/1
$_[6] || 1
$_[6]
Truth table0%0/2
A
A= $_[6]
Aresult
11
00
4905  
4906 0 ▶ croak "Insufficient arguments in insert_image()" unless @_ >= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 3 0 0 0
4907 0 ▶ croak "Couldn't locate $image: $!" unless -e $image;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $image 0 0 0
4908  
4909 0 $self->{_images}->{$row}->{$col} = [
4910 $row,
4911 $col,
4912 $image,
4913 $x_offset,
4914 $y_offset,
4915 $scale_x,
4916 $scale_y,
4917 ];
4918  
4919 }
4920  
4921 # Older method name for backwards compatibility.
4922 32 *insert_bitmap = *insert_image;
4923  
4924  
4925 ###############################################################################
4926 #
4927 # _position_object()
4928 #
4929 # Calculate the vertices that define the position of a graphical object within
4930 # the worksheet.
4931 #
4932 # +------------+------------+
4933 # | A | B |
4934 # +-----+------------+------------+
4935 # | |(x1,y1) | |
4936 # | 1 |(A1)._______|______ |
4937 # | | | | |
4938 # | | | | |
4939 # +-----+----| BITMAP |-----+
4940 # | | | | |
4941 # | 2 | |______________. |
4942 # | | | (B2)|
4943 # | | | (x2,y2)|
4944 # +---- +------------+------------+
4945 #
4946 # Example of a bitmap that covers some of the area from cell A1 to cell B2.
4947 #
4948 # Based on the width and height of the bitmap we need to calculate 8 vars:
4949 # $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
4950 # The width and height of the cells are also variable and have to be taken into
4951 # account.
4952 # The values of $col_start and $row_start are passed in from the calling
4953 # function. The values of $col_end and $row_end are calculated by subtracting
4954 # the width and height of the bitmap from the width and height of the
4955 # underlying cells.
4956 # The vertices are expressed as a percentage of the underlying cell width as
4957 # follows (rhs values are in pixels):
4958 #
4959 # x1 = X / W *1024
4960 # y1 = Y / H *256
4961 # x2 = (X-1) / W *1024
4962 # y2 = (Y-1) / H *256
4963 #
4964 # Where: X is distance from the left side of the underlying cell
4965 # Y is distance from the top of the underlying cell
4966 # W is the width of the cell
4967 # H is the height of the cell
4968 #
4969 # Note: the SDK incorrectly states that the height should be expressed as a
4970 # percentage of 1024.
4971 #
4972 22589 ▶sub _position_object {
Subroutine _position_object: called CC=9 SCAR=22.5
4973  
4974 22589 my $self = shift;
4975  
4976 22589 my $col_start; # Col containing upper left corner of object
4977 my $x1; # Distance to left side of object
4978  
4979 22589 my $row_start; # Row containing top left corner of object
4980 22589 my $y1; # Distance to top of object
4981  
4982 22589 my $col_end; # Col containing lower right corner of object
4983 22589 my $x2; # Distance to right side of object
4984  
4985 22589 my $row_end; # Row containing bottom right corner of object
4986 22589 my $y2; # Distance to bottom of object
4987  
4988 22589 my $width; # Width of image frame
4989 22589 my $height; # Height of image frame
4990  
4991 22589 ($col_start, $row_start, $x1, $y1, $width, $height) = @_;
4992  
4993  
4994 # Adjust start column for offsets that are greater than the col width
4995 22589 ▶ while ($x1 >= $self->_size_col($col_start)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 >= $self->_size_col($col_start)) 0 22589 22589
4996 0 $x1 -= $self->_size_col($col_start);
4997 0 $col_start++;
4998 }
4999  
5000 # Adjust start row for offsets that are greater than the row height
5001 22589 ▶ while ($y1 >= $self->_size_row($row_start)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 >= $self->_size_row($row_start)) 0 22589 22589
5002 0 $y1 -= $self->_size_row($row_start);
5003 0 $row_start++;
5004 }
5005  
5006  
5007 # Initialise end cell to the same as the start cell
5008 22589 $col_end = $col_start;
5009 22589 $row_end = $row_start;
5010  
5011 22589 $width = $width + $x1;
5012 22589 $height = $height + $y1;
5013  
5014  
5015 # Subtract the underlying cell widths to find the end cell of the image
5016 22589 ▶ while ($width >= $self->_size_col($col_end)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width >= $self->_size_col($col_end)) 45145 22589 67734
5017 45145 $width -= $self->_size_col($col_end);
5018 45145 $col_end++;
5019 }
5020  
5021 # Subtract the underlying cell heights to find the end cell of the image
5022 22589 ▶ while ($height >= $self->_size_row($row_end)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($height >= $self->_size_row($row_end)) 90277 22589 112866
5023 90277 $height -= $self->_size_row($row_end);
5024 90277 $row_end++;
5025 }
5026  
5027 # Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
5028 # with zero eight or width.
5029 #
5030 22589 ▶ return if $self->_size_col($col_start) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_size_col($col_start) == 0 0 22589 22589
5031 22589 ▶ return if $self->_size_col($col_end) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_size_col($col_end) == 0 0 22589 22589
5032 22589 ▶ return if $self->_size_row($row_start) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_size_row($row_start) == 0 0 22589 22589
5033 22589 ▶ return if $self->_size_row($row_end) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_size_row($row_end) == 0 0 22589 22589
5034  
5035 # Convert the pixel values to the percentage value expected by Excel
5036 22589 $x1 = $x1 / $self->_size_col($col_start) * 1024;
5037 22589 $y1 = $y1 / $self->_size_row($row_start) * 256;
5038 22589 $x2 = $width / $self->_size_col($col_end) * 1024;
5039 22589 $y2 = $height / $self->_size_row($row_end) * 256;
5040  
5041 # Simulate ceil() without calling POSIX::ceil().
5042 22589 $x1 = int($x1 +0.5);
5043 22589 $y1 = int($y1 +0.5);
5044 22589 $x2 = int($x2 +0.5);
5045 22589 $y2 = int($y2 +0.5);
5046  
5047 22589 return( $col_start, $x1,
5048 $row_start, $y1,
5049 $col_end, $x2,
5050 $row_end, $y2
5051 );
5052 }
5053  
5054  
5055 ###############################################################################
5056 #
5057 # _size_col($col)
5058 #
5059 # Convert the width of a cell from user's units to pixels. Excel rounds the
5060 # column width to the nearest pixel. If the width hasn't been set by the user
5061 # we use the default value. If the column is hidden we use a value of zero.
5062 #
5063 225824 ▶sub _size_col {
Subroutine _size_col: called CC=3 SCAR=11.1
5064  
5065 225824 my $self = shift;
5066 225824 my $col = $_[0];
5067  
5068 # Look up the cell value to see if it has been changed
5069 225824 ▶ if (exists $self->{_col_sizes}->{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_col_sizes'}{$col}) { } 17 225807 225824
5070 17 my $width = $self->{_col_sizes}->{$col};
5071  
5072 # The relationship is different for user units less than 1.
5073 17 ▶ if ($width < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width < 1) { } 0 17 17
5074 0 return int($width *12);
5075 }
5076 else {
5077 17 return int($width *7 ) +5;
5078 }
5079 }
5080 else {
5081 225807 return 64;
5082 }
5083 }
5084  
5085  
5086 ###############################################################################
5087 #
5088 # _size_row($row)
5089 #
5090 # Convert the height of a cell from user's units to pixels. By interpolation
5091 # the relationship is: y = 4/3x. If the height hasn't been set by the user we
5092 # use the default value. If the row is hidden we use a value of zero. (Not
5093 # possible to hide row yet).
5094 #
5095 316088 ▶sub _size_row {
Subroutine _size_row: called CC=3 SCAR=11.2
5096  
5097 316088 my $self = shift;
5098 316088 my $row = $_[0];
5099  
5100 # Look up the cell value to see if it has been changed
5101 316088 ▶ if (exists $self->{_row_sizes}->{$row}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_row_sizes'}{$row}) { } 11 316077 316088
5102 11 ▶ if ($self->{_row_sizes}->{$row} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_row_sizes'}{$row} == 0) { } 0 11 11
5103 0 return 0;
5104 }
5105 else {
5106 11 return int (4/3 * $self->{_row_sizes}->{$row});
5107 }
5108 }
5109 else {
5110 316077 return 17;
5111 }
5112 }
5113  
5114  
5115 ###############################################################################
5116 #
5117 # _store_zoom($zoom)
5118 #
5119 #
5120 # Store the window zoom factor. This should be a reduced fraction but for
5121 # simplicity we will store all fractions with a numerator of 100.
5122 #
5123 137 ▶sub _store_zoom {
Subroutine _store_zoom: called CC=2 SCAR=11.6
5124  
5125 137 my $self = shift;
5126  
5127 # If scale is 100 we don't need to write a record
5128 137 ▶ return if $self->{_zoom} == 100;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_zoom"} == 100 137 0 137
5129  
5130 0 my $record = 0x00A0; # Record identifier
5131 0 my $length = 0x0004; # Bytes to follow
5132  
5133 0 my $header = pack("vv", $record, $length );
5134 0 my $data = pack("vv", $self->{_zoom}, 100);
5135  
5136 0 $self->_append($header, $data);
5137 }
5138  
5139  
5140 ###############################################################################
5141 #
5142 # write_utf16be_string($row, $col, $string, $format)
5143 #
5144 # Write a Unicode string to the specified row and column (zero indexed).
5145 # $format is optional.
5146 # Returns 0 : normal termination
5147 # -1 : insufficient number of arguments
5148 # -2 : row or column out of range
5149 # -3 : long string truncated to 255 chars
5150 #
5151 2 ▶sub write_utf16be_string {
Subroutine write_utf16be_string: called CC=8 SCAR=21.7
Pod undocumented
5152  
5153 2 my $self = shift;
5154  
5155 # Check for a cell reference in A1 notation and substitute row and column
5156 2 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 2 2
5157 0 @_ = $self->_substitute_cellref(@_);
5158 }
5159  
5160 0 ▶ if (@_ < 3) { return -1 } # Check the number of args
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3) 0 2 2
5161  
5162 2 my $record = 0x00FD; # Record identifier
5163 2 my $length = 0x000A; # Bytes to follow
5164  
5165 2 my $row = $_[0]; # Zero indexed row
5166 2 my $col = $_[1]; # Zero indexed column
5167 2 my $strlen = length($_[2]);
5168 2 my $str = $_[2];
5169 2 my $xf = _XF($self, $row, $col, $_[3]); # The cell format
5170 2 my $encoding = 0x1;
5171 2 my $str_error = 0;
5172  
5173 # Check that row and col are valid and store max and min values
5174 2 ▶ return -2 if $self->_check_dimensions($row, $col);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_dimensions($row, $col) 0 2 2
5175  
5176 # Limit the utf16 string to the max number of chars (not bytes).
5177 2 ▶ if ($strlen > 32767* 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strlen > 65534) 0 2 2
5178 0 $str = substr($str, 0, 32767*2);
5179 0 $str_error = -3;
5180 }
5181  
5182  
5183 2 my $num_bytes = length $str;
5184 2 my $num_chars = int($num_bytes / 2);
5185  
5186  
5187 # Check for a valid 2-byte char string.
5188 2 ▶ croak "Uneven number of bytes in Unicode string" if $num_bytes % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $num_bytes % 2 0 2 2
5189  
5190  
5191 # Change from UTF16 big-endian to little endian
5192 2 $str = pack "v*", unpack "n*", $str;
5193  
5194  
5195 # Add the encoding and length header to the string.
5196 2 my $str_header = pack("vC", $num_chars, $encoding);
5197 2 $str = $str_header . $str;
5198  
5199  
5200 2 ▶ if (not exists ${$self->{_str_table}}->{$str}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists ${$$self{"_str_table"};}->{$str}) 2 0 2
5201 2 ${$self->{_str_table}}->{$str} = ${$self->{_str_unique}}++;
5202 }
5203  
5204  
5205 2 ${$self->{_str_total}}++;
5206  
5207  
5208 2 my $header = pack("vv", $record, $length);
5209 2 my $data = pack("vvvV", $row, $col, $xf, ${$self->{_str_table}}->{$str});
5210  
5211 # Store the data or write immediately depending on the compatibility mode.
5212 2 ▶ if ($self->{_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_compatibility'}) { } 0 2 2
5213 0 $self->{_table}->[$row]->[$col] = $header . $data;
5214 }
5215 else {
5216 2 $self->_append($header, $data);
5217 }
5218  
5219 2 return $str_error;
5220 }
5221  
5222  
5223 ###############################################################################
5224 #
5225 # write_utf16le_string($row, $col, $string, $format)
5226 #
5227 # Write a UTF-16LE string to the specified row and column (zero indexed).
5228 # $format is optional.
5229 # Returns 0 : normal termination
5230 # -1 : insufficient number of arguments
5231 # -2 : row or column out of range
5232 # -3 : long string truncated to 255 chars
5233 #
5234 1 ▶sub write_utf16le_string {
Subroutine write_utf16le_string: called CC=3 SCAR=11.4
Pod undocumented
5235  
5236 1 my $self = shift;
5237  
5238 # Check for a cell reference in A1 notation and substitute row and column
5239 1 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 0 1 1
5240 0 @_ = $self->_substitute_cellref(@_);
5241 }
5242  
5243 0 ▶ if (@_ < 3) { return -1 } # Check the number of args
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3) 0 1 1
5244  
5245 1 my $record = 0x00FD; # Record identifier
5246 1 my $length = 0x000A; # Bytes to follow
5247  
5248 1 my $row = $_[0]; # Zero indexed row
5249 1 my $col = $_[1]; # Zero indexed column
5250 1 my $str = $_[2];
5251 1 my $format = $_[3]; # The cell format
5252  
5253  
5254 # Change from UTF16 big-endian to little endian
5255 1 $str = pack "v*", unpack "n*", $str;
5256  
5257  
5258 1 return $self->write_utf16be_string($row, $col, $str, $format);
5259 }
5260  
5261  
5262 # Older method name for backwards compatibility.
5263 32 *write_unicode = *write_utf16be_string;
5264 32 *write_unicode_le = *write_utf16le_string;
5265  
5266  
5267  
5268 ###############################################################################
5269 #
5270 # _store_autofilters()
5271 #
5272 # Function to iterate through the columns that form part of an autofilter
5273 # range and write Biff AUTOFILTER records if a filter expression has been set.
5274 #
5275 133 ▶sub _store_autofilters {
Subroutine _store_autofilters: called CC=4 SCAR=25.0
5276  
5277 133 my $self = shift;
5278  
5279 # Skip all columns if no filter have been set.
5280 133 ▶ return unless $self->{_filter_on};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_filter_on"} 133 0 133
5281  
5282 0 my (undef, undef, $col1, $col2) = @{$self->{_filter_area}};
5283  
5284 0 for my $i ($col1 .. $col2) {
5285 # Reverse order since records are being pre-pended.
5286 0 my $col = $col2 -$i;
5287  
5288 # Skip if column doesn't have an active filter.
5289 0 ▶ next unless $self->{_filter_cols}->{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_filter_cols"}{$col} 0 0 0
5290  
5291 # Retrieve the filter tokens
5292 0 my @tokens = @{$self->{_filter_cols}->{$col}};
5293  
5294 # Filter columns are relative to the first column in the filter.
5295 0 my $filter_col = $col - $col1;
5296  
5297 # Write the autofilter records.
5298 0 $self->_store_autofilter($filter_col, @tokens);
5299 }
5300 }
5301  
5302  
5303 ###############################################################################
5304 #
5305 # _store_autofilter()
5306 #
5307 # Function to write worksheet AUTOFILTER records. These contain 2 Biff Doper
5308 # structures to represent the 2 possible filter conditions.
5309 #
5310 29 ▶sub _store_autofilter {
Subroutine _store_autofilter: called CC=13 SCAR=25.7
5311  
5312 29 my $self = shift;
5313  
5314 29 my $record = 0x009E;
5315 29 my $length = 0x0000;
5316  
5317 29 my $index = $_[0];
5318 29 my $operator_1 = $_[1];
5319 29 my $token_1 = $_[2];
5320 29 my $join = $_[3]; # And/Or
5321 29 my $operator_2 = $_[4];
5322 29 my $token_2 = $_[5];
5323  
5324 29 my $top10_active = 0;
5325 29 my $top10_direction = 0;
5326 29 my $top10_percent = 0;
5327 29 my $top10_value = 101;
5328  
5329 29 my $grbit = $join;
5330 29 my $optimised_1 = 0;
5331 29 my $optimised_2 = 0;
5332 29 my $doper_1 = '';
5333 29 my $doper_2 = '';
5334 29 my $string_1 = '';
5335 29 my $string_2 = '';
5336  
5337 # Excel used an optimisation in the case of a simple equality.
5338 29 ▶ $optimised_1 = 1 if $operator_1 == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $operator_1 == 2 6 23 29
5339 29 ▶ $optimised_2 = 1 if defined $operator_2 and $operator_2 == 2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $operator_2 and $operator_2 == 2 1 28 29
Condition100%3/3
defined $operator_2 and $operator_2 == 2
!ll&&!rl&&r
2621
MC/DC100%2/2
defined $operator_2 and $operator_2 == 2
defined $operator_2 $operator_2 == 2
Truth table100%3/3
A and B
A= defined $operator_2B= $operator_2 == 2
ABresult
0X0
100
111
5340  
5341  
5342 # Convert non-simple equalities back to type 2. See _parse_filter_tokens().
5343 29 ▶ $operator_1 = 2 if $operator_1 == 22;
Branch: 1 branch
BranchTrueFalseTotal
if $operator_1 == 22 5 24 29
5344 29 ▶ $operator_2 = 2 if defined $operator_2 and $operator_2 == 22;
Branch: 1 branch
BranchTrueFalseTotal
if defined $operator_2 and $operator_2 == 22 0 29 29
Condition66%2/3
defined $operator_2 and $operator_2 == 22
!ll&&!rl&&r
2630
MC/DC0%0/2
defined $operator_2 and $operator_2 == 22
defined $operator_2 $operator_2 == 22
Truth table66%2/3
A and B
A= defined $operator_2B= $operator_2 == 22
ABresult
0X0
100
111
5345  
5346  
5347 # Handle a "Top" style expression.
5348 29 ▶ if ($operator_1 >= 30) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operator_1 >= 30) 7 22 29
5349 # Remove the second expression if present.
5350 7 $operator_2 = undef;
5351 7 $token_2 = undef;
5352  
5353 # Set the active flag.
5354 7 $top10_active = 1;
5355  
5356 7 ▶ if ($operator_1 == 30 or $operator_1 == 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operator_1 == 30 or $operator_1 == 31) 5 2 7
Condition100%3/3
$operator_1 == 30 or $operator_1 == 31
l!l&&r!l&&!r
412
MC/DC100%2/2
$operator_1 == 30 or $operator_1 == 31
$operator_1 == 30 $operator_1 == 31
Truth table100%3/3
A or B
A= $operator_1 == 30B= $operator_1 == 31
ABresult
1X1
011
000
5357 5 $top10_direction = 1;
5358 }
5359  
5360 7 ▶ if ($operator_1 == 31 or $operator_1 == 33) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operator_1 == 31 or $operator_1 == 33) 2 5 7
Condition100%3/3
$operator_1 == 31 or $operator_1 == 33
l!l&&r!l&&!r
115
MC/DC100%2/2
$operator_1 == 31 or $operator_1 == 33
$operator_1 == 31 $operator_1 == 33
Truth table100%3/3
A or B
A= $operator_1 == 31B= $operator_1 == 33
ABresult
1X1
011
000
5361 2 $top10_percent = 1;
5362 }
5363  
5364 7 ▶ if ($top10_direction == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top10_direction == 1) { } 5 2 7
5365 5 $operator_1 = 6
5366 }
5367 else {
5368 2 $operator_1 = 3
5369 }
5370  
5371 7 $top10_value = $token_1;
5372 7 $token_1 = 0;
5373 }
5374  
5375  
5376 29 $grbit |= $optimised_1 << 2;
5377 29 $grbit |= $optimised_2 << 3;
5378 29 $grbit |= $top10_active << 4;
5379 29 $grbit |= $top10_direction << 5;
5380 29 $grbit |= $top10_percent << 6;
5381 29 $grbit |= $top10_value << 7;
5382  
5383 29 ($doper_1, $string_1) = $self->_pack_doper($operator_1, $token_1);
5384 29 ($doper_2, $string_2) = $self->_pack_doper($operator_2, $token_2);
5385  
5386 29 my $data = pack 'v', $index;
5387 29 $data .= pack 'v', $grbit;
5388 29 $data .= $doper_1;
5389 29 $data .= $doper_2;
5390 29 $data .= $string_1;
5391 29 $data .= $string_2;
5392  
5393 29 $length = length $data;
5394 29 my $header = pack('vv', $record, $length);
5395  
5396 29 $self->_prepend($header, $data);
5397 }
5398  
5399  
5400 ###############################################################################
5401 #
5402 # _pack_doper()
5403 #
5404 # Create a Biff Doper structure that represents a filter expression. Depending
5405 # on the type of the token we pack an Empty, String or Number doper.
5406 #
5407 58 ▶sub _pack_doper {
Subroutine _pack_doper: called CC=7 SCAR=19.6
5408  
5409 58 my $self = shift;
5410  
5411 58 my $operator = $_[0];
5412 58 my $token = $_[1];
5413  
5414 58 my $doper = '';
5415 58 my $string = '';
5416  
5417  
5418 # Return default doper for non-defined filters.
5419 58 ▶ if (not defined $operator) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $operator) 26 32 58
5420 26 return ($self->_pack_unused_doper, $string);
5421 }
5422  
5423  
5424 32 ▶ if ($token =~ /^blanks|nonblanks$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token =~ /^blanks|nonblanks$/i) { } 2 30 32
elsif ($operator == 2 or not $token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) { } 14 16 30
5425 2 $doper = $self->_pack_blanks_doper($operator, $token);
5426 }
5427 ▶ elsif ($operator == 2 or
Condition100%3/3
$operator == 2 or not $token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
l!l&&r!l&&!r
10416
MC/DC100%2/2
$operator == 2 or not $token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
$operator == 2 not $token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
Truth table100%3/3
A or not(B)
A= $operator == 2B= not $token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
ABresult
1X1
011
000
5428 $token !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
5429 {
5430 # Excel treats all tokens as strings if the operator is equality, =.
5431  
5432 14 $string = $token;
5433  
5434 14 my $encoding = 0;
5435 14 my $length = length $string;
5436  
5437 # Handle utf8 strings in perl 5.8.
5438 14 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 14 0 14
5439 14 require Encode;
5440  
5441 14 ▶ if (Encode::is_utf8($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($string)) 0 14 14
5442 0 $string = Encode::encode("UTF-16BE", $string);
5443 0 $encoding = 1;
5444 }
5445 }
5446  
5447 14 $string = pack('C', $encoding) . $string;
5448 14 $doper = $self->_pack_string_doper($operator, $length);
5449 }
5450 else {
5451 16 $string = '';
5452 16 $doper = $self->_pack_number_doper($operator, $token);
5453 }
5454  
5455 32 return ($doper, $string);
5456 }
5457  
5458  
5459 ###############################################################################
5460 #
5461 # _pack_unused_doper()
5462 #
5463 # Pack an empty Doper structure.
5464 #
5465 26 ▶sub _pack_unused_doper {
Subroutine _pack_unused_doper: called CC=1 SCAR=0.0
5466  
5467 26 my $self = shift;
5468  
5469 26 return pack 'C10', (0x0) x 10;
5470 }
5471  
5472  
5473 ###############################################################################
5474 #
5475 # _pack_blanks_doper()
5476 #
5477 # Pack an Blanks/NonBlanks Doper structure.
5478 #
5479 2 ▶sub _pack_blanks_doper {
Subroutine _pack_blanks_doper: called CC=2 SCAR=6.9
5480  
5481 2 my $self = shift;
5482  
5483 2 my $operator = $_[0];
5484 2 my $token = $_[1];
5485 2 my $type;
5486  
5487 2 ▶ if ($token eq 'blanks') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq 'blanks') { } 1 1 2
5488 1 $type = 0x0C;
5489 1 $operator = 2;
5490  
5491 }
5492 else {
5493 1 $type = 0x0E;
5494 1 $operator = 5;
5495 }
5496  
5497  
5498 2 my $doper = pack 'CCVV', $type, # Data type
5499 $operator, #
5500 0x0000, # Reserved
5501 0x0000; # Reserved
5502 2 return $doper;
5503 }
5504  
5505  
5506 ###############################################################################
5507 #
5508 # _pack_string_doper()
5509 #
5510 # Pack an string Doper structure.
5511 #
5512 14 ▶sub _pack_string_doper {
Subroutine _pack_string_doper: called CC=1 SCAR=0.0
5513  
5514 14 my $self = shift;
5515  
5516 14 my $operator = $_[0];
5517 14 my $length = $_[1];
5518 14 my $doper = pack 'CCVCCCC', 0x06, # Data type
5519 $operator, #
5520 0x0000, # Reserved
5521 $length, # String char length.
5522 0x0, 0x0, 0x0; # Reserved
5523 14 return $doper;
5524 }
5525  
5526  
5527 ###############################################################################
5528 #
5529 # _pack_number_doper()
5530 #
5531 # Pack an IEEE double number Doper structure.
5532 #
5533 16 ▶sub _pack_number_doper {
Subroutine _pack_number_doper: called CC=2 SCAR=7.0
5534  
5535 16 my $self = shift;
5536  
5537 16 my $operator = $_[0];
5538 16 my $number = $_[1];
5539 16 $number = pack 'd', $number;
5540 16 ▶ $number = reverse $number if $self->{_byte_order};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_byte_order"} 0 16 16
5541  
5542 16 my $doper = pack 'CC', 0x04, $operator;
5543 16 $doper .= $number;
5544  
5545 16 return $doper;
5546 }
5547  
5548  
5549 #
5550 # Methods related to comments and MSO objects.
5551 #
5552  
5553  
5554 ###############################################################################
5555 #
5556 # _prepare_images()
5557 #
5558 # Turn the HoH that stores the images into an array for easier handling.
5559 #
5560 166 ▶sub _prepare_images {
Subroutine _prepare_images: called CC=3 SCAR=13.2
5561  
5562 166 my $self = shift;
5563  
5564 166 my $count = 0;
5565 166 my @images;
5566  
5567  
5568 # We sort the images by row and column but that isn't strictly required.
5569 #
5570 0 my @rows = sort {$a <=> $b} keys %{$self->{_images}};
5571  
5572 166 for my $row (@rows) {
5573 0 my @cols = sort {$a <=> $b} keys %{$self->{_images}->{$row}};
5574  
5575 0 for my $col (@cols) {
5576 0 push @images, $self->{_images}->{$row}->{$col};
5577 0 $count++;
5578 }
5579 }
5580  
5581 166 $self->{_images} = {};
5582 166 $self->{_images_array} = \@images;
5583  
5584 166 return $count;
5585 }
5586  
5587  
5588 ###############################################################################
5589 #
5590 # _prepare_comments()
5591 #
5592 # Turn the HoH that stores the comments into an array for easier handling.
5593 #
5594 166 ▶sub _prepare_comments {
Subroutine _prepare_comments: called CC=3 SCAR=11.0
5595  
5596 166 my $self = shift;
5597  
5598 166 my $count = 0;
5599 166 my @comments;
5600  
5601  
5602 # We sort the comments by row and column but that isn't strictly required.
5603 #
5604 166 my @rows = sort {$a <=> $b} keys %{$self->{_comments}};
5605  
5606 166 for my $row (@rows) {
5607 0 my @cols = sort {$a <=> $b} keys %{$self->{_comments}->{$row}};
5608  
5609 20492 for my $col (@cols) {
5610 20492 push @comments, $self->{_comments}->{$row}->{$col};
5611 20492 $count++;
5612 }
5613 }
5614  
5615 166 $self->{_comments} = {};
5616 166 $self->{_comments_array} = \@comments;
5617  
5618 166 return $count;
5619 }
5620  
5621  
5622 ###############################################################################
5623 #
5624 # _prepare_charts()
5625 #
5626 # Turn the HoH that stores the charts into an array for easier handling.
5627 #
5628 166 ▶sub _prepare_charts {
Subroutine _prepare_charts: called CC=3 SCAR=13.2
5629  
5630 166 my $self = shift;
5631  
5632 166 my $count = 0;
5633 166 my @charts;
5634  
5635  
5636 # We sort the charts by row and column but that isn't strictly required.
5637 #
5638 0 my @rows = sort {$a <=> $b} keys %{$self->{_charts}};
5639  
5640 166 for my $row (@rows) {
5641 0 my @cols = sort {$a <=> $b} keys %{$self->{_charts}->{$row}};
5642  
5643 0 for my $col (@cols) {
5644 0 push @charts, $self->{_charts}->{$row}->{$col};
5645 0 $count++;
5646 }
5647 }
5648  
5649 166 $self->{_charts} = {};
5650 166 $self->{_charts_array} = \@charts;
5651  
5652 166 return $count;
5653 }
5654  
5655  
5656 ###############################################################################
5657 #
5658 # _store_images()
5659 #
5660 # Store the collections of records that make up images.
5661 #
5662 133 ▶sub _store_images {
Subroutine _store_images: called CC=6 SCAR=31.2
5663  
5664 133 my $self = shift;
5665  
5666 133 my $record = 0x00EC; # Record identifier
5667 133 my $length = 0x0000; # Bytes to follow
5668  
5669 133 my @ids = @{$self->{_object_ids }};
5670 133 my $spid = shift @ids;
5671  
5672 133 my @images = @{$self->{_images_array}};
5673 133 my $num_images = scalar @images;
5674  
5675 133 my $num_filters = $self->{_filter_count};
5676 133 my $num_comments = @{$self->{_comments_array}};
5677 133 my $num_charts = @{$self->{_charts_array }};
5678  
5679 # Skip this if there aren't any images.
5680 133 ▶ return unless $num_images;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_images 133 0 133
5681  
5682 0 for my $i (0 .. $num_images-1) {
5683 0 my $row = $images[$i]->[0];
5684 0 my $col = $images[$i]->[1];
5685 0 my $name = $images[$i]->[2];
5686 0 my $x_offset = $images[$i]->[3];
5687 0 my $y_offset = $images[$i]->[4];
5688 0 my $scale_x = $images[$i]->[5];
5689 0 my $scale_y = $images[$i]->[6];
5690 0 my $image_id = $images[$i]->[7];
5691 0 my $type = $images[$i]->[8];
5692 0 my $width = $images[$i]->[9];
5693 0 my $height = $images[$i]->[10];
5694  
5695 0 ▶ $width *= $scale_x if $scale_x;
Branch: 1 branch
BranchTrueFalseTotal
if $scale_x 0 0 0
5696 0 ▶ $height *= $scale_y if $scale_y;
Branch: 1 branch
BranchTrueFalseTotal
if $scale_y 0 0 0
5697  
5698  
5699 # Calculate the positions of image object.
5700 0 my @vertices = $self->_position_object( $col,
5701 $row,
5702 $x_offset,
5703 $y_offset,
5704 $width,
5705 $height
5706 );
5707  
5708 0 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
5709 # Write the parent MSODRAWIING record.
5710 0 my $dg_length = 156 + 84*($num_images -1);
5711 0 my $spgr_length = 132 + 84*($num_images -1);
5712  
5713 0 $dg_length += 120 *$num_charts;
5714 0 $spgr_length += 120 *$num_charts;
5715  
5716 0 $dg_length += 96 *$num_filters;
5717 0 $spgr_length += 96 *$num_filters;
5718  
5719 0 $dg_length += 128 *$num_comments;
5720 0 $spgr_length += 128 *$num_comments;
5721  
5722  
5723  
5724 0 my $data = $self->_store_mso_dg_container($dg_length);
5725 0 $data .= $self->_store_mso_dg(@ids);
5726 0 $data .= $self->_store_mso_spgr_container($spgr_length);
5727 0 $data .= $self->_store_mso_sp_container(40);
5728 0 $data .= $self->_store_mso_spgr();
5729 0 $data .= $self->_store_mso_sp(0x0, $spid++, 0x0005);
5730 0 $data .= $self->_store_mso_sp_container(76);
5731 0 $data .= $self->_store_mso_sp(75, $spid++, 0x0A00);
5732 0 $data .= $self->_store_mso_opt_image($image_id);
5733 0 $data .= $self->_store_mso_client_anchor(2, @vertices);
5734 0 $data .= $self->_store_mso_client_data();
5735  
5736 0 $length = length $data;
5737 0 my $header = pack("vv", $record, $length);
5738 0 $self->_append($header, $data);
5739  
5740 }
5741 else {
5742 # Write the child MSODRAWIING record.
5743 0 my $data = $self->_store_mso_sp_container(76);
5744 0 $data .= $self->_store_mso_sp(75, $spid++, 0x0A00);
5745 0 $data .= $self->_store_mso_opt_image($image_id);
5746 0 $data .= $self->_store_mso_client_anchor(2, @vertices);
5747 0 $data .= $self->_store_mso_client_data();
5748  
5749 0 $length = length $data;
5750 0 my $header = pack("vv", $record, $length);
5751 0 $self->_append($header, $data);
5752  
5753  
5754 }
5755  
5756 0 $self->_store_obj_image($i+1);
5757 }
5758  
5759 0 $self->{_object_ids}->[0] = $spid;
5760 }
5761  
5762  
5763  
5764 ###############################################################################
5765 #
5766 # _store_charts()
5767 #
5768 # Store the collections of records that make up charts.
5769 #
5770 133 ▶sub _store_charts {
Subroutine _store_charts: called CC=9 SCAR=39.3
5771  
5772 133 my $self = shift;
5773  
5774 133 my $record = 0x00EC; # Record identifier
5775 133 my $length = 0x0000; # Bytes to follow
5776  
5777 133 my @ids = @{$self->{_object_ids}};
5778 133 my $spid = shift @ids;
5779  
5780 133 my @charts = @{$self->{_charts_array}};
5781 133 my $num_charts = scalar @charts;
5782  
5783 133 my $num_filters = $self->{_filter_count};
5784 133 my $num_comments = @{$self->{_comments_array}};
5785  
5786 # Number of objects written so far.
5787 133 my $num_objects = @{$self->{_images_array}};
5788  
5789 # Skip this if there aren't any charts.
5790 133 ▶ return unless $num_charts;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_charts 133 0 133
5791  
5792 0 for my $i (0 .. $num_charts-1 ) {
5793 0 my $row = $charts[$i]->[0];
5794 0 my $col = $charts[$i]->[1];
5795 0 my $chart = $charts[$i]->[2];
5796 0 my $x_offset = $charts[$i]->[3];
5797 0 my $y_offset = $charts[$i]->[4];
5798 0 my $scale_x = $charts[$i]->[5];
5799 0 my $scale_y = $charts[$i]->[6];
5800 0 my $width = 526;
5801 0 my $height = 319;
5802  
5803 0 ▶ $width *= $scale_x if $scale_x;
Branch: 1 branch
BranchTrueFalseTotal
if $scale_x 0 0 0
5804 0 ▶ $height *= $scale_y if $scale_y;
Branch: 1 branch
BranchTrueFalseTotal
if $scale_y 0 0 0
5805  
5806 # Calculate the positions of chart object.
5807 0 my @vertices = $self->_position_object( $col,
5808 $row,
5809 $x_offset,
5810 $y_offset,
5811 $width,
5812 $height
5813 );
5814  
5815  
5816 0 ▶ if ($i == 0 and not $num_objects) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0 and not $num_objects) { } 0 0 0
Condition0%0/3
$i == 0 and not $num_objects
!ll&&!rl&&r
000
MC/DC0%0/2
$i == 0 and not $num_objects
$i == 0 not $num_objects
Truth table0%0/3
A and not(B)
A= $i == 0B= not $num_objects
ABresult
0X0
100
111
5817 # Write the parent MSODRAWIING record.
5818 0 my $dg_length = 192 + 120*($num_charts -1);
5819 0 my $spgr_length = 168 + 120*($num_charts -1);
5820  
5821 0 $dg_length += 96 *$num_filters;
5822 0 $spgr_length += 96 *$num_filters;
5823  
5824 0 $dg_length += 128 *$num_comments;
5825 0 $spgr_length += 128 *$num_comments;
5826  
5827  
5828 0 my $data = $self->_store_mso_dg_container($dg_length);
5829 0 $data .= $self->_store_mso_dg(@ids);
5830 0 $data .= $self->_store_mso_spgr_container($spgr_length);
5831 0 $data .= $self->_store_mso_sp_container(40);
5832 0 $data .= $self->_store_mso_spgr();
5833 0 $data .= $self->_store_mso_sp(0x0, $spid++, 0x0005);
5834 0 $data .= $self->_store_mso_sp_container(112);
5835 0 $data .= $self->_store_mso_sp(201, $spid++, 0x0A00);
5836 0 $data .= $self->_store_mso_opt_chart();
5837 0 $data .= $self->_store_mso_client_anchor(0, @vertices);
5838 0 $data .= $self->_store_mso_client_data();
5839  
5840 0 $length = length $data;
5841 0 my $header = pack("vv", $record, $length);
5842 0 $self->_append($header, $data);
5843  
5844 }
5845 else {
5846 # Write the child MSODRAWIING record.
5847 0 my $data = $self->_store_mso_sp_container(112);
5848 0 $data .= $self->_store_mso_sp(201, $spid++, 0x0A00);
5849 0 $data .= $self->_store_mso_opt_chart();
5850 0 $data .= $self->_store_mso_client_anchor(0, @vertices);
5851 0 $data .= $self->_store_mso_client_data();
5852  
5853 0 $length = length $data;
5854 0 my $header = pack("vv", $record, $length);
5855 0 $self->_append($header, $data);
5856  
5857  
5858 }
5859  
5860 0 $self->_store_obj_chart($num_objects+$i+1);
5861 0 $self->_store_chart_binary($chart);
5862 }
5863  
5864  
5865 # Simulate the EXTERNSHEET link between the chart and data using a formula
5866 # such as '=Sheet1!A1'.
5867 # TODO. Won't work for external data refs. Also should use a more direct
5868 # method.
5869 #
5870 0 my $name = $self->{_name};
5871 0 ▶ if ($self->{_encoding} && $] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_encoding"} and $] >= "5.008") 0 0 0
Condition0%0/3
$self->{"_encoding"} and $] >= "5.008"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_encoding"} and $] >= "5.008"
$self->{"_encoding"} $] >= "5.008"
Truth table0%0/3
A and B
A= $self->{"_encoding"}B= $] >= "5.008"
ABresult
0X0
100
111
5872 0 require Encode;
5873 0 $name = Encode::decode('UTF-16BE', $name);
5874 }
5875 0 $self->store_formula("='$name'!A1");
5876  
5877 0 $self->{_object_ids}->[0] = $spid;
5878 }
5879  
5880  
5881 ###############################################################################
5882 #
5883 # _store_chart_binary
5884 #
5885 # Add the binary data for a chart. This could either be from a Chart object
5886 # or from an external binary file (for backwards compatibility).
5887 #
5888 0 ▶sub _store_chart_binary {
Subroutine _store_chart_binary: not called CC=4 SCAR=30.0
5889  
5890 0 my $self = shift;
5891 0 my $chart = $_[0];
5892 0 my $tmp;
5893  
5894  
5895 0 ▶ if ( ref $chart ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $chart) { } 0 0 0
5896 0 $chart->_close();
5897 0 my $tmp = $chart->get_data();
5898 0 $self->_append( $tmp );
5899 }
5900 else {
5901  
5902 0 ▶ my $filehandle = FileHandle->new( $chart )
Branch: 1 branch
BranchTrueFalseTotal
my $filehandle = "FileHandle"->new($chart) or die "Couldn't open $chart in insert_chart(): $!.\n" 0 0 0
5903 or die "Couldn't open $chart in insert_chart(): $!.\n";
5904  
5905 0 binmode( $filehandle );
5906  
5907 0 ▶ while ( read( $filehandle, $tmp, 4096 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $filehandle, $tmp, 4096) 0 0 0
5908 0 $self->_append( $tmp );
5909 }
5910 }
5911 }
5912  
5913  
5914 ###############################################################################
5915 #
5916 # _store_filters()
5917 #
5918 # Store the collections of records that make up filters.
5919 #
5920 133 ▶sub _store_filters {
Subroutine _store_filters: called CC=5 SCAR=16.1
5921  
5922 133 my $self = shift;
5923  
5924 133 my $record = 0x00EC; # Record identifier
5925 133 my $length = 0x0000; # Bytes to follow
5926  
5927 133 my @ids = @{$self->{_object_ids}};
5928 133 my $spid = shift @ids;
5929  
5930 133 my $filter_area = $self->{_filter_area};
5931 133 my $num_filters = $self->{_filter_count};
5932  
5933 133 my $num_comments = @{$self->{_comments_array}};
5934  
5935 # Number of objects written so far.
5936 133 my $num_objects = @{$self->{_images_array}}
5937 133 + @{$self->{_charts_array}};
5938  
5939 # Skip this if there aren't any filters.
5940 133 ▶ return unless $num_filters;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_filters 130 3 133
5941  
5942  
5943 3 my ($row1, $row2, $col1, $col2) = @$filter_area;
5944  
5945 3 for my $i (0 .. $num_filters-1 ) {
5946  
5947 9 my @vertices = ( $col1 +$i,
5948 0,
5949 $row1,
5950 0,
5951 $col1 +$i +1,
5952 0,
5953 $row1 +1,
5954 0);
5955  
5956 9 ▶ if ($i == 0 and not $num_objects) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0 and not $num_objects) { } 3 6 9
Condition66%2/3
$i == 0 and not $num_objects
!ll&&!rl&&r
603
MC/DC50%1/2
$i == 0 and not $num_objects
$i == 0 not $num_objects
Truth table66%2/3
A and not(B)
A= $i == 0B= not $num_objects
ABresult
0X0
100
111
5957 # Write the parent MSODRAWIING record.
5958 3 my $dg_length = 168 + 96*($num_filters -1);
5959 3 my $spgr_length = 144 + 96*($num_filters -1);
5960  
5961 3 $dg_length += 128 *$num_comments;
5962 3 $spgr_length += 128 *$num_comments;
5963  
5964  
5965 3 my $data = $self->_store_mso_dg_container($dg_length);
5966 3 $data .= $self->_store_mso_dg(@ids);
5967 3 $data .= $self->_store_mso_spgr_container($spgr_length);
5968 3 $data .= $self->_store_mso_sp_container(40);
5969 3 $data .= $self->_store_mso_spgr();
5970 3 $data .= $self->_store_mso_sp(0x0, $spid++, 0x0005);
5971 3 $data .= $self->_store_mso_sp_container(88);
5972 3 $data .= $self->_store_mso_sp(201, $spid++, 0x0A00);
5973 3 $data .= $self->_store_mso_opt_filter();
5974 3 $data .= $self->_store_mso_client_anchor(1, @vertices);
5975 3 $data .= $self->_store_mso_client_data();
5976  
5977 3 $length = length $data;
5978 3 my $header = pack("vv", $record, $length);
5979 3 $self->_append($header, $data);
5980  
5981 }
5982 else {
5983 # Write the child MSODRAWIING record.
5984 6 my $data = $self->_store_mso_sp_container(88);
5985 6 $data .= $self->_store_mso_sp(201, $spid++, 0x0A00);
5986 6 $data .= $self->_store_mso_opt_filter();
5987 6 $data .= $self->_store_mso_client_anchor(1, @vertices);
5988 6 $data .= $self->_store_mso_client_data();
5989  
5990 6 $length = length $data;
5991 6 my $header = pack("vv", $record, $length);
5992 6 $self->_append($header, $data);
5993  
5994  
5995 }
5996  
5997 9 $self->_store_obj_filter($num_objects+$i+1, $col1 +$i);
5998 }
5999  
6000  
6001 # Simulate the EXTERNSHEET link between the filter and data using a formula
6002 # such as '=Sheet1!A1'.
6003 # TODO. Won't work for external data refs. Also should use a more direct
6004 # method.
6005 #
6006 3 my $formula = "='$self->{_name}'!A1";
6007 3 $self->store_formula($formula);
6008  
6009 3 $self->{_object_ids}->[0] = $spid;
6010 }
6011  
6012  
6013 ###############################################################################
6014 #
6015 # _store_comments()
6016 #
6017 # Store the collections of records that make up cell comments.
6018 #
6019 # NOTE: We write the comment objects last since that makes it a little easier
6020 # to write the NOTE records directly after the MSODRAWIING records.
6021 #
6022 133 ▶sub _store_comments {
Subroutine _store_comments: called CC=7 SCAR=19.8
6023  
6024 133 my $self = shift;
6025  
6026 133 my $record = 0x00EC; # Record identifier
6027 133 my $length = 0x0000; # Bytes to follow
6028  
6029 133 my @ids = @{$self->{_object_ids}};
6030 133 my $spid = shift @ids;
6031  
6032 133 my @comments = @{$self->{_comments_array}};
6033 133 my $num_comments = scalar @comments;
6034  
6035 # Number of objects written so far.
6036 133 my $num_objects = @{$self->{_images_array}}
6037 + $self->{_filter_count}
6038 133 + @{$self->{_charts_array}};
6039  
6040 # Skip this if there aren't any comments.
6041 133 ▶ return unless $num_comments;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_comments 132 1 133
6042  
6043 1 for my $i (0 .. $num_comments-1) {
6044  
6045 1 my $row = $comments[$i]->[0];
6046 1 my $col = $comments[$i]->[1];
6047 1 my $str = $comments[$i]->[2];
6048 1 my $encoding = $comments[$i]->[3];
6049 1 my $visible = $comments[$i]->[6];
6050 1 my $color = $comments[$i]->[7];
6051 1 my @vertices = @{$comments[$i]->[8]};
6052 1 my $str_len = length $str;
6053 1 ▶ $str_len /= 2 if $encoding; # Num of chars not bytes.
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 0 1 1
6054 1 my $formats = [[0, 9], [$str_len, 0]];
6055  
6056  
6057 1 ▶ if ($i == 0 and not $num_objects) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0 and not $num_objects) { } 1 0 1
Condition33%1/3
$i == 0 and not $num_objects
!ll&&!rl&&r
001
MC/DC0%0/2
$i == 0 and not $num_objects
$i == 0 not $num_objects
Truth table33%1/3
A and not(B)
A= $i == 0B= not $num_objects
ABresult
0X0
100
111
6058 # Write the parent MSODRAWIING record.
6059 1 my $dg_length = 200 + 128*($num_comments -1);
6060 1 my $spgr_length = 176 + 128*($num_comments -1);
6061  
6062 1 my $data = $self->_store_mso_dg_container($dg_length);
6063 1 $data .= $self->_store_mso_dg(@ids);
6064 1 $data .= $self->_store_mso_spgr_container($spgr_length);
6065 1 $data .= $self->_store_mso_sp_container(40);
6066 1 $data .= $self->_store_mso_spgr();
6067 1 $data .= $self->_store_mso_sp(0x0, $spid++, 0x0005);
6068 1 $data .= $self->_store_mso_sp_container(120);
6069 1 $data .= $self->_store_mso_sp(202, $spid++, 0x0A00);
6070 1 $data .= $self->_store_mso_opt_comment(0x80, $visible, $color);
6071 1 $data .= $self->_store_mso_client_anchor(3, @vertices);
6072 1 $data .= $self->_store_mso_client_data();
6073  
6074 1 $length = length $data;
6075 1 my $header = pack("vv", $record, $length);
6076 1 $self->_append($header, $data);
6077  
6078 }
6079 else {
6080 # Write the child MSODRAWIING record.
6081 0 my $data = $self->_store_mso_sp_container(120);
6082 0 $data .= $self->_store_mso_sp(202, $spid++, 0x0A00);
6083 0 $data .= $self->_store_mso_opt_comment(0x80, $visible, $color);
6084 0 $data .= $self->_store_mso_client_anchor(3, @vertices);
6085 0 $data .= $self->_store_mso_client_data();
6086  
6087 0 $length = length $data;
6088 0 my $header = pack("vv", $record, $length);
6089 0 $self->_append($header, $data);
6090  
6091  
6092 }
6093  
6094 1 $self->_store_obj_comment($num_objects+$i+1);
6095 1 $self->_store_mso_drawing_text_box();
6096 1 $self->_store_txo($str_len);
6097 1 $self->_store_txo_continue_1($str, $encoding);
6098 1 $self->_store_txo_continue_2($formats);
6099 }
6100  
6101  
6102 # Write the NOTE records after MSODRAWIING records.
6103 1 for my $i (0 .. $num_comments-1) {
6104  
6105 1 my $row = $comments[$i]->[0];
6106 1 my $col = $comments[$i]->[1];
6107 1 my $author = $comments[$i]->[4];
6108 1 my $author_enc = $comments[$i]->[5];
6109 1 my $visible = $comments[$i]->[6];
6110  
6111 1 $self->_store_note($row, $col, $num_objects+$i+1,
6112 $author, $author_enc, $visible);
6113 }
6114 }
6115  
6116  
6117 ###############################################################################
6118 #
6119 # _store_mso_dg_container()
6120 #
6121 # Write the Escher DgContainer record that is part of MSODRAWING.
6122 #
6123 5 ▶sub _store_mso_dg_container {
Subroutine _store_mso_dg_container: called CC=1 SCAR=0.0
6124  
6125 5 my $self = shift;
6126  
6127 5 my $type = 0xF002;
6128 5 my $version = 15;
6129 5 my $instance = 0;
6130 5 my $data = '';
6131 5 my $length = $_[0];
6132  
6133  
6134 5 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6135 }
6136  
6137  
6138 ###############################################################################
6139 #
6140 # _store_mso_dg()
6141 #
6142 # Write the Escher Dg record that is part of MSODRAWING.
6143 #
6144 5 ▶sub _store_mso_dg {
Subroutine _store_mso_dg: called CC=1 SCAR=0.0
6145  
6146 5 my $self = shift;
6147  
6148 5 my $type = 0xF008;
6149 5 my $version = 0;
6150 5 my $instance = $_[0];
6151 5 my $data = '';
6152 5 my $length = 8;
6153  
6154 5 my $num_shapes = $_[1];
6155 5 my $max_spid = $_[2];
6156  
6157 5 $data = pack "VV", $num_shapes, $max_spid;
6158  
6159 5 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6160 }
6161  
6162  
6163 ###############################################################################
6164 #
6165 # _store_mso_spgr_container()
6166 #
6167 # Write the Escher SpgrContainer record that is part of MSODRAWING.
6168 #
6169 5 ▶sub _store_mso_spgr_container {
Subroutine _store_mso_spgr_container: called CC=1 SCAR=0.0
6170  
6171 5 my $self = shift;
6172  
6173 5 my $type = 0xF003;
6174 5 my $version = 15;
6175 5 my $instance = 0;
6176 5 my $data = '';
6177 5 my $length = $_[0];
6178  
6179  
6180 5 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6181 }
6182  
6183  
6184 ###############################################################################
6185 #
6186 # _store_mso_sp_container()
6187 #
6188 # Write the Escher SpContainer record that is part of MSODRAWING.
6189 #
6190 15 ▶sub _store_mso_sp_container {
Subroutine _store_mso_sp_container: called CC=1 SCAR=0.0
6191  
6192 15 my $self = shift;
6193  
6194 15 my $type = 0xF004;
6195 15 my $version = 15;
6196 15 my $instance = 0;
6197 15 my $data = '';
6198 15 my $length = $_[0];
6199  
6200  
6201 15 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6202 }
6203  
6204  
6205 ###############################################################################
6206 #
6207 # _store_mso_spgr()
6208 #
6209 # Write the Escher Spgr record that is part of MSODRAWING.
6210 #
6211 4 ▶sub _store_mso_spgr {
Subroutine _store_mso_spgr: called CC=1 SCAR=0.0
6212  
6213 4 my $self = shift;
6214  
6215 4 my $type = 0xF009;
6216 4 my $version = 1;
6217 4 my $instance = 0;
6218 4 my $data = pack "VVVV", 0, 0, 0, 0;
6219 4 my $length = 16;
6220  
6221  
6222 4 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6223 }
6224  
6225  
6226 ###############################################################################
6227 #
6228 # _store_mso_sp()
6229 #
6230 # Write the Escher Sp record that is part of MSODRAWING.
6231 #
6232 16 ▶sub _store_mso_sp {
Subroutine _store_mso_sp: called CC=1 SCAR=0.0
6233  
6234 16 my $self = shift;
6235  
6236 16 my $type = 0xF00A;
6237 16 my $version = 2;
6238 16 my $instance = $_[0];
6239 16 my $data = '';
6240 16 my $length = 8;
6241  
6242 16 my $spid = $_[1];
6243 16 my $options = $_[2];
6244  
6245 16 $data = pack "VV", $spid, $options;
6246  
6247 16 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6248 }
6249  
6250  
6251 ###############################################################################
6252 #
6253 # _store_mso_opt_comment()
6254 #
6255 # Write the Escher Opt record that is part of MSODRAWING.
6256 #
6257 2 ▶sub _store_mso_opt_comment {
Subroutine _store_mso_opt_comment: called CC=5 SCAR=16.4
6258  
6259 2 my $self = shift;
6260  
6261 2 my $type = 0xF00B;
6262 2 my $version = 3;
6263 2 my $instance = 9;
6264 2 my $data = '';
6265 2 my $length = 54;
6266  
6267 2 my $spid = $_[0];
6268 2 my $visible = $_[1];
6269 2 ▶ my $colour = $_[2] || 0x50;
Condition100%2/2
$_[2] || 80
l!l
11
MC/DC100%1/1
$_[2] || 80
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
6270  
6271  
6272 # Use the visible flag if set by the user or else use the worksheet value.
6273 # Note that the value used is the opposite of _store_note().
6274 #
6275 2 ▶ if (defined $visible) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $visible) { } 0 2 2
6276 0 ▶ $visible = $visible ? 0x0000 : 0x0002;
Branch: 1 branch
BranchTrueFalseTotal
$visible ? : 0 0 0
6277 }
6278 else {
6279 2 ▶ $visible = $self->{_comments_visible} ? 0x0000 : 0x0002;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_comments_visible'} ? : 0 2 2
6280 }
6281  
6282  
6283 2 $data = pack "V", $spid;
6284 2 $data .= pack "H*", '0000BF00080008005801000000008101' ;
6285 2 $data .= pack "C", $colour;
6286 2 $data .= pack "H*", '000008830150000008BF011000110001' .
6287 '02000000003F0203000300BF03';
6288 2 $data .= pack "v", $visible;
6289 2 $data .= pack "H*", '0A00';
6290  
6291  
6292 2 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6293 }
6294  
6295  
6296 ###############################################################################
6297 #
6298 # _store_mso_opt_image()
6299 #
6300 # Write the Escher Opt record that is part of MSODRAWING.
6301 #
6302 0 ▶sub _store_mso_opt_image {
Subroutine _store_mso_opt_image: not called CC=1 SCAR=6.9
6303  
6304 0 my $self = shift;
6305  
6306 0 my $type = 0xF00B;
6307 0 my $version = 3;
6308 0 my $instance = 3;
6309 0 my $data = '';
6310 0 my $length = undef;
6311 0 my $spid = $_[0];
6312  
6313 0 $data = pack 'v', 0x4104; # Blip -> pib
6314 0 $data .= pack 'V', $spid;
6315 0 $data .= pack 'v', 0x01BF; # Fill Style -> fNoFillHitTest
6316 0 $data .= pack 'V', 0x00010000;
6317 0 $data .= pack 'v', 0x03BF; # Group Shape -> fPrint
6318 0 $data .= pack 'V', 0x00080000;
6319  
6320  
6321 0 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6322 }
6323  
6324  
6325 ###############################################################################
6326 #
6327 # _store_mso_opt_chart()
6328 #
6329 # Write the Escher Opt record that is part of MSODRAWING.
6330 #
6331 0 ▶sub _store_mso_opt_chart {
Subroutine _store_mso_opt_chart: not called CC=1 SCAR=6.9
6332  
6333 0 my $self = shift;
6334  
6335 0 my $type = 0xF00B;
6336 0 my $version = 3;
6337 0 my $instance = 9;
6338 0 my $data = '';
6339 0 my $length = undef;
6340  
6341 0 $data = pack 'v', 0x007F; # Protection -> fLockAgainstGrouping
6342 0 $data .= pack 'V', 0x01040104;
6343  
6344 0 $data .= pack 'v', 0x00BF; # Text -> fFitTextToShape
6345 0 $data .= pack 'V', 0x00080008;
6346  
6347 0 $data .= pack 'v', 0x0181; # Fill Style -> fillColor
6348 0 $data .= pack 'V', 0x0800004E ;
6349  
6350 0 $data .= pack 'v', 0x0183; # Fill Style -> fillBackColor
6351 0 $data .= pack 'V', 0x0800004D;
6352  
6353 0 $data .= pack 'v', 0x01BF; # Fill Style -> fNoFillHitTest
6354 0 $data .= pack 'V', 0x00110010;
6355  
6356 0 $data .= pack 'v', 0x01C0; # Line Style -> lineColor
6357 0 $data .= pack 'V', 0x0800004D;
6358  
6359 0 $data .= pack 'v', 0x01FF; # Line Style -> fNoLineDrawDash
6360 0 $data .= pack 'V', 0x00080008;
6361  
6362 0 $data .= pack 'v', 0x023F; # Shadow Style -> fshadowObscured
6363 0 $data .= pack 'V', 0x00020000;
6364  
6365 0 $data .= pack 'v', 0x03BF; # Group Shape -> fPrint
6366 0 $data .= pack 'V', 0x00080000;
6367  
6368  
6369 0 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6370 }
6371  
6372  
6373 ###############################################################################
6374 #
6375 # _store_mso_opt_filter()
6376 #
6377 # Write the Escher Opt record that is part of MSODRAWING.
6378 #
6379 9 ▶sub _store_mso_opt_filter {
Subroutine _store_mso_opt_filter: called CC=1 SCAR=0.0
6380  
6381 9 my $self = shift;
6382  
6383 9 my $type = 0xF00B;
6384 9 my $version = 3;
6385 9 my $instance = 5;
6386 9 my $data = '';
6387 9 my $length = undef;
6388  
6389  
6390  
6391 9 $data = pack 'v', 0x007F; # Protection -> fLockAgainstGrouping
6392 9 $data .= pack 'V', 0x01040104;
6393  
6394 9 $data .= pack 'v', 0x00BF; # Text -> fFitTextToShape
6395 9 $data .= pack 'V', 0x00080008;
6396  
6397 9 $data .= pack 'v', 0x01BF; # Fill Style -> fNoFillHitTest
6398 9 $data .= pack 'V', 0x00010000;
6399  
6400 9 $data .= pack 'v', 0x01FF; # Line Style -> fNoLineDrawDash
6401 9 $data .= pack 'V', 0x00080000;
6402  
6403 9 $data .= pack 'v', 0x03BF; # Group Shape -> fPrint
6404 9 $data .= pack 'V', 0x000A0000;
6405  
6406  
6407 9 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6408 }
6409  
6410  
6411 ###############################################################################
6412 #
6413 # _store_mso_client_anchor()
6414 #
6415 # Write the Escher ClientAnchor record that is part of MSODRAWING.
6416 #
6417 26 ▶sub _store_mso_client_anchor {
Subroutine _store_mso_client_anchor: called CC=1 SCAR=0.0
6418  
6419 26 my $self = shift;
6420  
6421 26 my $type = 0xF010;
6422 26 my $version = 0;
6423 26 my $instance = 0;
6424 26 my $data = '';
6425 26 my $length = 18;
6426  
6427 26 my $flag = shift;
6428  
6429 26 my $col_start = $_[0]; # Col containing upper left corner of object
6430 26 my $x1 = $_[1]; # Distance to left side of object
6431  
6432 26 my $row_start = $_[2]; # Row containing top left corner of object
6433 26 my $y1 = $_[3]; # Distance to top of object
6434  
6435 26 my $col_end = $_[4]; # Col containing lower right corner of object
6436 26 my $x2 = $_[5]; # Distance to right side of object
6437  
6438 26 my $row_end = $_[6]; # Row containing bottom right corner of object
6439 26 my $y2 = $_[7]; # Distance to bottom of object
6440  
6441 26 $data = pack "v9", $flag,
6442 $col_start, $x1,
6443 $row_start, $y1,
6444 $col_end, $x2,
6445 $row_end, $y2;
6446  
6447  
6448  
6449 26 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6450 }
6451  
6452  
6453 ###############################################################################
6454 #
6455 # _store_mso_client_data()
6456 #
6457 # Write the Escher ClientData record that is part of MSODRAWING.
6458 #
6459 11 ▶sub _store_mso_client_data {
Subroutine _store_mso_client_data: called CC=1 SCAR=0.0
6460  
6461 11 my $self = shift;
6462  
6463 11 my $type = 0xF011;
6464 11 my $version = 0;
6465 11 my $instance = 0;
6466 11 my $data = '';
6467 11 my $length = 0;
6468  
6469  
6470 11 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6471 }
6472  
6473  
6474 ###############################################################################
6475 #
6476 # _store_obj_comment()
6477 #
6478 # Write the OBJ record that is part of cell comments.
6479 #
6480 2 ▶sub _store_obj_comment {
Subroutine _store_obj_comment: called CC=1 SCAR=0.0
6481  
6482 2 my $self = shift;
6483  
6484 2 my $record = 0x005D; # Record identifier
6485 2 my $length = 0x0034; # Bytes to follow
6486  
6487 2 my $obj_id = $_[0]; # Object ID number.
6488 2 my $obj_type = 0x0019; # Object type (comment).
6489 2 my $data = ''; # Record data.
6490  
6491 2 my $sub_record = 0x0000; # Sub-record identifier.
6492 2 my $sub_length = 0x0000; # Length of sub-record.
6493 2 my $sub_data = ''; # Data of sub-record.
6494 2 my $options = 0x4011;
6495 2 my $reserved = 0x0000;
6496  
6497 # Add ftCmo (common object data) subobject
6498 2 $sub_record = 0x0015; # ftCmo
6499 2 $sub_length = 0x0012;
6500 2 $sub_data = pack "vvvVVV", $obj_type, $obj_id, $options,
6501 $reserved, $reserved, $reserved;
6502 2 $data = pack("vv", $sub_record, $sub_length);
6503 2 $data .= $sub_data;
6504  
6505  
6506 # Add ftNts (note structure) subobject
6507 2 $sub_record = 0x000D; # ftNts
6508 2 $sub_length = 0x0016;
6509 2 $sub_data = pack "VVVVVv", ($reserved) x 6;
6510 2 $data .= pack("vv", $sub_record, $sub_length);
6511 2 $data .= $sub_data;
6512  
6513  
6514 # Add ftEnd (end of object) subobject
6515 2 $sub_record = 0x0000; # ftNts
6516 2 $sub_length = 0x0000;
6517 2 $data .= pack("vv", $sub_record, $sub_length);
6518  
6519  
6520 # Pack the record.
6521 2 my $header = pack("vv", $record, $length);
6522  
6523 2 $self->_append($header, $data);
6524  
6525 }
6526  
6527  
6528 ###############################################################################
6529 #
6530 # _store_obj_image()
6531 #
6532 # Write the OBJ record that is part of image records.
6533 #
6534 0 ▶sub _store_obj_image {
Subroutine _store_obj_image: not called CC=1 SCAR=6.9
6535  
6536 0 my $self = shift;
6537  
6538 0 my $record = 0x005D; # Record identifier
6539 0 my $length = 0x0026; # Bytes to follow
6540  
6541 0 my $obj_id = $_[0]; # Object ID number.
6542 0 my $obj_type = 0x0008; # Object type (Picture).
6543 0 my $data = ''; # Record data.
6544  
6545 0 my $sub_record = 0x0000; # Sub-record identifier.
6546 0 my $sub_length = 0x0000; # Length of sub-record.
6547 0 my $sub_data = ''; # Data of sub-record.
6548 0 my $options = 0x6011;
6549 0 my $reserved = 0x0000;
6550  
6551 # Add ftCmo (common object data) subobject
6552 0 $sub_record = 0x0015; # ftCmo
6553 0 $sub_length = 0x0012;
6554 0 $sub_data = pack 'vvvVVV', $obj_type, $obj_id, $options,
6555 $reserved, $reserved, $reserved;
6556 0 $data = pack 'vv', $sub_record, $sub_length;
6557 0 $data .= $sub_data;
6558  
6559  
6560 # Add ftCf (Clipboard format) subobject
6561 0 $sub_record = 0x0007; # ftCf
6562 0 $sub_length = 0x0002;
6563 0 $sub_data = pack 'v', 0xFFFF;
6564 0 $data .= pack 'vv', $sub_record, $sub_length;
6565 0 $data .= $sub_data;
6566  
6567 # Add ftPioGrbit (Picture option flags) subobject
6568 0 $sub_record = 0x0008; # ftPioGrbit
6569 0 $sub_length = 0x0002;
6570 0 $sub_data = pack 'v', 0x0001;
6571 0 $data .= pack 'vv', $sub_record, $sub_length;
6572 0 $data .= $sub_data;
6573  
6574  
6575 # Add ftEnd (end of object) subobject
6576 0 $sub_record = 0x0000; # ftNts
6577 0 $sub_length = 0x0000;
6578 0 $data .= pack 'vv', $sub_record, $sub_length;
6579  
6580  
6581 # Pack the record.
6582 0 my $header = pack('vv', $record, $length);
6583  
6584 0 $self->_append($header, $data);
6585  
6586 }
6587  
6588  
6589 ###############################################################################
6590 #
6591 # _store_obj_chart()
6592 #
6593 # Write the OBJ record that is part of chart records.
6594 #
6595 0 ▶sub _store_obj_chart {
Subroutine _store_obj_chart: not called CC=1 SCAR=6.9
6596  
6597 0 my $self = shift;
6598  
6599 0 my $record = 0x005D; # Record identifier
6600 0 my $length = 0x001A; # Bytes to follow
6601  
6602 0 my $obj_id = $_[0]; # Object ID number.
6603 0 my $obj_type = 0x0005; # Object type (chart).
6604 0 my $data = ''; # Record data.
6605  
6606 0 my $sub_record = 0x0000; # Sub-record identifier.
6607 0 my $sub_length = 0x0000; # Length of sub-record.
6608 0 my $sub_data = ''; # Data of sub-record.
6609 0 my $options = 0x6011;
6610 0 my $reserved = 0x0000;
6611  
6612 # Add ftCmo (common object data) subobject
6613 0 $sub_record = 0x0015; # ftCmo
6614 0 $sub_length = 0x0012;
6615 0 $sub_data = pack 'vvvVVV', $obj_type, $obj_id, $options,
6616 $reserved, $reserved, $reserved;
6617 0 $data = pack 'vv', $sub_record, $sub_length;
6618 0 $data .= $sub_data;
6619  
6620 # Add ftEnd (end of object) subobject
6621 0 $sub_record = 0x0000; # ftNts
6622 0 $sub_length = 0x0000;
6623 0 $data .= pack 'vv', $sub_record, $sub_length;
6624  
6625  
6626 # Pack the record.
6627 0 my $header = pack('vv', $record, $length);
6628  
6629 0 $self->_append($header, $data);
6630  
6631 }
6632  
6633  
6634  
6635  
6636 ###############################################################################
6637 #
6638 # _store_obj_filter()
6639 #
6640 # Write the OBJ record that is part of filter records.
6641 #
6642 9 ▶sub _store_obj_filter {
Subroutine _store_obj_filter: called CC=2 SCAR=6.9
6643  
6644 9 my $self = shift;
6645  
6646 9 my $record = 0x005D; # Record identifier
6647 9 my $length = 0x0046; # Bytes to follow
6648  
6649 9 my $obj_id = $_[0]; # Object ID number.
6650 9 my $obj_type = 0x0014; # Object type (combo box).
6651 9 my $data = ''; # Record data.
6652  
6653 9 my $sub_record = 0x0000; # Sub-record identifier.
6654 9 my $sub_length = 0x0000; # Length of sub-record.
6655 9 my $sub_data = ''; # Data of sub-record.
6656 9 my $options = 0x2101;
6657 9 my $reserved = 0x0000;
6658  
6659 # Add ftCmo (common object data) subobject
6660 9 $sub_record = 0x0015; # ftCmo
6661 9 $sub_length = 0x0012;
6662 9 $sub_data = pack 'vvvVVV', $obj_type, $obj_id, $options,
6663 $reserved, $reserved, $reserved;
6664 9 $data = pack 'vv', $sub_record, $sub_length;
6665 9 $data .= $sub_data;
6666  
6667 # Add ftSbs Scroll bar subobject
6668 9 $sub_record = 0x000C; # ftSbs
6669 9 $sub_length = 0x0014;
6670 9 $sub_data = pack 'H*', '0000000000000000640001000A00000010000100';
6671 9 $data .= pack 'vv', $sub_record, $sub_length;
6672 9 $data .= $sub_data;
6673  
6674  
6675 # Add ftLbsData (List box data) subobject
6676 9 $sub_record = 0x0013; # ftLbsData
6677 9 $sub_length = 0x1FEE; # Special case (undocumented).
6678  
6679  
6680 # If the filter is active we set one of the undocumented flags.
6681 9 my $col = $_[1];
6682  
6683 9 ▶ if ($self->{_filter_cols}->{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_filter_cols'}{$col}) { } 0 9 9
6684 0 $sub_data = pack 'H*', '000000000100010300000A0008005700';
6685 }
6686 else {
6687 9 $sub_data = pack 'H*', '00000000010001030000020008005700';
6688 }
6689  
6690 9 $data .= pack 'vv', $sub_record, $sub_length;
6691 9 $data .= $sub_data;
6692  
6693  
6694 # Add ftEnd (end of object) subobject
6695 9 $sub_record = 0x0000; # ftNts
6696 9 $sub_length = 0x0000;
6697 9 $data .= pack 'vv', $sub_record, $sub_length;
6698  
6699 # Pack the record.
6700 9 my $header = pack('vv', $record, $length);
6701  
6702 9 $self->_append($header, $data);
6703 }
6704  
6705  
6706 ###############################################################################
6707 #
6708 # _store_mso_drawing_text_box()
6709 #
6710 # Write the MSODRAWING ClientTextbox record that is part of comments.
6711 #
6712 1 ▶sub _store_mso_drawing_text_box {
Subroutine _store_mso_drawing_text_box: called CC=1 SCAR=0.0
6713  
6714 1 my $self = shift;
6715  
6716 1 my $record = 0x00EC; # Record identifier
6717 1 my $length = 0x0008; # Bytes to follow
6718  
6719  
6720 1 my $data = $self->_store_mso_client_text_box();
6721 1 my $header = pack("vv", $record, $length);
6722  
6723 1 $self->_append($header, $data);
6724 }
6725  
6726  
6727 ###############################################################################
6728 #
6729 # _store_mso_client_text_box()
6730 #
6731 # Write the Escher ClientTextbox record that is part of MSODRAWING.
6732 #
6733 2 ▶sub _store_mso_client_text_box {
Subroutine _store_mso_client_text_box: called CC=1 SCAR=0.0
6734  
6735 2 my $self = shift;
6736  
6737 2 my $type = 0xF00D;
6738 2 my $version = 0;
6739 2 my $instance = 0;
6740 2 my $data = '';
6741 2 my $length = 0;
6742  
6743  
6744 2 return $self->_add_mso_generic($type, $version, $instance, $data, $length);
6745 }
6746  
6747  
6748 ###############################################################################
6749 #
6750 # _store_txo()
6751 #
6752 # Write the worksheet TXO record that is part of cell comments.
6753 #
6754 2 ▶sub _store_txo {
Subroutine _store_txo: called CC=3 SCAR=11.1
6755  
6756 2 my $self = shift;
6757  
6758 2 my $record = 0x01B6; # Record identifier
6759 2 my $length = 0x0012; # Bytes to follow
6760  
6761 2 my $string_len = $_[0]; # Length of the note text.
6762 2 ▶ my $format_len = $_[1] || 16; # Length of the format runs.
Condition50%1/2
$_[1] || 16
l!l
02
MC/DC0%0/1
$_[1] || 16
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
6763 2 ▶ my $rotation = $_[2] || 0; # Options
Condition50%1/2
$_[2] || 0
l!l
02
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table50%1/2
A
A= $_[2]
Aresult
11
00
6764 2 my $grbit = 0x0212; # Options
6765 2 my $reserved = 0x0000; # Options
6766  
6767 # Pack the record.
6768 2 my $header = pack("vv", $record, $length);
6769 2 my $data = pack("vvVvvvV", $grbit, $rotation, $reserved, $reserved,
6770 $string_len, $format_len, $reserved);
6771  
6772 2 $self->_append($header, $data);
6773  
6774 }
6775  
6776  
6777 ###############################################################################
6778 #
6779 # _store_txo_continue_1()
6780 #
6781 # Write the first CONTINUE record to follow the TXO record. It contains the
6782 # text data.
6783 #
6784 2 ▶sub _store_txo_continue_1 {
Subroutine _store_txo_continue_1: called CC=3 SCAR=12.4
6785  
6786 2 my $self = shift;
6787  
6788 2 my $record = 0x003C; # Record identifier
6789 2 my $string = $_[0]; # Comment string.
6790 2 ▶ my $encoding = $_[1] || 0; # Encoding of the string.
Condition50%1/2
$_[1] || 0
l!l
02
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
6791  
6792  
6793 # Split long comment strings into smaller continue blocks if necessary.
6794 # We can't let BIFFwriter::_add_continue() handled this since an extra
6795 # encoding byte has to be added similar to the SST block.
6796 #
6797 # We make the limit size smaller than the _add_continue() size and even
6798 # so that UTF16 chars occur in the same block.
6799 #
6800 2 my $limit = 8218;
6801 2 ▶ while (length($string) > $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string > $limit) 0 2 2
6802 0 my $tmp_str = substr($string, 0, $limit, "");
6803  
6804 0 my $data = pack("C", $encoding) . $tmp_str;
6805 0 my $length = length $data;
6806 0 my $header = pack("vv", $record, $length);
6807  
6808 0 $self->_append($header, $data);
6809 }
6810  
6811 # Pack the record.
6812 2 my $data = pack("C", $encoding) . $string;
6813 2 my $length = length $data;
6814 2 my $header = pack("vv", $record, $length);
6815  
6816 2 $self->_append($header, $data);
6817  
6818 }
6819  
6820  
6821 ###############################################################################
6822 #
6823 # _store_txo_continue_2()
6824 #
6825 # Write the second CONTINUE record to follow the TXO record. It contains the
6826 # formatting information for the string.
6827 #
6828 2 ▶sub _store_txo_continue_2 {
Subroutine _store_txo_continue_2: called CC=2 SCAR=6.9
6829  
6830 2 my $self = shift;
6831  
6832 2 my $record = 0x003C; # Record identifier
6833 2 my $length = 0x0000; # Bytes to follow
6834 2 my $formats = $_[0]; # Formatting information
6835  
6836  
6837 # Pack the record.
6838 2 my $data = '';
6839  
6840 2 for my $a_ref (@$formats) {
6841 4 $data .= pack "vvV", $a_ref->[0], $a_ref->[1], 0x0;
6842 }
6843  
6844 2 $length = length $data;
6845 2 my $header = pack("vv", $record, $length);
6846  
6847  
6848 2 $self->_append($header, $data);
6849  
6850 }
6851  
6852  
6853 ###############################################################################
6854 #
6855 # _store_note()
6856 #
6857 # Write the worksheet NOTE record that is part of cell comments.
6858 #
6859 6 ▶sub _store_note {
Subroutine _store_note: called CC=7 SCAR=19.6
6860  
6861 6 my $self = shift;
6862  
6863 6 my $record = 0x001C; # Record identifier
6864 6 my $length = 0x000C; # Bytes to follow
6865  
6866 6 my $row = $_[0];
6867 6 my $col = $_[1];
6868 6 my $obj_id = $_[2];
6869 6 ▶ my $author = $_[3] || $self->{_comments_author};
Condition66%2/3
$_[3] || $self->{'_comments_author'}
l!l&&r!l&&!r
402
MC/DC50%1/2
$_[3] || $self->{'_comments_author'}
$_[3] $self->{'_comments_author'}
Truth table66%2/3
A || B
A= $_[3]B= $self->{'_comments_author'}
ABresult
1X1
011
000
6870 6 ▶ my $author_enc = $_[4] || $self->{_comments_author_enc};
Condition66%2/3
$_[4] || $self->{'_comments_author_enc'}
l!l&&r!l&&!r
204
MC/DC50%1/2
$_[4] || $self->{'_comments_author_enc'}
$_[4] $self->{'_comments_author_enc'}
Truth table66%2/3
A || B
A= $_[4]B= $self->{'_comments_author_enc'}
ABresult
1X1
011
000
6871 6 my $visible = $_[5];
6872  
6873  
6874 # Use the visible flag if set by the user or else use the worksheet value.
6875 # The flag is also set in _store_mso_opt_comment() but with the opposite
6876 # value.
6877 6 ▶ if (defined $visible) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $visible) { } 1 5 6
6878 1 ▶ $visible = $visible ? 0x0002 : 0x0000;
Branch: 1 branch
BranchTrueFalseTotal
$visible ? : 1 0 1
6879 }
6880 else {
6881 5 ▶ $visible = $self->{_comments_visible} ? 0x0002 : 0x0000;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_comments_visible'} ? : 0 5 5
6882 }
6883  
6884  
6885 # Get the number of chars in the author string (not bytes).
6886 6 my $num_chars = length $author;
6887 6 ▶ $num_chars /= 2 if $author_enc;
Branch: 1 branch
BranchTrueFalseTotal
if $author_enc 2 4 6
6888  
6889  
6890 # Null terminate the author string.
6891 6 $author .= "\0";
6892  
6893  
6894 # Pack the record.
6895 6 my $data = pack("vvvvvC", $row, $col, $visible, $obj_id,
6896 $num_chars, $author_enc);
6897  
6898 6 $length = length($data) + length($author);
6899 6 my $header = pack("vv", $record, $length);
6900  
6901 6 $self->_append($header, $data, $author);
6902 }
6903  
6904  
6905 ###############################################################################
6906 #
6907 # _comment_params()
6908 #
6909 # This method handles the additional optional parameters to write_comment() as
6910 # well as calculating the comment object position and vertices.
6911 #
6912 22562 ▶sub _comment_params {
Subroutine _comment_params: called CC=34 SCAR=37.0
6913  
6914 22562 my $self = shift;
6915  
6916 22562 my $row = shift;
6917 22562 my $col = shift;
6918 22562 my $string = shift;
6919  
6920 22562 my $default_width = 128;
6921 22562 my $default_height = 74;
6922  
6923 22562 my %params = (
6924 author => '',
6925 author_encoding => 0,
6926 encoding => 0,
6927 color => undef,
6928 start_cell => undef,
6929 start_col => undef,
6930 start_row => undef,
6931 visible => undef,
6932 width => $default_width,
6933 height => $default_height,
6934 x_offset => undef,
6935 x_scale => 1,
6936 y_offset => undef,
6937 y_scale => 1,
6938 );
6939  
6940  
6941 # Overwrite the defaults with any user supplied values. Incorrect or
6942 # misspelled parameters are silently ignored.
6943 22562 %params = (%params, @_);
6944  
6945  
6946 # Ensure that a width and height have been set.
6947 22562 ▶ $params{width} = $default_width if not $params{width};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"width"} 0 22562 22562
6948 22562 ▶ $params{height} = $default_height if not $params{height};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"height"} 0 22562 22562
6949  
6950  
6951 # Check that utf16 strings have an even number of bytes.
6952 22562 ▶ if ($params{encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"encoding"}) 0 22562 22562
6953 0 ▶ croak "Uneven number of bytes in comment string"
Branch: 1 branch
BranchTrueFalseTotal
if length($string) % 2 0 0 0
6954 if length($string) % 2;
6955  
6956 # Change from UTF-16BE to UTF-16LE
6957 0 $string = pack 'v*', unpack 'n*', $string;
6958 }
6959  
6960 22562 ▶ if ($params{author_encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"author_encoding"}) 1 22561 22562
6961 croak "Uneven number of bytes in author string"
6962 1 ▶ if length($params{author}) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if length($params{"author"}) % 2 0 1 1
6963  
6964 # Change from UTF-16BE to UTF-16LE
6965 1 $params{author} = pack 'v*', unpack 'n*', $params{author};
6966 }
6967  
6968  
6969 # Handle utf8 strings in perl 5.8.
6970 22562 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 22562 0 22562
6971 22562 require Encode;
6972  
6973 22562 ▶ if (Encode::is_utf8($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($string)) 0 22562 22562
6974 0 $string = Encode::encode("UTF-16LE", $string);
6975 0 $params{encoding} = 1;
6976 }
6977  
6978 22562 ▶ if (Encode::is_utf8($params{author})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($params{"author"})) 1 22561 22562
6979 1 $params{author} = Encode::encode("UTF-16LE", $params{author});
6980 1 $params{author_encoding} = 1;
6981 }
6982 }
6983  
6984  
6985 # Limit the string to the max number of chars (not bytes).
6986 22562 my $max_len = 32767;
6987 22562 ▶ $max_len *= 2 if $params{encoding};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"encoding"} 0 22562 22562
6988  
6989 22562 ▶ if (length($string) > $max_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string > $max_len) 0 22562 22562
6990 0 $string = substr($string, 0, $max_len);
6991 }
6992  
6993  
6994 # Set the comment background colour.
6995 22562 my $color = $params{color};
6996 22562 $color = &Spreadsheet::WriteExcel::Format::_get_color($color);
6997 22562 ▶ $color = 0x50 if $color == 0x7FFF; # Default color.
Branch: 1 branch
BranchTrueFalseTotal
if $color == 32767 22562 0 22562
6998 22562 $params{color} = $color;
6999  
7000  
7001 # Convert a cell reference to a row and column.
7002 22562 ▶ if (defined $params{start_cell}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"start_cell"}) 0 22562 22562
7003 0 my ($row, $col) = $self->_substitute_cellref($params{start_cell});
7004 0 $params{start_row} = $row;
7005 0 $params{start_col} = $col;
7006 }
7007  
7008  
7009 # Set the default start cell and offsets for the comment. These are
7010 # generally fixed in relation to the parent cell. However there are
7011 # some edge cases for cells at the, er, edges.
7012 #
7013 22562 ▶ if (not defined $params{start_row}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"start_row"}) 22562 0 22562
7014  
7015 22562 ▶ if ($row == 0 ) {$params{start_row} = 0 }
Branch: 4 branches
BranchTrueFalseTotal
if ($row == 0) { } 37 22525 22562
elsif ($row == 65533) { } 1 22524 22525
elsif ($row == 65534) { } 1 22523 22524
elsif ($row == 65535) { } 1 22522 22523
7016 1 elsif ($row == 65533) {$params{start_row} = 65529 }
7017 1 elsif ($row == 65534) {$params{start_row} = 65530 }
7018 1 elsif ($row == 65535) {$params{start_row} = 65531 }
7019 22522 else {$params{start_row} = $row -1}
7020 }
7021  
7022 22562 ▶ if (not defined $params{y_offset}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"y_offset"}) 22561 1 22562
7023  
7024 22561 ▶ if ($row == 0 ) {$params{y_offset} = 2 }
Branch: 4 branches
BranchTrueFalseTotal
if ($row == 0) { } 37 22524 22561
elsif ($row == 65533) { } 1 22523 22524
elsif ($row == 65534) { } 1 22522 22523
elsif ($row == 65535) { } 1 22521 22522
7025 1 elsif ($row == 65533) {$params{y_offset} = 4 }
7026 1 elsif ($row == 65534) {$params{y_offset} = 4 }
7027 1 elsif ($row == 65535) {$params{y_offset} = 2 }
7028 22521 else {$params{y_offset} = 7 }
7029 }
7030  
7031 22562 ▶ if (not defined $params{start_col}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"start_col"}) 22562 0 22562
7032  
7033 22562 ▶ if ($col == 253 ) {$params{start_col} = 250 }
Branch: 3 branches
BranchTrueFalseTotal
if ($col == 253) { } 1 22561 22562
elsif ($col == 254) { } 1 22560 22561
elsif ($col == 255) { } 1 22559 22560
7034 1 elsif ($col == 254 ) {$params{start_col} = 251 }
7035 1 elsif ($col == 255 ) {$params{start_col} = 252 }
7036 22559 else {$params{start_col} = $col +1}
7037 }
7038  
7039 22562 ▶ if (not defined $params{x_offset}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"x_offset"}) 22561 1 22562
7040  
7041 22561 ▶ if ($col == 253 ) {$params{x_offset} = 49 }
Branch: 3 branches
BranchTrueFalseTotal
if ($col == 253) { } 1 22560 22561
elsif ($col == 254) { } 1 22559 22560
elsif ($col == 255) { } 1 22558 22559
7042 1 elsif ($col == 254 ) {$params{x_offset} = 49 }
7043 1 elsif ($col == 255 ) {$params{x_offset} = 49 }
7044 22558 else {$params{x_offset} = 15 }
7045 }
7046  
7047  
7048 # Scale the size of the comment box if required.
7049 22562 ▶ if ($params{x_scale}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"x_scale"}) 22562 0 22562
7050 22562 $params{width} = $params{width} * $params{x_scale};
7051 }
7052  
7053 22562 ▶ if ($params{y_scale}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"y_scale"}) 22562 0 22562
7054 22562 $params{height} = $params{height} * $params{y_scale};
7055 }
7056  
7057  
7058 # Calculate the positions of comment object.
7059 my @vertices = $self->_position_object( $params{start_col},
7060 $params{start_row},
7061 $params{x_offset},
7062 $params{y_offset},
7063 $params{width},
7064 $params{height}
7065 22562 );
7066  
7067 return(
7068 $row,
7069 $col,
7070 $string,
7071 $params{encoding},
7072 $params{author},
7073 $params{author_encoding},
7074 $params{visible},
7075 $params{color},
7076 22562 [@vertices]
7077 );
7078 }
7079  
7080  
7081  
7082 #
7083 # DATA VALIDATION
7084 #
7085  
7086 ###############################################################################
7087 #
7088 # data_validation($row, $col, {...})
7089 #
7090 # This method handles the interface to Excel data validation.
7091 # Somewhat ironically the this requires a lot of validation code since the
7092 # interface is flexible and covers a several types of data validation.
7093 #
7094 # We allow data validation to be called on one cell or a range of cells. The
7095 # hashref contains the validation parameters and must be the last param:
7096 # data_validation($row, $col, {...})
7097 # data_validation($first_row, $first_col, $last_row, $last_col, {...})
7098 #
7099 # Returns 0 : normal termination
7100 # -1 : insufficient number of arguments
7101 # -2 : row or column out of range
7102 # -3 : incorrect parameter.
7103 #
7104 43 ▶sub data_validation {
Subroutine data_validation: called CC=36 SCAR=39.9
Pod undocumented
7105  
7106 43 my $self = shift;
7107  
7108 # Check for a cell reference in A1 notation and substitute row and column
7109 43 ▶ if ($_[0] =~ /^\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\D/) 38 5 43
7110 38 @_ = $self->_substitute_cellref(@_);
7111 }
7112  
7113 # Check for a valid number of args.
7114 0 ▶ if (@_ != 5 && @_ != 3) { return -1 }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 5 and @_ != 3) 0 43 43
Condition66%2/3
@_ != 5 and @_ != 3
!ll&&!rl&&r
5380
MC/DC0%0/2
@_ != 5 and @_ != 3
@_ != 5 @_ != 3
Truth table66%2/3
A and B
A= @_ != 5B= @_ != 3
ABresult
0X0
100
111
7115  
7116 # The final hashref contains the validation parameters.
7117 43 my $param = pop;
7118  
7119 # Make the last row/col the same as the first if not defined.
7120 43 my ($row1, $col1, $row2, $col2) = @_;
7121 43 ▶ if (!defined $row2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row2) 38 5 43
7122 38 $row2 = $row1;
7123 38 $col2 = $col1;
7124 }
7125  
7126 # Check that row and col are valid without storing the values.
7127 43 ▶ return -2 if $self->_check_dimensions($row1, $col1, 1, 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_dimensions($row1, $col1, 1, 1) 0 43 43
7128 43 ▶ return -2 if $self->_check_dimensions($row2, $col2, 1, 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_dimensions($row2, $col2, 1, 1) 0 43 43
7129  
7130  
7131 # Check that the last parameter is a hash list.
7132 43 ▶ if (ref $param ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param ne "HASH") 0 43 43
7133 0 carp "Last parameter '$param' in data_validation() must be a hash ref";
7134 0 return -3;
7135 }
7136  
7137 # List of valid input parameters.
7138 43 my %valid_parameter = (
7139 validate => 1,
7140 criteria => 1,
7141 value => 1,
7142 source => 1,
7143 minimum => 1,
7144 maximum => 1,
7145 ignore_blank => 1,
7146 dropdown => 1,
7147 show_input => 1,
7148 input_title => 1,
7149 input_message => 1,
7150 show_error => 1,
7151 error_title => 1,
7152 error_message => 1,
7153 error_type => 1,
7154 other_cells => 1,
7155 );
7156  
7157 # Check for valid input parameters.
7158 43 for my $param_key (keys %$param) {
7159 171 ▶ if (not exists $valid_parameter{$param_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $valid_parameter{$param_key}) 0 171 171
7160 0 carp "Unknown parameter '$param_key' in data_validation()";
7161 0 return -3;
7162 }
7163 }
7164  
7165 # Map alternative parameter names 'source' or 'minimum' to 'value'.
7166 43 ▶ $param->{value} = $param->{source} if defined $param->{source};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param->{"source"} 3 40 43
7167 43 ▶ $param->{value} = $param->{minimum} if defined $param->{minimum};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param->{"minimum"} 19 24 43
7168  
7169 # 'validate' is a required parameter.
7170 43 ▶ if (not exists $param->{validate}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param->{"validate"}) 0 43 43
7171 0 carp "Parameter 'validate' is required in data_validation()";
7172 0 return -3;
7173 }
7174  
7175  
7176 # List of valid validation types.
7177 43 my %valid_type = (
7178 'any' => 0,
7179 'any value' => 0,
7180 'whole number' => 1,
7181 'whole' => 1,
7182 'integer' => 1,
7183 'decimal' => 2,
7184 'list' => 3,
7185 'date' => 4,
7186 'time' => 5,
7187 'text length' => 6,
7188 'length' => 6,
7189 'custom' => 7,
7190 );
7191  
7192  
7193 # Check for valid validation types.
7194 43 ▶ if (not exists $valid_type{lc($param->{validate})}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $valid_type{lc $param->{'validate'}}) { } 0 43 43
7195 0 carp "Unknown validation type '$param->{validate}' for parameter " .
7196 "'validate' in data_validation()";
7197 0 return -3;
7198 }
7199 else {
7200 43 $param->{validate} = $valid_type{lc($param->{validate})};
7201 }
7202  
7203  
7204 # No action is required for validation type 'any'.
7205 # TODO: we should perhaps store 'any' for message only validations.
7206 43 ▶ return 0 if $param->{validate} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"validate"} == 0 1 42 43
7207  
7208  
7209 # The list and custom validations don't have a criteria so we use a default
7210 # of 'between'.
7211 42 ▶ if ($param->{validate} == 3 || $param->{validate} == 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"validate"} == 3 or $param->{"validate"} == 7) 4 38 42
Condition100%3/3
$param->{"validate"} == 3 or $param->{"validate"} == 7
l!l&&r!l&&!r
3138
MC/DC100%2/2
$param->{"validate"} == 3 or $param->{"validate"} == 7
$param->{"validate"} == 3 $param->{"validate"} == 7
Truth table100%3/3
A or B
A= $param->{"validate"} == 3B= $param->{"validate"} == 7
ABresult
1X1
011
000
7212 4 $param->{criteria} = 'between';
7213 4 $param->{maximum} = undef;
7214 }
7215  
7216 # 'criteria' is a required parameter.
7217 42 ▶ if (not exists $param->{criteria}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param->{"criteria"}) 0 42 42
7218 0 carp "Parameter 'criteria' is required in data_validation()";
7219 0 return -3;
7220 }
7221  
7222  
7223 # List of valid criteria types.
7224 42 my %criteria_type = (
7225 'between' => 0,
7226 'not between' => 1,
7227 'equal to' => 2,
7228 '=' => 2,
7229 '==' => 2,
7230 'not equal to' => 3,
7231 '!=' => 3,
7232 '<>' => 3,
7233 'greater than' => 4,
7234 '>' => 4,
7235 'less than' => 5,
7236 '<' => 5,
7237 'greater than or equal to' => 6,
7238 '>=' => 6,
7239 'less than or equal to' => 7,
7240 '<=' => 7,
7241 );
7242  
7243 # Check for valid criteria types.
7244 42 ▶ if (not exists $criteria_type{lc($param->{criteria})}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $criteria_type{lc $param->{'criteria'}}) { } 0 42 42
7245 0 carp "Unknown criteria type '$param->{criteria}' for parameter " .
7246 "'criteria' in data_validation()";
7247 0 return -3;
7248 }
7249 else {
7250 42 $param->{criteria} = $criteria_type{lc($param->{criteria})};
7251 }
7252  
7253  
7254 # 'Between' and 'Not between' criteria require 2 values.
7255 42 ▶ if ($param->{criteria} == 0 || $param->{criteria} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{'criteria'} == 0 or $param->{'criteria'} == 1) { } 23 19 42
Condition100%3/3
$param->{"criteria"} == 0 or $param->{"criteria"} == 1
l!l&&r!l&&!r
22119
MC/DC100%2/2
$param->{"criteria"} == 0 or $param->{"criteria"} == 1
$param->{"criteria"} == 0 $param->{"criteria"} == 1
Truth table100%3/3
A or B
A= $param->{"criteria"} == 0B= $param->{"criteria"} == 1
ABresult
1X1
011
000
7256 23 ▶ if (not exists $param->{maximum}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param->{"maximum"}) 0 23 23
7257 0 carp "Parameter 'maximum' is required in data_validation() " .
7258 "when using 'between' or 'not between' criteria";
7259 0 return -3;
7260 }
7261 }
7262 else {
7263 19 $param->{maximum} = undef;
7264 }
7265  
7266  
7267  
7268 # List of valid error dialog types.
7269 42 my %error_type = (
7270 'stop' => 0,
7271 'warning' => 1,
7272 'information' => 2,
7273 );
7274  
7275 # Check for valid error dialog types.
7276 42 ▶ if (not exists $param->{error_type}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $param->{'error_type'}) { } 40 2 42
elsif (not exists $error_type{lc $param->{'error_type'}}) { } 0 2 2
7277 40 $param->{error_type} = 0;
7278 }
7279 elsif (not exists $error_type{lc($param->{error_type})}) {
7280 0 carp "Unknown criteria type '$param->{error_type}' for parameter " .
7281 "'error_type' in data_validation()";
7282 0 return -3;
7283 }
7284 else {
7285 2 $param->{error_type} = $error_type{lc($param->{error_type})};
7286 }
7287  
7288  
7289 # Convert date/times value if required.
7290 42 ▶ if ($param->{validate} == 4 || $param->{validate} == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"validate"} == 4 or $param->{"validate"} == 5) 5 37 42
Condition100%3/3
$param->{"validate"} == 4 or $param->{"validate"} == 5
l!l&&r!l&&!r
3237
MC/DC100%2/2
$param->{"validate"} == 4 or $param->{"validate"} == 5
$param->{"validate"} == 4 $param->{"validate"} == 5
Truth table100%3/3
A or B
A= $param->{"validate"} == 4B= $param->{"validate"} == 5
ABresult
1X1
011
000
7291 5 ▶ if ($param->{value} =~ /T/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"value"} =~ /T/) 3 2 5
7292 3 my $date_time = $self->convert_date_time($param->{value});
7293  
7294 3 ▶ if (!defined $date_time) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $date_time) { } 0 3 3
7295 0 carp "Invalid date/time value '$param->{value}' " .
7296 "in data_validation()";
7297 0 return -3;
7298 }
7299 else {
7300 3 $param->{value} = $date_time;
7301 }
7302 }
7303 5 ▶ if (defined $param->{maximum} && $param->{maximum} =~ /T/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param->{"maximum"} and $param->{"maximum"} =~ /T/) 1 4 5
Condition66%2/3
defined $param->{"maximum"} and $param->{"maximum"} =~ /T/
!ll&&!rl&&r
401
MC/DC50%1/2
defined $param->{"maximum"} and $param->{"maximum"} =~ /T/
defined $param->{"maximum"} $param->{"maximum"} =~ /T/
Truth table66%2/3
A and B
A= defined $param->{"maximum"}B= $param->{"maximum"} =~ /T/
ABresult
0X0
100
111
7304 1 my $date_time = $self->convert_date_time($param->{maximum});
7305  
7306 1 ▶ if (!defined $date_time) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $date_time) { } 0 1 1
7307 0 carp "Invalid date/time value '$param->{maximum}' " .
7308 "in data_validation()";
7309 0 return -3;
7310 }
7311 else {
7312 1 $param->{maximum} = $date_time;
7313 }
7314 }
7315 }
7316  
7317  
7318 # Set some defaults if they haven't been defined by the user.
7319 42 ▶ $param->{ignore_blank} = 1 if !defined $param->{ignore_blank};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param->{"ignore_blank"} 41 1 42
7320 42 ▶ $param->{dropdown} = 1 if !defined $param->{dropdown};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param->{"dropdown"} 41 1 42
7321 42 ▶ $param->{show_input} = 1 if !defined $param->{show_input};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param->{"show_input"} 40 2 42
7322 42 ▶ $param->{show_error} = 1 if !defined $param->{show_error};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param->{"show_error"} 41 1 42
7323  
7324  
7325 # These are the cells to which the validation is applied.
7326 42 $param->{cells} = [[$row1, $col1, $row2, $col2]];
7327  
7328 # A (for now) undocumented parameter to pass additional cell ranges.
7329 42 ▶ if (exists $param->{other_cells}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"other_cells"}) 3 39 42
7330  
7331 3 push @{$param->{cells}}, @{$param->{other_cells}};
7332 }
7333  
7334 # Store the validation information until we close the worksheet.
7335 42 push @{$self->{_validations}}, $param;
7336 }
7337  
7338  
7339 ###############################################################################
7340 #
7341 # _store_validation_count()
7342 #
7343 # Store the count of the DV records to follow.
7344 #
7345 # Note, this could be wrapped into _store_dv() but we may require separate
7346 # handling of the object id at a later stage.
7347 #
7348 133 ▶sub _store_validation_count {
Subroutine _store_validation_count: called CC=2 SCAR=6.9
7349  
7350 133 my $self = shift;
7351  
7352 133 my $dv_count = @{$self->{_validations}};
7353 133 my $obj_id = -1;
7354  
7355 133 ▶ return unless $dv_count;
Branch: 1 branch
BranchTrueFalseTotal
unless $dv_count 132 1 133
7356  
7357 1 $self->_store_dval($obj_id , $dv_count);
7358 }
7359  
7360  
7361 ###############################################################################
7362 #
7363 # _store_validations()
7364 #
7365 # Store the data_validation records.
7366 #
7367 176 ▶sub _store_validations {
Subroutine _store_validations: called CC=3 SCAR=11.0
7368  
7369 176 my $self = shift;
7370  
7371 176 ▶ return unless scalar @{$self->{_validations}};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"_validations"};} 133 43 176
7372  
7373 43 for my $param (@{$self->{_validations}}) {
7374 $self->_store_dv( $param->{cells},
7375 $param->{validate},
7376 $param->{criteria},
7377 $param->{value},
7378 $param->{maximum},
7379 $param->{input_title},
7380 $param->{input_message},
7381 $param->{error_title},
7382 $param->{error_message},
7383 $param->{error_type},
7384 $param->{ignore_blank},
7385 $param->{dropdown},
7386 $param->{show_input},
7387 $param->{show_error},
7388 43 );
7389 }
7390 }
7391  
7392  
7393 ###############################################################################
7394 #
7395 # _store_dval()
7396 #
7397 # Store the DV record which contains the number of and information common to
7398 # all DV structures.
7399 #
7400 4 ▶sub _store_dval {
Subroutine _store_dval: called CC=1 SCAR=0.0
7401  
7402 4 my $self = shift;
7403  
7404 4 my $record = 0x01B2; # Record identifier
7405 4 my $length = 0x0012; # Bytes to follow
7406  
7407 4 my $obj_id = $_[0]; # Object ID number.
7408 4 my $dv_count = $_[1]; # Count of DV structs to follow.
7409  
7410 4 my $flags = 0x0004; # Option flags.
7411 4 my $x_coord = 0x00000000; # X coord of input box.
7412 4 my $y_coord = 0x00000000; # Y coord of input box.
7413  
7414  
7415 # Pack the record.
7416 4 my $header = pack('vv', $record, $length);
7417 4 my $data = pack('vVVVV', $flags, $x_coord, $y_coord, $obj_id, $dv_count);
7418  
7419 4 $self->_append($header, $data);
7420 }
7421  
7422  
7423 ###############################################################################
7424 #
7425 # _store_dv()
7426 #
7427 # Store the DV record that specifies the data validation criteria and options
7428 # for a range of cells..
7429 #
7430 43 ▶sub _store_dv {
Subroutine _store_dv: called CC=4 SCAR=13.9
7431  
7432 43 my $self = shift;
7433  
7434 43 my $record = 0x01BE; # Record identifier
7435 43 my $length = 0x0000; # Bytes to follow
7436  
7437 43 my $flags = 0x00000000; # DV option flags.
7438  
7439 43 my $cells = $_[0]; # Aref of cells to which DV applies.
7440 43 my $validation_type = $_[1]; # Type of data validation.
7441 43 my $criteria_type = $_[2]; # Validation criteria.
7442 43 my $formula_1 = $_[3]; # Value/Source/Minimum formula.
7443 43 my $formula_2 = $_[4]; # Maximum formula.
7444 43 my $input_title = $_[5]; # Title of input message.
7445 43 my $input_message = $_[6]; # Text of input message.
7446 43 my $error_title = $_[7]; # Title of error message.
7447 43 my $error_message = $_[8]; # Text of input message.
7448 43 my $error_type = $_[9]; # Error dialog type.
7449 43 my $ignore_blank = $_[10]; # Ignore blank cells.
7450 43 my $dropdown = $_[11]; # Display dropdown with list.
7451 43 my $input_box = $_[12]; # Display input box.
7452 43 my $error_box = $_[13]; # Display error box.
7453 43 my $ime_mode = 0; # IME input mode for far east fonts.
7454 43 my $str_lookup = 0; # See below.
7455  
7456 # Set the string lookup flag for 'list' validations with a string array.
7457 43 ▶ if ($validation_type == 3 && ref $formula_1 eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($validation_type == 3 and ref $formula_1 eq "ARRAY") 2 41 43
Condition100%3/3
$validation_type == 3 and ref $formula_1 eq "ARRAY"
!ll&&!rl&&r
4012
MC/DC100%2/2
$validation_type == 3 and ref $formula_1 eq "ARRAY"
$validation_type == 3 ref $formula_1 eq "ARRAY"
Truth table100%3/3
A and B
A= $validation_type == 3B= ref $formula_1 eq "ARRAY"
ABresult
0X0
100
111
7458 2 $str_lookup = 1;
7459 }
7460  
7461 # The dropdown flag is stored as a negated value.
7462 43 my $no_dropdown = not $dropdown;
7463  
7464 # Set the required flags.
7465 43 $flags |= $validation_type;
7466 43 $flags |= $error_type << 4;
7467 43 $flags |= $str_lookup << 7;
7468 43 $flags |= $ignore_blank << 8;
7469 43 $flags |= $no_dropdown << 9;
7470 43 $flags |= $ime_mode << 10;
7471 43 $flags |= $input_box << 18;
7472 43 $flags |= $error_box << 19;
7473 43 $flags |= $criteria_type << 20;
7474  
7475 # Pack the validation formulas.
7476 43 $formula_1 = $self->_pack_dv_formula($formula_1);
7477 43 $formula_2 = $self->_pack_dv_formula($formula_2);
7478  
7479 # Pack the input and error dialog strings.
7480 43 $input_title = $self->_pack_dv_string($input_title, 32 );
7481 43 $error_title = $self->_pack_dv_string($error_title, 32 );
7482 43 $input_message = $self->_pack_dv_string($input_message, 255);
7483 43 $error_message = $self->_pack_dv_string($error_message, 255);
7484  
7485 # Pack the DV cell data.
7486 43 my $dv_count = scalar @$cells;
7487 43 my $dv_data = pack 'v', $dv_count;
7488 43 for my $range (@$cells) {
7489 48 $dv_data .= pack 'vvvv', $range->[0],
7490 $range->[2],
7491 $range->[1],
7492 $range->[3];
7493 }
7494  
7495 # Pack the record.
7496 43 my $data = pack 'V', $flags;
7497 43 $data .= $input_title;
7498 43 $data .= $error_title;
7499 43 $data .= $input_message;
7500 43 $data .= $error_message;
7501 43 $data .= $formula_1;
7502 43 $data .= $formula_2;
7503 43 $data .= $dv_data;
7504  
7505 43 my $header = pack('vv', $record, length $data);
7506  
7507 43 $self->_append($header, $data);
7508 }
7509  
7510  
7511 ###############################################################################
7512 #
7513 # _pack_dv_string()
7514 #
7515 # Pack the strings used in the input and error dialog captions and messages.
7516 # Captions are limited to 32 characters. Messages are limited to 255 chars.
7517 #
7518 180 ▶sub _pack_dv_string {
Subroutine _pack_dv_string: called CC=6 SCAR=17.9
7519  
7520 180 my $self = shift;
7521  
7522 180 my $string = $_[0];
7523 180 my $max_length = $_[1];
7524  
7525 180 my $str_length = 0;
7526 180 my $encoding = 0;
7527  
7528 # The default empty string is "\0".
7529 180 ▶ if (!defined $string || $string eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $string or $string eq "") 156 24 180
Condition100%3/3
not defined $string or $string eq ""
l!l&&r!l&&!r
155124
MC/DC100%2/2
not defined $string or $string eq ""
not defined $string $string eq ""
Truth table100%3/3
not(A) or B
A= not defined $stringB= $string eq ""
ABresult
1X1
011
000
7530 156 $string = "\0";
7531 }
7532  
7533 # Excel limits DV captions to 32 chars and messages to 255.
7534 180 ▶ if (length $string > $max_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string > $max_length) 2 178 180
7535 2 $string = substr($string, 0, $max_length);
7536 }
7537  
7538 180 $str_length = length $string;
7539  
7540 # Handle utf8 strings in perl 5.8.
7541 180 ▶ if ($] >= 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008") 180 0 180
7542 180 require Encode;
7543  
7544 180 ▶ if (Encode::is_utf8($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($string)) 2 178 180
7545 2 $string = Encode::encode("UTF-16LE", $string);
7546 2 $encoding = 1;
7547 }
7548 }
7549  
7550 180 return pack('vC', $str_length, $encoding) . $string;
7551 }
7552  
7553  
7554 ###############################################################################
7555 #
7556 # _pack_dv_formula()
7557 #
7558 # Pack the formula used in the DV record. This is the same as an cell formula
7559 # with some additional header information. Note, DV formulas in Excel use
7560 # relative addressing (R1C1 and ptgXxxN) however we use the Formula.pm's
7561 # default absolute addressing (A1 and ptgXxx).
7562 #
7563 98 ▶sub _pack_dv_formula {
Subroutine _pack_dv_formula: called CC=7 SCAR=19.5
7564  
7565 98 my $self = shift;
7566  
7567 98 my $formula = $_[0];
7568 98 my $encoding = 0;
7569 98 my $length = 0;
7570 98 my $unused = 0x0000;
7571 98 my @tokens;
7572  
7573 # Return a default structure for unused formulas.
7574 98 ▶ if (!defined $formula || $formula eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $formula or $formula eq "") 25 73 98
Condition100%3/3
not defined $formula or $formula eq ""
l!l&&r!l&&!r
24173
MC/DC100%2/2
not defined $formula or $formula eq ""
not defined $formula $formula eq ""
Truth table100%3/3
not(A) or B
A= not defined $formulaB= $formula eq ""
ABresult
1X1
011
000
7575 25 return pack('vv', 0, $unused);
7576 }
7577  
7578 # Pack a list array ref as a null separated string.
7579 73 ▶ if (ref $formula eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $formula eq "ARRAY") 4 69 73
7580 4 $formula = join "\0", @$formula;
7581 4 $formula = qq("$formula");
7582 }
7583  
7584 # Strip the = sign at the beginning of the formula string
7585 73 $formula =~ s(^=)();
7586  
7587 # Parse the formula using the parser in Formula.pm
7588 73 my $parser = $self->{_parser};
7589  
7590 # In order to raise formula errors from the point of view of the calling
7591 # program we use an eval block and re-raise the error from here.
7592 #
7593 73 eval { @tokens = $parser->parse_formula($formula) };
7594  
7595 73 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 73 73
7596 0 $@ =~ s/\n$//; # Strip the \n used in the Formula.pm die()
7597 0 croak $@; # Re-raise the error
7598 }
7599 else {
7600 # TODO test for non valid ptgs such as Sheet2!A1
7601 }
7602 # Force 2d ranges to be a reference class.
7603 73 s/_range2d/_range2dR/ for @tokens;
7604 73 s/_name/_nameR/ for @tokens;
7605  
7606 # Parse the tokens into a formula string.
7607 73 $formula = $parser->parse_tokens(@tokens);
7608  
7609  
7610 73 return pack('vv', length $formula, $unused) . $formula;
7611 }
7612  
7613  
7614  
7615  
7616  
7617 32 1;
7618  
7619  
7620 __END__
« WriteExcel/Formula.pm ↑ Summary WriteExcel/Utility.pm »