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-2020, 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 1081     1081   17404 use 5.008002;
  1081         3717  
17 1081     1081   5334 use strict;
  1081         3459  
  1081         22814  
18 1081     1081   6265 use warnings;
  1081         2168  
  1081         25088  
19 1081     1081   7756 use Carp;
  1081         3653  
  1081         56501  
20 1081     1081   7963 use Excel::Writer::XLSX::Package::XMLwriter;
  1081         2402  
  1081         44798  
21 1081     1081   9004 use Excel::Writer::XLSX::Utility qw(xl_rowcol_to_cell);
  1081         3536  
  1081         1092517  
22              
23              
24             our @ISA = qw(Excel::Writer::XLSX::Package::XMLwriter);
25             our $VERSION = '1.07';
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 51     51 0 5344 my $class = shift;
44 51         233 my $fh = shift;
45 51         441 my $self = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );
46              
47 51         260 $self->{_author_ids} = {};
48              
49 51         218 bless $self, $class;
50              
51 51         132 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 45     45   124 my $self = shift;
64 45         91 my $comments_data = shift;
65              
66 45         263 $self->xml_declaration;
67              
68             # Write the comments element.
69 45         316 $self->_write_comments();
70              
71             # Write the authors element.
72 45         325 $self->_write_authors( $comments_data );
73              
74             # Write the commentList element.
75 45         187 $self->_write_comment_list( $comments_data );
76              
77 45         161 $self->xml_end_tag( 'comments' );
78              
79             # Close the XML writer filehandle.
80 45         242 $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 45     45   104 my $self = shift;
107 45         101 my $xmlns = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';
108              
109 45         143 my @attributes = ( 'xmlns' => $xmlns );
110              
111 45         355 $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 45     45   98 my $self = shift;
124 45         86 my $comment_data = shift;
125 45         90 my $author_count = 0;
126              
127 45         146 $self->xml_start_tag( 'authors' );
128              
129 45         147 for my $comment ( @$comment_data ) {
130 4163         6466 my $author = $comment->[3];
131              
132 4163 100 66     10279 if ( defined $author && !exists $self->{_author_ids}->{$author} ) {
133              
134             # Store the author id.
135 46         161 $self->{_author_ids}->{$author} = $author_count++;
136              
137             # Write the author element.
138 46         165 $self->_write_author( $author );
139             }
140             }
141              
142 45         261 $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 46     46   196 my $self = shift;
155 46         193 my $data = shift;
156              
157 46         340 $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 45     45   96 my $self = shift;
170 45         102 my $comment_data = shift;
171              
172 45         181 $self->xml_start_tag( 'commentList' );
173              
174 45         119 for my $comment ( @$comment_data ) {
175 4163         8848 my $row = $comment->[0];
176 4163         5095 my $col = $comment->[1];
177 4163         5876 my $text = $comment->[2];
178 4163         5513 my $author = $comment->[3];
179 4163         5594 my $font_name = $comment->[6];
180 4163         4846 my $font_size = $comment->[7];
181 4163         5141 my $font_family = $comment->[8];
182              
183             # Look up the author id.
184 4163         4928 my $author_id = undef;
185 4163 50       7644 $author_id = $self->{_author_ids}->{$author} if defined $author;
186              
187             # Write the comment element.
188 4163         6659 my $font = [ $font_name, $font_size, $font_family ];
189 4163         7125 $self->_write_comment( $row, $col, $text, $author_id, $font );
190             }
191              
192 45         175 $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 4163     4163   5452 my $self = shift;
205 4163         5104 my $row = shift;
206 4163         4847 my $col = shift;
207 4163         4858 my $text = shift;
208 4163         4671 my $author_id = shift;
209 4163         7996 my $ref = xl_rowcol_to_cell( $row, $col );
210 4163         5668 my $font = shift;
211              
212              
213 4163         7306 my @attributes = ( 'ref' => $ref );
214              
215 4163 50       7919 push @attributes, ( 'authorId' => $author_id ) if defined $author_id;
216              
217              
218 4163         9720 $self->xml_start_tag( 'comment', @attributes );
219              
220             # Write the text element.
221 4163         9450 $self->_write_text( $text, $font );
222              
223 4163         7453 $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 4163     4163   5461 my $self = shift;
236 4163         5571 my $text = shift;
237 4163         4727 my $font = shift;
238              
239 4163         8701 $self->xml_start_tag( 'text' );
240              
241             # Write the text r element.
242 4163         9513 $self->_write_text_r( $text, $font );
243              
244 4163         7538 $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 4163     4163   5159 my $self = shift;
257 4163         5173 my $text = shift;
258 4163         4610 my $font = shift;
259              
260 4163         8097 $self->xml_start_tag( 'r' );
261              
262             # Write the rPr element.
263 4163         8972 $self->_write_r_pr($font);
264              
265             # Write the text r element.
266 4163         9476 $self->_write_text_t( $text );
267              
268 4163         8028 $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 4168     4168   5114 my $self = shift;
281 4168         5168 my $text = shift;
282              
283 4168         5077 my @attributes = ();
284              
285 4168 100 100     15648 if ( $text =~ /^\s/ || $text =~ /\s$/ ) {
286 4         7 push @attributes, ( 'xml:space' => 'preserve' );
287             }
288              
289 4168         8184 $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 4163     4163   5049 my $self = shift;
302 4163         4880 my $font = shift;
303              
304 4163         7986 $self->xml_start_tag( 'rPr' );
305              
306             # Write the sz element.
307 4163         9303 $self->_write_sz($font->[1]);
308              
309             # Write the color element.
310 4163         9078 $self->_write_color();
311              
312             # Write the rFont element.
313 4163         9371 $self->_write_r_font($font->[0]);
314              
315             # Write the family element.
316 4163         9169 $self->_write_family($font->[2]);
317              
318 4163         8551 $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 4163     4163   5248 my $self = shift;
331 4163         5535 my $val = shift;
332              
333 4163         6465 my @attributes = ( 'val' => $val );
334              
335 4163         7682 $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 4163     4163   5494 my $self = shift;
348 4163         4965 my $indexed = 81;
349              
350 4163         6695 my @attributes = ( 'indexed' => $indexed );
351              
352 4163         7150 $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 4163     4163   5212 my $self = shift;
365 4163         5266 my $val = shift;
366              
367 4163         6207 my @attributes = ( 'val' => $val );
368              
369 4163         7364 $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 4163     4163   5376 my $self = shift;
382 4163         4805 my $val = shift;
383              
384 4163         6388 my @attributes = ( 'val' => $val );
385              
386 4163         7080 $self->xml_empty_tag( 'family', @attributes );
387             }
388              
389              
390             1;
391              
392              
393             __END__