File Coverage

blib/lib/Excel/Writer/XLSX/Package/Custom.pm
Criterion Covered Total %
statement 70 70 100.0
branch 10 10 100.0
condition n/a
subroutine 15 15 100.0
pod 0 1 0.0
total 95 96 98.9


line stmt bran cond sub pod time code
1              
2             ###############################################################################
3             #
4             # Custom - A class for writing the Excel XLSX custom.xml file for custom
5             # workbook properties.
6             #
7             # Used in conjunction with Excel::Writer::XLSX
8             #
9             # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
10             #
11             # Documentation after __END__
12             #
13              
14             # perltidy with the following options: -mbl=2 -pt=0 -nola
15              
16             use 5.008002;
17 1124     1124   16004 use strict;
  1124         3181  
18 1124     1124   4973 use warnings;
  1124         1959  
  1124         19988  
19 1124     1124   4624 use Carp;
  1124         2111  
  1124         24917  
20 1124     1124   5358 use Excel::Writer::XLSX::Package::XMLwriter;
  1124         2376  
  1124         52069  
21 1124     1124   6530  
  1124         2290  
  1124         565449  
22             our @ISA = qw(Excel::Writer::XLSX::Package::XMLwriter);
23             our $VERSION = '1.09';
24              
25              
26             ###############################################################################
27             #
28             # Public and private API methods.
29             #
30             ###############################################################################
31              
32              
33             ###############################################################################
34             #
35             # new()
36             #
37             # Constructor.
38             #
39              
40             my $class = shift;
41             my $fh = shift;
42 892     892 0 2052 my $self = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );
43 892         1870  
44 892         2880 $self->{_properties} = [];
45             $self->{_pid} = 1;
46 892         2716  
47 892         2240 bless $self, $class;
48              
49 892         1995 return $self;
50             }
51 892         2094  
52              
53             ###############################################################################
54             #
55             # _assemble_xml_file()
56             #
57             # Assemble and write the XML file.
58             #
59              
60             my $self = shift;
61              
62             $self->xml_declaration;
63 4     4   8  
64             $self->_write_properties();
65 4         27  
66             $self->xml_end_tag( 'Properties' );
67 4         17  
68             # Close the XML writer filehandle.
69 4         30 $self->xml_get_fh()->close();
70             }
71              
72 4         67  
73             ###############################################################################
74             #
75             # _set_properties()
76             #
77             # Set the document properties.
78             #
79              
80             my $self = shift;
81             my $properties = shift;
82              
83             $self->{_properties} = $properties;
84 4     4   9 }
85 4         8  
86              
87 4         20 ###############################################################################
88             #
89             # Internal methods.
90             #
91             ###############################################################################
92              
93              
94             ###############################################################################
95             #
96             # XML writing methods.
97             #
98             ###############################################################################
99              
100              
101             ###############################################################################
102             #
103             # _write_properties()
104             #
105             # Write the <Properties> element.
106             #
107              
108             my $self = shift;
109             my $schema = 'http://schemas.openxmlformats.org/officeDocument/2006/';
110             my $xmlns = $schema . 'custom-properties';
111             my $xmlns_vt = $schema . 'docPropsVTypes';
112              
113 4     4   6 my @attributes = (
114 4         7 'xmlns' => $xmlns,
115 4         11 'xmlns:vt' => $xmlns_vt,
116 4         10 );
117              
118 4         11 $self->xml_start_tag( 'Properties', @attributes );
119              
120             for my $property ( @{ $self->{_properties} } ) {
121              
122             # Write the property element.
123 4         18 $self->_write_property( $property );
124             }
125 4         8 }
  4         13  
