File Coverage

blib/lib/Excel/Writer/XLSX/Package/Table.pm
Criterion Covered Total %
statement 91 91 100.0
branch 16 16 100.0
condition 3 6 50.0
subroutine 14 14 100.0
pod 0 1 0.0
total 124 128 96.8


line stmt bran cond sub pod time code
1             package Excel::Writer::XLSX::Package::Table;
2              
3             ###############################################################################
4             #
5             # Table - A class for writing the Excel XLSX Table file.
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   18744 use 5.008002;
  1040         5215  
17 1040     1040   8459 use strict;
  1040         2083  
  1040         24021  
18 1040     1040   6449 use warnings;
  1040         2047  
  1040         41070  
19 1040     1040   8398 use Carp;
  1040         3441  
  1040         56988  
20 1040     1040   8662 use Excel::Writer::XLSX::Package::XMLwriter;
  1040         5174  
  1040         889194  
21              
22             our @ISA = qw(Excel::Writer::XLSX::Package::XMLwriter);
23             our $VERSION = '1.03';
24              
25              
26             ###############################################################################
27             #
28             # Public and private API methods.
29             #
30             ###############################################################################
31              
32              
33             ###############################################################################
34             #
35             # new()
36             #
37             # Constructor.
38             #
39             sub new {
40              
41 50     50 0 12676 my $class = shift;
42 50         138 my $fh = shift;
43 50         338 my $self = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );
44              
45 50         248 $self->{_properties} = {};
46              
47 50         136 bless $self, $class;
48              
49 50         153 return $self;
50             }
51              
52              
53             ###############################################################################
54             #
55             # _assemble_xml_file()
56             #
57             # Assemble and write the XML file.
58             #
59             sub _assemble_xml_file {
60              
61 47     47   148 my $self = shift;
62              
63 47         315 $self->xml_declaration;
64              
65             # Write the table element.
66 47         455 $self->_write_table();
67              
68             # Write the autoFilter element.
69 47         216 $self->_write_auto_filter();
70              
71             # Write the tableColumns element.
72 47         222 $self->_write_table_columns();
73              
74             # Write the tableStyleInfo element.
75 47         208 $self->_write_table_style_info();
76              
77              
78             # Close the table tag.
79 47         325 $self->xml_end_tag( 'table' );
80              
81             # Close the XML writer filehandle.
82 47         376 $self->xml_get_fh()->close();
83             }
84              
85              
86             ###############################################################################
87             #
88             # _set_properties()
89             #
90             # Set the document properties.
91             #
92             sub _set_properties {
93              
94 47     47   227 my $self = shift;
95 47         128 my $properties = shift;
96              
97 47         222 $self->{_properties} = $properties;
98             }
99              
100              
101             ###############################################################################
102             #
103             # Internal methods.
104             #
105             ###############################################################################
106              
107              
108             ###############################################################################
109             #
110             # XML writing methods.
111             #
112             ###############################################################################
113              
114              
115             ##############################################################################
116             #
117             # _write_table()
118             #
119             # Write the element.
120             #
121             sub _write_table {
122              
123 47     47   105 my $self = shift;
124 47         243 my $schema = 'http://schemas.openxmlformats.org/';
125 47         180 my $xmlns = $schema . 'spreadsheetml/2006/main';
126 47         143 my $id = $self->{_properties}->{_id};
127 47         260 my $name = $self->{_properties}->{_name};
128 47         115 my $display_name = $self->{_properties}->{_name};
129 47         130 my $ref = $self->{_properties}->{_range};
130 47         117 my $totals_row_shown = $self->{_properties}->{_totals_row_shown};
131 47         222 my $header_row_count = $self->{_properties}->{_header_row_count};
132              
133 47         251 my @attributes = (
134             'xmlns' => $xmlns,
135             'id' => $id,
136             'name' => $name,
137             'displayName' => $display_name,
138             'ref' => $ref,
139             );
140              
141 47 100       215 push @attributes, ( 'headerRowCount' => 0 ) if !$header_row_count;
142              
143 47 100       166 if ( $totals_row_shown ) {
144 10         43 push @attributes, ( 'totalsRowCount' => 1 );
145             }
146             else {
147 37         123 push @attributes, ( 'totalsRowShown' => 0 );
148             }
149              
150              
151 47         347 $self->xml_start_tag( 'table', @attributes );
152             }
153              
154              
155             ##############################################################################
156             #
157             # _write_auto_filter()
158             #
159             # Write the element.
160             #
161             sub _write_auto_filter {
162              
163 48     48   136 my $self = shift;
164 48         128 my $autofilter = $self->{_properties}->{_autofilter};
165              
166 48 100       224 return unless $autofilter;
167              
168 44         148 my @attributes = ( 'ref' => $autofilter, );
169              
170 44         260 $self->xml_empty_tag( 'autoFilter', @attributes );
171             }
172              
173              
174             ##############################################################################
175             #
176             # _write_table_columns()
177             #
178             # Write the element.
179             #
180             sub _write_table_columns {
181              
182 47     47   108 my $self = shift;
183 47         115 my @columns = @{ $self->{_properties}->{_columns} };
  47         173  
184              
185 47         224 my $count = scalar @columns;
186              
187 47         150 my @attributes = ( 'count' => $count, );
188              
189 47         279 $self->xml_start_tag( 'tableColumns', @attributes );
190              
191 47         265 for my $col_data ( @columns ) {
192              
193             # Write the tableColumn element.
194 210         731 $self->_write_table_column( $col_data );
195             }
196              
197 47         297 $self->xml_end_tag( 'tableColumns' );
198             }
199              
200              
201             ##############################################################################
202             #
203             # _write_table_column()
204             #
205             # Write the element.
206             #
207             sub _write_table_column {
208              
209 211     211   521 my $self = shift;
210 211         496 my $col_data = shift;
211              
212             my @attributes = (
213             'id' => $col_data->{_id},
214             'name' => $col_data->{_name},
215 211         716 );
216              
217              
218 211 100       786 if ( $col_data->{_total_string} ) {
    100          
219 9         42 push @attributes, ( totalsRowLabel => $col_data->{_total_string} );
220             }
221             elsif ( $col_data->{_total_function} ) {
222 40         79 push @attributes, ( totalsRowFunction => $col_data->{_total_function} );
223             }
224              
225              
226 211 100       480 if ( defined $col_data->{_format} ) {
227 9         24 push @attributes, ( dataDxfId => $col_data->{_format} );
228             }
229              
230 211 100       491 if ( $col_data->{_formula} ) {
231 3         11 $self->xml_start_tag( 'tableColumn', @attributes );
232              
233             # Write the calculatedColumnFormula element.
234 3         21 $self->_write_calculated_column_formula( $col_data->{_formula} );
235              
236 3         30 $self->xml_end_tag( 'tableColumn' );
237             }
238             else {
239 208         570 $self->xml_empty_tag( 'tableColumn', @attributes );
240             }
241              
242             }
243              
244              
245             ##############################################################################
246             #
247             # _write_table_style_info()
248             #
249             # Write the element.
250             #
251             sub _write_table_style_info {
252              
253 48     48   144 my $self = shift;
254 48         129 my $props = $self->{_properties};
255              
256 48         112 my @attributes = ();
257 48         144 my $name = $props->{_style};
258 48         122 my $show_first_column = $props->{_show_first_col};
259 48         119 my $show_last_column = $props->{_show_last_col};
260 48         110 my $show_row_stripes = $props->{_show_row_stripes};
261 48         106 my $show_column_stripes = $props->{_show_col_stripes};
262              
263 48 100 33     479 if ( $name && $name ne '' && $name ne 'None' ) {
      66        
264 47         147 push @attributes, ( 'name' => $name );
265             }
266              
267 48         147 push @attributes, ( 'showFirstColumn' => $show_first_column );
268 48         322 push @attributes, ( 'showLastColumn' => $show_last_column );
269 48         306 push @attributes, ( 'showRowStripes' => $show_row_stripes );
270 48         145 push @attributes, ( 'showColumnStripes' => $show_column_stripes );
271              
272 48         218 $self->xml_empty_tag( 'tableStyleInfo', @attributes );
273             }
274              
275              
276             ##############################################################################
277             #
278             # _write_calculated_column_formula()
279             #
280             # Write the element.
281             #
282             sub _write_calculated_column_formula {
283              
284 3     3   7 my $self = shift;
285 3         6 my $formula = shift;
286              
287 3         21 $self->xml_data_element( 'calculatedColumnFormula', $formula );
288             }
289              
290              
291             1;
292              
293              
294             __END__