File Coverage

blib/lib/Excel/Writer/XLSX/Package/Comments.pm
Criterion Covered Total %
statement 119 119 100.0
branch 6 8 75.0
condition 5 6 83.3
subroutine 21 21 100.0
pod 0 1 0.0
total 151 155 97.4


line stmt bran cond sub pod time code
1             package Excel::Writer::XLSX::Package::Comments;
2              
3             ###############################################################################
4             #
5             # Comments - A class for writing the Excel XLSX Comments files.
6             #
7             # Used in conjunction with Excel::Writer::XLSX
8             #
9             # Copyright 2000-2019, 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 1040     1040   17929 use 5.008002;
  1040         4948  
17 1040     1040   8321 use strict;
  1040         3585  
  1040         22103  
18 1040     1040   7711 use warnings;
  1040         3521  
  1040         25078  
19 1040     1040   6522 use Carp;
  1040         5062  
  1040         59705  
20 1040     1040   8638 use Excel::Writer::XLSX::Package::XMLwriter;
  1040         2637  
  1040         47444  
21 1040     1040   8068 use Excel::Writer::XLSX::Utility qw(xl_rowcol_to_cell);
  1040         2463  
  1040         1106346  
22              
23              
24             our @ISA = qw(Excel::Writer::XLSX::Package::XMLwriter);
25             our $VERSION = '1.03';
26              
27              
28             ###############################################################################
29             #
30             # Public and private API methods.
31             #
32             ###############################################################################
33              
34              
35             ###############################################################################
36             #
37             # new()
38             #
39             # Constructor.
40             #
41             sub new {
42              
43 47     47 0 6014 my $class = shift;
44 47         95 my $fh = shift;
45 47         347 my $self = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );
46              
47 47         172 $self->{_author_ids} = {};
48              
49 47         117 bless $self, $class;
50              
51 47         133 return $self;
52             }
53              
54              
55             ###############################################################################
56             #
57             # _assemble_xml_file()
58             #
59             # Assemble and write the XML file.
60             #
61             sub _assemble_xml_file {
62              
63 41     41   137 my $self = shift;
64 41         80 my $comments_data = shift;
65              
66 41         243 $self->xml_declaration;
67              
68             # Write the comments element.
69 41         261 $self->_write_comments();
70              
71             # Write the authors element.
72 41         247 $self->_write_authors( $comments_data );
73              
74             # Write the commentList element.
75 41         193 $self->_write_comment_list( $comments_data );
76              
77 41         159 $self->xml_end_tag( 'comments' );
78              
79             # Close the XML writer filehandle.
80 41         245 $self->xml_get_fh()->close();
81             }
82              
83              
84             ###############################################################################
85             #
86             # Internal methods.
87             #
88             ###############################################################################
89              
90              
91             ###############################################################################
92             #
93             # XML writing methods.
94             #
95             ###############################################################################
96              
97              
98             ##############################################################################
99             #
100             # _write_comments()
101             #
102             # Write the element.
103             #
104             sub _write_comments {
105              
106 41     41   106 my $self = shift;
107 41         110 my $xmlns = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';
108              
109 41         146 my @attributes = ( 'xmlns' => $xmlns );
110              
111 41         236 $self->xml_start_tag( 'comments', @attributes );
112             }
113              
114              
115             ##############################################################################
116             #
117             # _write_authors()
118             #
119             # Write the element.
120             #
121             sub _write_authors {
122              
123 41     41   105 my $self = shift;
124 41         100 my $comment_data = shift;
125 41         88 my $author_count = 0;
126              
127 41         172 $self->xml_start_tag( 'authors' );
128              
129 41         175 for my $comment ( @$comment_data ) {
130 4155         8273 my $author = $comment->[3];
131              
132 4155 100 66     12606 if ( defined $author && !exists $self->{_author_ids}->{$author} ) {
133              
134             # Store the author id.
135 42         174 $self->{_author_ids}->{$author} = $author_count++;
136              
137             # Write the author element.
138 42         170 $self->_write_author( $author );
139             }
140             }
141              
142 41         272 $self->xml_end_tag( 'authors' );
143             }
144              
145              
146             ##############################################################################
147             #
148             # _write_author()
149             #
150             # Write the element.
151             #
152             sub _write_author {
153              
154 42     42   90 my $self = shift;
155 42         89 my $data = shift;
156              
157 42         308 $self->xml_data_element( 'author', $data );
158             }
159              
160              
161             ##############################################################################
162             #
163             # _write_comment_list()
164             #
165             # Write the element.
166             #
167             sub _write_comment_list {
168              
169 41     41   96 my $self = shift;
170 41         79 my $comment_data = shift;
171              
172 41         211 $self->xml_start_tag( 'commentList' );
173              
174 41         164 for my $comment ( @$comment_data ) {
175 4155         12253 my $row = $comment->[0];
176 4155         6337 my $col = $comment->[1];
177 4155         6914 my $text = $comment->[2];
178 4155         6650 my $author = $comment->[3];
179 4155         6667 my $font_name = $comment->[6];
180 4155         6385 my $font_size = $comment->[7];
181 4155         6127 my $font_family = $comment->[8];
182              
183             # Look up the author id.
184 4155         6009 my $author_id = undef;
185 4155 50       9401 $author_id = $self->{_author_ids}->{$author} if defined $author;
186              
187             # Write the comment element.
188 4155         8288 my $font = [ $font_name, $font_size, $font_family ];
189 4155         8475 $self->_write_comment( $row, $col, $text, $author_id, $font );
190             }
191              
192 41         194 $self->xml_end_tag( 'commentList' );
193             }
194              
195              
196             ##############################################################################
197             #
198             # _write_comment()
199             #
200             # Write the element.
201             #
202             sub _write_comment {
203              
204 4155     4155   6177 my $self = shift;
205 4155         6328 my $row = shift;
206 4155         5706 my $col = shift;
207 4155         5965 my $text = shift;
208 4155         5720 my $author_id = shift;
209 4155         9477 my $ref = xl_rowcol_to_cell( $row, $col );
210 4155         6771 my $font = shift;
211              
212              
213 4155         8716 my @attributes = ( 'ref' => $ref );
214              
215 4155 50       9253 push @attributes, ( 'authorId' => $author_id ) if defined $author_id;
216              
217              
218 4155         11543 $self->xml_start_tag( 'comment', @attributes );
219              
220             # Write the text element.
221 4155         10677 $self->_write_text( $text, $font );
222              
223 4155         8964 $self->xml_end_tag( 'comment' );
224             }
225              
226              
227             ##############################################################################
228             #
229             # _write_text()
230             #
231             # Write the element.
232             #
233             sub _write_text {
234              
235 4155     4155   6625 my $self = shift;
236 4155         6359 my $text = shift;
237 4155         6178 my $font = shift;
238              
239 4155         10230 $self->xml_start_tag( 'text' );
240              
241             # Write the text r element.
242 4155         10875 $self->_write_text_r( $text, $font );
243              
244 4155         9134 $self->xml_end_tag( 'text' );
245             }
246              
247              
248             ##############################################################################
249             #
250             # _write_text_r()
251             #
252             # Write the element.
253             #
254             sub _write_text_r {
255              
256 4155     4155   6241 my $self = shift;
257 4155         6124 my $text = shift;
258 4155         5930 my $font = shift;
259              
260 4155         9883 $self->xml_start_tag( 'r' );
261              
262             # Write the rPr element.
263 4155         10549 $self->_write_r_pr($font);
264              
265             # Write the text r element.
266 4155         10660 $self->_write_text_t( $text );
267              
268 4155         9829 $self->xml_end_tag( 'r' );
269             }
270              
271              
272             ##############################################################################
273             #
274             # _write_text_t()
275             #
276             # Write the text element.
277             #
278             sub _write_text_t {
279              
280 4160     4160   6261 my $self = shift;
281 4160         6151 my $text = shift;
282              
283 4160         6300 my @attributes = ();
284              
285 4160 100 100     19579 if ( $text =~ /^\s/ || $text =~ /\s$/ ) {
286 4         9 push @attributes, ( 'xml:space' => 'preserve' );
287             }
288              
289 4160         10088 $self->xml_data_element( 't', $text, @attributes );
290             }
291              
292              
293             ##############################################################################
294             #
295             # _write_r_pr()
296             #
297             # Write the element.
298             #
299             sub _write_r_pr {
300              
301 4155     4155   6288 my $self = shift;
302 4155         5846 my $font = shift;
303              
304 4155         9899 $self->xml_start_tag( 'rPr' );
305              
306             # Write the sz element.
307 4155         10941 $self->_write_sz($font->[1]);
308              
309             # Write the color element.
310 4155         10437 $self->_write_color();
311              
312             # Write the rFont element.
313 4155         10975 $self->_write_r_font($font->[0]);
314              
315             # Write the family element.
316 4155         10950 $self->_write_family($font->[2]);
317              
318 4155         9829 $self->xml_end_tag( 'rPr' );
319             }
320              
321              
322             ##############################################################################
323             #
324             # _write_sz()
325             #
326             # Write the element.
327             #
328             sub _write_sz {
329              
330 4155     4155   6133 my $self = shift;
331 4155         5909 my $val = shift;
332              
333 4155         7754 my @attributes = ( 'val' => $val );
334              
335 4155         9221 $self->xml_empty_tag( 'sz', @attributes );
336             }
337              
338              
339             ##############################################################################
340             #
341             # _write_color()
342             #
343             # Write the element.
344             #
345             sub _write_color {
346              
347 4155     4155   6253 my $self = shift;
348 4155         6021 my $indexed = 81;
349              
350 4155         7447 my @attributes = ( 'indexed' => $indexed );
351              
352 4155         8464 $self->xml_empty_tag( 'color', @attributes );
353             }
354              
355              
356             ##############################################################################
357             #
358             # _write_r_font()
359             #
360             # Write the element.
361             #
362             sub _write_r_font {
363              
364 4155     4155   6653 my $self = shift;
365 4155         6250 my $val = shift;
366              
367 4155         7608 my @attributes = ( 'val' => $val );
368              
369 4155         8935 $self->xml_empty_tag( 'rFont', @attributes );
370             }
371              
372              
373             ##############################################################################
374             #
375             # _write_family()
376             #
377             # Write the element.
378             #
379             sub _write_family {
380              
381 4155     4155   6337 my $self = shift;
382 4155         5904 my $val = shift;
383              
384 4155         7841 my @attributes = ( 'val' => $val );
385              
386 4155         8825 $self->xml_empty_tag( 'family', @attributes );
387             }
388              
389              
390             1;
391              
392              
393             __END__