File Coverage

blib/lib/Excel/Writer/XLSX/Chartsheet.pm
Criterion Covered Total %
statement 90 102 88.2
branch 9 10 90.0
condition 10 11 90.9
subroutine 17 29 58.6
pod 0 20 0.0
total 126 172 73.2


line stmt bran cond sub pod time code
1             package Excel::Writer::XLSX::Chartsheet;
2              
3             ###############################################################################
4             #
5             # Chartsheet - A class for writing the Excel XLSX Chartsheet 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   28295 use 5.008002;
  1040         4237  
17 1040     1040   6424 use strict;
  1040         2481  
  1040         25789  
18 1040     1040   5738 use warnings;
  1040         2573  
  1040         32448  
19 1040     1040   5650 use Exporter;
  1040         2358  
  1040         34385  
20 1040     1040   6283 use Excel::Writer::XLSX::Worksheet;
  1040         2505  
  1040         896268  
21              
22             our @ISA = qw(Excel::Writer::XLSX::Worksheet);
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 29     29 0 6410 my $class = shift;
42 29         189 my $self = Excel::Writer::XLSX::Worksheet->new( @_ );
43              
44 29         104 $self->{_drawing} = 1;
45 29         84 $self->{_is_chartsheet} = 1;
46 29         74 $self->{_chart} = undef;
47 29         113 $self->{_charts} = [1];
48 29         69 $self->{_zoom_scale_normal} = 0;
49 29         82 $self->{_orientation} = 0;
50              
51 29         72 bless $self, $class;
52              
53 29         95 return $self;
54             }
55              
56              
57             ###############################################################################
58             #
59             # _assemble_xml_file()
60             #
61             # Assemble and write the XML file.
62             #
63             sub _assemble_xml_file {
64              
65 21     21   66 my $self = shift;
66              
67 21         233 $self->xml_declaration;
68              
69             # Write the root chartsheet element.
70 21         124 $self->_write_chartsheet();
71              
72             # Write the worksheet properties.
73 21         98 $self->_write_sheet_pr();
74              
75             # Write the sheet view properties.
76 21         125 $self->_write_sheet_views();
77              
78             # Write the sheetProtection element.
79 21         133 $self->_write_sheet_protection();
80              
81             # Write the printOptions element.
82 21         123 $self->_write_print_options();
83              
84             # Write the worksheet page_margins.
85 21         107 $self->_write_page_margins();
86              
87             # Write the worksheet page setup.
88 21         139 $self->_write_page_setup();
89              
90             # Write the headerFooter element.
91 21         165 $self->_write_header_footer();
92              
93             # Write the drawing element.
94 21         138 $self->_write_drawings();
95              
96             # Close the worksheet tag.
97 21         100 $self->xml_end_tag( 'chartsheet' );
98              
99             # Close the XML writer filehandle.
100 21         108 $self->xml_get_fh()->close();
101             }
102              
103              
104             ###############################################################################
105             #
106             # Public methods.
107             #
108             ###############################################################################
109              
110             # Over-ride parent protect() method to protect both worksheet and chart.
111             sub protect {
112              
113 8     8 0 154 my $self = shift;
114 8   100     26 my $password = shift || '';
115 8         14 my $user_options = shift;
116 8         15 my $options = {};
117              
118             # Objects are default on for chartsheets.
119 8 100       31 if ( defined $user_options->{objects} ) {
120 4         12 $options->{objects} = not $user_options->{objects};
121             }
122             else {
123 4         11 $options->{objects} = 0;
124             }
125              
126 8 100       18 if ( defined $user_options->{content} ) {
127 3         7 $options->{content} = $user_options->{content};
128             }
129             else {
130 5         13 $options->{content} = 1;
131             }
132              
133             # If objects and content are off then the chartsheet isn't locked, except
134             # if it has a password.
135 8 100 100     38 if ( $password eq '' && $options->{objects} && !$options->{content} ) {
      100        
136 1         4 return;
137             }
138              
139 7         20 $self->{_chart}->{_protection} = 1;
140              
141             # Turn off worksheet defaults.
142 7         21 $options->{sheet} = 0;
143 7         15 $options->{scenarios} = 1;
144              
145 7         30 $self->SUPER::protect( $password, $options );
146             }
147              
148              
149             ###############################################################################
150             #
151             # Encapsulated Chart methods.
152             #
153             ###############################################################################
154              
155 56     56 0 511 sub add_series { return shift->{_chart}->add_series( @_ ) }
156 0     0 0 0 sub combine { return shift->{_chart}->combine( @_ ) }
157 1     1 0 16 sub set_x_axis { return shift->{_chart}->set_x_axis( @_ ) }
158 1     1 0 16 sub set_y_axis { return shift->{_chart}->set_y_axis( @_ ) }
159 0     0 0 0 sub set_x2_axis { return shift->{_chart}->set_x2_axis( @_ ) }
160 0     0 0 0 sub set_y2_axis { return shift->{_chart}->set_y2_axis( @_ ) }
161 1     1 0 12 sub set_title { return shift->{_chart}->set_title( @_ ) }
162 1     1 0 12 sub set_legend { return shift->{_chart}->set_legend( @_ ) }
163 0     0 0 0 sub set_plotarea { return shift->{_chart}->set_plotarea( @_ ) }
164 0     0 0 0 sub set_chartarea { return shift->{_chart}->set_chartarea( @_ ) }
165 0     0 0 0 sub set_style { return shift->{_chart}->set_style( @_ ) }
166 1     1 0 14 sub show_blanks_as { return shift->{_chart}->show_blanks_as( @_ ) }
167 0     0 0 0 sub show_hidden_data { return shift->{_chart}->show_hidden_data( @_ ) }
168 0     0 0 0 sub set_size { return shift->{_chart}->set_size( @_ ) }
169 0     0 0 0 sub set_table { return shift->{_chart}->set_table( @_ ) }
170 0     0 0 0 sub set_up_down_bars { return shift->{_chart}->set_up_down_bars( @_ ) }
171 0     0 0 0 sub set_drop_lines { return shift->{_chart}->set_drop_lines( @_ ) }
172 0     0 0 0 sub set_high_low_lines { return shift->{_chart}->high_low_lines( @_ ) }
173              
174              
175              
176             ###############################################################################
177             #
178             # Internal methods.
179             #
180             ###############################################################################
181              
182              
183             ###############################################################################
184             #
185             # _prepare_chart()
186             #
187             # Set up chart/drawings.
188             #
189             sub _prepare_chart {
190              
191 20     20   50 my $self = shift;
192 20         48 my $index = shift;
193 20         57 my $chart_id = shift;
194 20         68 my $drawing_id = shift;
195              
196 20         72 $self->{_chart}->{_id} = $chart_id -1;
197              
198 20         104 my $drawing = Excel::Writer::XLSX::Drawing->new();
199 20         224 $self->{_drawing} = $drawing;
200 20         108 $self->{_drawing}->{_orientation} = $self->{_orientation};
201              
202 20         46 push @{ $self->{_external_drawing_links} },
  20         127  
203             [ '/drawing', '../drawings/drawing' . $drawing_id . '.xml' ];
204              
205 20         63 push @{ $self->{_drawing_links} },
  20         167  
206             [ '/chart', '../charts/chart' . $chart_id . '.xml' ];
207             }
208              
209              
210             ###############################################################################
211             #
212             # XML writing methods.
213             #
214             ###############################################################################
215              
216              
217             ###############################################################################
218             #
219             # _write_chartsheet()
220             #
221             # Write the element. This is the root element of Chartsheet.
222             #
223             sub _write_chartsheet {
224              
225 21     21   52 my $self = shift;
226 21         53 my $schema = 'http://schemas.openxmlformats.org/';
227 21         76 my $xmlns = $schema . 'spreadsheetml/2006/main';
228 21         65 my $xmlns_r = $schema . 'officeDocument/2006/relationships';
229 21         60 my $xmlns_mc = $schema . 'markup-compatibility/2006';
230 21         45 my $xmlns_mv = 'urn:schemas-microsoft-com:mac:vml';
231 21         43 my $mc_ignorable = 'mv';
232 21         47 my $mc_preserve_attributes = 'mv:*';
233              
234 21         100 my @attributes = (
235             'xmlns' => $xmlns,
236             'xmlns:r' => $xmlns_r,
237             );
238              
239 21         176 $self->xml_start_tag( 'chartsheet', @attributes );
240             }
241              
242              
243             ###############################################################################
244             #
245             # _write_sheet_pr()
246             #
247             # Write the element for Sheet level properties.
248             #
249             sub _write_sheet_pr {
250              
251 21     21   62 my $self = shift;
252 21         57 my @attributes = ();
253              
254              
255 21 50       107 push @attributes, ( 'filterMode' => 1 ) if $self->{_filter_on};
256              
257 21 100 66     190 if ( $self->{_fit_page} || $self->{_tab_color} ) {
258 1         5 $self->xml_start_tag( 'sheetPr', @attributes );
259 1         9 $self->_write_tab_color();
260 1         16 $self->_write_page_set_up_pr();
261 1         14 $self->xml_end_tag( 'sheetPr' );
262             }
263             else {
264 20         137 $self->xml_empty_tag( 'sheetPr', @attributes );
265             }
266             }
267              
268             1;
269              
270              
271             __END__