126              
127             ##############################################################################
128 18         37 #
129             # _write_property()
130             #
131             # Write the <property> element.
132             #
133              
134             my $self = shift;
135             my $property = shift;
136             my $fmtid = '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}';
137              
138             $self->{_pid}++;
139              
140 18     18   26 my ( $name, $value, $type ) = @$property;
141 18         22  
142 18         23  
143             my @attributes = (
144 18         34 'fmtid' => $fmtid,
145             'pid' => $self->{_pid},
146 18         92 'name' => $name,
147             );
148              
149             $self->xml_start_tag( 'property', @attributes );
150              
151             if ( $type eq 'date' ) {
152 18         40  
153             # Write the vt:filetime element.
154             $self->_write_vt_filetime( $value );
155 18         56 }
156             elsif ( $type eq 'number' ) {
157 18 100       74  
    100          
    100          
    100          
158             # Write the vt:r8 element.
159             $self->_write_vt_r8( $value );
160 2         5 }
161             elsif ( $type eq 'number_int' ) {
162              
163             # Write the vt:i4 element.
164             $self->_write_vt_i4( $value );
165 4         10 }
166             elsif ( $type eq 'bool' ) {
167              
168             # Write the vt:bool element.
169             $self->_write_vt_bool( $value );
170 2         5 }
171             else {
172              
173             # Write the vt:lpwstr element.
174             $self->_write_vt_lpwstr( $value );
175 4         8 }
176              
177              
178             $self->xml_end_tag( 'property' );
179             }
180 6         47  
181              
182             ##############################################################################
183             #
184 18         45 # _write_vt_lpwstr()
185             #
186             # Write the <vt:lpwstr> element.
187             #
188              
189             my $self = shift;
190             my $data = shift;
191              
192             $self->xml_data_element( 'vt:lpwstr', $data );
193             }
194              
195              
196 6     6   16 ##############################################################################
197 6         9 #
198             # _write_vt_i4()
199 6         24 #
200             # Write the <vt:i4> element.
201             #
202              
203             my $self = shift;
204             my $data = shift;
205              
206             $self->xml_data_element( 'vt:i4', $data );
207             }
208              
209              
210             ##############################################################################
211 2     2   4 #
212 2         4 # _write_vt_r8()
213             #
214 2         5 # Write the <vt:r8> element.
215             #
216              
217             my $self = shift;
218             my $data = shift;
219              
220             $self->xml_data_element( 'vt:r8', $data );
221             }
222              
223              
224             ##############################################################################
225             #
226 4     4   6 # _write_vt_bool()
227 4         6 #
228             # Write the <vt:bool> element.
229 4         13 #
230              
231             my $self = shift;
232             my $data = shift;
233              
234             if ( $data ) {
235             $data = 'true';
236             }
237             else {
238             $data = 'false';
239             }
240              
241 4     4   5 $self->xml_data_element( 'vt:bool', $data );
242 4         6 }
243              
244 4 100       8 ##############################################################################
245 2         4 #
246             # _write_vt_filetime()
247             #
248 2         4 # Write the <vt:filetime> element.
249             #
250              
251 4         8 my $self = shift;
252             my $data = shift;
253              
254             $self->xml_data_element( 'vt:filetime', $data );
255             }
256              
257              
258             1;
259              
260              
261              
262 2     2   4 =pod
263 2         3  
264             =head1 NAME
265 2         6  
266             Custom - A class for writing the Excel XLSX custom.xml file.
267              
268             =head1 SYNOPSIS
269              
270             See the documentation for L<Excel::Writer::XLSX>.
271              
272             =head1 DESCRIPTION
273              
274             This module is used in conjunction with L<Excel::Writer::XLSX>.
275              
276             =head1 AUTHOR
277              
278             John McNamara jmcnamara@cpan.org
279              
280             =head1 COPYRIGHT
281              
282             (c) MM-MMXXI, John McNamara.
283              
284             All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
285              
286             =head1 LICENSE
287              
288             Either the Perl Artistic Licence L<http://dev.perl.org/licenses/artistic.html> or the GPL L<http://www.opensource.org/licenses/gpl-license.php>.
289              
290             =head1 DISCLAIMER OF WARRANTY
291              
292             See the documentation for L<Excel::Writer::XLSX>.
293              
294             =cut