XLSX.pm

filter: Statementstmt 92.5% 25 / 27 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.4% 34 / 36 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« build.pl ↑ Summary
1 package Excel::Template::XLSX;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base 'Excel::Writer::XLSX';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Excel::Writer::XLSX::Utility;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 1 ▶use version; our $VERSION = version->declare("v1.0.9");
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 1 ▶use Archive::Zip;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Graphics::ColorUtils 'rgb2hls', 'hls2rgb';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util 'openhandle';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use XML::Twig;
Subroutine BEGIN: called CC=1 SCAR=2.6
14
15 1; # Return True from module
16
17 ###############################################################################
18 sub __podhead {
19
20 =for pod
21
22 =head1 NAME
23
24 Excel-Template-XLSX - Create Excel .xlsx files starting from (one or more) template(s).
25
26 =head1 SYNOPSIS
27
28 use Excel::Template::XLSX;
29 my ($self, $workbook) = Excel::Template::XLSX->new('perl.xlsx', 'template1.xlsx', 'template2.xlsx', ...);
30 $self->parse_template();
31
32 # Add a worksheet, ... and anything else you would do with Excel::Writer::XLSX
33 $worksheet = $workbook->add_worksheet();
34
35 # Although Excel::Writer::XLSX says the workbook will automatically get
36 # closed during global destruction. This wrapper around Excel::Writer::XLSX may
37 # mess this up, and it is better to specifically close your workbook when you are done.
38 $workbook->close();
39
40 =head1 DESCRIPTION
41
42 This module is a companion to
43 L<Excel::Writer::XLSX|https://metacpan.org/release/Excel-Writer-XLSX>(EWX), or
44 if you prefer, a wrapper to that module. It uses EWX as a base class. It opens
45 an existing spreadsheet file (.xlsx format), and also creates a new EWX object.
46 As it parses the template file(s), it calls EWX methods to re-create the template
47 contents in the EWX object.
48
49 When parsing is complete, the workbook object is left open for the calling perl
50 script to add additional content.
51
52 The purpose of this module is to separate the roles of content/presentation vs
53 programming in an Excel document, in much the same way that HTML templating
54 engines work. A user who is knowledgeable in Excel can create an Excel file for
55 use as a template, without requiring the skill set of Perl or
56 Excel::Writer::XLSX. Conversely, the Perl programmer who is creating dynamic
57 content does not need design skills to layout the presentation in the template.
58
59 =head1 WHAT IT CAN DO
60
61 Cell Values (strings, numbers, dates, ... )
62 Cell Formulas
63 Cell Hyperlinks
64
65 Cell Formatting (borders, shading, fonts, font sizes, colors)
66
67 Column Widths
68 Row Widths
69
70 Headers and Footers
71
72 Simple template variables (via callback) See L</"template_callback">
73
74 =head1 WHAT IT CAN NOT DO
75
76 Excel::Template::Excel can not modify Excel files in place! It is not
77 intended to. Since the parser directly adds content to the EWX workbook object
78 as the contents are parsed, both the template, and the output file must
79 be open at the same time.
80
81 It may be possible to open the output file to a file handle, and
82 after parsing of the template is complete, write the contents of file
83 over the template. The author has not tried this.
84
85 It is not the design of this module to faithfully re-create the entire
86 contents of the template file in the EWX output. If you are using this
87 module to rewrite Excel files, you are on your own.
88
89 These items are completely dropped from the output file:
90
91 Images in the Sheet
92 Images in Headers/Footers
93 Charts
94 Shapes
95 Themes (gradients, fonts, fills, styles)
96 macros
97 modules (vba code)
98
99 And probably other things. See the tests (t directory of the distribution)
100 for examples of what does work.
101
102 =head1 SUBROUTINES AND METHODS
103
104 =head2 __podhead
105
106 Dummy subroutine to allow me to hide this pod documentation when using code
107 folding in the editor.
108
109 =cut
110
111 }
112 ###############################################################################
113 sub new {
114
115 =head2 new
116
117 Creates a new Excel::Template::XLSX object, and also creates a new
118 Excel::Writer::XLSX object. A workbook object is created for the output file.
119
120 Returns the Template object, and the workbook object. Workbook object is also
121 available as $self->{EWX}; If the caller is only expecting a single
122 return value, then just the $self object is returned.
123
124 =cut
125
126 my ( $class, $output_file, @template_files ) = @_;
127 my $self = {
128 FORMATS => [],
129 HYPERLINKS => {},
130 NEED_PROPS => 1,
131 PRINT_AREA => {},
132 PRINT_TITLES => {},
133 MERGED_RANGES => {},
134 SHARED_STRINGS => [],
135 THEMES => [],
136 ZIP => [],
137
138 template_callback => undef,
139 };
140
141 # Create a new Excel workbook
142 $self->{EWX} = Excel::Writer::XLSX->new($output_file);
143 if ( defined $self->{EWX} ) {
144 $self->{DEFAULT_FORMAT} = $self->{EWX}->add_format();
145 bless $self, $class;
146 }
147 else {
148 die
149 "Can't create new Excel::Writer::XLSX object using file ($output_file) $!";
150 }
151
152 foreach my $template_file (@template_files) {
153 my $zip = Archive::Zip->new;
154 if ( openhandle($template_file) ) {
155 bless $template_file, 'IO::File'
156 if ref($template_file) eq 'GLOB'; # sigh
157 my $status = $zip->readFromFileHandle($template_file);
158 unless ( $status == Archive::Zip::AZ_OK ) {
159 warn "Can't open filehandle as a zip file, skipping";
160 $zip = undef;
161 }
162 }
163 elsif ( !ref($template_file) ) {
164 my $status = $zip->read($template_file);
165 unless ( $status == Archive::Zip::AZ_OK ) {
166 $template_file //= '(undef)';
167 warn "Can't open file '$template_file' as a zip file, skipping";
168 $zip = undef;
169 }
170 }
171 else {
172 warn
173 "Argument to 'new' must be a filename or open filehandle. skipping $template_file";
174 $zip = undef;
175 }
176
177 # Create a list of template files to add to the workbook
178 push @{ $self->{ZIP} }, $zip;
179 }
180 if (wantarray) {
181 return ( $self, $self->{EWX} );
182 }
183 else {
184 return $self;
185 }
186 }
187 ###############################################################################
188 sub parse_template {
189
190 =head2 parse_template
191
192 Parses common elements of the Spreadsheet, such as themes, styles, and strings.
193 These are stored in the main object ($self).
194
195 Finds each sheet in the workbook, and initiates parsing of each sheet.
196
197 Properties for the created workbook are set from the first template that has
198 properties. Properties in subsequent workbooks are ignored.
199
200 =cut
201
202 my $self = shift;
203
204 my $remap = {
205 title => 'title',
206 subject => 'subject',
207 creator => 'author',
208 keywords => 'keywords',
209 description => 'comments',
210
211 manager => 'manager',
212 company => 'company',
213 category => 'category',
214 status => 'status',
215 };
216
217 for my $z ( 0 .. $#{ $self->{ZIP} } ) {
218 my $zip = $self->{ZIP}[$z] // next;
219 $self->{PRINT_TITLES} = {};
220 $self->{SHARED_STRINGS} = [];
221 $self->{FORMATS} = [];
222
223 my $files = $self->_extract_files($zip);
224
225 my $callback = $self->{template_callback};
226 my $call = ref($callback) eq 'CODE';
227 if ( $self->{NEED_PROPS} ) {
228 if ( my @core_nodes
229 = $files->{core}->find_nodes('//cp:coreProperties') )
230 {
231 my $core = shift @core_nodes;
232 my %hash = map {
233 my $prop = $core->first_child( "dc:" . $_ )
234 // $core->first_child( "cp:" . $_ );
235 my %pair = ();
236 if ($prop) {
237 my $text = $prop->text();
238 $call and $self->$callback( \$text );
239 %pair = ( $remap->{$_}, $text );
240 }
241 %pair;
242 } keys %$remap;
243 $self->{EWX}->set_properties(%hash);
244 $self->{NEED_PROPS} = 0;
245 }
246 }
247
248 $self->{THEMES}
249 = $self->_parse_themes( ( values %{ $files->{themes} } )[0] );
250
251 $self->_parse_styles( $files->{styles} );
252 $self->_parse_shared_strings( $files->{strings} );
253
254 # Defined Names (includes print area, print titles)
255 map {
256 my $name = $_->att('name') // '';
257 my $address = $_->text();
258
259 # Print Titles (may contain none, one, or both. Delimited by comma if both supplied)
260 # e.g. Title_Page!$A:$A
261 if ( $name eq '_xlnm.Print_Titles' ) {
262 my @title = split( ',', $address );
263 foreach (@title) {
264 my ( $sheet_name, $range ) = split('!');
265 push @{ $self->{PRINT_TITLES}{$sheet_name} }, $range;
266 }
267
268 # Print Area (Save it until sheets are processed)
269 }
270 elsif ( $name eq '_xlnm.Print_Area' ) {
271 my @title = split( ',', $address );
272 my ( $sheet_name, $range ) = split( '!', $address );
273 $self->{PRINT_AREA}{$sheet_name} = $range;
274 }
275 else {
276 $self->{EWX}->define_name( $name, $address );
277 }
278 } $files->{workbook}->find_nodes('//definedNames/definedName');
279
280 # Sheets: Add a worksheet for each sheet in workbook
281 # Rename sheet if template(s) already has a sheet by that name
282 map {
283 my $name = $_->att('name');
284 my $test = $name;
285 for ( my $i = 1; ; $i++ ) {
286 last unless $self->{EWX}->get_worksheet_by_name($test);
287 $test = $name . "($i)";
288 }
289 my $sheet = $self->{EWX}->add_worksheet($test);
290
291 my $range = $self->{PRINT_AREA}{$name};
292 $sheet->print_area($range) if $range;
293
294 foreach my $range ( @{ $self->{PRINT_TITLES}{$name} } ) {
295
296 if ($range =~ m/[A-Za-z]/) { # Column Range like $A:$A
297 $sheet->repeat_columns( $range )
298 } else {
299 # Row Range like $1:$1
300 $range =~ s/\$//g;
301 my ($first, $last) = split('\:', $range);
302 $sheet->repeat_rows( --$first, --$last );
303 }
304 }
305
306 # Parse the contents of the sheet
307 my $idx = $_->att('r:id');
308 $self->_parse_sheet( $sheet, $files->{sheets}{$idx} );
309 } $files->{workbook}->find_nodes('//sheets/sheet');
310 $self->{ZIP}[$z] = undef;
311 }
312 }
313 ###############################################################################
314 sub template_callback {
315
316 =head2 template_callback
317
318 Place holder method for a callback routine to modify the content of the template
319 before being written to the output spreadsheet.
320
321 This callback is activated for all shared string (both plain and rich text
322 strings), and also for header/footer text.
323
324 The callback is supplied with the two parameters: The object name (since this is
325 a method), and the text to be processed. This is passed as a reference to single
326 scalar.
327
328 This method is called numerous times during processing (e.g. once for each
329 unique string in the spreadsheet, so the user is advised to keep it efficient.
330
331 This callback approach does not force any particular templating system on the
332 user. They are free to use whatever system they choose.
333
334 Note that templating can only do simple scalars. Complex templating (if-then-
335 else, loops, etc) do not make sense in that the callback is supplied with the
336 contents of a single cell. Having said that, remember that the full power of
337 Excel::Writer::XLSX is available to the user to modify the template after it is
338 processed.
339
340 # A snippet of code to replace [% template %] in the
341 # template spreadsheet with 'Output'
342
343 my ($self, $wbk) = Excel::Template::XLSX->new($output_xlsx, $template_xlsx);
344
345 use Template::Tiny;
346 my $template = Template::Tiny->new( TRIM => 1 );
347 $self->{template_callback} = sub {
348 my ($self, $textref) = @_;
349 $template->process($textref, { template => 'Output' }, $textref );
350 };
351
352 $self->parse_template();
353
354 =cut
355
356 my $self = shift;
357 my ($text) = @_;
358 }
359 ###############################################################################
360 sub _apply_tint {
361
362 =head2 _apply_tint
363
364 Applies tinting to a color object, if the tint attribute is encountered in
365 parsing.
366
367 =cut
368
369 my $self = shift;
370 my ( $color, $tint ) = @_;
371
372 my ( $r, $g, $b ) = map { oct("0x$_") } $color =~ /#(..)(..)(..)/;
373 my ( $h, $l, $s ) = rgb2hls( $r, $g, $b );
374
375 if ( $tint < 0 ) {
376 $l = $l * ( 1.0 + $tint );
377 }
378 else {
379 $l = $l * ( 1.0 - $tint ) + ( 1.0 - 1.0 * ( 1.0 - $tint ) );
380 }
381
382 return scalar hls2rgb( $h, $l, $s );
383 }
384 ###############################################################################
385 sub _base_path_for {
386
387 =head2 _base_path_for
388
389 Manipulates the path to a member in the zip file, to find the associated
390 rels file.
391
392 =cut
393
394 my $self = shift;
395 my ($file) = @_;
396
397 my @path = split '/', $file;
398 pop @path;
399
400 return join( '/', @path ) . '/';
401 }
402 ###############################################################################
403 sub _cell_to_row_col {
404
405 =head2 _cell_to_row_col
406
407 Converts an A1 style cell reference to a row and column index.
408
409 =cut
410
411 my $self = shift;
412 my $cell = shift;
413
414 my ( $col, $row ) = $cell =~ /([A-Z]+)([0-9]+)/;
415
416 my $ncol = 0;
417 for my $char ( split //, $col ) {
418 $ncol *= 26;
419 $ncol += ord($char) - ord('A') + 1;
420 }
421 $ncol = $ncol - 1;
422 my $nrow = $row - 1;
423 return ( $nrow, $ncol );
424 }
425 ###############################################################################
426 sub _color {
427
428 =head2 _color
429
430 Parses color element (rgb, index, theme, and tint)
431
432 =cut
433
434 my $self = shift;
435 my ( $color_node, $fill ) = @_;
436
437 my $themes = $self->{THEMES};
438 my $color;
439 if ( $color_node && !$color_node->att('auto') ) {
440 my $rgb = $color_node->att('rgb');
441 my $theme = $color_node->att('theme');
442 my $index = $color_node->att('indexed');
443 my $tint = $color_node->att('tint');
444
445 # see https://rt.cpan.org/Public/Bug/Display.html?id=93065 (still needed for XLSX??)
446 # defined $index and $color = ($fill && $index == 64) ? '#FFFFFF' : $index;
447 $rgb and $color = '#' . substr( $rgb, 2, 6 );
448 defined $theme and $color = '#' . $themes->{Color}[$theme];
449 $tint and $color = $self->_apply_tint( $color, $tint );
450 }
451 return $color;
452 }
453 ###############################################################################
454 sub _extract_files {
455
456 =head2 _extract_files
457
458 Called by parse_template to fetch the xml strings from the zip file. XML
459 strings are parsed, except for worksheets. Individual worksheets are
460 parsed separately.
461
462 =cut
463
464 my $self = shift;
465 my ($zip) = @_;
466
467 my $type_base
468 = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
469
470 my $rels = $self->_parse_xml( $zip, $self->_rels_for('') );
471
472 my $node = qq<//Relationship[\@Type="$type_base/officeDocument"]>;
473 my $wb_name = ( $rels->find_nodes($node) )[0]->att('Target');
474 my $wb_xml = $self->_parse_xml( $zip, $wb_name );
475
476 my $path_base = $self->_base_path_for($wb_name);
477 my $wb_rels = $self->_parse_xml( $zip, $self->_rels_for($wb_name) );
478
479 my $string_xpath = qq<//Relationship[\@Type="$type_base/sharedStrings"]>;
480 my ($strings_xml) = map {
481 $zip->memberNamed( $path_base . $_->att('Target') )->contents
482 } $wb_rels->find_nodes($string_xpath);
483
484 my $style_xpath = qq<//Relationship[\@Type="$type_base/styles"]>;
485 my $style_target
486 = ( $wb_rels->find_nodes($style_xpath) )[0]->att('Target');
487 my $styles_xml = $self->_parse_xml( $zip, $path_base . $style_target );
488
489 my %sheet_rels;
490 my $wks_xpath = qq<//Relationship[\@Type="$type_base/worksheet"]>;
491 my %worksheet_xml = map {
492
493 my $sheet_file = $path_base . $_->att('Target');
494 my $rels_file = $self->_rels_for($sheet_file);
495 my $sheet_rels = '';
496 if ( $zip->memberNamed($rels_file) ) {
497 $sheet_rels = $self->_parse_xml( $zip, $rels_file );
498 }
499
500 if ( my $contents = $zip->memberNamed($sheet_file)->contents ) {
501 ( $_->att('Id') => { 'xml' => $contents, 'rels' => $sheet_rels } );
502 }
503
504 } $wb_rels->find_nodes($wks_xpath);
505
506 my %themes_xml = map {
507 $_->att('Id') =>
508 $self->_parse_xml( $zip, $path_base . $_->att('Target') )
509 } $wb_rels->find_nodes(qq<//Relationship[\@Type="$type_base/theme"]>);
510
511 my $core_base
512 = 'http://schemas.openxmlformats.org/package/2006/relationships/metadata';
513 my $core_full = qq<//Relationship[\@Type="$core_base/core-properties"]>;
514 my $core_name = ( $rels->find_nodes($core_full) )[0]->att('Target');
515 my $core_xml = $self->_parse_xml( $zip, $core_name );
516
517 return {
518 workbook => $wb_xml,
519 styles => $styles_xml,
520 sheets => \%worksheet_xml,
521 themes => \%themes_xml,
522 core => $core_xml,
523 ( $strings_xml ? ( strings => $strings_xml ) : () ),
524 };
525 }
526 ###############################################################################
527 sub _parse_alignment {
528
529 =head2 _parse_alignment
530
531 Parses horizontal and vertical cell alignments in a sheet.
532
533 =cut
534
535 my $self = shift;
536 my ($node) = @_;
537
538 my %align_map = (
539 horizontal => 'align',
540 vertical => 'valign',
541 textRotation => 'rotation',
542 indent => 'indent',
543 wrapText => 'text_wrap',
544 shrinkToFit => 'shrink',
545 );
546 my %align = ();
547 if ( my $alignment = $node->first_child('alignment') ) {
548 map {
549 my $v = $alignment->att($_);
550 if ( defined $v ) {
551 $v = 'vcenter' if ( $_ eq 'vertical' ) and ( $v eq 'center' );
552 $align{ $align_map{$_} } = $v;
553 }
554 } keys %align_map;
555 }
556 return %align;
557 }
558 ###############################################################################
559 sub _parse_borders {
560
561 =head2 _parse_borders
562
563 Parses cell borders and diagonal borders.
564
565 =cut
566
567 my $self = shift;
568 my ($styles) = @_;
569
570 my $borders = [];
571 my %border_map = (
572 dashDot => 9,
573 dashDotDot => 11,
574 dashed => 3,
575 dotted => 4,
576 double => 6,
577 hair => 7,
578 medium => 2,
579 mediumDashDot => 10,
580 mediumDashDotDot => 12,
581 mediumDashed => 8,
582 none => 0,
583 slantDashDot => 13,
584 thick => 5,
585 thin => 1,
586 );
587 push @$borders, map {
588 my $border = $_;
589
590 # XXX specs say "begin" and "end" rather than "left" and "right",
591 # but... that's not what seems to be in the file itself (sigh)
592
593 my %colors = ();
594 map {
595 my $color
596 = $self->_color(
597 $border->first_child($_)->first_child('color') );
598 $colors{ $_ . '_color' } = $color if $color;
599 } qw(left right top bottom);
600
601 my %types = ();
602 map {
603 my $style = $border->first_child($_)->att('style');
604 $types{$_} = $border_map{$style} if $style;
605 } qw(left right top bottom);
606
607 my %diag = ();
608 my $down = $border->att('diagonalDown') // 0;
609 my $up = $border->att('diagonalUp') // 0;
610 $diag{'diag_type'} = 2 * $down + $up if $down + $up;
611 my $dborder = $border->first_child('diagonal')->att('style');
612 $diag{'diag_border'} = $border_map{$dborder} if $dborder;
613 my $dcolor = $border->first_child('diagonal')->first_child('color');
614 $diag{'diag_color'} = $self->_color($dcolor) if $dcolor;
615
616 my $border_ref = { %colors, %types, %diag };
617 } $styles->find_nodes('//borders/border');
618 return $borders;
619 }
620 ###############################################################################
621 sub _parse_fills {
622
623 =head2 _parse_fills
624
625 Parses styles for cell fills (pattern, foreground and background colors.
626 horizontal and horizontal and vertical cell alignments in a sheet.
627
628 Gradients are parsed, but since EWX does not support gradients, a
629 pattern is substituted.
630
631 =cut
632
633 my $self = shift;
634 my ($styles) = @_;
635 my %fill_map = (
636 darkDown => 7,
637 darkGray => 3,
638 darkGrid => 9,
639 darkHorizontal => 5,
640 darkTrellis => 10,
641 darkUp => 8,
642 darkVertical => 6,
643 gray0625 => 18,
644 gray125 => 17,
645 lightDown => 13,
646 lightGray => 4,
647 lightGrid => 15,
648 lightHorizontal => 11,
649 lightTrellis => 16,
650 lightUp => 14,
651 lightVertical => 12,
652 mediumGray => 2,
653 none => 0,
654 solid => 1,
655 );
656
657 # Pattern Fills / # Gradient Fills
658 # EWX does not support Gradient fills (yet??)
659 # so, substitute a pattern fill to keep indices aligned
660 my $fills = [];
661 push @$fills, map {
662 my ( $fill, @color );
663 my $pat = $_->first_child('patternFill');
664 if ($pat) {
665 for (qw[fg bg]) {
666 my $fgbg = $self->_color( $pat->first_child("${_}Color"), 1 );
667 push @color, ( "${_}_color", $fgbg ) if $fgbg;
668 }
669 $fill = { pattern => $fill_map{ $pat->att('patternType') }, @color };
670 }
671 my $gradient = $_->first_child('gradientFill');
672 if ($gradient) {
673 my @stop_colors = $gradient->find_nodes('stop/color');
674 my $fg = $self->_color( $stop_colors[0], 1 );
675 my $bg = $self->_color( $stop_colors[1], 1 );
676 my %hfg = ( 'fg_color' => ( $fg // 'white' ) );
677 my %hbg = ( 'bg_color' => ( $bg // 'black' ) );
678
679 ### ?? Create a lightGrid pattern in place of a gradient for now
680 $fill = { pattern => $fill_map{'lightGrid'}, %hfg, %hbg };
681 }
682 $fill;
683 } $styles->find_nodes('//fills/fill');
684 $fills;
685 }
686 ###############################################################################
687 sub _parse_fonts {
688
689 =head2 _parse_fonts
690
691 Parses font information (font name, size, super/sub scripts, alignment
692 colors, underline, bold, italic, and strikeout attributes).
693
694 =cut
695
696 my $self = shift;
697 my ( $styles, $xpath ) = @_;
698 $xpath //= '//fonts/font';
699
700 my $fonts = [];
701 @$fonts = map {
702
703 my $u = $_->first_child('u');
704 my $vert = $_->first_child('vertAlign');
705 my $font;
706
707 my $size = $_->first_child('sz')->att('val');
708 $font->{'size'} = $size if $size;
709
710 # XXX if color tag is missing is it black?? '#000000'
711 my $color = $_->first_child('color');
712 $font->{'color'} = $self->_color($color) if $color;
713
714 my $script_map = {
715 'superscript' => 1,
716 'subscript' => 2,
717 };
718
719 if ( defined $vert ) {
720 my $script = $vert->att('val');
721 $font->{'font_script'} = $script_map->{$script} if $script;
722 }
723
724 my $u_map = {
725 'single' => 1,
726 'double' => 2,
727 'singleAccounting' => 33,
728 'doubleAccounting' => 34,
729 };
730 if ( defined $u ) {
731
732 # XXX sometimes style xml files can contain just <u/> with no
733 # val attribute. i think this means single underline, but not sure
734 my $underline = $u->att('val') // 'single';
735 $font->{'underline'} = $u_map->{$underline} if $underline;
736 }
737
738 my $font_name = $_->first_child('name');
739 $font->{'font'} = $font_name->att('val') if $font_name;
740
741 # Alternate for rich strings (embedded font)
742 my $rFont = $_->first_child('rFont');
743 $font->{'font'} = $rFont->att('val') if $rFont;
744
745 my $bold = $_->first_child('b');
746 $font->{'bold'} = 1 if $bold;
747
748 my $italic = $_->first_child('i');
749 $font->{'italic'} = 1 if $italic;
750
751 my $strike = $_->first_child('strike');
752 $font->{'font_strikeout'} = 1 if $strike;
753
754 $font;
755 } $styles->find_nodes($xpath);
756 return $fonts;
757 }
758 ###############################################################################
759 sub _parse_numbers {
760
761 =head2 _parse_numbers
762
763 Parses styles for cell number formats (financial, decimal, exponential, date-time, ...)
764
765 =cut
766
767 my $self = shift;
768 my ($styles) = @_;
769 my $number_format = { 0 => {} };
770 map {
771 my $id = $_->att('numFmtId') // 0;
772
773 # defaults are from
774 #http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/thread/e27aaf16-b900-4654-8210-83c5774a179c
775 # Defaults do not need to be re-created.
776 my $code = $_->att('formatCode') // $id;
777 $number_format->{$id} = { num_format => $code } if $id;
778 } $styles->find_nodes('//numFmts/numFmt');
779 return $number_format;
780 }
781 ###############################################################################
782 sub _parse_protection {
783
784 =head2 _parse_protection
785
786 Parses locked and hidden attributes for a cell. These are only
787 useful if the worksheet is locked.
788
789 This module does not lock the workbook or the worksheet.
790
791 =cut
792
793 my $self = shift;
794 my ($node) = @_;
795 my @protection = qw(locked hidden);
796 my %prot = ();
797 if ( my $protection = $_->first_child('protection') ) {
798 map {
799 my $v = $protection->att($_);
800 $prot{$_} = $v if defined $v;
801 } @protection;
802 }
803 return %prot;
804 }
805 ###############################################################################
806 sub _parse_shared_strings {
807
808 =head2 _parse_shared_strings
809
810 Parses the shared strings file. Excel does not directly store
811 string values with the cell, but stores an index into the shared
812 strings table instead, to save memory, if a string value is
813 referenced more than once. Shared strings also contain
814 formatting if multiple formats are applied within a cell (See
815 write_rich_string in EWX.
816
817 =cut
818
819 my $self = shift;
820 my ($strings) = @_;
821
822 return unless $strings;
823 my $xml = XML::Twig->new(
824 twig_handlers => {
825 'si' => sub {
826 my ( $twig, $si ) = @_;
827
828 my $callback = $self->{template_callback};
829 my $call = ref($callback) eq 'CODE';
830
831 # plain text strings
832 my $t = $si->first_child('t');
833 if ($t) {
834 my $text = $t->text();
835 $call and $self->$callback( \$text );
836 push @{ $self->{SHARED_STRINGS} }, $text;
837 }
838
839 # rich text strings; String item (si) with multiple
840 # text elements, with optional formatting
841 my $rich = [];
842 for my $r ( $si->find_nodes('r') ) {
843 my $text = $r->first_child('t')->text();
844 $call and $self->$callback( \$text );
845 my $rPr = $r->first_child('rPr');
846
847 if ($rPr) {
848 my $xml = $r->first_child('rPr')->outer_xml();
849 my $twig = XML::Twig->parse($xml);
850 my $fonts = $self->_parse_fonts( $twig, '//rPr' );
851 my $format = $self->{EWX}->add_format( %{ $fonts->[0] } );
852 push @$rich, $format, $text;
853 }
854 else {
855 push @$rich, $text;
856 }
857 }
858 push( @{ $self->{SHARED_STRINGS} }, $rich ) if scalar(@$rich);
859 $twig->purge;
860 }
861 }
862 ); # } twig_handlers ) new
863 $xml->parse($strings);
864 }
865 ###############################################################################
866 sub _parse_sheet {
867
868 =head2 _parse_sheet
869
870 Parses an individual worksheet. This is done in two passes.
871 See _parse_sheet_pass1 and _parse_sheet_pass2 for what elements are
872 parsed. This is necessary because the parse order of XML::Twig callbacks
873 are in the wrong order for some sheet information (header/footer information,
874 hyperlinks, and merged cells).
875
876 =cut
877
878 my $self = shift;
879 my ( $sheet, $sheet_file ) = @_;
880
881 # Hyperlinks are local to each sheet
882 $self->{HYPERLINKS} = {};
883 my $pass1
884 = XML::Twig->new( twig_roots => $self->_parse_sheet_pass1($sheet) );
885 $pass1->parse( $sheet_file->{xml} );
886
887 # Half time show - track down the URLs for hyperlinks found in pass 1
888 while ( my ( $a1, $rid ) = each %{ $self->{HYPERLINKS} } ) {
889 my $xpath = qq<//Relationship[\@Id="$rid"]>;
890 my $url = ( $sheet_file->{rels}->find_nodes($xpath) )[0];
891 if ($url) {
892 my $target = $url->att('Target');
893 my $mode = lc( $url->att('TargetMode') );
894 $self->{HYPERLINKS}{$a1} = "$target";
895 }
896 }
897
898 # 2nd pass: cell/row building is dependent on having parsed the merge definitions
899 # beforehand. Also header/footer margins must be parsed before setting header/footer
900 my $pass2
901 = XML::Twig->new( twig_roots => $self->_parse_sheet_pass2($sheet) );
902 $pass2->parse( $sheet_file->{xml} );
903 }
904 ###############################################################################
905 sub _parse_sheet_pass1 {
906
907 =head2 _parse_sheet_pass1
908
909 Parses some elements in a worksheet ( pageMargins, headerFooter,
910 hyperlinks, pageSetup, Merged Cells, Sheet Formatting Row and Column
911 heights, Sheet selection, and Tab Color)
912
913 =cut
914
915 my $self = shift;
916 my ($sheet) = @_;
917
918 my $default_row_height = 15;
919 my $default_column_width = 10;
920 my %hf_margin;
921
922 return {
923 'pageMargins' => sub {
924 my ( $twig, $margin ) = @_;
925 map {
926 my $method = "set_margin_" . $_;
927 $sheet->$method( $margin->att($_) // 0 );
928 } qw( left right top bottom );
929
930 # Capture header/footer margin, for use with headerFooter callback
931 $hf_margin{Header} = $margin->att('header');
932 $hf_margin{Footer} = $margin->att('footer');
933 $twig->purge;
934 },
935
936 # Headers/Footers
937 'headerFooter' => sub {
938 my ( $twig, $hf ) = @_;
939
940 my $callback = $self->{template_callback};
941 my $call = ref($callback) eq 'CODE';
942 for (qw[Header Footer]) {
943 my $child = $hf->first_child( 'odd' . $_ );
944 my $text = $child ? $child->text() : '';
945 $call and $self->$callback( \$text );
946 my $method = 'set_' . lc($_);
947 $sheet->$method( $text, $hf_margin{$_} );
948 }
949
950 $twig->purge;
951 },
952
953 # Hyperlinks
954 'hyperlinks/hyperlink ' => sub {
955 my ( $twig, $link ) = @_;
956 my $a1 = $link->att('ref');
957 $self->{HYPERLINKS}{$a1} = $link->att('r:id');
958 $twig->purge;
959 },
960
961 # Paper/page setup
962 'pageSetup' => sub {
963 my ( $twig, $setup ) = @_;
964 my %lookup = (
965 orientation => => 'set_portrait',
966 firstPageNumber => 'set_start_page',
967 scale => 'set_print_scale',
968 paperSize => 'set_paper'
969
970 # horizontalDpi ??
971 # verticalDpi
972 );
973
974 my @page
975 = qw(scale orientation horizontalDpi verticalDpi paperSize firstPageNumber scale);
976 foreach (@page) {
977
978 # Ignore if we do not have a EWX method for this attribute
979 my $method = $lookup{$_} // next;
980
981 # Ignore if no value defined for this attribute
982 next unless my $set = $setup->att($_);
983
984 # Special case; no generic method to set portrait/landscape
985 $method = 'set_landscape' if $set eq 'landscape';
986 $sheet->$method($set);
987 }
988
989 $twig->purge;
990 },
991
992 # Merged cells (Create the ranges: content will be added later)
993 'mergeCells/mergeCell' => sub {
994 my ( $twig, $merge_area ) = @_;
995
996 if ( my $ref = $merge_area->att('ref') ) {
997 my ( $topleft, $bottomright ) = $ref =~ /([^:]+):([^:]+)/;
998 # my ( $tr, $lc ) = $self->_cell_to_row_col($topleft);
999 # my ( $br, $rc ) = $self->_cell_to_row_col($bottomright);
1000
1001 # Merged Ranges/Areas: save the address for pass 2.
1002 # cells within the merged range will be processed with
1003 # merge_range_type(), instead of write()
1004 $self->{MERGED_RANGES}{$sheet->{_index}}{$topleft} = $ref;
1005 }
1006 $twig->purge;
1007 },
1008
1009 # Default row height
1010 'sheetFormatPr' => sub {
1011 my ( $twig, $format ) = @_;
1012 $default_row_height //= $format->att('defaultRowHeight');
1013 $default_column_width //= $format->att('baseColWidth');
1014 $sheet->set_default_row($default_row_height);
1015 $twig->purge;
1016 },
1017
1018 'col' => sub {
1019 my ( $twig, $col ) = @_;
1020
1021 for my $ci ( $col->att('min') .. $col->att('max') ) {
1022 #set_column($first,$last,$width,$fmt,$hide,$level,$collapsed )
1023 $sheet->set_column( $ci - 1, $ci - 1, $col->att('width') );
1024 #?? just sets width, not $col->att('style')
1025 }
1026 $twig->purge;
1027 },
1028
1029 'row' => sub {
1030 my ( $twig, $row ) = @_;
1031
1032 # ?? just sets row height. No formatting yet
1033 # set_row( $row, $height, $format, $hidden, $level, $collapsed )
1034 $sheet->set_row( $row->att('r') - 1, $row->att('ht') );
1035 $twig->purge;
1036 },
1037
1038 'sheetView/selection' => sub {
1039 my ( $twig, $selection ) = @_;
1040 my $range = $selection->att('sqref')
1041 // $selection->att('activeCell') // 'A1';
1042 $sheet->set_selection($range);
1043 $twig->purge;
1044 },
1045
1046 'sheetPr/tabColor' => sub {
1047 my ( $twig, $tab_color ) = @_;
1048 $sheet->set_tab_color( $tab_color->att('rgb') );
1049 $twig->purge;
1050 }
1051
1052 } # return hashref
1053 }
1054 ###############################################################################
1055 sub _parse_sheet_pass2 {
1056
1057 =head2 _parse_sheet_pass2
1058
1059 Parses cell contents (first by row, then by column). Cells can contain
1060 inline strings, string references, direct string values, formulas,
1061 and hyperlinks. Each cell may also contain formatting information.
1062 The format is in an index to formatting for borders, shading, alignment,
1063 font, and number formats.
1064
1065 =cut
1066
1067 my $self = shift;
1068 my ($sheet) = @_;
1069
1070 return {
1071 'sheetData/row' => sub {
1072 my ( $twig, $row_elt ) = @_;
1073 my $sheet_idx = $sheet->{_index};
1074 for my $cell ( $row_elt->children('c') ) {
1075 my $string_index = 0;
1076 my $a1 = $cell->att('r'); # Cell Address
1077 my $t = $cell->att('t') || 'n'; # Cell Type
1078 my $s = $cell->att('s'); # Cell String Index
1079 my $val_xml
1080 = $t eq 'inlineStr'
1081 ? $cell->first_child('is')->first_child('t')
1082 : $cell->first_child('v');
1083 my $val = $val_xml ? $val_xml->text() : undef;
1084
1085 my $format_idx = $s // 0;
1086 my $format = $self->{FORMATS}[$format_idx];
1087
1088 # Formatted cell, no contents
1089 if ( !defined($val) ) {
1090 $sheet->write_blank($a1, $val, $format);
1091 next;
1092 }
1093
1094 if ( $t eq 's' ) {
1095 $string_index = $val;
1096 $val = $self->{SHARED_STRINGS}[$val];
1097
1098 my $is_array = ref($val) eq 'ARRAY';
1099 my @aval = $is_array ? @$val : ($val);
1100 if ( my $ref = $self->{MERGED_RANGES}{$sheet_idx}{$a1} ) {
1101 my $type = $is_array ? 'rich_string' : 'string';
1102 $sheet->merge_range_type($type, $ref, @aval, $format );
1103 next;
1104 }
1105
1106 # Special case for multiple formats in a cell
1107 # see _parse_shared_strings for rPr nodes
1108 if ( $is_array ) {
1109 $sheet->write_rich_string( $a1, @aval );
1110 next;
1111 }
1112 if ( my $url = $self->{HYPERLINKS}{$a1} ) {
1113 $sheet->write_url( $a1, $url, $format, $val );
1114 next;
1115 }
1116 $sheet->write_string( $a1, $val, $format );
1117 next;
1118 }
1119
1120 # In-line string (not seen in practice)
1121 elsif ( $t eq 'str' ) {
1122 $val = '=' . $cell->first_child('f')->text();
1123 }
1124
1125 # Formulas
1126 elsif ( $t eq 'n' ) {
1127 if ( my $form_child = $cell->first_child('f') ) {
1128 my $is_array = $form_child->att('t');
1129 my $ref = $form_child->att('ref');
1130 my $formula = $form_child->text() // q[="No Formula Found"];
1131 if ($is_array and $ref) {
1132 $sheet->write_array_formula( $ref, "=${formula}", $format, $val );
1133 }
1134 else {
1135 #if ( my $ref = $self->{MERGED_RANGES}{$a1} ) {
1136 if ( my $ref = $self->{MERGED_RANGES}{$sheet_idx}{$a1} ) {
1137 $sheet->merge_range_type('formula', $ref, "=${formula}", $format, $val);
1138 } else {
1139 $sheet->write_formula( $a1, "=${formula}", $format,
1140 $val );
1141 }
1142 }
1143 next;
1144 }
1145
1146 }
1147 elsif ( $t eq 'b' ) {
1148 $val = $val ? "TRUE" : "FALSE";
1149 }
1150 elsif ( $t eq 'e' ) {
1151 }
1152 elsif ( $t eq 'str' || $t eq 'inlineStr' ) {
1153 }
1154 else {
1155 warn "unimplemented type $t found in cell $a1"; # XXX
1156 }
1157
1158 $sheet->write( $a1, $val, $format );
1159 }
1160
1161 $twig->purge;
1162 }
1163 };
1164 }
1165 ###############################################################################
1166 sub _parse_styles {
1167
1168 =head2 _parse_styles
1169
1170 Parses style information.
1171 Parses number formats directly. Calls subroutines to parse
1172 fonts, fills, and borders, alignment, and protection.
1173
1174 Finally, parses Cell Xfs elements to Combine fonts, borders, number formats,
1175 alignment, patterns, into a single format specification.
1176
1177 Calls EWX add_formats to create a format, and stores the format information
1178 in a FORMAT array within the object.
1179
1180 =cut
1181
1182 my $self = shift;
1183 my ($styles) = @_;
1184
1185 # Number Formats
1186 my $numfmt = $self->_parse_numbers($styles);
1187
1188 # Fonts / Fills / Borders
1189 my $fonts = $self->_parse_fonts( $styles, '//fonts/font' );
1190 my $fills = $self->_parse_fills($styles);
1191 my $borders = $self->_parse_borders($styles);
1192
1193 # Cell Xfs
1194 # Combine fonts, borders, number formats, alignment, patterns, into a single format spec
1195 map {
1196 # Also has applyAlignment property, which we do not examine
1197 # same for ApplyFont, ApplyBorder ApplyProtection
1198
1199 my %halign = $self->_parse_alignment($_);
1200 my %hprot = $self->_parse_protection($_);
1201 my %hfont = %{ $fonts->[ $_->att('fontId') // 0 ] };
1202
1203 my $numFmtId = $_->att('numFmtId') // 0;
1204
1205 # Use custom format, or built-in if custom not found
1206 my $ref = $numfmt->{$numFmtId} // { num_format => $numFmtId };
1207 my %hnumfmt = %$ref;
1208
1209 my %hbord = %{ $borders->[ $_->att('borderId') // 0 ] };
1210 my %hfill = %{ $fills->[ $_->att('fillId') // 0 ] };
1211
1212 my $fmt
1213 = $self->{EWX}
1214 ->add_format( %hfont, %hnumfmt, %hbord, %halign, %hprot, %hfill );
1215 push @{ $self->{FORMATS} }, $fmt;
1216 } $styles->find_nodes('//cellXfs/xf');
1217 }
1218 ###############################################################################
1219 sub _parse_themes {
1220
1221 =head2 _parse_themes
1222
1223 Parses theme information. Some color settings are referenced by an
1224 index to the theme.
1225
1226 =cut
1227
1228 my $self = shift;
1229 my ($themes) = @_;
1230
1231 return {} unless $themes;
1232
1233 my @color
1234 = map { $_->name eq 'a:sysClr' ? $_->att('lastClr') : $_->att('val') }
1235 $themes->find_nodes('//a:clrScheme/*/*');
1236
1237 # this shouldn't be necessary, but the documentation is wrong here
1238 # see http://stackoverflow.com/questions/2760976/theme-confusion-in-spreadsheetml
1239 ( $color[0], $color[1] ) = ( $color[1], $color[0] );
1240 ( $color[2], $color[3] ) = ( $color[3], $color[2] );
1241
1242 return { Color => \@color };
1243 }
1244 ###############################################################################
1245 sub _parse_xml {
1246
1247 =head2 _parse_xml
1248
1249 Low level subroutine to parse an entire member of a zip file. Used
1250 for small files, such as xxx.xml.rels, where the entire file is parsed.
1251
1252 For larger files, XML::Twig::twig_handlers are used.
1253
1254 =cut
1255
1256 my $self = shift;
1257 my ( $zip, $subfile ) = @_;
1258
1259 my $member = $zip->memberNamed($subfile);
1260 die "no subfile named $subfile" unless $member;
1261
1262 my $xml = XML::Twig->new;
1263 $xml->parse( scalar $member->contents );
1264 return $xml;
1265 }
1266 ###############################################################################
1267 sub _rels_for {
1268
1269 =head2 _rels_for
1270
1271 Returns the .rels file name for a sibling workbook or worksheet.
1272
1273 =cut
1274
1275 my $self = shift;
1276 my ($file) = @_;
1277
1278 my @path = split '/', $file;
1279 my $name = pop @path;
1280 $name = '' unless defined $name;
1281 push @path, '_rels';
1282 push @path, "$name.rels";
1283
1284 return join '/', @path;
1285 }
1286 ###############################################################################
1287 sub zzpodtail {
1288
1289 =for pod
1290
1291 =head2 zzpodtail
1292
1293 Dummy subroutine to allow me to hide pod documentation when using code
1294 folding in the editor.
1295
1296 =head1 INSTALLATION
1297
1298 Install with CPAN
1299
1300 cpan Excel::Template::XLSX
1301
1302 or, use the standard Unix style installation.
1303
1304 Unzip and untar the module as follows:
1305
1306 tar -zxvf Excel::Template::XLSX-nnn.tar.gz
1307
1308 The module can be installed using the standard Perl procedure:
1309
1310 perl Makefile.PL
1311 make
1312 make test
1313 make install # As sudo/root
1314
1315 =head1 BUGS
1316
1317 =over 4
1318
1319 =item Large spreadsheets may cause segfaults on perl 5.14 and earlier
1320
1321 This module internally uses XML::Twig, which makes it potentially subject to
1322 L<Bug #71636 for XML-Twig: Segfault with medium-sized document|https://rt.cpan.org/Public/Bug/Display.html?id=71636>
1323 on perl versions 5.14 and below (the underlying bug with perl weak references
1324 was fixed in perl 5.15.5). The larger and more complex the spreadsheet, the
1325 more likely to be affected, but the actual size at which it segfaults is
1326 platform dependent. On a 64-bit perl with 7.6gb memory, it was seen on
1327 spreadsheets about 300mb and above. You can work around this adding
1328 C<XML::Twig::_set_weakrefs(0)> to your code before parsing the spreadsheet,
1329 although this may have other consequences such as memory leaks.
1330
1331 Please report any bugs to GitHub Issues at
1332 L<https://github.com/davidsclarke/Excel-Template-XLSX/issues>.
1333
1334 =back
1335
1336 =head1 SUPPORT
1337
1338 You can find this documentation for this module with the perldoc command.
1339
1340 perldoc Excel::Template::XLSX
1341
1342 You can also look for information at:
1343
1344 =over 4
1345
1346 =item * MetaCPAN
1347
1348 L<https://metacpan.org/release/Excel-Template-XLSX>
1349
1350 =item * RT: CPAN's request tracker
1351
1352 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Excel-Template-XLSX>
1353
1354 =item * Github
1355
1356 L<https://github.com/davidsclarke/Excel-Template-XLSX>
1357
1358 =item * CPAN Ratings
1359
1360 L<http://cpanratings.perl.org/d/Excel-Template-XLSX>
1361
1362 =back
1363
1364 =head1 DEBUGGING TIPS
1365
1366 Using the Perl debugger gets complicated because of XML::Twig. The objects
1367 created by XML::Twig are HUGE. Also, stepping through the code often results
1368 in exceeding a stack depth of >100. The author found it helpful to take
1369 advantage of the simplify() method in XML::Twig when using the debugger 'x'
1370 command to examine variables.
1371
1372 x $node->simplify()
1373
1374 Also, it is helpful to use the 'c' command to jump over XML::Twig subroutine calls and callbacks.
1375
1376 =head1 BUGS
1377
1378 Please report any bugs or feature requests to the author.
1379
1380 =head1 TO DO
1381
1382 Worksheet Activation
1383 Table Formatting/Styles
1384 Calculation Mode
1385
1386 =head1 REPOSITORY
1387
1388 The Excel::Template::XLSX source code is hosted on github:
1389 L<http://github.com/davidsclarke/Excel-Template-xlsx>.
1390
1391 =head1 SEE ALSO
1392
1393 Excel::Writer::XLSX
1394
1395 This module does not provide much documentation on the capabilites of methods
1396 for creating Excel content. The documentation provided with EWX is excellent,
1397 and also has numerous examples included.
1398
1399 Spreadsheet::ParseXLSX
1400
1401 Although this module does not use Spreadsheet::ParseXLSX, the parsing and
1402 comments regarding issues involved with parsing spreadsheets came from this module.
1403
1404 XML::Twig and Archive::Zip
1405
1406 Excel .xlsx files are zippped .xml files. These two modules are used to
1407 unzip the .xlsx file, extract the members, and parse the relative portions
1408 of the .xml files inside.
1409
1410 =head1 ACKNOWLEDGEMENTS
1411
1412 This module leverages the methods in L<Excel::Writer::XLSX|https://metacpan.org/release/Excel-Writer-XLSX>, maintained by L<John McNamara|https://metacpan.org/author/JMCNAMARA>
1413 to recreate the template.
1414
1415 The parser was developed using L<Spreadsheet::ParseXLSX|https://metacpan.org/release/Spreadsheet-ParseXLSX> as a starting point, maintained by L<Jesse Luehrs|https://metacpan.org/author/DOY>.
1416 This parser calls methods in EWX directly when a token is resolved rather than building
1417 up an object representing the parsed content.
1418
1419 =head1 LICENSE AND COPYRIGHT
1420
1421 Either the Perl Artistic Licence L<http://dev.perl.org/licenses/artistic.html>
1422 or the GPL L<http://www.opensource.org/licenses/gpl-license.php>.
1423
1424 AUTHOR
1425
1426 David Clarke dclarke@cpan.org
1427
1428 =cut
1429
1430 }
1431
« build.pl ↑ Summary