File Coverage

blib/lib/Excel/Writer/XLSX.pm
Criterion Covered Total %
statement 18 18 100.0
branch 1 2 50.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 26 27 96.3


line stmt bran cond sub pod time code
1              
2             ###############################################################################
3             #
4             # Excel::Writer::XLSX - Create a new file in the Excel 2007+ XLSX format.
5             #
6             # Copyright 2000-2021, John McNamara, jmcnamara@cpan.org
7             #
8             # Documentation after __END__
9             #
10              
11             use 5.008002;
12 1124     1124   89887756 use strict;
  1124         13393  
13 1124     1124   5106 use warnings;
  1124         1799  
  1124         20673  
14 1124     1124   4498 use Exporter;
  1124         1881  
  1124         24819  
15 1124     1124   4772  
  1124         2208  
  1124         37688  
16             use Excel::Writer::XLSX::Workbook;
17 1124     1124   598528  
  1124         3168  
  1124         134317  
18             our @ISA = qw(Excel::Writer::XLSX::Workbook Exporter);
19             our $VERSION = '1.09';
20              
21              
22             ###############################################################################
23             #
24             # new()
25             #
26              
27             my $class = shift;
28             my $self = Excel::Writer::XLSX::Workbook->new( @_ );
29 892     892 1 297961  
30 892         7265 # Check for file creation failures before re-blessing
31             bless $self, $class if defined $self;
32              
33 892 50       4864 return $self;
34             }
35 892         2855  
36              
37             1;
38              
39              
40              
41              
42              
43             =head1 NAME
44              
45             Excel::Writer::XLSX - Create a new file in the Excel 2007+ XLSX format.
46              
47             =head1 SYNOPSIS
48              
49             To write a string, a formatted string, a number and a formula to the first worksheet in an Excel workbook called perl.xlsx:
50              
51             use Excel::Writer::XLSX;
52              
53             # Create a new Excel workbook
54             my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );
55              
56             # Add a worksheet
57             $worksheet = $workbook->add_worksheet();
58              
59             # Add and define a format
60             $format = $workbook->add_format();
61             $format->set_bold();
62             $format->set_color( 'red' );
63             $format->set_align( 'center' );
64              
65             # Write a formatted and unformatted string, row and column notation.
66             $col = $row = 0;
67             $worksheet->write( $row, $col, 'Hi Excel!', $format );
68             $worksheet->write( 1, $col, 'Hi Excel!' );
69              
70             # Write a number and a formula using A1 notation
71             $worksheet->write( 'A3', 1.2345 );
72             $worksheet->write( 'A4', '=SIN(PI()/4)' );
73              
74             $workbook->close();
75              
76              
77              
78             =head1 DESCRIPTION
79              
80             The C<Excel::Writer::XLSX> module can be used to create an Excel file in the 2007+ XLSX format.
81              
82             Multiple worksheets can be added to a workbook and formatting can be applied to cells. Text, numbers, and formulas can be written to the cells.
83              
84              
85              
86              
87             =head1 Excel::Writer::XLSX and Spreadsheet::WriteExcel
88              
89             C<Excel::Writer::XLSX> uses the same interface as the L<Spreadsheet::WriteExcel> module which produces an Excel file in binary XLS format.
90              
91              
92              
93              
94             =head1 QUICK START
95              
96             Excel::Writer::XLSX tries to provide an interface to as many of Excel's features as possible. As a result there is a lot of documentation to accompany the interface and it can be difficult at first glance to see what it important and what is not. So for those of you who prefer to assemble Ikea furniture first and then read the instructions, here are four easy steps:
97              
98             1. Create a new Excel I<workbook> (i.e. file) using C<new()>.
99              
100             2. Add a worksheet to the new workbook using C<add_worksheet()>.
101              
102             3. Write to the worksheet using C<write()>.
103              
104             4. C<close()> the file.
105              
106             Like this:
107              
108             use Excel::Writer::XLSX; # Step 0
109              
110             my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' ); # Step 1
111             $worksheet = $workbook->add_worksheet(); # Step 2
112             $worksheet->write( 'A1', 'Hi Excel!' ); # Step 3
113              
114             $workbook->close(); # Step 4
115              
116              
117             This will create an Excel file called C<perl.xlsx> with a single worksheet and the text C<'Hi Excel!'> in the relevant cell. And that's it. Okay, so there is actually a zeroth step as well, but C<use module> goes without saying. There are many examples that come with the distribution and which you can use to get you started. See L</EXAMPLES>.
118              
119             Those of you who read the instructions first and assemble the furniture afterwards will know how to proceed. ;-)
120              
121              
122              
123              
124             =head1 WORKBOOK METHODS
125              
126             The Excel::Writer::XLSX module provides an object oriented interface to a new Excel workbook. The following methods are available through a new workbook.
127              
128             new()
129             add_worksheet()
130             add_format()
131             add_chart()
132             add_shape()
133             add_vba_project()
134             set_vba_name()
135             close()
136             set_properties()
137             set_custom_property()
138             define_name()
139             set_tempdir()
140             set_custom_color()
141             sheets()
142             get_worksheet_by_name()
143             set_1904()
144             set_optimization()
145             set_calc_mode()
146             get_default_url_format()
147             read_only_recommended()
148              
149             If you are unfamiliar with object oriented interfaces or the way that they are implemented in Perl have a look at C<perlobj> and C<perltoot> in the main Perl documentation.
150              
151              
152              
153              
154             =head2 new()
155              
156             A new Excel workbook is created using the C<new()> constructor which accepts either a filename or a filehandle as a parameter. The following example creates a new Excel file based on a filename:
157              
158             my $workbook = Excel::Writer::XLSX->new( 'filename.xlsx' );
159             my $worksheet = $workbook->add_worksheet();
160             $worksheet->write( 0, 0, 'Hi Excel!' );
161             $workbook->close();
162              
163              
164             Here are some other examples of using C<new()> with filenames:
165              
166             my $workbook1 = Excel::Writer::XLSX->new( $filename );
167             my $workbook2 = Excel::Writer::XLSX->new( '/tmp/filename.xlsx' );
168             my $workbook3 = Excel::Writer::XLSX->new( "c:\\tmp\\filename.xlsx" );
169             my $workbook4 = Excel::Writer::XLSX->new( 'c:\tmp\filename.xlsx' );
170              
171             The last two examples demonstrates how to create a file on DOS or Windows where it is necessary to either escape the directory separator C<\> or to use single quotes to ensure that it isn't interpolated. For more information see C<perlfaq5: Why can't I use "C:\temp\foo" in DOS paths?>.
172              
173             It is recommended that the filename uses the extension C<.xlsx> rather than C<.xls> since the latter causes an Excel warning when used with the XLSX format.
174              
175             The C<new()> constructor returns a Excel::Writer::XLSX object that you can use to add worksheets and store data. It should be noted that although C<my> is not specifically required it defines the scope of the new workbook variable and, in the majority of cases, ensures that the workbook is closed properly without explicitly calling the C<close()> method.
176              
177             If the file cannot be created, due to file permissions or some other reason, C<new> will return C<undef>. Therefore, it is good practice to check the return value of C<new> before proceeding. As usual the Perl variable C<$!> will be set if there is a file creation error. You will also see one of the warning messages detailed in L</DIAGNOSTICS>:
178              
179             my $workbook = Excel::Writer::XLSX->new( 'protected.xlsx' );
180             die "Problems creating new Excel file: $!" unless defined $workbook;
181              
182             You can also pass a valid filehandle to the C<new()> constructor. For example in a CGI program you could do something like this:
183              
184             binmode( STDOUT );
185             my $workbook = Excel::Writer::XLSX->new( \*STDOUT );
186              
187             The requirement for C<binmode()> is explained below.
188              
189             See also, the C<cgi.pl> program in the C<examples> directory of the distro.
190              
191             In C<mod_perl> programs where you will have to do something like the following:
192              
193             # mod_perl 1
194             ...
195             tie *XLSX, 'Apache';
196             binmode( XLSX );
197             my $workbook = Excel::Writer::XLSX->new( \*XLSX );
198             ...
199              
200             # mod_perl 2
201             ...
202             tie *XLSX => $r; # Tie to the Apache::RequestRec object
203             binmode( *XLSX );
204             my $workbook = Excel::Writer::XLSX->new( \*XLSX );
205             ...
206              
207             See also, the C<mod_perl1.pl> and C<mod_perl2.pl> programs in the C<examples> directory of the distro.
208              
209             Filehandles can also be useful if you want to stream an Excel file over a socket or if you want to store an Excel file in a scalar.
210              
211             For example here is a way to write an Excel file to a scalar:
212              
213             #!/usr/bin/perl -w
214              
215             use strict;
216             use Excel::Writer::XLSX;
217              
218             open my $fh, '>', \my $str or die "Failed to open filehandle: $!";
219              
220             my $workbook = Excel::Writer::XLSX->new( $fh );
221             my $worksheet = $workbook->add_worksheet();
222              
223             $worksheet->write( 0, 0, 'Hi Excel!' );
224              
225             $workbook->close();
226              
227             # The Excel file in now in $str. Remember to binmode() the output
228             # filehandle before printing it.
229             binmode STDOUT;
230             print $str;
231              
232             See also the C<write_to_scalar.pl> and C<filehandle.pl> programs in the C<examples> directory of the distro.
233              
234             B<Note about the requirement for> C<binmode()>. An Excel file is comprised of binary data. Therefore, if you are using a filehandle you should ensure that you C<binmode()> it prior to passing it to C<new()>.You should do this regardless of whether you are on a Windows platform or not.
235              
236             You don't have to worry about C<binmode()> if you are using filenames instead of filehandles. Excel::Writer::XLSX performs the C<binmode()> internally when it converts the filename to a filehandle. For more information about C<binmode()> see C<perlfunc> and C<perlopentut> in the main Perl documentation.
237              
238              
239              
240              
241              
242             =head2 add_worksheet( $sheetname )
243              
244             At least one worksheet should be added to a new workbook. A worksheet is used to write data into cells:
245              
246             $worksheet1 = $workbook->add_worksheet(); # Sheet1
247             $worksheet2 = $workbook->add_worksheet( 'Foglio2' ); # Foglio2
248             $worksheet3 = $workbook->add_worksheet( 'Data' ); # Data
249             $worksheet4 = $workbook->add_worksheet(); # Sheet4
250              
251             If C<$sheetname> is not specified the default Excel convention will be followed, i.e. Sheet1, Sheet2, etc.
252              
253             The worksheet name must be a valid Excel worksheet name, i.e:
254              
255             =over
256              
257             =item * It must be less than 32 characters.
258              
259             =item * It cannot contain any of the following characters: C<[ ] : * ? / \>
260              
261             =item * It cannot start or end with an apostrophe.
262              
263             =item * It cannot be the same as an existing worksheet name (or a case insensitive variant).
264              
265             =back
266              
267             Note, the sheetname should not be "History" (case insensitive) which is reserved in English language versions of Excel. Non-English versions may have restrictions on the equivalent word.
268              
269             See the Excel worksheet naming rules at L<https://support.office.com/en-ie/article/rename-a-worksheet-3f1f7148-ee83-404d-8ef0-9ff99fbad1f9>.
270              
271              
272             =head2 add_format( %properties )
273              
274             The C<add_format()> method can be used to create new Format objects which are used to apply formatting to a cell. You can either define the properties at creation time via a hash of property values or later via method calls.
275              
276             $format1 = $workbook->add_format( %props ); # Set properties at creation
277             $format2 = $workbook->add_format(); # Set properties later
278              
279             See the L</CELL FORMATTING> section for more details about Format properties and how to set them.
280              
281              
282              
283              
284             =head2 add_chart( %properties )
285              
286             This method is use to create a new chart either as a standalone worksheet (the default) or as an embeddable object that can be inserted into a worksheet via the C<insert_chart()> Worksheet method.
287              
288             my $chart = $workbook->add_chart( type => 'column' );
289              
290             The properties that can be set are:
291              
292             type (required)
293             subtype (optional)
294             name (optional)
295             embedded (optional)
296              
297             =over
298              
299             =item * C<type>
300              
301             This is a required parameter. It defines the type of chart that will be created.
302              
303             my $chart = $workbook->add_chart( type => 'line' );
304              
305             The available types are:
306              
307             area
308             bar
309             column
310             line
311             pie
312             doughnut
313             scatter
314             stock
315              
316             =item * C<subtype>
317              
318             Used to define a chart subtype where available.
319              
320             my $chart = $workbook->add_chart( type => 'bar', subtype => 'stacked' );
321              
322             See the L<Excel::Writer::XLSX::Chart> documentation for a list of available chart subtypes.
323              
324             =item * C<name>
325              
326             Set the name for the chart sheet. The name property is optional and if it isn't supplied will default to C<Chart1 .. n>. The name must be a valid Excel worksheet name. See C<add_worksheet()> for more details on valid sheet names. The C<name> property can be omitted for embedded charts.
327              
328             my $chart = $workbook->add_chart( type => 'line', name => 'Results Chart' );
329              
330             =item * C<embedded>
331              
332             Specifies that the Chart object will be inserted in a worksheet via the C<insert_chart()> Worksheet method. It is an error to try insert a Chart that doesn't have this flag set.
333              
334             my $chart = $workbook->add_chart( type => 'line', embedded => 1 );
335              
336             # Configure the chart.
337             ...
338              
339             # Insert the chart into the a worksheet.
340             $worksheet->insert_chart( 'E2', $chart );
341              
342             =back
343              
344             See Excel::Writer::XLSX::Chart for details on how to configure the chart object once it is created. See also the C<chart_*.pl> programs in the examples directory of the distro.
345              
346              
347              
348             =head2 add_shape( %properties )
349              
350             The C<add_shape()> method can be used to create new shapes that may be inserted into a worksheet.
351              
352             You can either define the properties at creation time via a hash of property values or later via method calls.
353              
354             # Set properties at creation.
355             $plus = $workbook->add_shape(
356             type => 'plus',
357             id => 3,
358             width => $pw,
359             height => $ph
360             );
361              
362              
363             # Default rectangle shape. Set properties later.
364             $rect = $workbook->add_shape();
365              
366             See L<Excel::Writer::XLSX::Shape> for details on how to configure the shape object once it is created.
367              
368             See also the C<shape*.pl> programs in the examples directory of the distro.
369              
370              
371              
372             =head2 add_vba_project( 'vbaProject.bin' )
373              
374             The C<add_vba_project()> method can be used to add macros or functions to an Excel::Writer::XLSX file using a binary VBA project file that has been extracted from an existing Excel C<xlsm> file.
375              
376             my $workbook = Excel::Writer::XLSX->new( 'file.xlsm' );
377              
378             $workbook->add_vba_project( './vbaProject.bin' );
379              
380             The supplied C<extract_vba> utility can be used to extract the required C<vbaProject.bin> file from an existing Excel file:
381              
382             $ extract_vba file.xlsm
383             Extracted 'vbaProject.bin' successfully
384              
385             Macros can be tied to buttons using the worksheet C<insert_button()> method (see the L</WORKSHEET METHODS> section for details):
386              
387             $worksheet->insert_button( 'C2', { macro => 'my_macro' } );
388              
389             Note, Excel uses the file extension C<xlsm> instead of C<xlsx> for files that contain macros. It is advisable to follow the same convention.
390              
391             See also the C<macros.pl> example file and the L<WORKING WITH VBA MACROS>.
392              
393              
394              
395             =head2 set_vba_name()
396              
397             The C<set_vba_name()> method can be used to set the VBA codename for the workbook. This is sometimes required when a C<vbaProject macro> included via C<add_vba_project()> refers to the workbook. The default Excel VBA name of C<ThisWorkbook> is used if a user defined name isn't specified. See also L<WORKING WITH VBA MACROS>.
398              
399              
400             =head2 close()
401              
402             In general your Excel file will be closed automatically when your program ends or when the Workbook object goes out of scope. However it is recommended to explicitly call the C<close()> method close the Excel file and avoid the potential issues outlined below. The C<close()> method is called like this:
403              
404             $workbook->close();
405              
406             The return value of C<close()> is the same as that returned by perl when it closes the file created by C<new()>. This allows you to handle error conditions in the usual way:
407              
408             $workbook->close() or die "Error closing file: $!";
409              
410             An explicit C<close()> is required if the file must be closed prior to performing some external action on it such as copying it, reading its size or attaching it to an email.
411              
412             In addition, C<close()> may be required to prevent perl's garbage collector from disposing of the Workbook, Worksheet and Format objects in the wrong order. Situations where this can occur are:
413              
414             =over 4
415              
416             =item *
417              
418             If C<my()> was not used to declare the scope of a workbook variable created using C<new()>.
419              
420             =item *
421              
422             If the C<new()>, C<add_worksheet()> or C<add_format()> methods are called in subroutines.
423              
424             =back
425              
426             The reason for this is that Excel::Writer::XLSX relies on Perl's C<DESTROY> mechanism to trigger destructor methods in a specific sequence. This may not happen in cases where the Workbook, Worksheet and Format variables are not lexically scoped or where they have different lexical scopes.
427              
428             To avoid these issues it is recommended that you always close the Excel::Writer::XLSX filehandle using C<close()>.
429              
430              
431              
432              
433             =head2 set_size( $width, $height )
434              
435             The C<set_size()> method can be used to set the size of a workbook window.
436              
437             $workbook->set_size(1200, 800);
438              
439             The Excel window size was used in Excel 2007 to define the width and height of a workbook window within the Multiple Document Interface (MDI). In later versions of Excel for Windows this interface was dropped. This method is currently only useful when setting the window size in Excel for Mac 2011. The units are pixels and the default size is 1073 x 644.
440              
441             Note, this doesn't equate exactly to the Excel for Mac pixel size since it is based on the original Excel 2007 for Windows sizing.
442              
443              
444              
445              
446             =head2 set_tab_ratio( $tab_ratio )
447              
448              
449             The C<set_tab_ratio()> method can be used to set the ratio between worksheet tabs and the horizontal slider at the bottom of a workbook. This can be increased to give more room to the tabs or reduced to increase the size of the horizontal slider:
450              
451             $workbook->set_tab_ratio(75);
452              
453             The default value in Excel is 60.
454              
455              
456              
457              
458             =head2 set_properties()
459              
460             The C<set_properties> method can be used to set the document properties of the Excel file created by C<Excel::Writer::XLSX>. These properties are visible when you use the C<< Office Button -> Prepare -> Properties >> option in Excel and are also available to external applications that read or index Windows files.
461              
462             The properties should be passed in hash format as follows:
463              
464             $workbook->set_properties(
465             title => 'This is an example spreadsheet',
466             author => 'John McNamara',
467             comments => 'Created with Perl and Excel::Writer::XLSX',
468             );
469              
470             The properties that can be set are:
471              
472             title
473             subject
474             author
475             manager
476             company
477             category
478             keywords
479             comments
480             status
481             hyperlink_base
482             created - File create date. Such be an aref of gmtime() values.
483              
484             See also the C<properties.pl> program in the examples directory of the distro.
485              
486              
487              
488              
489             =head2 set_custom_property( $name, $value, $type)
490              
491             The C<set_custom_property> method can be used to set one of more custom document properties not covered by the C<set_properties()> method above. These properties are visible when you use the C<< Office Button -> Prepare -> Properties -> Advanced Properties -> Custom >> option in Excel and are also available to external applications that read or index Windows files.
492              
493             The C<set_custom_property> method takes 3 parameters:
494              
495             $workbook-> set_custom_property( $name, $value, $type);
496              
497             Where the available types are:
498              
499             text
500             date
501             number
502             bool
503              
504             For example:
505              
506             $workbook->set_custom_property( 'Checked by', 'Eve', 'text' );
507             $workbook->set_custom_property( 'Date completed', '2016-12-12T23:00:00Z', 'date' );
508             $workbook->set_custom_property( 'Document number', '12345' , 'number' );
509             $workbook->set_custom_property( 'Reference', '1.2345', 'number' );
510             $workbook->set_custom_property( 'Has review', 1, 'bool' );
511             $workbook->set_custom_property( 'Signed off', 0, 'bool' );
512             $workbook->set_custom_property( 'Department', $some_string, 'text' );
513             $workbook->set_custom_property( 'Scale', '1.2345678901234', 'number' );
514              
515             Dates should by in ISO8601 C<yyyy-mm-ddThh:mm:ss.sssZ> date format in Zulu time, as shown above.
516              
517             The C<text> and C<number> types are optional since they can usually be inferred from the data:
518              
519             $workbook->set_custom_property( 'Checked by', 'Eve' );
520             $workbook->set_custom_property( 'Reference', '1.2345' );
521              
522              
523             The C<$name> and C<$value> parameters are limited to 255 characters by Excel.
524              
525              
526              
527              
528             =head2 define_name()
529              
530             This method is used to defined a name that can be used to represent a value, a single cell or a range of cells in a workbook.
531              
532             For example to set a global/workbook name:
533              
534             # Global/workbook names.
535             $workbook->define_name( 'Exchange_rate', '=0.96' );
536             $workbook->define_name( 'Sales', '=Sheet1!$G$1:$H$10' );
537              
538             It is also possible to define a local/worksheet name by prefixing the name with the sheet name using the syntax C<sheetname!definedname>:
539              
540             # Local/worksheet name.
541             $workbook->define_name( 'Sheet2!Sales', '=Sheet2!$G$1:$G$10' );
542              
543             If the sheet name contains spaces or special characters you must enclose it in single quotes like in Excel:
544              
545             $workbook->define_name( "'New Data'!Sales", '=Sheet2!$G$1:$G$10' );
546              
547             See the defined_name.pl program in the examples dir of the distro.
548              
549             Refer to the following to see Excel's syntax rules for defined names: L<http://office.microsoft.com/en-001/excel-help/define-and-use-names-in-formulas-HA010147120.aspx#BMsyntax_rules_for_names>
550              
551              
552              
553              
554             =head2 set_tempdir()
555              
556             C<Excel::Writer::XLSX> stores worksheet data in temporary files prior to assembling the final workbook.
557              
558             The C<File::Temp> module is used to create these temporary files. File::Temp uses C<File::Spec> to determine an appropriate location for these files such as C</tmp> or C<c:\windows\temp>. You can find out which directory is used on your system as follows:
559              
560             perl -MFile::Spec -le "print File::Spec->tmpdir()"
561              
562             If the default temporary file directory isn't accessible to your application, or doesn't contain enough space, you can specify an alternative location using the C<set_tempdir()> method:
563              
564             $workbook->set_tempdir( '/tmp/writeexcel' );
565             $workbook->set_tempdir( 'c:\windows\temp\writeexcel' );
566              
567             The directory for the temporary file must exist, C<set_tempdir()> will not create a new directory.
568              
569              
570              
571              
572              
573             =head2 set_custom_color( $index, $red, $green, $blue )
574              
575             The method is maintained for backward compatibility with Spreadsheet::WriteExcel. Excel::Writer::XLSX programs don't require this method and colours can be specified using a Html style C<#RRGGBB> value, see L</WORKING WITH COLOURS>.
576              
577              
578              
579              
580             =head2 sheets( 0, 1, ... )
581              
582             The C<sheets()> method returns a list, or a sliced list, of the worksheets in a workbook.
583              
584             If no arguments are passed the method returns a list of all the worksheets in the workbook. This is useful if you want to repeat an operation on each worksheet:
585              
586             for $worksheet ( $workbook->sheets() ) {
587             print $worksheet->get_name();
588             }
589              
590              
591             You can also specify a slice list to return one or more worksheet objects:
592              
593             $worksheet = $workbook->sheets( 0 );
594             $worksheet->write( 'A1', 'Hello' );
595              
596              
597             Or since the return value from C<sheets()> is a reference to a worksheet object you can write the above example as:
598              
599             $workbook->sheets( 0 )->write( 'A1', 'Hello' );
600              
601              
602             The following example returns the first and last worksheet in a workbook:
603              
604             for $worksheet ( $workbook->sheets( 0, -1 ) ) {
605             # Do something
606             }
607              
608              
609             Array slices are explained in the C<perldata> manpage.
610              
611              
612              
613              
614             =head2 get_worksheet_by_name()
615              
616             The C<get_worksheet_by_name()> function return a worksheet or chartsheet object in the workbook using the sheetname:
617              
618             $worksheet = $workbook->get_worksheet_by_name('Sheet1');
619              
620              
621              
622              
623             =head2 set_1904()
624              
625             Excel stores dates as real numbers where the integer part stores the number of days since the epoch and the fractional part stores the percentage of the day. The epoch can be either 1900 or 1904. Excel for Windows uses 1900 and Excel for Macintosh uses 1904. However, Excel on either platform will convert automatically between one system and the other.
626              
627             Excel::Writer::XLSX stores dates in the 1900 format by default. If you wish to change this you can call the C<set_1904()> workbook method. You can query the current value by calling the C<get_1904()> workbook method. This returns 0 for 1900 and 1 for 1904.
628              
629             See also L</DATES AND TIME IN EXCEL> for more information about working with Excel's date system.
630              
631             In general you probably won't need to use C<set_1904()>.
632              
633              
634              
635              
636             =head2 set_optimization()
637              
638             The C<set_optimization()> method is used to turn on optimizations in the Excel::Writer::XLSX module. Currently there is only one optimization available and that is to reduce memory usage.
639              
640             $workbook->set_optimization();
641              
642              
643             See L</SPEED AND MEMORY USAGE> for more background information.
644              
645             Note, that with this optimization turned on a row of data is written and then discarded when a cell in a new row is added via one of the Worksheet C<write_*()> methods. As such data should be written in sequential row order once the optimization is turned on.
646              
647             This method must be called before any calls to C<add_worksheet()>.
648              
649              
650              
651             =head2 set_calc_mode( $mode )
652              
653             Set the calculation mode for formulas in the workbook. This is mainly of use for workbooks with slow formulas where you want to allow the user to calculate them manually.
654              
655             The mode parameter can be one of the following strings:
656              
657             =over
658              
659             =item C<auto>
660              
661             The default. Excel will re-calculate formulas when a formula or a value affecting the formula changes.
662              
663             =item C<manual>
664              
665             Only re-calculate formulas when the user requires it. Generally by pressing F9.
666              
667             =item C<auto_except_tables>
668              
669             Excel will automatically re-calculate formulas except for tables.
670              
671             =back
672              
673              
674              
675              
676             =head2 get_default_url_format()
677              
678             The C<get_default_url_format()> method gets a copy of the default url format used when a user defined format isn't specified with the worksheet C<write_url()> method. The format is the hyperlink style defined by Excel for the default theme:
679              
680             my $url_format = $workbook->get_default_url_format();
681              
682              
683              
684              
685             =head2 read_only_recommended()
686              
687             The C<read_only_recommended()> method can be used to set the Excel "Read-only Recommended" option that is available when saving a file. This presents the user of the file with an option to open it in "read-only" mode. This means that any changes to the file can't be saved back to the same file and must be saved to a new file. It can be set as follows:
688              
689             $workbook->read_only_recommended();
690              
691              
692              
693              
694             =head1 WORKSHEET METHODS
695              
696             A new worksheet is created by calling the C<add_worksheet()> method from a workbook object:
697              
698             $worksheet1 = $workbook->add_worksheet();
699             $worksheet2 = $workbook->add_worksheet();
700              
701             The following methods are available through a new worksheet:
702              
703             write()
704             write_number()
705             write_string()
706             write_rich_string()
707             keep_leading_zeros()
708             write_blank()
709             write_row()
710             write_col()
711             write_date_time()
712             write_url()
713             write_url_range()
714             write_formula()
715             write_boolean()
716             write_comment()
717             show_comments()
718             set_comments_author()
719             add_write_handler()
720             insert_image()
721             insert_chart()
722             insert_shape()
723             insert_button()
724             data_validation()
725             conditional_formatting()
726             add_sparkline()
727             add_table()
728             get_name()
729             activate()
730             select()
731             hide()
732             set_first_sheet()
733             protect()
734             unprotect_range()
735             set_selection()
736             set_row()
737             set_row_pixels()
738             set_default_row()
739             set_column()
740             set_column_pixels()
741             outline_settings()
742             freeze_panes()
743             split_panes()
744             merge_range()
745             merge_range_type()
746             set_zoom()
747             right_to_left()
748             hide_zero()
749             set_background()
750             set_tab_color()
751             autofilter()
752             filter_column()
753             filter_column_list()
754             set_vba_name()
755             ignore_errors()
756              
757              
758             =head2 Cell notation
759              
760             Excel::Writer::XLSX supports two forms of notation to designate the position of cells: Row-column notation and A1 notation.
761              
762             Row-column notation uses a zero based index for both row and column while A1 notation uses the standard Excel alphanumeric sequence of column letter and 1-based row. For example:
763              
764             (0, 0) # The top left cell in row-column notation.
765             ('A1') # The top left cell in A1 notation.
766              
767             (1999, 29) # Row-column notation.
768             ('AD2000') # The same cell in A1 notation.
769              
770             Row-column notation is useful if you are referring to cells programmatically:
771              
772             for my $i ( 0 .. 9 ) {
773             $worksheet->write( $i, 0, 'Hello' ); # Cells A1 to A10
774             }
775              
776             A1 notation is useful for setting up a worksheet manually and for working with formulas:
777              
778             $worksheet->write( 'H1', 200 );
779             $worksheet->write( 'H2', '=H1+1' );
780              
781             In formulas and applicable methods you can also use the C<A:A> column notation:
782              
783             $worksheet->write( 'A1', '=SUM(B:B)' );
784              
785             The C<Excel::Writer::XLSX::Utility> module that is included in the distro contains helper functions for dealing with A1 notation, for example:
786              
787             use Excel::Writer::XLSX::Utility;
788              
789             ( $row, $col ) = xl_cell_to_rowcol( 'C2' ); # (1, 2)
790             $str = xl_rowcol_to_cell( 1, 2 ); # C2
791              
792             For simplicity, the parameter lists for the worksheet method calls in the following sections are given in terms of row-column notation. In all cases it is also possible to use A1 notation.
793              
794             Note: in Excel it is also possible to use a R1C1 notation. This is not supported by Excel::Writer::XLSX.
795              
796              
797              
798              
799             =head2 write( $row, $column, $token, $format )
800              
801             Excel makes a distinction between data types such as strings, numbers, blanks, formulas and hyperlinks. To simplify the process of writing data the C<write()> method acts as a general alias for several more specific methods:
802              
803             write_string()
804             write_number()
805             write_blank()
806             write_formula()
807             write_url()
808             write_row()
809             write_col()
810              
811             The general rule is that if the data looks like a I<something> then a I<something> is written. Here are some examples in both row-column and A1 notation:
812              
813             # Same as:
814             $worksheet->write( 0, 0, 'Hello' ); # write_string()
815             $worksheet->write( 1, 0, 'One' ); # write_string()
816             $worksheet->write( 2, 0, 2 ); # write_number()
817             $worksheet->write( 3, 0, 3.00001 ); # write_number()
818             $worksheet->write( 4, 0, "" ); # write_blank()
819             $worksheet->write( 5, 0, '' ); # write_blank()
820             $worksheet->write( 6, 0, undef ); # write_blank()
821             $worksheet->write( 7, 0 ); # write_blank()
822             $worksheet->write( 8, 0, 'http://www.perl.com/' ); # write_url()
823             $worksheet->write( 'A9', 'ftp://ftp.cpan.org/' ); # write_url()
824             $worksheet->write( 'A10', 'internal:Sheet1!A1' ); # write_url()
825             $worksheet->write( 'A11', 'external:c:\foo.xlsx' ); # write_url()
826             $worksheet->write( 'A12', '=A3 + 3*A4' ); # write_formula()
827             $worksheet->write( 'A13', '=SIN(PI()/4)' ); # write_formula()
828             $worksheet->write( 'A14', \@array ); # write_row()
829             $worksheet->write( 'A15', [\@array] ); # write_col()
830              
831             # And if the keep_leading_zeros property is set:
832             $worksheet->write( 'A16', '2' ); # write_number()
833             $worksheet->write( 'A17', '02' ); # write_string()
834             $worksheet->write( 'A18', '00002' ); # write_string()
835              
836             # Write an array formula. Not available in Spreadsheet::WriteExcel.
837             $worksheet->write( 'A19', '{=SUM(A1:B1*A2:B2)}' ); # write_formula()
838              
839              
840             The "looks like" rule is defined by regular expressions:
841              
842             C<write_number()> if C<$token> is a number based on the following regex: C<$token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/>.
843              
844             C<write_string()> if C<keep_leading_zeros()> is set and C<$token> is an integer with leading zeros based on the following regex: C<$token =~ /^0\d+$/>.
845              
846             C<write_blank()> if C<$token> is undef or a blank string: C<undef>, C<""> or C<''>.
847              
848             C<write_url()> if C<$token> is a http, https, ftp or mailto URL based on the following regexes: C<$token =~ m|^[fh]tt?ps?://|> or C<$token =~ m|^mailto:|>.
849              
850             C<write_url()> if C<$token> is an internal or external sheet reference based on the following regex: C<$token =~ m[^(in|ex)ternal:]>.
851              
852             C<write_formula()> if the first character of C<$token> is C<"=">.
853              
854             C<write_array_formula()> if the C<$token> matches C</^{=.*}$/>.
855              
856             C<write_row()> if C<$token> is an array ref.
857              
858             C<write_col()> if C<$token> is an array ref of array refs.
859              
860             C<write_string()> if none of the previous conditions apply.
861              
862             The C<$format> parameter is optional. It should be a valid Format object, see L</CELL FORMATTING>:
863              
864             my $format = $workbook->add_format();
865             $format->set_bold();
866             $format->set_color( 'red' );
867             $format->set_align( 'center' );
868              
869             $worksheet->write( 4, 0, 'Hello', $format ); # Formatted string
870              
871             The write() method will ignore empty strings or C<undef> tokens unless a format is also supplied. As such you needn't worry about special handling for empty or C<undef> values in your data. See also the C<write_blank()> method.
872              
873             One problem with the C<write()> method is that occasionally data looks like a number but you don't want it treated as a number. For example, zip codes or ID numbers often start with a leading zero. If you write this data as a number then the leading zero(s) will be stripped. You can change this default behaviour by using the C<keep_leading_zeros()> method. While this property is in place any integers with leading zeros will be treated as strings and the zeros will be preserved. See the C<keep_leading_zeros()> section for a full discussion of this issue.
874              
875             You can also add your own data handlers to the C<write()> method using C<add_write_handler()>.
876              
877             The C<write()> method will also handle Unicode strings in C<UTF-8> format.
878              
879             The C<write> methods return:
880              
881             0 for success.
882             -1 for insufficient number of arguments.
883             -2 for row or column out of bounds.
884             -3 for string too long.
885              
886              
887              
888              
889             =head2 write_number( $row, $column, $number, $format )
890              
891             Write an integer or a float to the cell specified by C<$row> and C<$column>:
892              
893             $worksheet->write_number( 0, 0, 123456 );
894             $worksheet->write_number( 'A2', 2.3451 );
895              
896             See the note about L</Cell notation>. The C<$format> parameter is optional.
897              
898             In general it is sufficient to use the C<write()> method.
899              
900             B<Note>: some versions of Excel 2007 do not display the calculated values of formulas written by Excel::Writer::XLSX. Applying all available Service Packs to Excel should fix this.
901              
902              
903              
904             =head2 write_string( $row, $column, $string, $format )
905              
906             Write a string to the cell specified by C<$row> and C<$column>:
907              
908             $worksheet->write_string( 0, 0, 'Your text here' );
909             $worksheet->write_string( 'A2', 'or here' );
910              
911             The maximum string size is 32767 characters. However the maximum string segment that Excel can display in a cell is 1000. All 32767 characters can be displayed in the formula bar.
912              
913             The C<$format> parameter is optional.
914              
915             The C<write()> method will also handle strings in C<UTF-8> format. See also the C<unicode_*.pl> programs in the examples directory of the distro.
916              
917             In general it is sufficient to use the C<write()> method. However, you may sometimes wish to use the C<write_string()> method to write data that looks like a number but that you don't want treated as a number. For example, zip codes or phone numbers:
918              
919             # Write as a plain string
920             $worksheet->write_string( 'A1', '01209' );
921              
922             However, if the user edits this string Excel may convert it back to a number. To get around this you can use the Excel text format C<@>:
923              
924             # Format as a string. Doesn't change to a number when edited
925             my $format1 = $workbook->add_format( num_format => '@' );
926             $worksheet->write_string( 'A2', '01209', $format1 );
927              
928             See also the note about L</Cell notation>.
929              
930              
931              
932              
933             =head2 write_rich_string( $row, $column, $format, $string, ..., $cell_format )
934              
935             The C<write_rich_string()> method is used to write strings with multiple formats. For example to write the string "This is B<bold> and this is I<italic>" you would use the following:
936              
937             my $bold = $workbook->add_format( bold => 1 );
938             my $italic = $workbook->add_format( italic => 1 );
939              
940             $worksheet->write_rich_string( 'A1',
941             'This is ', $bold, 'bold', ' and this is ', $italic, 'italic' );
942              
943             The basic rule is to break the string into fragments and put a C<$format> object before the fragment that you want to format. For example:
944              
945             # Unformatted string.
946             'This is an example string'
947              
948             # Break it into fragments.
949             'This is an ', 'example', ' string'
950              
951             # Add formatting before the fragments you want formatted.
952             'This is an ', $format, 'example', ' string'
953              
954             # In Excel::Writer::XLSX.
955             $worksheet->write_rich_string( 'A1',
956             'This is an ', $format, 'example', ' string' );
957              
958             String fragments that don't have a format are given a default format. So for example when writing the string "Some B<bold> text" you would use the first example below but it would be equivalent to the second:
959              
960             # With default formatting:
961             my $bold = $workbook->add_format( bold => 1 );
962              
963             $worksheet->write_rich_string( 'A1',
964             'Some ', $bold, 'bold', ' text' );
965              
966             # Or more explicitly:
967             my $bold = $workbook->add_format( bold => 1 );
968             my $default = $workbook->add_format();
969              
970             $worksheet->write_rich_string( 'A1',
971             $default, 'Some ', $bold, 'bold', $default, ' text' );
972              
973             As with Excel, only the font properties of the format such as font name, style, size, underline, color and effects are applied to the string fragments. Other features such as border, background, text wrap and alignment must be applied to the cell.
974              
975             The C<write_rich_string()> method allows you to do this by using the last argument as a cell format (if it is a format object). The following example centers a rich string in the cell:
976              
977             my $bold = $workbook->add_format( bold => 1 );
978             my $center = $workbook->add_format( align => 'center' );
979              
980             $worksheet->write_rich_string( 'A5',
981             'Some ', $bold, 'bold text', ' centered', $center );
982              
983             See the C<rich_strings.pl> example in the distro for more examples.
984              
985             my $bold = $workbook->add_format( bold => 1 );
986             my $italic = $workbook->add_format( italic => 1 );
987             my $red = $workbook->add_format( color => 'red' );
988             my $blue = $workbook->add_format( color => 'blue' );
989             my $center = $workbook->add_format( align => 'center' );
990             my $super = $workbook->add_format( font_script => 1 );
991              
992              
993             # Write some strings with multiple formats.
994             $worksheet->write_rich_string( 'A1',
995             'This is ', $bold, 'bold', ' and this is ', $italic, 'italic' );
996              
997             $worksheet->write_rich_string( 'A3',
998             'This is ', $red, 'red', ' and this is ', $blue, 'blue' );
999              
1000             $worksheet->write_rich_string( 'A5',
1001             'Some ', $bold, 'bold text', ' centered', $center );
1002              
1003             $worksheet->write_rich_string( 'A7',
1004             $italic, 'j = k', $super, '(n-1)', $center );
1005              
1006             =begin html
1007              
1008             <p><center><img src="http://jmcnamara.github.io/excel-writer-xlsx/images/examples/rich_strings.jpg" width="640" height="420" alt="Output from rich_strings.pl" /></center></p>
1009              
1010             =end html
1011              
1012             As with C<write_sting()> the maximum string size is 32767 characters. See also the note about L</Cell notation>.
1013              
1014              
1015              
1016              
1017             =head2 keep_leading_zeros()
1018              
1019             This method changes the default handling of integers with leading zeros when using the C<write()> method.
1020              
1021             The C<write()> method uses regular expressions to determine what type of data to write to an Excel worksheet. If the data looks like a number it writes a number using C<write_number()>. One problem with this approach is that occasionally data looks like a number but you don't want it treated as a number.
1022              
1023             Zip codes and ID numbers, for example, often start with a leading zero. If you write this data as a number then the leading zero(s) will be stripped. This is the also the default behaviour when you enter data manually in Excel.
1024              
1025             To get around this you can use one of three options. Write a formatted number, write the number as a string or use the C<keep_leading_zeros()> method to change the default behaviour of C<write()>:
1026              
1027             # Implicitly write a number, the leading zero is removed: 1209
1028             $worksheet->write( 'A1', '01209' );
1029              
1030             # Write a zero padded number using a format: 01209
1031             my $format1 = $workbook->add_format( num_format => '00000' );
1032             $worksheet->write( 'A2', '01209', $format1 );
1033              
1034             # Write explicitly as a string: 01209
1035             $worksheet->write_string( 'A3', '01209' );
1036              
1037             # Write implicitly as a string: 01209
1038             $worksheet->keep_leading_zeros();
1039             $worksheet->write( 'A4', '01209' );
1040              
1041              
1042             The above code would generate a worksheet that looked like the following:
1043              
1044             -----------------------------------------------------------
1045             | | A | B | C | D | ...
1046             -----------------------------------------------------------
1047             | 1 | 1209 | | | | ...
1048             | 2 | 01209 | | | | ...
1049             | 3 | 01209 | | | | ...
1050             | 4 | 01209 | | | | ...
1051              
1052              
1053             The examples are on different sides of the cells due to the fact that Excel displays strings with a left justification and numbers with a right justification by default. You can change this by using a format to justify the data, see L</CELL FORMATTING>.
1054              
1055             It should be noted that if the user edits the data in examples C<A3> and C<A4> the strings will revert back to numbers. Again this is Excel's default behaviour. To avoid this you can use the text format C<@>:
1056              
1057             # Format as a string (01209)
1058             my $format2 = $workbook->add_format( num_format => '@' );
1059             $worksheet->write_string( 'A5', '01209', $format2 );
1060              
1061             The C<keep_leading_zeros()> property is off by default. The C<keep_leading_zeros()> method takes 0 or 1 as an argument. It defaults to 1 if an argument isn't specified:
1062              
1063             $worksheet->keep_leading_zeros(); # Set on
1064             $worksheet->keep_leading_zeros( 1 ); # Set on
1065             $worksheet->keep_leading_zeros( 0 ); # Set off
1066              
1067             See also the C<add_write_handler()> method.
1068              
1069              
1070             =head2 write_blank( $row, $column, $format )
1071              
1072             Write a blank cell specified by C<$row> and C<$column>:
1073              
1074             $worksheet->write_blank( 0, 0, $format );
1075              
1076             This method is used to add formatting to a cell which doesn't contain a string or number value.
1077              
1078             Excel differentiates between an "Empty" cell and a "Blank" cell. An "Empty" cell is a cell which doesn't contain data whilst a "Blank" cell is a cell which doesn't contain data but does contain formatting. Excel stores "Blank" cells but ignores "Empty" cells.
1079              
1080             As such, if you write an empty cell without formatting it is ignored:
1081              
1082             $worksheet->write( 'A1', undef, $format ); # write_blank()
1083             $worksheet->write( 'A2', undef ); # Ignored
1084              
1085             This seemingly uninteresting fact means that you can write arrays of data without special treatment for C<undef> or empty string values.
1086              
1087             See the note about L</Cell notation>.
1088              
1089              
1090              
1091              
1092             =head2 write_row( $row, $column, $array_ref, $format )
1093              
1094             The C<write_row()> method can be used to write a 1D or 2D array of data in one go. This is useful for converting the results of a database query into an Excel worksheet. You must pass a reference to the array of data rather than the array itself. The C<write()> method is then called for each element of the data. For example:
1095              
1096             @array = ( 'awk', 'gawk', 'mawk' );
1097             $array_ref = \@array;
1098              
1099             $worksheet->write_row( 0, 0, $array_ref );
1100              
1101             # The above example is equivalent to:
1102             $worksheet->write( 0, 0, $array[0] );
1103             $worksheet->write( 0, 1, $array[1] );
1104             $worksheet->write( 0, 2, $array[2] );
1105              
1106              
1107             Note: For convenience the C<write()> method behaves in the same way as C<write_row()> if it is passed an array reference. Therefore the following two method calls are equivalent:
1108              
1109             $worksheet->write_row( 'A1', $array_ref ); # Write a row of data
1110             $worksheet->write( 'A1', $array_ref ); # Same thing
1111              
1112             As with all of the write methods the C<$format> parameter is optional. If a format is specified it is applied to all the elements of the data array.
1113              
1114             Array references within the data will be treated as columns. This allows you to write 2D arrays of data in one go. For example:
1115              
1116             @eec = (
1117             ['maggie', 'milly', 'molly', 'may' ],
1118             [13, 14, 15, 16 ],
1119             ['shell', 'star', 'crab', 'stone']
1120             );
1121              
1122             $worksheet->write_row( 'A1', \@eec );
1123              
1124              
1125             Would produce a worksheet as follows:
1126              
1127             -----------------------------------------------------------
1128             | | A | B | C | D | E | ...
1129             -----------------------------------------------------------
1130             | 1 | maggie | 13 | shell | ... | ... | ...
1131             | 2 | milly | 14 | star | ... | ... | ...
1132             | 3 | molly | 15 | crab | ... | ... | ...
1133             | 4 | may | 16 | stone | ... | ... | ...
1134             | 5 | ... | ... | ... | ... | ... | ...
1135             | 6 | ... | ... | ... | ... | ... | ...
1136              
1137              
1138             To write the data in a row-column order refer to the C<write_col()> method below.
1139              
1140             Any C<undef> values in the data will be ignored unless a format is applied to the data, in which case a formatted blank cell will be written. In either case the appropriate row or column value will still be incremented.
1141              
1142             To find out more about array references refer to C<perlref> and C<perlreftut> in the main Perl documentation. To find out more about 2D arrays or "lists of lists" refer to C<perllol>.
1143              
1144             The C<write_row()> method returns the first error encountered when writing the elements of the data or zero if no errors were encountered. See the return values described for the C<write()> method above.
1145              
1146             See also the C<write_arrays.pl> program in the C<examples> directory of the distro.
1147              
1148             The C<write_row()> method allows the following idiomatic conversion of a text file to an Excel file:
1149              
1150             #!/usr/bin/perl -w
1151              
1152             use strict;
1153             use Excel::Writer::XLSX;
1154              
1155             my $workbook = Excel::Writer::XLSX->new( 'file.xlsx' );
1156             my $worksheet = $workbook->add_worksheet();
1157              
1158             open INPUT, 'file.txt' or die "Couldn't open file: $!";
1159              
1160             $worksheet->write( $. -1, 0, [split] ) while <INPUT>;
1161              
1162             $workbook->close();
1163              
1164              
1165              
1166             =head2 write_col( $row, $column, $array_ref, $format )
1167              
1168             The C<write_col()> method can be used to write a 1D or 2D array of data in one go. This is useful for converting the results of a database query into an Excel worksheet. You must pass a reference to the array of data rather than the array itself. The C<write()> method is then called for each element of the data. For example:
1169              
1170             @array = ( 'awk', 'gawk', 'mawk' );
1171             $array_ref = \@array;
1172              
1173             $worksheet->write_col( 0, 0, $array_ref );
1174              
1175             # The above example is equivalent to:
1176             $worksheet->write( 0, 0, $array[0] );
1177             $worksheet->write( 1, 0, $array[1] );
1178             $worksheet->write( 2, 0, $array[2] );
1179              
1180             As with all of the write methods the C<$format> parameter is optional. If a format is specified it is applied to all the elements of the data array.
1181              
1182             Array references within the data will be treated as rows. This allows you to write 2D arrays of data in one go. For example:
1183              
1184             @eec = (
1185             ['maggie', 'milly', 'molly', 'may' ],
1186             [13, 14, 15, 16 ],
1187             ['shell', 'star', 'crab', 'stone']
1188             );
1189              
1190             $worksheet->write_col( 'A1', \@eec );
1191              
1192              
1193             Would produce a worksheet as follows:
1194              
1195             -----------------------------------------------------------
1196             | | A | B | C | D | E | ...
1197             -----------------------------------------------------------
1198             | 1 | maggie | milly | molly | may | ... | ...
1199             | 2 | 13 | 14 | 15 | 16 | ... | ...
1200             | 3 | shell | star | crab | stone | ... | ...
1201             | 4 | ... | ... | ... | ... | ... | ...
1202             | 5 | ... | ... | ... | ... | ... | ...
1203             | 6 | ... | ... | ... | ... | ... | ...
1204              
1205              
1206             To write the data in a column-row order refer to the C<write_row()> method above.
1207              
1208             Any C<undef> values in the data will be ignored unless a format is applied to the data, in which case a formatted blank cell will be written. In either case the appropriate row or column value will still be incremented.
1209              
1210             As noted above the C<write()> method can be used as a synonym for C<write_row()> and C<write_row()> handles nested array refs as columns. Therefore, the following two method calls are equivalent although the more explicit call to C<write_col()> would be preferable for maintainability:
1211              
1212             $worksheet->write_col( 'A1', $array_ref ); # Write a column of data
1213             $worksheet->write( 'A1', [ $array_ref ] ); # Same thing
1214              
1215             To find out more about array references refer to C<perlref> and C<perlreftut> in the main Perl documentation. To find out more about 2D arrays or "lists of lists" refer to C<perllol>.
1216              
1217             The C<write_col()> method returns the first error encountered when writing the elements of the data or zero if no errors were encountered. See the return values described for the C<write()> method above.
1218              
1219             See also the C<write_arrays.pl> program in the C<examples> directory of the distro.
1220              
1221              
1222              
1223              
1224             =head2 write_date_time( $row, $col, $date_string, $format )
1225              
1226             The C<write_date_time()> method can be used to write a date or time to the cell specified by C<$row> and C<$column>:
1227              
1228             $worksheet->write_date_time( 'A1', '2004-05-13T23:20', $date_format );
1229              
1230             The C<$date_string> should be in the following format:
1231              
1232             yyyy-mm-ddThh:mm:ss.sss
1233              
1234             This conforms to an ISO8601 date but it should be noted that the full range of ISO8601 formats are not supported.
1235              
1236             The following variations on the C<$date_string> parameter are permitted:
1237              
1238             yyyy-mm-ddThh:mm:ss.sss # Standard format
1239             yyyy-mm-ddT # No time
1240             Thh:mm:ss.sss # No date
1241             yyyy-mm-ddThh:mm:ss.sssZ # Additional Z (but not time zones)
1242             yyyy-mm-ddThh:mm:ss # No fractional seconds
1243             yyyy-mm-ddThh:mm # No seconds
1244              
1245             Note that the C<T> is required in all cases.
1246              
1247             A date should always have a C<$format>, otherwise it will appear as a number, see L</DATES AND TIME IN EXCEL> and L</CELL FORMATTING>. Here is a typical example:
1248              
1249             my $date_format = $workbook->add_format( num_format => 'mm/dd/yy' );
1250             $worksheet->write_date_time( 'A1', '2004-05-13T23:20', $date_format );
1251              
1252             Valid dates should be in the range 1900-01-01 to 9999-12-31, for the 1900 epoch and 1904-01-01 to 9999-12-31, for the 1904 epoch. As with Excel, dates outside these ranges will be written as a string.
1253              
1254             See also the date_time.pl program in the C<examples> directory of the distro.
1255              
1256              
1257              
1258              
1259             =head2 write_url( $row, $col, $url, $format, $label )
1260              
1261             Write a hyperlink to a URL in the cell specified by C<$row> and C<$column>. The hyperlink is comprised of two elements: the visible label and the invisible link. The visible label is the same as the link unless an alternative label is specified. The C<$label> parameter is optional. The label is written using the C<write()> method. Therefore it is possible to write strings, numbers or formulas as labels.
1262              
1263             The C<$format> parameter is also optional and the default Excel hyperlink style will be used if it isn't specified. If required you can access the default url format using the Workbook C<get_default_url_format> method:
1264              
1265             my $url_format = $workbook->get_default_url_format();
1266              
1267             There are four web style URI's supported: C<http://>, C<https://>, C<ftp://> and C<mailto:>:
1268              
1269             $worksheet->write_url( 0, 0, 'ftp://www.perl.org/' );
1270             $worksheet->write_url( 'A3', 'http://www.perl.com/' );
1271             $worksheet->write_url( 'A4', 'mailto:jmcnamara@cpan.org' );
1272              
1273             You can display an alternative string using the C<$label> parameter:
1274              
1275             $worksheet->write_url( 1, 0, 'http://www.perl.com/', undef, 'Perl' );
1276              
1277             If you wish to have some other cell data such as a number or a formula you can overwrite the cell using another call to C<write_*()>:
1278              
1279             $worksheet->write_url( 'A1', 'http://www.perl.com/' );
1280              
1281             # Overwrite the URL string with a formula. The cell is still a link.
1282             # Note the use of the default url format for consistency with other links.
1283             my $url_format = $workbook->get_default_url_format();
1284             $worksheet->write_formula( 'A1', '=1+1', $url_format );
1285              
1286             There are two local URIs supported: C<internal:> and C<external:>. These are used for hyperlinks to internal worksheet references or external workbook and worksheet references:
1287              
1288             $worksheet->write_url( 'A6', 'internal:Sheet2!A1' );
1289             $worksheet->write_url( 'A7', 'internal:Sheet2!A1' );
1290             $worksheet->write_url( 'A8', 'internal:Sheet2!A1:B2' );
1291             $worksheet->write_url( 'A9', q{internal:'Sales Data'!A1} );
1292             $worksheet->write_url( 'A10', 'external:c:\temp\foo.xlsx' );
1293             $worksheet->write_url( 'A11', 'external:c:\foo.xlsx#Sheet2!A1' );
1294             $worksheet->write_url( 'A12', 'external:..\foo.xlsx' );
1295             $worksheet->write_url( 'A13', 'external:..\foo.xlsx#Sheet2!A1' );
1296             $worksheet->write_url( 'A13', 'external:\\\\NET\share\foo.xlsx' );
1297              
1298             All of the these URI types are recognised by the C<write()> method, see above.
1299              
1300             Worksheet references are typically of the form C<Sheet1!A1>. You can also refer to a worksheet range using the standard Excel notation: C<Sheet1!A1:B2>.
1301              
1302             In external links the workbook and worksheet name must be separated by the C<#> character: C<external:Workbook.xlsx#Sheet1!A1'>.
1303              
1304             You can also link to a named range in the target worksheet. For example say you have a named range called C<my_name> in the workbook C<c:\temp\foo.xlsx> you could link to it as follows:
1305              
1306             $worksheet->write_url( 'A14', 'external:c:\temp\foo.xlsx#my_name' );
1307              
1308             Excel requires that worksheet names containing spaces or non alphanumeric characters are single quoted as follows C<'Sales Data'!A1>. If you need to do this in a single quoted string then you can either escape the single quotes C<\'> or use the quote operator C<q{}> as described in C<perlop> in the main Perl documentation.
1309              
1310             Links to network files are also supported. MS/Novell Network files normally begin with two back slashes as follows C<\\NETWORK\etc>. In order to generate this in a single or double quoted string you will have to escape the backslashes, C<'\\\\NETWORK\etc'>.
1311              
1312             If you are using double quote strings then you should be careful to escape anything that looks like a metacharacter. For more information see C<perlfaq5: Why can't I use "C:\temp\foo" in DOS paths?>.
1313              
1314             Finally, you can avoid most of these quoting problems by using forward slashes. These are translated internally to backslashes:
1315              
1316             $worksheet->write_url( 'A14', "external:c:/temp/foo.xlsx" );
1317             $worksheet->write_url( 'A15', 'external://NETWORK/share/foo.xlsx' );
1318              
1319             Note: Excel::Writer::XLSX will escape the following characters in URLs as required by Excel: C<< \s " < > \ [ ] ` ^ { } >> unless the URL already contains C<%xx> style escapes. In which case it is assumed that the URL was escaped correctly by the user and will by passed directly to Excel.
1320              
1321             Versions of Excel prior to Excel 2015 limited hyperlink links and anchor/locations to 255 characters each. Versions after that support urls up to 2079 characters. Excel::Writer::XLSX versions >= 1.0.2 support the new longer limit by default.
1322              
1323              
1324             See also, the note about L</Cell notation>.
1325              
1326              
1327              
1328              
1329             =head2 write_formula( $row, $column, $formula, $format, $value )
1330              
1331             Write a formula or function to the cell specified by C<$row> and C<$column>:
1332              
1333             $worksheet->write_formula( 0, 0, '=$B$3 + B4' );
1334             $worksheet->write_formula( 1, 0, '=SIN(PI()/4)' );
1335             $worksheet->write_formula( 2, 0, '=SUM(B1:B5)' );
1336             $worksheet->write_formula( 'A4', '=IF(A3>1,"Yes", "No")' );
1337             $worksheet->write_formula( 'A5', '=AVERAGE(1, 2, 3, 4)' );
1338             $worksheet->write_formula( 'A6', '=DATEVALUE("1-Jan-2001")' );
1339              
1340             Array formulas are also supported:
1341              
1342             $worksheet->write_formula( 'A7', '{=SUM(A1:B1*A2:B2)}' );
1343              
1344             See also the C<write_array_formula()> method below.
1345              
1346             See the note about L</Cell notation>. For more information about writing Excel formulas see L</FORMULAS AND FUNCTIONS IN EXCEL>
1347              
1348             If required, it is also possible to specify the calculated value of the formula. This is occasionally necessary when working with non-Excel applications that don't calculate the value of the formula. The calculated C<$value> is added at the end of the argument list:
1349              
1350             $worksheet->write( 'A1', '=2+2', $format, 4 );
1351              
1352             However, this probably isn't something that you will ever need to do. If you do use this feature then do so with care.
1353              
1354              
1355              
1356              
1357             =head2 write_array_formula($first_row, $first_col, $last_row, $last_col, $formula, $format, $value)
1358              
1359             Write an array formula to a cell range. In Excel an array formula is a formula that performs a calculation on a set of values. It can return a single value or a range of values.
1360              
1361             An array formula is indicated by a pair of braces around the formula: C<{=SUM(A1:B1*A2:B2)}>. If the array formula returns a single value then the C<$first_> and C<$last_> parameters should be the same:
1362              
1363             $worksheet->write_array_formula('A1:A1', '{=SUM(B1:C1*B2:C2)}');
1364              
1365             It this case however it is easier to just use the C<write_formula()> or C<write()> methods:
1366              
1367             # Same as above but more concise.
1368             $worksheet->write( 'A1', '{=SUM(B1:C1*B2:C2)}' );
1369             $worksheet->write_formula( 'A1', '{=SUM(B1:C1*B2:C2)}' );
1370              
1371             For array formulas that return a range of values you must specify the range that the return values will be written to:
1372              
1373             $worksheet->write_array_formula( 'A1:A3', '{=TREND(C1:C3,B1:B3)}' );
1374             $worksheet->write_array_formula( 0, 0, 2, 0, '{=TREND(C1:C3,B1:B3)}' );
1375              
1376             If required, it is also possible to specify the calculated value of the formula. This is occasionally necessary when working with non-Excel applications that don't calculate the value of the formula. However, using this parameter only writes a single value to the upper left cell in the result array. For a multi-cell array formula where the results are required, the other result values can be specified by using C<write_number()> to write to the appropriate cell:
1377              
1378             # Specify the result for a single cell range.
1379             $worksheet->write_array_formula( 'A1:A3', '{=SUM(B1:C1*B2:C2)}, $format, 2005 );
1380              
1381             # Specify the results for a multi cell range.
1382             $worksheet->write_array_formula( 'A1:A3', '{=TREND(C1:C3,B1:B3)}', $format, 105 );
1383             $worksheet->write_number( 'A2', 12, format );
1384             $worksheet->write_number( 'A3', 14, format );
1385              
1386             In addition, some early versions of Excel 2007 don't calculate the values of array formulas when they aren't supplied. Installing the latest Office Service Pack should fix this issue.
1387              
1388             See also the C<array_formula.pl> program in the C<examples> directory of the distro.
1389              
1390             Note: Array formulas are not supported by Spreadsheet::WriteExcel.
1391              
1392              
1393              
1394              
1395             =head2 write_boolean( $row, $column, $value, $format )
1396              
1397             Write an Excel boolean value to the cell specified by C<$row> and C<$column>:
1398              
1399             $worksheet->write_boolean( 'A1', 1 ); # TRUE
1400             $worksheet->write_boolean( 'A2', 0 ); # FALSE
1401             $worksheet->write_boolean( 'A3', undef ); # FALSE
1402             $worksheet->write_boolean( 'A3', 0, $format ); # FALSE, with format.
1403              
1404             A C<$value> that is true or false using Perl's rules will be written as an Excel boolean C<TRUE> or C<FALSE> value.
1405              
1406             See the note about L</Cell notation>.
1407              
1408              
1409              
1410              
1411             =head2 store_formula( $formula )
1412              
1413             Deprecated. This is a Spreadsheet::WriteExcel method that is no longer required by Excel::Writer::XLSX. See below.
1414              
1415              
1416              
1417              
1418             =head2 repeat_formula( $row, $col, $formula, $format )
1419              
1420             Deprecated. This is a Spreadsheet::WriteExcel method that is no longer required by Excel::Writer::XLSX.
1421              
1422             In Spreadsheet::WriteExcel it was computationally expensive to write formulas since they were parsed by a recursive descent parser. The C<store_formula()> and C<repeat_formula()> methods were used as a way of avoiding the overhead of repeated formulas by reusing a pre-parsed formula.
1423              
1424             In Excel::Writer::XLSX this is no longer necessary since it is just as quick to write a formula as it is to write a string or a number.
1425              
1426             The methods remain for backward compatibility but new Excel::Writer::XLSX programs shouldn't use them.
1427              
1428              
1429              
1430              
1431              
1432             =head2 write_comment( $row, $column, $string, ... )
1433              
1434             The C<write_comment()> method is used to add a comment to a cell. A cell comment is indicated in Excel by a small red triangle in the upper right-hand corner of the cell. Moving the cursor over the red triangle will reveal the comment.
1435              
1436             The following example shows how to add a comment to a cell:
1437              
1438             $worksheet->write ( 2, 2, 'Hello' );
1439             $worksheet->write_comment( 2, 2, 'This is a comment.' );
1440              
1441             As usual you can replace the C<$row> and C<$column> parameters with an C<A1> cell reference. See the note about L</Cell notation>.
1442              
1443             $worksheet->write ( 'C3', 'Hello');
1444             $worksheet->write_comment( 'C3', 'This is a comment.' );
1445              
1446             The C<write_comment()> method will also handle strings in C<UTF-8> format.
1447              
1448             $worksheet->write_comment( 'C3', "\x{263a}" ); # Smiley
1449             $worksheet->write_comment( 'C4', 'Comment ca va?' );
1450              
1451             In addition to the basic 3 argument form of C<write_comment()> you can pass in several optional key/value pairs to control the format of the comment. For example:
1452              
1453             $worksheet->write_comment( 'C3', 'Hello', visible => 1, author => 'Perl' );
1454              
1455             Most of these options are quite specific and in general the default comment behaves will be all that you need. However, should you need greater control over the format of the cell comment the following options are available:
1456              
1457             author
1458             visible
1459             x_scale
1460             width
1461             y_scale
1462             height
1463             color
1464             start_cell
1465             start_row
1466             start_col
1467             x_offset
1468             y_offset
1469             font
1470             font_size
1471              
1472              
1473             =over 4
1474              
1475             =item Option: author
1476              
1477             This option is used to indicate who is the author of the cell comment. Excel displays the author of the comment in the status bar at the bottom of the worksheet. This is usually of interest in corporate environments where several people might review and provide comments to a workbook.
1478              
1479             $worksheet->write_comment( 'C3', 'Atonement', author => 'Ian McEwan' );
1480              
1481             The default author for all cell comments can be set using the C<set_comments_author()> method (see below).
1482              
1483             $worksheet->set_comments_author( 'Perl' );
1484              
1485              
1486             =item Option: visible
1487              
1488             This option is used to make a cell comment visible when the worksheet is opened. The default behaviour in Excel is that comments are initially hidden. However, it is also possible in Excel to make individual or all comments visible. In Excel::Writer::XLSX individual comments can be made visible as follows:
1489              
1490             $worksheet->write_comment( 'C3', 'Hello', visible => 1 );
1491              
1492             It is possible to make all comments in a worksheet visible using the C<show_comments()> worksheet method (see below). Alternatively, if all of the cell comments have been made visible you can hide individual comments:
1493              
1494             $worksheet->write_comment( 'C3', 'Hello', visible => 0 );
1495              
1496              
1497             =item Option: x_scale
1498              
1499             This option is used to set the width of the cell comment box as a factor of the default width.
1500              
1501             $worksheet->write_comment( 'C3', 'Hello', x_scale => 2 );
1502             $worksheet->write_comment( 'C4', 'Hello', x_scale => 4.2 );
1503              
1504              
1505             =item Option: width
1506              
1507             This option is used to set the width of the cell comment box explicitly in pixels.
1508              
1509             $worksheet->write_comment( 'C3', 'Hello', width => 200 );
1510              
1511              
1512             =item Option: y_scale
1513              
1514             This option is used to set the height of the cell comment box as a factor of the default height.
1515              
1516             $worksheet->write_comment( 'C3', 'Hello', y_scale => 2 );
1517             $worksheet->write_comment( 'C4', 'Hello', y_scale => 4.2 );
1518              
1519              
1520             =item Option: height
1521              
1522             This option is used to set the height of the cell comment box explicitly in pixels.
1523              
1524             $worksheet->write_comment( 'C3', 'Hello', height => 200 );
1525              
1526              
1527             =item Option: color
1528              
1529             This option is used to set the background colour of cell comment box. You can use one of the named colours recognised by Excel::Writer::XLSX or a Html style C<#RRGGBB> colour. See L</WORKING WITH COLOURS>.
1530              
1531             $worksheet->write_comment( 'C3', 'Hello', color => 'green' );
1532             $worksheet->write_comment( 'C4', 'Hello', color => '#FF6600' ); # Orange
1533              
1534              
1535             =item Option: start_cell
1536              
1537             This option is used to set the cell in which the comment will appear. By default Excel displays comments one cell to the right and one cell above the cell to which the comment relates. However, you can change this behaviour if you wish. In the following example the comment which would appear by default in cell C<D2> is moved to C<E2>.
1538              
1539             $worksheet->write_comment( 'C3', 'Hello', start_cell => 'E2' );
1540              
1541              
1542             =item Option: start_row
1543              
1544             This option is used to set the row in which the comment will appear. See the C<start_cell> option above. The row is zero indexed.
1545              
1546             $worksheet->write_comment( 'C3', 'Hello', start_row => 0 );
1547              
1548              
1549             =item Option: start_col
1550              
1551             This option is used to set the column in which the comment will appear. See the C<start_cell> option above. The column is zero indexed.
1552              
1553             $worksheet->write_comment( 'C3', 'Hello', start_col => 4 );
1554              
1555              
1556             =item Option: x_offset
1557              
1558             This option is used to change the x offset, in pixels, of a comment within a cell:
1559              
1560             $worksheet->write_comment( 'C3', $comment, x_offset => 30 );
1561              
1562              
1563             =item Option: y_offset
1564              
1565             This option is used to change the y offset, in pixels, of a comment within a cell:
1566              
1567             $worksheet->write_comment('C3', $comment, x_offset => 30);
1568              
1569             =item Option: font
1570              
1571             This option is used to change the font used in the comment from 'Tahoma' which is the default.
1572              
1573             $worksheet->write_comment('C3', $comment, font => 'Calibri');
1574              
1575             =item Option: font_size
1576              
1577             This option is used to change the font size used in the comment from 8 which is the default.
1578              
1579             $worksheet->write_comment('C3', $comment, font_size => 20);
1580              
1581              
1582             =back
1583              
1584             You can apply as many of these options as you require.
1585              
1586             B<Note about using options that adjust the position of the cell comment such as start_cell, start_row, start_col, x_offset and y_offset>: Excel only displays offset cell comments when they are displayed as "visible". Excel does B<not> display hidden cells as moved when you mouse over them.
1587              
1588             B<Note about row height and comments>. If you specify the height of a row that contains a comment then Excel::Writer::XLSX will adjust the height of the comment to maintain the default or user specified dimensions. However, the height of a row can also be adjusted automatically by Excel if the text wrap property is set or large fonts are used in the cell. This means that the height of the row is unknown to the module at run time and thus the comment box is stretched with the row. Use the C<set_row()> method to specify the row height explicitly and avoid this problem.
1589              
1590              
1591              
1592              
1593             =head2 show_comments()
1594              
1595             This method is used to make all cell comments visible when a worksheet is opened.
1596              
1597             $worksheet->show_comments();
1598              
1599             Individual comments can be made visible using the C<visible> parameter of the C<write_comment> method (see above):
1600              
1601             $worksheet->write_comment( 'C3', 'Hello', visible => 1 );
1602              
1603             If all of the cell comments have been made visible you can hide individual comments as follows:
1604              
1605             $worksheet->show_comments();
1606             $worksheet->write_comment( 'C3', 'Hello', visible => 0 );
1607              
1608              
1609              
1610             =head2 set_comments_author()
1611              
1612             This method is used to set the default author of all cell comments.
1613              
1614             $worksheet->set_comments_author( 'Perl' );
1615              
1616             Individual comment authors can be set using the C<author> parameter of the C<write_comment> method (see above).
1617              
1618             The default comment author is an empty string, C<''>, if no author is specified.
1619              
1620              
1621              
1622              
1623             =head2 add_write_handler( $re, $code_ref )
1624              
1625             This method is used to extend the Excel::Writer::XLSX write() method to handle user defined data.
1626              
1627             If you refer to the section on C<write()> above you will see that it acts as an alias for several more specific C<write_*> methods. However, it doesn't always act in exactly the way that you would like it to.
1628              
1629             One solution is to filter the input data yourself and call the appropriate C<write_*> method. Another approach is to use the C<add_write_handler()> method to add your own automated behaviour to C<write()>.
1630              
1631             The C<add_write_handler()> method take two arguments, C<$re>, a regular expression to match incoming data and C<$code_ref> a callback function to handle the matched data:
1632              
1633             $worksheet->add_write_handler( qr/^\d\d\d\d$/, \&my_write );
1634              
1635             (In the these examples the C<qr> operator is used to quote the regular expression strings, see L<perlop> for more details).
1636              
1637             The method is used as follows. say you wished to write 7 digit ID numbers as a string so that any leading zeros were preserved*, you could do something like the following:
1638              
1639             $worksheet->add_write_handler( qr/^\d{7}$/, \&write_my_id );
1640              
1641              
1642             sub write_my_id {
1643             my $worksheet = shift;
1644             return $worksheet->write_string( @_ );
1645             }
1646              
1647             * You could also use the C<keep_leading_zeros()> method for this.
1648              
1649             Then if you call C<write()> with an appropriate string it will be handled automatically:
1650              
1651             # Writes 0000000. It would normally be written as a number; 0.
1652             $worksheet->write( 'A1', '0000000' );
1653              
1654             The callback function will receive a reference to the calling worksheet and all of the other arguments that were passed to C<write()>. The callback will see an C<@_> argument list that looks like the following:
1655              
1656             $_[0] A ref to the calling worksheet. *
1657             $_[1] Zero based row number.
1658             $_[2] Zero based column number.
1659             $_[3] A number or string or token.
1660             $_[4] A format ref if any.
1661             $_[5] Any other arguments.
1662             ...
1663              
1664             * It is good style to shift this off the list so the @_ is the same
1665             as the argument list seen by write().
1666              
1667             Your callback should C<return()> the return value of the C<write_*> method that was called or C<undef> to indicate that you rejected the match and want C<write()> to continue as normal.
1668              
1669             So for example if you wished to apply the previous filter only to ID values that occur in the first column you could modify your callback function as follows:
1670              
1671              
1672             sub write_my_id {
1673             my $worksheet = shift;
1674             my $col = $_[1];
1675              
1676             if ( $col == 0 ) {
1677             return $worksheet->write_string( @_ );
1678             }
1679             else {
1680             # Reject the match and return control to write()
1681             return undef;
1682             }
1683             }
1684              
1685             Now, you will get different behaviour for the first column and other columns:
1686              
1687             $worksheet->write( 'A1', '0000000' ); # Writes 0000000
1688             $worksheet->write( 'B1', '0000000' ); # Writes 0
1689              
1690              
1691             You may add more than one handler in which case they will be called in the order that they were added.
1692              
1693             Note, the C<add_write_handler()> method is particularly suited for handling dates.
1694              
1695             See the C<write_handler 1-4> programs in the C<examples> directory for further examples.
1696              
1697              
1698              
1699              
1700             =head2 insert_image( $row, $col, $filename, { %options } )
1701              
1702             This method can be used to insert a image into a worksheet. The image can be in PNG, JPEG, GIF or BMP format.
1703              
1704             $worksheet1->insert_image( 'A1', 'perl.bmp' );
1705             $worksheet2->insert_image( 'A1', '../images/perl.bmp' );
1706             $worksheet3->insert_image( 'A1', '.c:\images\perl.bmp' );
1707              
1708             The optional C<options> hash/hashref parameter can be used to set various options for the image. The defaults are:
1709              
1710             %options = (
1711             x_offset => 0,
1712             y_offset => 0,
1713             x_scale => 1,
1714             y_scale => 1,
1715             object_position => 2,
1716             url => undef,
1717             tip => undef,
1718             description => $filename,
1719             decorative => 0,
1720             );
1721              
1722             The parameters C<x_offset> and C<y_offset> can be used to specify an offset from the top left hand corner of the cell specified by C<$row> and C<$col>. The offset values are in pixels.
1723              
1724             $worksheet1->insert_image('A1', 'perl.bmp', { x_offset =>32, y_offset => 10 });
1725              
1726             The offsets can be greater than the width or height of the underlying cell. This can be occasionally useful if you wish to align two or more images relative to the same cell.
1727              
1728             The parameters C<x_scale> and C<y_scale> can be used to scale the inserted image horizontally and vertically:
1729              
1730             # Scale the inserted image: width x 2.0, height x 0.8
1731             $worksheet->insert_image( 'A1', 'perl.bmp', { y_scale => 2, y_scale => 0.8 } );
1732              
1733             The positioning of the image when cells are resized can be set with the C<object_position> parameter:
1734              
1735             $worksheet->insert_image( 'A1', 'perl.bmp', { object_position => 1 } );
1736              
1737             The C<object_position> parameter can have one of the following allowable values:
1738              
1739             1. Move and size with cells.
1740             2. Move but don't size with cells.
1741             3. Don't move or size with cells.
1742             4. Same as Option 1, see below.
1743              
1744             Option 4 appears in Excel as Option 1. However, the worksheet object is sized to take hidden rows or columns into account. This allows the user to hide an image in a cell, possibly as part of an autofilter.
1745              
1746             The C<url> option can be use to used to add a hyperlink to an image:
1747              
1748             $worksheet->insert_image( 'A1', 'logo.png',
1749             { url => 'https://github.com/jmcnamara' } );
1750              
1751             The supported url formats are the same as those supported by the C<write_url()> method and the same rules/limits apply.
1752              
1753             The C<tip> option can be use to used to add a mouseover tip to the hyperlink:
1754              
1755             $worksheet->insert_image( 'A1', 'logo.png',
1756             {
1757             url => 'https://github.com/jmcnamara',
1758             tip => 'GitHub'
1759             }
1760             );
1761              
1762             The C<description> parameter can be used to specify a description or "alt text" string for the image. In general this would be used to provide a text description of the image to help accessibility. It is an optional parameter and defaults to the filename of the image. It can be used as follows:
1763              
1764             $worksheet->insert_image( 'E9', 'logo.png',
1765             {description => "This is some alternative text"} );
1766              
1767             The optional C<decorative> parameter is also used to help accessibility. It is used to mark the image as decorative, and thus uninformative, for automated screen readers. As in Excel, if this parameter is in use the C<description> field isn't written. It is used as follows:
1768              
1769             $worksheet->insert_image( 'E9', 'logo.png', {decorative => 1} );
1770              
1771             Note: you must call C<set_row()> or C<set_column()> before C<insert_image()> if you wish to change the default dimensions of any of the rows or columns that the image occupies. The height of a row can also change if you use a font that is larger than the default. This in turn will affect the scaling of your image. To avoid this you should explicitly set the height of the row using C<set_row()> if it contains a font size that will change the row height.
1772              
1773             BMP images must be 24 bit, true colour, bitmaps. In general it is best to avoid BMP images since they aren't compressed.
1774              
1775              
1776              
1777              
1778             =head2 insert_chart( $row, $col, $chart, { %options } )
1779              
1780             This method can be used to insert a Chart object into a worksheet. The Chart must be created by the C<add_chart()> Workbook method and it must have the C<embedded> option set.
1781              
1782             my $chart = $workbook->add_chart( type => 'line', embedded => 1 );
1783              
1784             # Configure the chart.
1785             ...
1786              
1787             # Insert the chart into the a worksheet.
1788             $worksheet->insert_chart( 'E2', $chart );
1789              
1790             See C<add_chart()> for details on how to create the Chart object and L<Excel::Writer::XLSX::Chart> for details on how to configure it. See also the C<chart_*.pl> programs in the examples directory of the distro.
1791              
1792             The optional C<options> hash/hashref parameter can be used to set various options for the chart. The defaults are:
1793              
1794             %options = (
1795             x_offset => 0,
1796             y_offset => 0,
1797             x_scale => 1,
1798             y_scale => 1,
1799             object_position => 1,
1800             );
1801              
1802             The parameters C<x_offset> and C<y_offset> can be used to specify an offset from the top left hand corner of the cell specified by C<$row> and C<$col>. The offset values are in pixels.
1803              
1804             $worksheet1->insert_chart( 'E2', $chart, { x_offset =>10, y_offset => 20 });
1805              
1806             The parameters C<x_scale> and C<y_scale> can be used to scale the inserted chart horizontally and vertically:
1807              
1808             # Scale the width by 120% and the height by 150%
1809             $worksheet->insert_chart( 'E2', $chart, { y_scale => 1.2, y_scale => 1.5 } );
1810              
1811             The positioning of the chart when cells are resized can be set with the C<object_position> parameter:
1812              
1813             $worksheet->insert_chart( 'E2', $chart, { object_position => 2 } );
1814              
1815             The C<object_position> parameter can have one of the following allowable values:
1816              
1817             1. Move and size with cells.
1818             2. Move but don't size with cells.
1819             3. Don't move or size with cells.
1820             4. Same as Option 1, see below.
1821              
1822             Option 4 appears in Excel as Option 1. However, the worksheet object is sized to take hidden rows or columns into account. This is generally only useful for images and not for charts.
1823              
1824              
1825             =head2 insert_shape( $row, $col, $shape, $x, $y, $x_scale, $y_scale )
1826              
1827             This method can be used to insert a Shape object into a worksheet. The Shape must be created by the C<add_shape()> Workbook method.
1828              
1829             my $shape = $workbook->add_shape( name => 'My Shape', type => 'plus' );
1830              
1831             # Configure the shape.
1832             $shape->set_text('foo');
1833             ...
1834              
1835             # Insert the shape into the a worksheet.
1836             $worksheet->insert_shape( 'E2', $shape );
1837              
1838             See C<add_shape()> for details on how to create the Shape object and L<Excel::Writer::XLSX::Shape> for details on how to configure it.
1839              
1840             The C<$x>, C<$y>, C<$x_scale> and C<$y_scale> parameters are optional.
1841              
1842             The parameters C<$x> and C<$y> can be used to specify an offset from the top left hand corner of the cell specified by C<$row> and C<$col>. The offset values are in pixels.
1843              
1844             $worksheet1->insert_shape( 'E2', $chart, 3, 3 );
1845              
1846             The parameters C<$x_scale> and C<$y_scale> can be used to scale the inserted shape horizontally and vertically:
1847              
1848             # Scale the width by 120% and the height by 150%
1849             $worksheet->insert_shape( 'E2', $shape, 0, 0, 1.2, 1.5 );
1850              
1851             See also the C<shape*.pl> programs in the examples directory of the distro.
1852              
1853              
1854              
1855              
1856             =head2 insert_button( $row, $col, { %options })
1857              
1858             The C<insert_button()> method can be used to insert an Excel form button into a worksheet.
1859              
1860             This method is generally only useful when used in conjunction with the Workbook C<add_vba_project()> method to tie the button to a macro from an embedded VBA project:
1861              
1862             my $workbook = Excel::Writer::XLSX->new( 'file.xlsm' );
1863             ...
1864             $workbook->add_vba_project( './vbaProject.bin' );
1865              
1866             $worksheet->insert_button( 'C2', { macro => 'my_macro' } );
1867              
1868             The properties of the button that can be set are:
1869              
1870             macro
1871             caption
1872             width
1873             height
1874             x_scale
1875             y_scale
1876             x_offset
1877             y_offset
1878              
1879              
1880             =over
1881              
1882             =item Option: macro
1883              
1884             This option is used to set the macro that the button will invoke when the user clicks on it. The macro should be included using the Workbook C<add_vba_project()> method shown above.
1885              
1886             $worksheet->insert_button( 'C2', { macro => 'my_macro' } );
1887              
1888             The default macro is C<ButtonX_Click> where X is the button number.
1889              
1890             =item Option: caption
1891              
1892             This option is used to set the caption on the button. The default is C<Button X> where X is the button number.
1893              
1894             $worksheet->insert_button( 'C2', { macro => 'my_macro', caption => 'Hello' } );
1895              
1896             =item Option: width
1897              
1898             This option is used to set the width of the button in pixels.
1899              
1900             $worksheet->insert_button( 'C2', { macro => 'my_macro', width => 128 } );
1901              
1902             The default button width is 64 pixels which is the width of a default cell.
1903              
1904             =item Option: height
1905              
1906             This option is used to set the height of the button in pixels.
1907              
1908             $worksheet->insert_button( 'C2', { macro => 'my_macro', height => 40 } );
1909              
1910             The default button height is 20 pixels which is the height of a default cell.
1911              
1912             =item Option: x_scale
1913              
1914             This option is used to set the width of the button as a factor of the default width.
1915              
1916             $worksheet->insert_button( 'C2', { macro => 'my_macro', x_scale => 2.0 );
1917              
1918             =item Option: y_scale
1919              
1920             This option is used to set the height of the button as a factor of the default height.
1921              
1922             $worksheet->insert_button( 'C2', { macro => 'my_macro', y_scale => 2.0 );
1923              
1924              
1925             =item Option: x_offset
1926              
1927             This option is used to change the x offset, in pixels, of a button within a cell:
1928              
1929             $worksheet->insert_button( 'C2', { macro => 'my_macro', x_offset => 2 );
1930              
1931             =item Option: y_offset
1932              
1933             This option is used to change the y offset, in pixels, of a comment within a cell.
1934              
1935             =back
1936              
1937              
1938             Note: Button is the only Excel form element that is available in Excel::Writer::XLSX. Form elements represent a lot of work to implement and the underlying VML syntax isn't very much fun.
1939              
1940              
1941              
1942              
1943             =head2 data_validation()
1944              
1945             The C<data_validation()> method is used to construct an Excel data validation or to limit the user input to a dropdown list of values.
1946              
1947              
1948             $worksheet->data_validation('B3',
1949             {
1950             validate => 'integer',
1951             criteria => '>',
1952             value => 100,
1953             });
1954              
1955             $worksheet->data_validation('B5:B9',
1956             {
1957             validate => 'list',
1958             value => ['open', 'high', 'close'],
1959             });
1960              
1961             This method contains a lot of parameters and is described in detail in a separate section L</DATA VALIDATION IN EXCEL>.
1962              
1963              
1964             See also the C<data_validate.pl> program in the examples directory of the distro
1965              
1966              
1967              
1968              
1969             =head2 conditional_formatting()
1970              
1971             The C<conditional_formatting()> method is used to add formatting to a cell or range of cells based on user defined criteria.
1972              
1973             $worksheet->conditional_formatting( 'A1:J10',
1974             {
1975             type => 'cell',
1976             criteria => '>=',
1977             value => 50,
1978             format => $format1,
1979             }
1980             );
1981              
1982             This method contains a lot of parameters and is described in detail in a separate section L<CONDITIONAL FORMATTING IN EXCEL>.
1983              
1984             See also the C<conditional_format.pl> program in the examples directory of the distro
1985              
1986              
1987              
1988              
1989             =head2 add_sparkline()
1990              
1991             The C<add_sparkline()> worksheet method is used to add sparklines to a cell or a range of cells.
1992              
1993             $worksheet->add_sparkline(
1994             {
1995             location => 'F2',
1996             range => 'Sheet1!A2:E2',
1997             type => 'column',
1998             style => 12,
1999             }
2000             );
2001              
2002             This method contains a lot of parameters and is described in detail in a separate section L</SPARKLINES IN EXCEL>.
2003              
2004             See also the C<sparklines1.pl> and C<sparklines2.pl> example programs in the C<examples> directory of the distro.
2005              
2006             B<Note:> Sparklines are a feature of Excel 2010+ only. You can write them to an XLSX file that can be read by Excel 2007 but they won't be displayed.
2007              
2008              
2009              
2010              
2011             =head2 add_table()
2012              
2013             The C<add_table()> method is used to group a range of cells into an Excel Table.
2014              
2015             $worksheet->add_table( 'B3:F7', { ... } );
2016              
2017             This method contains a lot of parameters and is described in detail in a separate section L<TABLES IN EXCEL>.
2018              
2019             See also the C<tables.pl> program in the examples directory of the distro
2020              
2021              
2022              
2023             =head2 get_name()
2024              
2025             The C<get_name()> method is used to retrieve the name of a worksheet. For example:
2026              
2027             for my $sheet ( $workbook->sheets() ) {
2028             print $sheet->get_name();
2029             }
2030              
2031             For reasons related to the design of Excel::Writer::XLSX and to the internals of Excel there is no C<set_name()> method. The only way to set the worksheet name is via the C<add_worksheet()> method.
2032              
2033              
2034              
2035              
2036             =head2 activate()
2037              
2038             The C<activate()> method is used to specify which worksheet is initially visible in a multi-sheet workbook:
2039              
2040             $worksheet1 = $workbook->add_worksheet( 'To' );
2041             $worksheet2 = $workbook->add_worksheet( 'the' );
2042             $worksheet3 = $workbook->add_worksheet( 'wind' );
2043              
2044             $worksheet3->activate();
2045              
2046             This is similar to the Excel VBA activate method. More than one worksheet can be selected via the C<select()> method, see below, however only one worksheet can be active.
2047              
2048             The default active worksheet is the first worksheet.
2049              
2050              
2051              
2052              
2053             =head2 select()
2054              
2055             The C<select()> method is used to indicate that a worksheet is selected in a multi-sheet workbook:
2056              
2057             $worksheet1->activate();
2058             $worksheet2->select();
2059             $worksheet3->select();
2060              
2061             A selected worksheet has its tab highlighted. Selecting worksheets is a way of grouping them together so that, for example, several worksheets could be printed in one go. A worksheet that has been activated via the C<activate()> method will also appear as selected.
2062              
2063              
2064              
2065              
2066             =head2 hide()
2067              
2068             The C<hide()> method is used to hide a worksheet:
2069              
2070             $worksheet2->hide();
2071              
2072             You may wish to hide a worksheet in order to avoid confusing a user with intermediate data or calculations.
2073              
2074             A hidden worksheet can not be activated or selected so this method is mutually exclusive with the C<activate()> and C<select()> methods. In addition, since the first worksheet will default to being the active worksheet, you cannot hide the first worksheet without activating another sheet:
2075              
2076             $worksheet2->activate();
2077             $worksheet1->hide();
2078              
2079              
2080              
2081              
2082             =head2 set_first_sheet()
2083              
2084             The C<activate()> method determines which worksheet is initially selected. However, if there are a large number of worksheets the selected worksheet may not appear on the screen. To avoid this you can select which is the leftmost visible worksheet using C<set_first_sheet()>:
2085              
2086             for ( 1 .. 20 ) {
2087             $workbook->add_worksheet;
2088             }
2089              
2090             $worksheet21 = $workbook->add_worksheet();
2091             $worksheet22 = $workbook->add_worksheet();
2092              
2093             $worksheet21->set_first_sheet();
2094             $worksheet22->activate();
2095              
2096             This method is not required very often. The default value is the first worksheet.
2097              
2098              
2099              
2100              
2101             =head2 protect( $password, \%options )
2102              
2103             The C<protect()> method is used to protect a worksheet from modification:
2104              
2105             $worksheet->protect();
2106              
2107             The C<protect()> method also has the effect of enabling a cell's C<locked> and C<hidden> properties if they have been set. A I<locked> cell cannot be edited and this property is on by default for all cells. A I<hidden> cell will display the results of a formula but not the formula itself.
2108              
2109             See the C<protection.pl> program in the examples directory of the distro for an illustrative example and the C<set_locked> and C<set_hidden> format methods in L</CELL FORMATTING>.
2110              
2111             You can optionally add a password to the worksheet protection:
2112              
2113             $worksheet->protect( 'drowssap' );
2114              
2115             Passing the empty string C<''> is the same as turning on protection without a password.
2116              
2117             Note, the worksheet level password in Excel provides very weak protection. It does not encrypt your data and is very easy to deactivate. Full workbook encryption is not supported by C<Excel::Writer::XLSX> since it requires a completely different file format and would take several man months to implement.
2118              
2119             You can specify which worksheet elements you wish to protect by passing a hash_ref with any or all of the following keys:
2120              
2121             # Default shown.
2122             %options = (
2123             objects => 0,
2124             scenarios => 0,
2125             format_cells => 0,
2126             format_columns => 0,
2127             format_rows => 0,
2128             insert_columns => 0,
2129             insert_rows => 0,
2130             insert_hyperlinks => 0,
2131             delete_columns => 0,
2132             delete_rows => 0,
2133             select_locked_cells => 1,
2134             sort => 0,
2135             autofilter => 0,
2136             pivot_tables => 0,
2137             select_unlocked_cells => 1,
2138             );
2139              
2140             The default boolean values are shown above. Individual elements can be protected as follows:
2141              
2142             $worksheet->protect( 'drowssap', { insert_rows => 1 } );
2143              
2144             For chartsheets the allowable options and default values are:
2145              
2146             %options = (
2147             objects => 1,
2148             content => 1,
2149             );
2150              
2151              
2152              
2153              
2154             =head2 unprotect_range( $cell_range, $range_name )
2155              
2156              
2157             The C<unprotect_range()> method is used to unprotect ranges in a protected worksheet. It can be used to set a single range or multiple ranges:
2158              
2159             $worksheet->unprotect_range( 'A1' );
2160             $worksheet->unprotect_range( 'C1' );
2161             $worksheet->unprotect_range( 'E1:E3' );
2162             $worksheet->unprotect_range( 'G1:K100' );
2163              
2164             As in Excel the ranges are given sequential names like C<Range1> and C<Range2> but a user defined name can also be specified:
2165              
2166             $worksheet->unprotect_range( 'G4:I6', 'MyRange' );
2167              
2168              
2169              
2170             =head2 set_selection( $first_row, $first_col, $last_row, $last_col )
2171              
2172             This method can be used to specify which cell or cells are selected in a worksheet. The most common requirement is to select a single cell, in which case C<$last_row> and C<$last_col> can be omitted. The active cell within a selected range is determined by the order in which C<$first> and C<$last> are specified. It is also possible to specify a cell or a range using A1 notation. See the note about L</Cell notation>.
2173              
2174             Examples:
2175              
2176             $worksheet1->set_selection( 3, 3 ); # 1. Cell D4.
2177             $worksheet2->set_selection( 3, 3, 6, 6 ); # 2. Cells D4 to G7.
2178             $worksheet3->set_selection( 6, 6, 3, 3 ); # 3. Cells G7 to D4.
2179             $worksheet4->set_selection( 'D4' ); # Same as 1.
2180             $worksheet5->set_selection( 'D4:G7' ); # Same as 2.
2181             $worksheet6->set_selection( 'G7:D4' ); # Same as 3.
2182              
2183             The default cell selections is (0, 0), 'A1'.
2184              
2185              
2186              
2187              
2188             =head2 set_row( $row, $height, $format, $hidden, $level, $collapsed )
2189              
2190             This method can be used to change the default properties of a row. All parameters apart from C<$row> are optional.
2191              
2192             The most common use for this method is to change the height of a row.
2193              
2194             $worksheet->set_row( 0, 20 ); # Row 1 height set to 20
2195              
2196             Note: the row height is in Excel character units. To set the height in pixels use the C<set_row_pixels()> method, see below.
2197              
2198             If you wish to set the format without changing the height you can pass C<undef> as the height parameter:
2199              
2200             $worksheet->set_row( 0, undef, $format );
2201              
2202             The C<$format> parameter will be applied to any cells in the row that don't have a format. For example
2203              
2204             $worksheet->set_row( 0, undef, $format1 ); # Set the format for row 1
2205             $worksheet->write( 'A1', 'Hello' ); # Defaults to $format1
2206             $worksheet->write( 'B1', 'Hello', $format2 ); # Keeps $format2
2207              
2208             If you wish to define a row format in this way you should call the method before any calls to C<write()>. Calling it afterwards will overwrite any format that was previously specified.
2209              
2210             The C<$hidden> parameter should be set to 1 if you wish to hide a row. This can be used, for example, to hide intermediary steps in a complicated calculation:
2211              
2212             $worksheet->set_row( 0, 20, $format, 1 );
2213             $worksheet->set_row( 1, undef, undef, 1 );
2214              
2215             The C<$level> parameter is used to set the outline level of the row. Outlines are described in L</OUTLINES AND GROUPING IN EXCEL>. Adjacent rows with the same outline level are grouped together into a single outline.
2216              
2217             The following example sets an outline level of 1 for rows 2 and 3 (zero-indexed):
2218              
2219             $worksheet->set_row( 1, undef, undef, 0, 1 );
2220             $worksheet->set_row( 2, undef, undef, 0, 1 );
2221              
2222             The C<$hidden> parameter can also be used to hide collapsed outlined rows when used in conjunction with the C<$level> parameter.
2223              
2224             $worksheet->set_row( 1, undef, undef, 1, 1 );
2225             $worksheet->set_row( 2, undef, undef, 1, 1 );
2226              
2227             For collapsed outlines you should also indicate which row has the collapsed C<+> symbol using the optional C<$collapsed> parameter.
2228              
2229             $worksheet->set_row( 3, undef, undef, 0, 0, 1 );
2230              
2231             For a more complete example see the C<outline.pl> and C<outline_collapsed.pl> programs in the examples directory of the distro.
2232              
2233             Excel allows up to 7 outline levels. Therefore the C<$level> parameter should be in the range C<0 E<lt>= $level E<lt>= 7>.
2234              
2235              
2236              
2237              
2238             =head2 set_row_pixels( $row, $height, $format, $hidden, $level, $collapsed )
2239              
2240             This method is the same as C<set_row()> except that C<$height> is in pixels.
2241              
2242             $worksheet->set_row ( 0, 24 ); # Set row height in character units
2243             $worksheet->set_row_pixels( 1, 18 ); # Set row to same height in pixels
2244              
2245              
2246              
2247              
2248             =head2 set_column( $first_col, $last_col, $width, $format, $hidden, $level, $collapsed )
2249              
2250             This method can be used to change the default properties of a single column or a range of columns. All parameters apart from C<$first_col> and C<$last_col> are optional.
2251              
2252             If C<set_column()> is applied to a single column the value of C<$first_col> and C<$last_col> should be the same. In the case where C<$last_col> is zero it is set to the same value as C<$first_col>.
2253              
2254             It is also possible, and generally clearer, to specify a column range using the form of A1 notation used for columns. See the note about L</Cell notation>.
2255              
2256             Examples:
2257              
2258             $worksheet->set_column( 0, 0, 20 ); # Column A width set to 20
2259             $worksheet->set_column( 1, 3, 30 ); # Columns B-D width set to 30
2260             $worksheet->set_column( 'E:E', 20 ); # Column E width set to 20
2261             $worksheet->set_column( 'F:H', 30 ); # Columns F-H width set to 30
2262              
2263             The width corresponds to the column width value that is specified in Excel. It is approximately equal to the length of a string in the default font of Calibri 11. To set the width in pixels use the C<set_column_pixels()> method, see below.
2264              
2265             Unfortunately, there is no way to specify "AutoFit" for a column in the Excel file format. This feature is only available at runtime from within Excel.
2266              
2267             As usual the C<$format> parameter is optional, for additional information, see L</CELL FORMATTING>. If you wish to set the format without changing the width you can pass C<undef> as the width parameter:
2268              
2269             $worksheet->set_column( 0, 0, undef, $format );
2270              
2271             The C<$format> parameter will be applied to any cells in the column that don't have a format. For example
2272              
2273             $worksheet->set_column( 'A:A', undef, $format1 ); # Set format for col 1
2274             $worksheet->write( 'A1', 'Hello' ); # Defaults to $format1
2275             $worksheet->write( 'A2', 'Hello', $format2 ); # Keeps $format2
2276              
2277             If you wish to define a column format in this way you should call the method before any calls to C<write()>. If you call it afterwards it won't have any effect.
2278              
2279             A default row format takes precedence over a default column format
2280              
2281             $worksheet->set_row( 0, undef, $format1 ); # Set format for row 1
2282             $worksheet->set_column( 'A:A', undef, $format2 ); # Set format for col 1
2283             $worksheet->write( 'A1', 'Hello' ); # Defaults to $format1
2284             $worksheet->write( 'A2', 'Hello' ); # Defaults to $format2
2285              
2286             The C<$hidden> parameter should be set to 1 if you wish to hide a column. This can be used, for example, to hide intermediary steps in a complicated calculation:
2287              
2288             $worksheet->set_column( 'D:D', 20, $format, 1 );
2289             $worksheet->set_column( 'E:E', undef, undef, 1 );
2290              
2291             The C<$level> parameter is used to set the outline level of the column. Outlines are described in L</OUTLINES AND GROUPING IN EXCEL>. Adjacent columns with the same outline level are grouped together into a single outline.
2292              
2293             The following example sets an outline level of 1 for columns B to G:
2294              
2295             $worksheet->set_column( 'B:G', undef, undef, 0, 1 );
2296              
2297             The C<$hidden> parameter can also be used to hide collapsed outlined columns when used in conjunction with the C<$level> parameter.
2298              
2299             $worksheet->set_column( 'B:G', undef, undef, 1, 1 );
2300              
2301             For collapsed outlines you should also indicate which row has the collapsed C<+> symbol using the optional C<$collapsed> parameter.
2302              
2303             $worksheet->set_column( 'H:H', undef, undef, 0, 0, 1 );
2304              
2305             For a more complete example see the C<outline.pl> and C<outline_collapsed.pl> programs in the examples directory of the distro.
2306              
2307             Excel allows up to 7 outline levels. Therefore the C<$level> parameter should be in the range C<0 E<lt>= $level E<lt>= 7>.
2308              
2309              
2310              
2311              
2312             =head2 set_column_pixels( $first_col, $last_col, $width, $format, $hidden, $level, $collapsed )
2313              
2314             This method is the same as C<set_column()> except that C<$width> is in pixels.
2315              
2316             $worksheet->set_column( 0, 0, 10 ); # Column A width set to 20 in character units
2317             $worksheet->set_column( 1, 1, 75 ); # Column B set to the same width in pixels
2318              
2319              
2320              
2321              
2322             =head2 set_default_row( $height, $hide_unused_rows )
2323              
2324             The C<set_default_row()> method is used to set the limited number of default row properties allowed by Excel. These are the default height and the option to hide unused rows.
2325              
2326             $worksheet->set_default_row( 24 ); # Set the default row height to 24.
2327              
2328             The option to hide unused rows is used by Excel as an optimisation so that the user can hide a large number of rows without generating a very large file with an entry for each hidden row.
2329              
2330             $worksheet->set_default_row( undef, 1 );
2331              
2332             See the C<hide_row_col.pl> example program.
2333              
2334              
2335              
2336              
2337             =head2 outline_settings( $visible, $symbols_below, $symbols_right, $auto_style )
2338              
2339             The C<outline_settings()> method is used to control the appearance of outlines in Excel. Outlines are described in L</OUTLINES AND GROUPING IN EXCEL>.
2340              
2341             The C<$visible> parameter is used to control whether or not outlines are visible. Setting this parameter to 0 will cause all outlines on the worksheet to be hidden. They can be unhidden in Excel by means of the "Show Outline Symbols" command button. The default setting is 1 for visible outlines.
2342              
2343             $worksheet->outline_settings( 0 );
2344              
2345             The C<$symbols_below> parameter is used to control whether the row outline symbol will appear above or below the outline level bar. The default setting is 1 for symbols to appear below the outline level bar.
2346              
2347             The C<$symbols_right> parameter is used to control whether the column outline symbol will appear to the left or the right of the outline level bar. The default setting is 1 for symbols to appear to the right of the outline level bar.
2348              
2349             The C<$auto_style> parameter is used to control whether the automatic outline generator in Excel uses automatic styles when creating an outline. This has no effect on a file generated by C<Excel::Writer::XLSX> but it does have an effect on how the worksheet behaves after it is created. The default setting is 0 for "Automatic Styles" to be turned off.
2350              
2351             The default settings for all of these parameters correspond to Excel's default parameters.
2352              
2353              
2354             The worksheet parameters controlled by C<outline_settings()> are rarely used.
2355              
2356              
2357              
2358              
2359             =head2 freeze_panes( $row, $col, $top_row, $left_col )
2360              
2361             This method can be used to divide a worksheet into horizontal or vertical regions known as panes and to also "freeze" these panes so that the splitter bars are not visible. This is the same as the C<Window-E<gt>Freeze Panes> menu command in Excel
2362              
2363             The parameters C<$row> and C<$col> are used to specify the location of the split. It should be noted that the split is specified at the top or left of a cell and that the method uses zero based indexing. Therefore to freeze the first row of a worksheet it is necessary to specify the split at row 2 (which is 1 as the zero-based index). This might lead you to think that you are using a 1 based index but this is not the case.
2364              
2365             You can set one of the C<$row> and C<$col> parameters as zero if you do not want either a vertical or horizontal split.
2366              
2367             Examples:
2368              
2369             $worksheet->freeze_panes( 1, 0 ); # Freeze the first row
2370             $worksheet->freeze_panes( 'A2' ); # Same using A1 notation
2371             $worksheet->freeze_panes( 0, 1 ); # Freeze the first column
2372             $worksheet->freeze_panes( 'B1' ); # Same using A1 notation
2373             $worksheet->freeze_panes( 1, 2 ); # Freeze first row and first 2 columns
2374             $worksheet->freeze_panes( 'C2' ); # Same using A1 notation
2375              
2376             The parameters C<$top_row> and C<$left_col> are optional. They are used to specify the top-most or left-most visible row or column in the scrolling region of the panes. For example to freeze the first row and to have the scrolling region begin at row twenty:
2377              
2378             $worksheet->freeze_panes( 1, 0, 20, 0 );
2379              
2380             You cannot use A1 notation for the C<$top_row> and C<$left_col> parameters.
2381              
2382              
2383             See also the C<panes.pl> program in the C<examples> directory of the distribution.
2384              
2385              
2386              
2387              
2388             =head2 split_panes( $y, $x, $top_row, $left_col )
2389              
2390              
2391             This method can be used to divide a worksheet into horizontal or vertical regions known as panes. This method is different from the C<freeze_panes()> method in that the splits between the panes will be visible to the user and each pane will have its own scroll bars.
2392              
2393             The parameters C<$y> and C<$x> are used to specify the vertical and horizontal position of the split. The units for C<$y> and C<$x> are the same as those used by Excel to specify row height and column width. However, the vertical and horizontal units are different from each other. Therefore you must specify the C<$y> and C<$x> parameters in terms of the row heights and column widths that you have set or the default values which are C<15> for a row and C<8.43> for a column.
2394              
2395             You can set one of the C<$y> and C<$x> parameters as zero if you do not want either a vertical or horizontal split. The parameters C<$top_row> and C<$left_col> are optional. They are used to specify the top-most or left-most visible row or column in the bottom-right pane.
2396              
2397             Example:
2398              
2399             $worksheet->split_panes( 15, 0, ); # First row
2400             $worksheet->split_panes( 0, 8.43 ); # First column
2401             $worksheet->split_panes( 15, 8.43 ); # First row and column
2402              
2403             You cannot use A1 notation with this method.
2404              
2405             See also the C<freeze_panes()> method and the C<panes.pl> program in the C<examples> directory of the distribution.
2406              
2407              
2408              
2409              
2410             =head2 merge_range( $first_row, $first_col, $last_row, $last_col, $token, $format )
2411              
2412             The C<merge_range()> method allows you to merge cells that contain other types of alignment in addition to the merging:
2413              
2414             my $format = $workbook->add_format(
2415             border => 6,
2416             valign => 'vcenter',
2417             align => 'center',
2418             );
2419              
2420             $worksheet->merge_range( 'B3:D4', 'Vertical and horizontal', $format );
2421              
2422             C<merge_range()> writes its C<$token> argument using the worksheet C<write()> method. Therefore it will handle numbers, strings, formulas or urls as required. If you need to specify the required C<write_*()> method use the C<merge_range_type()> method, see below.
2423              
2424             The full possibilities of this method are shown in the C<merge3.pl> to C<merge6.pl> programs in the C<examples> directory of the distribution.
2425              
2426              
2427              
2428              
2429             =head2 merge_range_type( $type, $first_row, $first_col, $last_row, $last_col, ... )
2430              
2431             The C<merge_range()> method, see above, uses C<write()> to insert the required data into to a merged range. However, there may be times where this isn't what you require so as an alternative the C<merge_range_type ()> method allows you to specify the type of data you wish to write. For example:
2432              
2433             $worksheet->merge_range_type( 'number', 'B2:C2', 123, $format1 );
2434             $worksheet->merge_range_type( 'string', 'B4:C4', 'foo', $format2 );
2435             $worksheet->merge_range_type( 'formula', 'B6:C6', '=1+2', $format3 );
2436              
2437             The C<$type> must be one of the following, which corresponds to a C<write_*()> method:
2438              
2439             'number'
2440             'string'
2441             'formula'
2442             'array_formula'
2443             'blank'
2444             'rich_string'
2445             'date_time'
2446             'url'
2447              
2448             Any arguments after the range should be whatever the appropriate method accepts:
2449              
2450             $worksheet->merge_range_type( 'rich_string', 'B8:C8',
2451             'This is ', $bold, 'bold', $format4 );
2452              
2453             Note, you must always pass a C<$format> object as an argument, even if it is a default format.
2454              
2455              
2456              
2457              
2458             =head2 set_zoom( $scale )
2459              
2460             Set the worksheet zoom factor in the range C<10 E<lt>= $scale E<lt>= 400>:
2461              
2462             $worksheet1->set_zoom( 50 );
2463             $worksheet2->set_zoom( 75 );
2464             $worksheet3->set_zoom( 300 );
2465             $worksheet4->set_zoom( 400 );
2466              
2467             The default zoom factor is 100. You cannot zoom to "Selection" because it is calculated by Excel at run-time.
2468              
2469             Note, C<set_zoom()> does not affect the scale of the printed page. For that you should use C<set_print_scale()>.
2470              
2471              
2472              
2473              
2474             =head2 right_to_left()
2475              
2476             The C<right_to_left()> method is used to change the default direction of the worksheet from left-to-right, with the A1 cell in the top left, to right-to-left, with the A1 cell in the top right.
2477              
2478             $worksheet->right_to_left();
2479              
2480             This is useful when creating Arabic, Hebrew or other near or far eastern worksheets that use right-to-left as the default direction.
2481              
2482              
2483              
2484              
2485             =head2 hide_zero()
2486              
2487             The C<hide_zero()> method is used to hide any zero values that appear in cells.
2488              
2489             $worksheet->hide_zero();
2490              
2491             In Excel this option is found under Tools->Options->View.
2492              
2493              
2494              
2495              
2496             =head2 set_background( $filename )
2497              
2498              
2499             The C<set_background()> method can be used to set the background image for the
2500             worksheet:
2501              
2502             $worksheet->set_background( 'logo.png' )
2503              
2504             The C<set_background()> method supports all the image formats supported by
2505             C<insert_image()>.
2506              
2507             Some people use this method to add a watermark background to their
2508             document. However, Microsoft recommends using a header image L<to set a
2509             watermark|https://support.microsoft.com/en-us/office/add-a-watermark-in-excel-a372182a-d733-484e-825c-18ddf3edf009>. The
2510             choice of method depends on whether you want the watermark to be visible in
2511             normal viewing mode or just when the file is printed. In Excel::Writer::XLSX
2512             you can get the header watermark effect using C<set_header()>:
2513              
2514             $worksheet->set_header( '&C&G', undef, { image_center => 'watermark.png' } )
2515              
2516              
2517              
2518             =head2 set_tab_color()
2519              
2520             The C<set_tab_color()> method is used to change the colour of the worksheet tab. You can use one of the standard colour names provided by the Format object or a Html style C<#RRGGBB> colour. See L</WORKING WITH COLOURS>.
2521              
2522             $worksheet1->set_tab_color( 'red' );
2523             $worksheet2->set_tab_color( '#FF6600' );
2524              
2525             See the C<tab_colors.pl> program in the examples directory of the distro.
2526              
2527              
2528              
2529              
2530             =head2 autofilter( $first_row, $first_col, $last_row, $last_col )
2531              
2532             This method allows an autofilter to be added to a worksheet. An autofilter is a way of adding drop down lists to the headers of a 2D range of worksheet data. This allows users to filter the data based on simple criteria so that some data is shown and some is hidden.
2533              
2534             To add an autofilter to a worksheet:
2535              
2536             $worksheet->autofilter( 0, 0, 10, 3 );
2537             $worksheet->autofilter( 'A1:D11' ); # Same as above in A1 notation.
2538              
2539             Filter conditions can be applied using the C<filter_column()> or C<filter_column_list()> method.
2540              
2541             See the C<autofilter.pl> program in the examples directory of the distro for a more detailed example.
2542              
2543              
2544              
2545              
2546             =head2 filter_column( $column, $expression )
2547              
2548             The C<filter_column> method can be used to filter columns in a autofilter range based on simple conditions.
2549              
2550             B<NOTE:> It isn't sufficient to just specify the filter condition. You must also hide any rows that don't match the filter condition. Rows are hidden using the C<set_row()> C<visible> parameter. C<Excel::Writer::XLSX> cannot do this automatically since it isn't part of the file format. See the C<autofilter.pl> program in the examples directory of the distro for an example.
2551              
2552             The conditions for the filter are specified using simple expressions:
2553              
2554             $worksheet->filter_column( 'A', 'x > 2000' );
2555             $worksheet->filter_column( 'B', 'x > 2000 and x < 5000' );
2556              
2557             The C<$column> parameter can either be a zero indexed column number or a string column name.
2558              
2559             The following operators are available:
2560              
2561             Operator Synonyms
2562             == = eq =~
2563             != <> ne !=
2564             >
2565             <
2566             >=
2567             <=
2568              
2569             and &&
2570             or ||
2571              
2572             The operator synonyms are just syntactic sugar to make you more comfortable using the expressions. It is important to remember that the expressions will be interpreted by Excel and not by perl.
2573              
2574             An expression can comprise a single statement or two statements separated by the C<and> and C<or> operators. For example:
2575              
2576             'x < 2000'
2577             'x > 2000'
2578             'x == 2000'
2579             'x > 2000 and x < 5000'
2580             'x == 2000 or x == 5000'
2581              
2582             Filtering of blank or non-blank data can be achieved by using a value of C<Blanks> or C<NonBlanks> in the expression:
2583              
2584             'x == Blanks'
2585             'x == NonBlanks'
2586              
2587             Excel also allows some simple string matching operations:
2588              
2589             'x =~ b*' # begins with b
2590             'x !~ b*' # doesn't begin with b
2591             'x =~ *b' # ends with b
2592             'x !~ *b' # doesn't end with b
2593             'x =~ *b*' # contains b
2594             'x !~ *b*' # doesn't contains b
2595              
2596             You can also use C<*> to match any character or number and C<?> to match any single character or number. No other regular expression quantifier is supported by Excel's filters. Excel's regular expression characters can be escaped using C<~>.
2597              
2598             The placeholder variable C<x> in the above examples can be replaced by any simple string. The actual placeholder name is ignored internally so the following are all equivalent:
2599              
2600             'x < 2000'
2601             'col < 2000'
2602             'Price < 2000'
2603              
2604             Also, note that a filter condition can only be applied to a column in a range specified by the C<autofilter()> Worksheet method.
2605              
2606             See the C<autofilter.pl> program in the examples directory of the distro for a more detailed example.
2607              
2608             B<Note> L<Spreadsheet::WriteExcel> supports Top 10 style filters. These aren't currently supported by Excel::Writer::XLSX but may be added later.
2609              
2610              
2611             =head2 filter_column_list( $column, @matches )
2612              
2613             Prior to Excel 2007 it was only possible to have either 1 or 2 filter conditions such as the ones shown above in the C<filter_column> method.
2614              
2615             Excel 2007 introduced a new list style filter where it is possible to specify 1 or more 'or' style criteria. For example if your column contained data for the first six months the initial data would be displayed as all selected as shown on the left. Then if you selected 'March', 'April' and 'May' they would be displayed as shown on the right.
2616              
2617             No criteria selected Some criteria selected.
2618              
2619             [/] (Select all) [X] (Select all)
2620             [/] January [ ] January
2621             [/] February [ ] February
2622             [/] March [/] March
2623             [/] April [/] April
2624             [/] May [/] May
2625             [/] June [ ] June
2626              
2627             The C<filter_column_list()> method can be used to represent these types of filters:
2628              
2629             $worksheet->filter_column_list( 'A', 'March', 'April', 'May' );
2630              
2631             The C<$column> parameter can either be a zero indexed column number or a string column name.
2632              
2633             One or more criteria can be selected:
2634              
2635             $worksheet->filter_column_list( 0, 'March' );
2636             $worksheet->filter_column_list( 1, 100, 110, 120, 130 );
2637              
2638             B<NOTE:> It isn't sufficient to just specify the filter condition. You must also hide any rows that don't match the filter condition. Rows are hidden using the C<set_row()> C<visible> parameter. C<Excel::Writer::XLSX> cannot do this automatically since it isn't part of the file format. See the C<autofilter.pl> program in the examples directory of the distro for an example.
2639              
2640              
2641              
2642             =head2 convert_date_time( $date_string )
2643              
2644             The C<convert_date_time()> method is used internally by the C<write_date_time()> method to convert date strings to a number that represents an Excel date and time.
2645              
2646             It is exposed as a public method for utility purposes.
2647              
2648             The C<$date_string> format is detailed in the C<write_date_time()> method.
2649              
2650              
2651              
2652             =head2 set_vba_name()
2653              
2654             The Worksheet C<set_vba_name()> method can be used to set the VBA codename for the worksheet (there is a similar method for the workbook VBA name). This is sometimes required when a C<vbaProject> macro included via C<add_vba_project()> refers to the worksheet. The default Excel VBA name of C<Sheet1>, etc., is used if a user defined name isn't specified.
2655              
2656             See also L<WORKING WITH VBA MACROS>.
2657              
2658              
2659              
2660             =head2 ignore_errors()
2661              
2662             The C<ignore_errors()> method can be used to ignore various worksheet cell errors/warnings. For example the following code writes a string that looks like a number:
2663              
2664             $worksheet->write_string('D2', '123');
2665              
2666             This causes Excel to display a small green triangle in the top left hand corner of the cell to indicate an error/warning.
2667              
2668             Sometimes these warnings are useful indicators that there is an issue in the spreadsheet but sometimes it is preferable to turn them off. Warnings can be turned off at the Excel level for all workbooks and worksheets by using the using "Excel options -> Formulas -> Error checking rules". Alternatively you can turn them off for individual cells in a worksheet, or ranges of cells, using the C<ignore_errors()> method with a hashref of options and ranges like this:
2669              
2670             $worksheet->ignore_errors({number_stored_as_text => 'A1:H50'});
2671              
2672             # Or for more than one option:
2673             $worksheet->ignore_errors({number_stored_as_text => 'A1:H50',
2674             eval_error => 'A1:H50'});
2675              
2676             The range can be a single cell, a range of cells, or multiple cells and ranges separated by spaces:
2677              
2678             # Single cell.
2679             $worksheet->ignore_errors({eval_error => 'C6'});
2680              
2681             # Or a single range:
2682             $worksheet->ignore_errors({eval_error => 'C6:G8'});
2683              
2684             # Or multiple cells and ranges:
2685             $worksheet->ignore_errors({eval_error => 'C6 E6 G1:G20 J2:J6'});
2686              
2687             Note: calling C<ignore_errors> multiple times will overwrite the previous settings.
2688              
2689             You can turn off warnings for an entire column by specifying the range from the first cell in the column to the last cell in the column:
2690              
2691             $worksheet->ignore_errors({number_stored_as_text => 'A1:A1048576'});
2692              
2693             Or for the entire worksheet by specifying the range from the first cell in the worksheet to the last cell in the worksheet:
2694              
2695             $worksheet->ignore_errors({number_stored_as_text => 'A1:XFD1048576'});
2696              
2697             The worksheet errors/warnings that can be ignored are:
2698              
2699             =over
2700              
2701             =item * C<number_stored_as_text>: Turn off errors/warnings for numbers stores as text.
2702              
2703             =item * C<eval_error>: Turn off errors/warnings for formula errors (such as divide by zero).
2704              
2705             =item * C<formula_differs>: Turn off errors/warnings for formulas that differ from surrounding formulas.
2706              
2707             =item * C<formula_range>: Turn off errors/warnings for formulas that omit cells in a range.
2708              
2709             =item * C<formula_unlocked>: Turn off errors/warnings for unlocked cells that contain formulas.
2710              
2711             =item * C<empty_cell_reference>: Turn off errors/warnings for formulas that refer to empty cells.
2712              
2713             =item * C<list_data_validation>: Turn off errors/warnings for cells in a table that do not comply with applicable data validation rules.
2714              
2715             =item * C<calculated_column>: Turn off errors/warnings for cell formulas that differ from the column formula.
2716              
2717             =item * C<two_digit_text_year>: Turn off errors/warnings for formulas that contain a two digit text representation of a year.
2718              
2719             =back
2720              
2721              
2722             =head1 PAGE SET-UP METHODS
2723              
2724             Page set-up methods affect the way that a worksheet looks when it is printed. They control features such as page headers and footers and margins. These methods are really just standard worksheet methods. They are documented here in a separate section for the sake of clarity.
2725              
2726             The following methods are available for page set-up:
2727              
2728             set_landscape()
2729             set_portrait()
2730             set_page_view()
2731             set_paper()
2732             center_horizontally()
2733             center_vertically()
2734             set_margins()
2735             set_header()
2736             set_footer()
2737             repeat_rows()
2738             repeat_columns()
2739             hide_gridlines()
2740             print_row_col_headers()
2741             print_area()
2742             print_across()
2743             fit_to_pages()
2744             set_start_page()
2745             set_print_scale()
2746             print_black_and_white()
2747             set_h_pagebreaks()
2748             set_v_pagebreaks()
2749              
2750             A common requirement when working with Excel::Writer::XLSX is to apply the same page set-up features to all of the worksheets in a workbook. To do this you can use the C<sheets()> method of the C<workbook> class to access the array of worksheets in a workbook:
2751              
2752             for $worksheet ( $workbook->sheets() ) {
2753             $worksheet->set_landscape();
2754             }
2755              
2756              
2757              
2758              
2759             =head2 set_landscape()
2760              
2761             This method is used to set the orientation of a worksheet's printed page to landscape:
2762              
2763             $worksheet->set_landscape(); # Landscape mode
2764              
2765              
2766              
2767              
2768             =head2 set_portrait()
2769              
2770             This method is used to set the orientation of a worksheet's printed page to portrait. The default worksheet orientation is portrait, so you won't generally need to call this method.
2771              
2772             $worksheet->set_portrait(); # Portrait mode
2773              
2774              
2775              
2776             =head2 set_page_view()
2777              
2778             This method is used to display the worksheet in "Page View/Layout" mode.
2779              
2780             $worksheet->set_page_view();
2781              
2782              
2783              
2784             =head2 set_paper( $index )
2785              
2786             This method is used to set the paper format for the printed output of a worksheet. The following paper styles are available:
2787              
2788             Index Paper format Paper size
2789             ===== ============ ==========
2790             0 Printer default -
2791             1 Letter 8 1/2 x 11 in
2792             2 Letter Small 8 1/2 x 11 in
2793             3 Tabloid 11 x 17 in
2794             4 Ledger 17 x 11 in
2795             5 Legal 8 1/2 x 14 in
2796             6 Statement 5 1/2 x 8 1/2 in
2797             7 Executive 7 1/4 x 10 1/2 in
2798             8 A3 297 x 420 mm
2799             9 A4 210 x 297 mm
2800             10 A4 Small 210 x 297 mm
2801             11 A5 148 x 210 mm
2802             12 B4 250 x 354 mm
2803             13 B5 182 x 257 mm
2804             14 Folio 8 1/2 x 13 in
2805             15 Quarto 215 x 275 mm
2806             16 - 10x14 in
2807             17 - 11x17 in
2808             18 Note 8 1/2 x 11 in
2809             19 Envelope 9 3 7/8 x 8 7/8
2810             20 Envelope 10 4 1/8 x 9 1/2
2811             21 Envelope 11 4 1/2 x 10 3/8
2812             22 Envelope 12 4 3/4 x 11
2813             23 Envelope 14 5 x 11 1/2
2814             24 C size sheet -
2815             25 D size sheet -
2816             26 E size sheet -
2817             27 Envelope DL 110 x 220 mm
2818             28 Envelope C3 324 x 458 mm
2819             29 Envelope C4 229 x 324 mm
2820             30 Envelope C5 162 x 229 mm
2821             31 Envelope C6 114 x 162 mm
2822             32 Envelope C65 114 x 229 mm
2823             33 Envelope B4 250 x 353 mm
2824             34 Envelope B5 176 x 250 mm
2825             35 Envelope B6 176 x 125 mm
2826             36 Envelope 110 x 230 mm
2827             37 Monarch 3.875 x 7.5 in
2828             38 Envelope 3 5/8 x 6 1/2 in
2829             39 Fanfold 14 7/8 x 11 in
2830             40 German Std Fanfold 8 1/2 x 12 in
2831             41 German Legal Fanfold 8 1/2 x 13 in
2832              
2833              
2834             Note, it is likely that not all of these paper types will be available to the end user since it will depend on the paper formats that the user's printer supports. Therefore, it is best to stick to standard paper types.
2835              
2836             $worksheet->set_paper( 1 ); # US Letter
2837             $worksheet->set_paper( 9 ); # A4
2838              
2839             If you do not specify a paper type the worksheet will print using the printer's default paper.
2840              
2841              
2842              
2843              
2844             =head2 center_horizontally()
2845              
2846             Center the worksheet data horizontally between the margins on the printed page:
2847              
2848             $worksheet->center_horizontally();
2849              
2850              
2851              
2852              
2853             =head2 center_vertically()
2854              
2855             Center the worksheet data vertically between the margins on the printed page:
2856              
2857             $worksheet->center_vertically();
2858              
2859              
2860              
2861              
2862             =head2 set_margins( $inches )
2863              
2864             There are several methods available for setting the worksheet margins on the printed page:
2865              
2866             set_margins() # Set all margins to the same value
2867             set_margins_LR() # Set left and right margins to the same value
2868             set_margins_TB() # Set top and bottom margins to the same value
2869             set_margin_left(); # Set left margin
2870             set_margin_right(); # Set right margin
2871             set_margin_top(); # Set top margin
2872             set_margin_bottom(); # Set bottom margin
2873              
2874             All of these methods take a distance in inches as a parameter. Note: 1 inch = 25.4mm. C<;-)> The default left and right margin is 0.7 inch. The default top and bottom margin is 0.75 inch. Note, these defaults are different from the defaults used in the binary file format by Spreadsheet::WriteExcel.
2875              
2876              
2877              
2878             =head2 set_header( $string, $margin )
2879              
2880             Headers and footers are generated using a C<$string> which is a combination of plain text and control characters. The C<$margin> parameter is optional.
2881              
2882             The available control character are:
2883              
2884             Control Category Description
2885             ======= ======== ===========
2886             &L Justification Left
2887             &C Center
2888             &R Right
2889              
2890             &P Information Page number
2891             &N Total number of pages
2892             &D Date
2893             &T Time
2894             &F File name
2895             &A Worksheet name
2896             &Z Workbook path
2897              
2898             &fontsize Font Font size
2899             &"font,style" Font name and style
2900             &U Single underline
2901             &E Double underline
2902             &S Strikethrough
2903             &X Superscript
2904             &Y Subscript
2905              
2906             &[Picture] Images Image placeholder
2907             &G Same as &[Picture]
2908              
2909             && Miscellaneous Literal ampersand &
2910              
2911              
2912             Text in headers and footers can be justified (aligned) to the left, center and right by prefixing the text with the control characters C<&L>, C<&C> and C<&R>.
2913              
2914             For example (with ASCII art representation of the results):
2915              
2916             $worksheet->set_header('&LHello');
2917              
2918             ---------------------------------------------------------------
2919             | |
2920             | Hello |
2921             | |
2922              
2923              
2924             $worksheet->set_header('&CHello');
2925              
2926             ---------------------------------------------------------------
2927             | |
2928             | Hello |
2929             | |
2930              
2931              
2932             $worksheet->set_header('&RHello');
2933              
2934             ---------------------------------------------------------------
2935             | |
2936             | Hello |
2937             | |
2938              
2939              
2940             For simple text, if you do not specify any justification the text will be centred. However, you must prefix the text with C<&C> if you specify a font name or any other formatting:
2941              
2942             $worksheet->set_header('Hello');
2943              
2944             ---------------------------------------------------------------
2945             | |
2946             | Hello |
2947             | |
2948              
2949              
2950             You can have text in each of the justification regions:
2951              
2952             $worksheet->set_header('&LCiao&CBello&RCielo');
2953              
2954             ---------------------------------------------------------------
2955             | |
2956             | Ciao Bello Cielo |
2957             | |
2958              
2959              
2960             The information control characters act as variables that Excel will update as the workbook or worksheet changes. Times and dates are in the users default format:
2961              
2962             $worksheet->set_header('&CPage &P of &N');
2963              
2964             ---------------------------------------------------------------
2965             | |
2966             | Page 1 of 6 |
2967             | |
2968              
2969              
2970             $worksheet->set_header('&CUpdated at &T');
2971              
2972             ---------------------------------------------------------------
2973             | |
2974             | Updated at 12:30 PM |
2975             | |
2976              
2977              
2978             Images can be inserted using the options shown below. Each image must have a placeholder in header string using the C<&[Picture]> or C<&G> control characters:
2979              
2980             $worksheet->set_header( '&L&G', 0.3, { image_left => 'logo.jpg' });
2981              
2982              
2983              
2984             You can specify the font size of a section of the text by prefixing it with the control character C<&n> where C<n> is the font size:
2985              
2986             $worksheet1->set_header( '&C&30Hello Big' );
2987             $worksheet2->set_header( '&C&10Hello Small' );
2988              
2989             You can specify the font of a section of the text by prefixing it with the control sequence C<&"font,style"> where C<fontname> is a font name such as "Courier New" or "Times New Roman" and C<style> is one of the standard Windows font descriptions: "Regular", "Italic", "Bold" or "Bold Italic":
2990              
2991             $worksheet1->set_header( '&C&"Courier New,Italic"Hello' );
2992             $worksheet2->set_header( '&C&"Courier New,Bold Italic"Hello' );
2993             $worksheet3->set_header( '&C&"Times New Roman,Regular"Hello' );
2994              
2995             It is possible to combine all of these features together to create sophisticated headers and footers. As an aid to setting up complicated headers and footers you can record a page set-up as a macro in Excel and look at the format strings that VBA produces. Remember however that VBA uses two double quotes C<""> to indicate a single double quote. For the last example above the equivalent VBA code looks like this:
2996              
2997             .LeftHeader = ""
2998             .CenterHeader = "&""Times New Roman,Regular""Hello"
2999             .RightHeader = ""
3000              
3001              
3002             To include a single literal ampersand C<&> in a header or footer you should use a double ampersand C<&&>:
3003              
3004             $worksheet1->set_header('&CCuriouser && Curiouser - Attorneys at Law');
3005              
3006             As stated above the margin parameter is optional. As with the other margins the value should be in inches. The default header and footer margin is 0.3 inch. Note, the default margin is different from the default used in the binary file format by Spreadsheet::WriteExcel. The header and footer margin size can be set as follows:
3007              
3008             $worksheet->set_header( '&CHello', 0.75 );
3009              
3010             The header and footer margins are independent of the top and bottom margins.
3011              
3012             The available options are:
3013              
3014             =over
3015              
3016             =item * C<image_left> The path to the image. Requires a C<&G> or C<&[Picture]> placeholder.
3017              
3018             =item * C<image_center> Same as above.
3019              
3020             =item * C<image_right> Same as above.
3021              
3022             =item * C<scale_with_doc> Scale header with document. Defaults to true.
3023              
3024             =item * C<align_with_margins> Align header to margins. Defaults to true.
3025              
3026             =back
3027              
3028             The image options must have an accompanying C<&[Picture]> or C<&G> control
3029             character in the header string:
3030              
3031             $worksheet->set_header(
3032             '&L&[Picture]&C&[Picture]&R&[Picture]',
3033             undef, # If you don't want to change the margin.
3034             {
3035             image_left => 'red.jpg',
3036             image_center => 'blue.jpg',
3037             image_right => 'yellow.jpg'
3038             }
3039             );
3040              
3041              
3042             Note, the header or footer string must be less than 255 characters. Strings longer than this will not be written and a warning will be generated.
3043              
3044             The C<set_header()> method can also handle Unicode strings in C<UTF-8> format.
3045              
3046             $worksheet->set_header( "&C\x{263a}" )
3047              
3048              
3049             See, also the C<headers.pl> program in the C<examples> directory of the distribution.
3050              
3051              
3052              
3053              
3054             =head2 set_footer( $string, $margin )
3055              
3056             The syntax of the C<set_footer()> method is the same as C<set_header()>, see above.
3057              
3058              
3059              
3060              
3061             =head2 repeat_rows( $first_row, $last_row )
3062              
3063             Set the number of rows to repeat at the top of each printed page.
3064              
3065             For large Excel documents it is often desirable to have the first row or rows of the worksheet print out at the top of each page. This can be achieved by using the C<repeat_rows()> method. The parameters C<$first_row> and C<$last_row> are zero based. The C<$last_row> parameter is optional if you only wish to specify one row:
3066              
3067             $worksheet1->repeat_rows( 0 ); # Repeat the first row
3068             $worksheet2->repeat_rows( 0, 1 ); # Repeat the first two rows
3069              
3070              
3071              
3072              
3073             =head2 repeat_columns( $first_col, $last_col )
3074              
3075             Set the columns to repeat at the left hand side of each printed page.
3076              
3077             For large Excel documents it is often desirable to have the first column or columns of the worksheet print out at the left hand side of each page. This can be achieved by using the C<repeat_columns()> method. The parameters C<$first_column> and C<$last_column> are zero based. The C<$last_column> parameter is optional if you only wish to specify one column. You can also specify the columns using A1 column notation, see the note about L</Cell notation>.
3078              
3079             $worksheet1->repeat_columns( 0 ); # Repeat the first column
3080             $worksheet2->repeat_columns( 0, 1 ); # Repeat the first two columns
3081             $worksheet3->repeat_columns( 'A:A' ); # Repeat the first column
3082             $worksheet4->repeat_columns( 'A:B' ); # Repeat the first two columns
3083              
3084              
3085              
3086              
3087             =head2 hide_gridlines( $option )
3088              
3089             This method is used to hide the gridlines on the screen and printed page. Gridlines are the lines that divide the cells on a worksheet. Screen and printed gridlines are turned on by default in an Excel worksheet. If you have defined your own cell borders you may wish to hide the default gridlines.
3090              
3091             $worksheet->hide_gridlines();
3092              
3093             The following values of C<$option> are valid:
3094              
3095             0 : Don't hide gridlines
3096             1 : Hide printed gridlines only
3097             2 : Hide screen and printed gridlines
3098              
3099             If you don't supply an argument or use C<undef> the default option is 1, i.e. only the printed gridlines are hidden.
3100              
3101              
3102              
3103              
3104             =head2 print_row_col_headers()
3105              
3106             Set the option to print the row and column headers on the printed page.
3107              
3108             An Excel worksheet looks something like the following;
3109              
3110             ------------------------------------------
3111             | | A | B | C | D | ...
3112             ------------------------------------------
3113             | 1 | | | | | ...
3114             | 2 | | | | | ...
3115             | 3 | | | | | ...
3116             | 4 | | | | | ...
3117             |...| ... | ... | ... | ... | ...
3118              
3119             The headers are the letters and numbers at the top and the left of the worksheet. Since these headers serve mainly as a indication of position on the worksheet they generally do not appear on the printed page. If you wish to have them printed you can use the C<print_row_col_headers()> method:
3120              
3121             $worksheet->print_row_col_headers();
3122              
3123             Do not confuse these headers with page headers as described in the C<set_header()> section above.
3124              
3125              
3126              
3127              
3128             =head2 hide_row_col_headers()
3129              
3130             Similar to C<print_row_col_headers()> above but set the option to hide the row and column headers within Excel so that they aren't visible to the user:
3131              
3132             $worksheet->hide_row_col_headers();
3133              
3134              
3135              
3136              
3137             =head2 print_area( $first_row, $first_col, $last_row, $last_col )
3138              
3139             This method is used to specify the area of the worksheet that will be printed. All four parameters must be specified. You can also use A1 notation, see the note about L</Cell notation>.
3140              
3141              
3142             $worksheet1->print_area( 'A1:H20' ); # Cells A1 to H20
3143             $worksheet2->print_area( 0, 0, 19, 7 ); # The same
3144             $worksheet2->print_area( 'A:H' ); # Columns A to H if rows have data
3145              
3146              
3147              
3148              
3149             =head2 print_across()
3150              
3151             The C<print_across> method is used to change the default print direction. This is referred to by Excel as the sheet "page order".
3152              
3153             $worksheet->print_across();
3154              
3155             The default page order is shown below for a worksheet that extends over 4 pages. The order is called "down then across":
3156              
3157             [1] [3]
3158             [2] [4]
3159              
3160             However, by using the C<print_across> method the print order will be changed to "across then down":
3161              
3162             [1] [2]
3163             [3] [4]
3164              
3165              
3166              
3167              
3168             =head2 fit_to_pages( $width, $height )
3169              
3170             The C<fit_to_pages()> method is used to fit the printed area to a specific number of pages both vertically and horizontally. If the printed area exceeds the specified number of pages it will be scaled down to fit. This guarantees that the printed area will always appear on the specified number of pages even if the page size or margins change.
3171              
3172             $worksheet1->fit_to_pages( 1, 1 ); # Fit to 1x1 pages
3173             $worksheet2->fit_to_pages( 2, 1 ); # Fit to 2x1 pages
3174             $worksheet3->fit_to_pages( 1, 2 ); # Fit to 1x2 pages
3175              
3176             The print area can be defined using the C<print_area()> method as described above.
3177              
3178             A common requirement is to fit the printed output to I<n> pages wide but have the height be as long as necessary. To achieve this set the C<$height> to zero:
3179              
3180             $worksheet1->fit_to_pages( 1, 0 ); # 1 page wide and as long as necessary
3181              
3182             Note that although it is valid to use both C<fit_to_pages()> and C<set_print_scale()> on the same worksheet only one of these options can be active at a time. The last method call made will set the active option.
3183              
3184             Note that C<fit_to_pages()> will override any manual page breaks that are defined in the worksheet.
3185              
3186             Note: When using C<fit_to_pages()> it may also be required to set the printer paper size using C<set_paper()> or else Excel will default to "US Letter".
3187              
3188              
3189             =head2 set_start_page( $start_page )
3190              
3191             The C<set_start_page()> method is used to set the number of the starting page when the worksheet is printed out. The default value is 1.
3192              
3193             $worksheet->set_start_page( 2 );
3194              
3195              
3196              
3197              
3198             =head2 set_print_scale( $scale )
3199              
3200             Set the scale factor of the printed page. Scale factors in the range C<10 E<lt>= $scale E<lt>= 400> are valid:
3201              
3202             $worksheet1->set_print_scale( 50 );
3203             $worksheet2->set_print_scale( 75 );
3204             $worksheet3->set_print_scale( 300 );
3205             $worksheet4->set_print_scale( 400 );
3206              
3207             The default scale factor is 100. Note, C<set_print_scale()> does not affect the scale of the visible page in Excel. For that you should use C<set_zoom()>.
3208              
3209             Note also that although it is valid to use both C<fit_to_pages()> and C<set_print_scale()> on the same worksheet only one of these options can be active at a time. The last method call made will set the active option.
3210              
3211              
3212              
3213              
3214             =head2 print_black_and_white()
3215              
3216             Set the option to print the worksheet in black and white:
3217              
3218             $worksheet->print_black_and_white();
3219              
3220              
3221              
3222              
3223             =head2 set_h_pagebreaks( @breaks )
3224              
3225             Add horizontal page breaks to a worksheet. A page break causes all the data that follows it to be printed on the next page. Horizontal page breaks act between rows. To create a page break between rows 20 and 21 you must specify the break at row 21. However in zero index notation this is actually row 20. So you can pretend for a small while that you are using 1 index notation:
3226              
3227             $worksheet1->set_h_pagebreaks( 20 ); # Break between row 20 and 21
3228              
3229             The C<set_h_pagebreaks()> method will accept a list of page breaks and you can call it more than once:
3230              
3231             $worksheet2->set_h_pagebreaks( 20, 40, 60, 80, 100 ); # Add breaks
3232             $worksheet2->set_h_pagebreaks( 120, 140, 160, 180, 200 ); # Add some more
3233              
3234             Note: If you specify the "fit to page" option via the C<fit_to_pages()> method it will override all manual page breaks.
3235              
3236             There is a silent limitation of about 1000 horizontal page breaks per worksheet in line with an Excel internal limitation.
3237              
3238              
3239              
3240              
3241             =head2 set_v_pagebreaks( @breaks )
3242              
3243             Add vertical page breaks to a worksheet. A page break causes all the data that follows it to be printed on the next page. Vertical page breaks act between columns. To create a page break between columns 20 and 21 you must specify the break at column 21. However in zero index notation this is actually column 20. So you can pretend for a small while that you are using 1 index notation:
3244              
3245             $worksheet1->set_v_pagebreaks(20); # Break between column 20 and 21
3246              
3247             The C<set_v_pagebreaks()> method will accept a list of page breaks and you can call it more than once:
3248              
3249             $worksheet2->set_v_pagebreaks( 20, 40, 60, 80, 100 ); # Add breaks
3250             $worksheet2->set_v_pagebreaks( 120, 140, 160, 180, 200 ); # Add some more
3251              
3252             Note: If you specify the "fit to page" option via the C<fit_to_pages()> method it will override all manual page breaks.
3253              
3254              
3255              
3256              
3257             =head1 CELL FORMATTING
3258              
3259             This section describes the methods and properties that are available for formatting cells in Excel. The properties of a cell that can be formatted include: fonts, colours, patterns, borders, alignment and number formatting.
3260              
3261              
3262             =head2 Creating and using a Format object
3263              
3264             Cell formatting is defined through a Format object. Format objects are created by calling the workbook C<add_format()> method as follows:
3265              
3266             my $format1 = $workbook->add_format(); # Set properties later
3267             my $format2 = $workbook->add_format( %props ); # Set at creation
3268              
3269             The format object holds all the formatting properties that can be applied to a cell, a row or a column. The process of setting these properties is discussed in the next section.
3270              
3271             Once a Format object has been constructed and its properties have been set it can be passed as an argument to the worksheet C<write> methods as follows:
3272              
3273             $worksheet->write( 0, 0, 'One', $format );
3274             $worksheet->write_string( 1, 0, 'Two', $format );
3275             $worksheet->write_number( 2, 0, 3, $format );
3276             $worksheet->write_blank( 3, 0, $format );
3277              
3278             Formats can also be passed to the worksheet C<set_row()> and C<set_column()> methods to define the default property for a row or column.
3279              
3280             $worksheet->set_row( 0, 15, $format );
3281             $worksheet->set_column( 0, 0, 15, $format );
3282              
3283              
3284              
3285              
3286             =head2 Format methods and Format properties
3287              
3288             The following table shows the Excel format categories, the formatting properties that can be applied and the equivalent object method:
3289              
3290              
3291             Category Description Property Method Name
3292             -------- ----------- -------- -----------
3293             Font Font type font set_font()
3294             Font size size set_size()
3295             Font color color set_color()
3296             Bold bold set_bold()
3297             Italic italic set_italic()
3298             Underline underline set_underline()
3299             Strikeout font_strikeout set_font_strikeout()
3300             Super/Subscript font_script set_font_script()
3301             Outline font_outline set_font_outline()
3302             Shadow font_shadow set_font_shadow()
3303              
3304             Number Numeric format num_format set_num_format()
3305              
3306             Protection Lock cells locked set_locked()
3307             Hide formulas hidden set_hidden()
3308              
3309             Alignment Horizontal align align set_align()
3310             Vertical align valign set_align()
3311             Rotation rotation set_rotation()
3312             Text wrap text_wrap set_text_wrap()
3313             Justify last text_justlast set_text_justlast()
3314             Center across center_across set_center_across()
3315             Indentation indent set_indent()
3316             Shrink to fit shrink set_shrink()
3317              
3318             Pattern Cell pattern pattern set_pattern()
3319             Background color bg_color set_bg_color()
3320             Foreground color fg_color set_fg_color()
3321              
3322             Border Cell border border set_border()
3323             Bottom border bottom set_bottom()
3324             Top border top set_top()
3325             Left border left set_left()
3326             Right border right set_right()
3327             Border color border_color set_border_color()
3328             Bottom color bottom_color set_bottom_color()
3329             Top color top_color set_top_color()
3330             Left color left_color set_left_color()
3331             Right color right_color set_right_color()
3332             Diagonal type diag_type set_diag_type()
3333             Diagonal border diag_border set_diag_border()
3334             Diagonal color diag_color set_diag_color()
3335              
3336             There are two ways of setting Format properties: by using the object method interface or by setting the property directly. For example, a typical use of the method interface would be as follows:
3337              
3338             my $format = $workbook->add_format();
3339             $format->set_bold();
3340             $format->set_color( 'red' );
3341              
3342             By comparison the properties can be set directly by passing a hash of properties to the Format constructor:
3343              
3344             my $format = $workbook->add_format( bold => 1, color => 'red' );
3345              
3346             or after the Format has been constructed by means of the C<set_format_properties()> method as follows:
3347              
3348             my $format = $workbook->add_format();
3349             $format->set_format_properties( bold => 1, color => 'red' );
3350              
3351             You can also store the properties in one or more named hashes and pass them to the required method:
3352              
3353             my %font = (
3354             font => 'Calibri',
3355             size => 12,
3356             color => 'blue',
3357             bold => 1,
3358             );
3359              
3360             my %shading = (
3361             bg_color => 'green',
3362             pattern => 1,
3363             );
3364              
3365              
3366             my $format1 = $workbook->add_format( %font ); # Font only
3367             my $format2 = $workbook->add_format( %font, %shading ); # Font and shading
3368              
3369              
3370             The provision of two ways of setting properties might lead you to wonder which is the best way. The method mechanism may be better if you prefer setting properties via method calls (which the author did when the code was first written) otherwise passing properties to the constructor has proved to be a little more flexible and self documenting in practice. An additional advantage of working with property hashes is that it allows you to share formatting between workbook objects as shown in the example above.
3371              
3372             The Perl/Tk style of adding properties is also supported:
3373              
3374             my %font = (
3375             -font => 'Calibri',
3376             -size => 12,
3377             -color => 'blue',
3378             -bold => 1,
3379             );
3380              
3381              
3382              
3383              
3384             =head2 Working with formats
3385              
3386             The default format is Calibri 11 with all other properties off.
3387              
3388             Each unique format in Excel::Writer::XLSX must have a corresponding Format object. It isn't possible to use a Format with a write() method and then redefine the Format for use at a later stage. This is because a Format is applied to a cell not in its current state but in its final state. Consider the following example:
3389              
3390             my $format = $workbook->add_format();
3391             $format->set_bold();
3392             $format->set_color( 'red' );
3393             $worksheet->write( 'A1', 'Cell A1', $format );
3394             $format->set_color( 'green' );
3395             $worksheet->write( 'B1', 'Cell B1', $format );
3396              
3397             Cell A1 is assigned the Format C<$format> which is initially set to the colour red. However, the colour is subsequently set to green. When Excel displays Cell A1 it will display the final state of the Format which in this case will be the colour green.
3398              
3399             In general a method call without an argument will turn a property on, for example:
3400              
3401             my $format1 = $workbook->add_format();
3402             $format1->set_bold(); # Turns bold on
3403             $format1->set_bold( 1 ); # Also turns bold on
3404             $format1->set_bold( 0 ); # Turns bold off
3405              
3406              
3407              
3408              
3409             =head1 FORMAT METHODS
3410              
3411             The Format object methods are described in more detail in the following sections. In addition, there is a Perl program called C<formats.pl> in the C<examples> directory of the WriteExcel distribution. This program creates an Excel workbook called C<formats.xlsx> which contains examples of almost all the format types.
3412              
3413             The following Format methods are available:
3414              
3415             set_font()
3416             set_size()
3417             set_color()
3418             set_bold()
3419             set_italic()
3420             set_underline()
3421             set_font_strikeout()
3422             set_font_script()
3423             set_font_outline()
3424             set_font_shadow()
3425             set_num_format()
3426             set_locked()
3427             set_hidden()
3428             set_align()
3429             set_rotation()
3430             set_text_wrap()
3431             set_text_justlast()
3432             set_center_across()
3433             set_indent()
3434             set_shrink()
3435             set_pattern()
3436             set_bg_color()
3437             set_fg_color()
3438             set_border()
3439             set_bottom()
3440             set_top()
3441             set_left()
3442             set_right()
3443             set_border_color()
3444             set_bottom_color()
3445             set_top_color()
3446             set_left_color()
3447             set_right_color()
3448             set_diag_type()
3449             set_diag_border()
3450             set_diag_color()
3451              
3452              
3453             The above methods can also be applied directly as properties. For example C<< $format->set_bold() >> is equivalent to C<< $workbook->add_format(bold => 1) >>.
3454              
3455              
3456             =head2 set_format_properties( %properties )
3457              
3458             The properties of an existing Format object can be also be set by means of C<set_format_properties()>:
3459              
3460             my $format = $workbook->add_format();
3461             $format->set_format_properties( bold => 1, color => 'red' );
3462              
3463             However, this method is here mainly for legacy reasons. It is preferable to set the properties in the format constructor:
3464              
3465             my $format = $workbook->add_format( bold => 1, color => 'red' );
3466              
3467              
3468             =head2 set_font( $fontname )
3469              
3470             Default state: Font is Calibri
3471             Default action: None
3472             Valid args: Any valid font name
3473              
3474             Specify the font used:
3475              
3476             $format->set_font('Times New Roman');
3477              
3478             Excel can only display fonts that are installed on the system that it is running on. Therefore it is best to use the fonts that come as standard such as 'Calibri', 'Times New Roman' and 'Courier New'. See also the Fonts worksheet created by formats.pl
3479              
3480              
3481              
3482              
3483             =head2 set_size()
3484              
3485             Default state: Font size is 10
3486             Default action: Set font size to 1
3487             Valid args: Integer values from 1 to as big as your screen.
3488              
3489              
3490             Set the font size. Excel adjusts the height of a row to accommodate the largest font size in the row. You can also explicitly specify the height of a row using the set_row() worksheet method.
3491              
3492             my $format = $workbook->add_format();
3493             $format->set_size( 30 );
3494              
3495              
3496              
3497              
3498              
3499             =head2 set_color()
3500              
3501             Default state: Excels default color, usually black
3502             Default action: Set the default color
3503             Valid args: Integers from 8..63 or the following strings:
3504             'black'
3505             'blue'
3506             'brown'
3507             'cyan'
3508             'gray'
3509             'green'
3510             'lime'
3511             'magenta'
3512             'navy'
3513             'orange'
3514             'pink'
3515             'purple'
3516             'red'
3517             'silver'
3518             'white'
3519             'yellow'
3520              
3521             Set the font colour. The C<set_color()> method is used as follows:
3522              
3523             my $format = $workbook->add_format();
3524             $format->set_color( 'red' );
3525             $worksheet->write( 0, 0, 'wheelbarrow', $format );
3526              
3527             Note: The C<set_color()> method is used to set the colour of the font in a cell. To set the colour of a cell use the C<set_bg_color()> and C<set_pattern()> methods.
3528              
3529             For additional examples see the 'Named colors' and 'Standard colors' worksheets created by formats.pl in the examples directory.
3530              
3531             See also L</WORKING WITH COLOURS>.
3532              
3533              
3534              
3535              
3536             =head2 set_bold()
3537              
3538             Default state: bold is off
3539             Default action: Turn bold on
3540             Valid args: 0, 1
3541              
3542             Set the bold property of the font:
3543              
3544             $format->set_bold(); # Turn bold on
3545              
3546              
3547              
3548              
3549             =head2 set_italic()
3550              
3551             Default state: Italic is off
3552             Default action: Turn italic on
3553             Valid args: 0, 1
3554              
3555             Set the italic property of the font:
3556              
3557             $format->set_italic(); # Turn italic on
3558              
3559              
3560              
3561              
3562             =head2 set_underline()
3563              
3564             Default state: Underline is off
3565             Default action: Turn on single underline
3566             Valid args: 0 = No underline
3567             1 = Single underline
3568             2 = Double underline
3569             33 = Single accounting underline
3570             34 = Double accounting underline
3571              
3572             Set the underline property of the font.
3573              
3574             $format->set_underline(); # Single underline
3575              
3576              
3577              
3578              
3579             =head2 set_font_strikeout()
3580              
3581             Default state: Strikeout is off
3582             Default action: Turn strikeout on
3583             Valid args: 0, 1
3584              
3585             Set the strikeout property of the font.
3586              
3587              
3588              
3589              
3590             =head2 set_font_script()
3591              
3592             Default state: Super/Subscript is off
3593             Default action: Turn Superscript on
3594             Valid args: 0 = Normal
3595             1 = Superscript
3596             2 = Subscript
3597              
3598             Set the superscript/subscript property of the font.
3599              
3600              
3601              
3602              
3603             =head2 set_font_outline()
3604              
3605             Default state: Outline is off
3606             Default action: Turn outline on
3607             Valid args: 0, 1
3608              
3609             Macintosh only.
3610              
3611              
3612              
3613              
3614             =head2 set_font_shadow()
3615              
3616             Default state: Shadow is off
3617             Default action: Turn shadow on
3618             Valid args: 0, 1
3619              
3620             Macintosh only.
3621              
3622              
3623              
3624              
3625             =head2 set_num_format()
3626              
3627             Default state: General format
3628             Default action: Format index 1
3629             Valid args: See the following table
3630              
3631             This method is used to define the numerical format of a number in Excel. It controls whether a number is displayed as an integer, a floating point number, a date, a currency value or some other user defined format.
3632              
3633             The numerical format of a cell can be specified by using a format string or an index to one of Excel's built-in formats:
3634              
3635             my $format1 = $workbook->add_format();
3636             my $format2 = $workbook->add_format();
3637             $format1->set_num_format( 'd mmm yyyy' ); # Format string
3638             $format2->set_num_format( 0x0f ); # Format index
3639              
3640             $worksheet->write( 0, 0, 36892.521, $format1 ); # 1 Jan 2001
3641             $worksheet->write( 0, 0, 36892.521, $format2 ); # 1-Jan-01
3642              
3643              
3644             Using format strings you can define very sophisticated formatting of numbers.
3645              
3646             $format01->set_num_format( '0.000' );
3647             $worksheet->write( 0, 0, 3.1415926, $format01 ); # 3.142
3648              
3649             $format02->set_num_format( '#,##0' );
3650             $worksheet->write( 1, 0, 1234.56, $format02 ); # 1,235
3651              
3652             $format03->set_num_format( '#,##0.00' );
3653             $worksheet->write( 2, 0, 1234.56, $format03 ); # 1,234.56
3654              
3655             $format04->set_num_format( '$0.00' );
3656             $worksheet->write( 3, 0, 49.99, $format04 ); # $49.99
3657              
3658             # Note you can use other currency symbols such as the pound or yen as well.
3659             # Other currencies may require the use of Unicode.
3660              
3661             $format07->set_num_format( 'mm/dd/yy' );
3662             $worksheet->write( 6, 0, 36892.521, $format07 ); # 01/01/01
3663              
3664             $format08->set_num_format( 'mmm d yyyy' );
3665             $worksheet->write( 7, 0, 36892.521, $format08 ); # Jan 1 2001
3666              
3667             $format09->set_num_format( 'd mmmm yyyy' );
3668             $worksheet->write( 8, 0, 36892.521, $format09 ); # 1 January 2001
3669              
3670             $format10->set_num_format( 'dd/mm/yyyy hh:mm AM/PM' );
3671             $worksheet->write( 9, 0, 36892.521, $format10 ); # 01/01/2001 12:30 AM
3672              
3673             $format11->set_num_format( '0 "dollar and" .00 "cents"' );
3674             $worksheet->write( 10, 0, 1.87, $format11 ); # 1 dollar and .87 cents
3675              
3676             # Conditional numerical formatting.
3677             $format12->set_num_format( '[Green]General;[Red]-General;General' );
3678             $worksheet->write( 11, 0, 123, $format12 ); # > 0 Green
3679             $worksheet->write( 12, 0, -45, $format12 ); # < 0 Red
3680             $worksheet->write( 13, 0, 0, $format12 ); # = 0 Default colour
3681              
3682             # Zip code
3683             $format13->set_num_format( '00000' );
3684             $worksheet->write( 14, 0, '01209', $format13 );
3685              
3686              
3687             The number system used for dates is described in L</DATES AND TIME IN EXCEL>.
3688              
3689             The colour format should have one of the following values:
3690              
3691             [Black] [Blue] [Cyan] [Green] [Magenta] [Red] [White] [Yellow]
3692              
3693             Alternatively you can specify the colour based on a colour index as follows: C<[Color n]>, where n is a standard Excel colour index - 7. See the 'Standard colors' worksheet created by formats.pl.
3694              
3695             For more information refer to the documentation on formatting in the C<docs> directory of the Excel::Writer::XLSX distro, the Excel on-line help or L<http://office.microsoft.com/en-gb/assistance/HP051995001033.aspx>.
3696              
3697             You should ensure that the format string is valid in Excel prior to using it in WriteExcel.
3698              
3699             Excel's built-in formats are shown in the following table:
3700              
3701             Index Index Format String
3702             0 0x00 General
3703             1 0x01 0
3704             2 0x02 0.00
3705             3 0x03 #,##0
3706             4 0x04 #,##0.00
3707             5 0x05 ($#,##0_);($#,##0)
3708             6 0x06 ($#,##0_);[Red]($#,##0)
3709             7 0x07 ($#,##0.00_);($#,##0.00)
3710             8 0x08 ($#,##0.00_);[Red]($#,##0.00)
3711             9 0x09 0%
3712             10 0x0a 0.00%
3713             11 0x0b 0.00E+00
3714             12 0x0c # ?/?
3715             13 0x0d # ??/??
3716             14 0x0e m/d/yy
3717             15 0x0f d-mmm-yy
3718             16 0x10 d-mmm
3719             17 0x11 mmm-yy
3720             18 0x12 h:mm AM/PM
3721             19 0x13 h:mm:ss AM/PM
3722             20 0x14 h:mm
3723             21 0x15 h:mm:ss
3724             22 0x16 m/d/yy h:mm
3725             .. .... ...........
3726             37 0x25 (#,##0_);(#,##0)
3727             38 0x26 (#,##0_);[Red](#,##0)
3728             39 0x27 (#,##0.00_);(#,##0.00)
3729             40 0x28 (#,##0.00_);[Red](#,##0.00)
3730             41 0x29 _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)
3731             42 0x2a _($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)
3732             43 0x2b _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)
3733             44 0x2c _($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)
3734             45 0x2d mm:ss
3735             46 0x2e [h]:mm:ss
3736             47 0x2f mm:ss.0
3737             48 0x30 ##0.0E+0
3738             49 0x31 @
3739              
3740              
3741             For examples of these formatting codes see the 'Numerical formats' worksheet created by formats.pl. See also the number_formats1.html and the number_formats2.html documents in the C<docs> directory of the distro.
3742              
3743             Note 1. Numeric formats 23 to 36 are not documented by Microsoft and may differ in international versions.
3744              
3745             Note 2. The built-in formats are localised according to the locale settings (regional settings on Windows) of the user when opening the file in Excel:
3746              
3747             =over
3748              
3749             =item * The dot appears as the defined local decimal separator.
3750              
3751             =item * The comma appears as the defined local digit groups separator.
3752              
3753             =item * The dollar sign appears as the defined local currency symbol.
3754              
3755             =item * The date, time and duration formats appear as the local equivalent date or time format.
3756              
3757             =back
3758              
3759              
3760              
3761              
3762             =head2 set_locked()
3763              
3764             Default state: Cell locking is on
3765             Default action: Turn locking on
3766             Valid args: 0, 1
3767              
3768             This property can be used to prevent modification of a cells contents. Following Excel's convention, cell locking is turned on by default. However, it only has an effect if the worksheet has been protected, see the worksheet C<protect()> method.
3769              
3770             my $locked = $workbook->add_format();
3771             $locked->set_locked( 1 ); # A non-op
3772              
3773             my $unlocked = $workbook->add_format();
3774             $locked->set_locked( 0 );
3775              
3776             # Enable worksheet protection
3777             $worksheet->protect();
3778              
3779             # This cell cannot be edited.
3780             $worksheet->write( 'A1', '=1+2', $locked );
3781              
3782             # This cell can be edited.
3783             $worksheet->write( 'A2', '=1+2', $unlocked );
3784              
3785             Note: This offers weak protection even with a password, see the note in relation to the C<protect()> method.
3786              
3787              
3788              
3789              
3790             =head2 set_hidden()
3791              
3792             Default state: Formula hiding is off
3793             Default action: Turn hiding on
3794             Valid args: 0, 1
3795              
3796             This property is used to hide a formula while still displaying its result. This is generally used to hide complex calculations from end users who are only interested in the result. It only has an effect if the worksheet has been protected, see the worksheet C<protect()> method.
3797              
3798             my $hidden = $workbook->add_format();
3799             $hidden->set_hidden();
3800              
3801             # Enable worksheet protection
3802             $worksheet->protect();
3803              
3804             # The formula in this cell isn't visible
3805             $worksheet->write( 'A1', '=1+2', $hidden );
3806              
3807              
3808             Note: This offers weak protection even with a password, see the note in relation to the C<protect()> method.
3809              
3810              
3811              
3812              
3813             =head2 set_align()
3814              
3815             Default state: Alignment is off
3816             Default action: Left alignment
3817             Valid args: 'left' Horizontal
3818             'center'
3819             'right'
3820             'fill'
3821             'justify'
3822             'center_across'
3823              
3824             'top' Vertical
3825             'vcenter'
3826             'bottom'
3827             'vjustify'
3828              
3829             This method is used to set the horizontal and vertical text alignment within a cell. Vertical and horizontal alignments can be combined. The method is used as follows:
3830              
3831             my $format = $workbook->add_format();
3832             $format->set_align( 'center' );
3833             $format->set_align( 'vcenter' );
3834             $worksheet->set_row( 0, 30 );
3835             $worksheet->write( 0, 0, 'X', $format );
3836              
3837             Text can be aligned across two or more adjacent cells using the C<center_across> property. However, for genuine merged cells it is better to use the C<merge_range()> worksheet method.
3838              
3839             The C<vjustify> (vertical justify) option can be used to provide automatic text wrapping in a cell. The height of the cell will be adjusted to accommodate the wrapped text. To specify where the text wraps use the C<set_text_wrap()> method.
3840              
3841              
3842             For further examples see the 'Alignment' worksheet created by formats.pl.
3843              
3844              
3845              
3846              
3847             =head2 set_center_across()
3848              
3849             Default state: Center across selection is off
3850             Default action: Turn center across on
3851             Valid args: 1
3852              
3853             Text can be aligned across two or more adjacent cells using the C<set_center_across()> method. This is an alias for the C<set_align('center_across')> method call.
3854              
3855             Only one cell should contain the text, the other cells should be blank:
3856              
3857             my $format = $workbook->add_format();
3858             $format->set_center_across();
3859              
3860             $worksheet->write( 1, 1, 'Center across selection', $format );
3861             $worksheet->write_blank( 1, 2, $format );
3862              
3863             See also the C<merge1.pl> to C<merge6.pl> programs in the C<examples> directory and the C<merge_range()> method.
3864              
3865              
3866              
3867             =head2 set_text_wrap()
3868              
3869             Default state: Text wrap is off
3870             Default action: Turn text wrap on
3871             Valid args: 0, 1
3872              
3873              
3874             Here is an example using the text wrap property, the escape character C<\n> is used to indicate the end of line:
3875              
3876             my $format = $workbook->add_format();
3877             $format->set_text_wrap();
3878             $worksheet->write( 0, 0, "It's\na bum\nwrap", $format );
3879              
3880             Excel will adjust the height of the row to accommodate the wrapped text. A similar effect can be obtained without newlines using the C<set_align('vjustify')> method. See the C<textwrap.pl> program in the C<examples> directory.
3881              
3882              
3883              
3884              
3885             =head2 set_rotation()
3886              
3887             Default state: Text rotation is off
3888             Default action: None
3889             Valid args: Integers in the range -90 to 90 and 270
3890              
3891             Set the rotation of the text in a cell. The rotation can be any angle in the range -90 to 90 degrees.
3892              
3893             my $format = $workbook->add_format();
3894             $format->set_rotation( 30 );
3895             $worksheet->write( 0, 0, 'This text is rotated', $format );
3896              
3897              
3898             The angle 270 is also supported. This indicates text where the letters run from top to bottom.
3899              
3900              
3901              
3902             =head2 set_indent()
3903              
3904             Default state: Text indentation is off
3905             Default action: Indent text 1 level
3906             Valid args: Positive integers
3907              
3908              
3909             This method can be used to indent text. The argument, which should be an integer, is taken as the level of indentation:
3910              
3911              
3912             my $format = $workbook->add_format();
3913             $format->set_indent( 2 );
3914             $worksheet->write( 0, 0, 'This text is indented', $format );
3915              
3916              
3917             Indentation is a horizontal alignment property. It will override any other horizontal properties but it can be used in conjunction with vertical properties.
3918              
3919              
3920              
3921              
3922             =head2 set_shrink()
3923              
3924             Default state: Text shrinking is off
3925             Default action: Turn "shrink to fit" on
3926             Valid args: 1
3927              
3928              
3929             This method can be used to shrink text so that it fits in a cell.
3930              
3931              
3932             my $format = $workbook->add_format();
3933             $format->set_shrink();
3934             $worksheet->write( 0, 0, 'Honey, I shrunk the text!', $format );
3935              
3936              
3937              
3938              
3939             =head2 set_text_justlast()
3940              
3941             Default state: Justify last is off
3942             Default action: Turn justify last on
3943             Valid args: 0, 1
3944              
3945              
3946             Only applies to Far Eastern versions of Excel.
3947              
3948              
3949              
3950              
3951             =head2 set_pattern()
3952              
3953             Default state: Pattern is off
3954             Default action: Solid fill is on
3955             Valid args: 0 .. 18
3956              
3957             Set the background pattern of a cell.
3958              
3959             Examples of the available patterns are shown in the 'Patterns' worksheet created by formats.pl. However, it is unlikely that you will ever need anything other than Pattern 1 which is a solid fill of the background color.
3960              
3961              
3962              
3963              
3964             =head2 set_bg_color()
3965              
3966             Default state: Color is off
3967             Default action: Solid fill.
3968             Valid args: See set_color()
3969              
3970             The C<set_bg_color()> method can be used to set the background colour of a pattern. Patterns are defined via the C<set_pattern()> method. If a pattern hasn't been defined then a solid fill pattern is used as the default.
3971              
3972             Here is an example of how to set up a solid fill in a cell:
3973              
3974             my $format = $workbook->add_format();
3975              
3976             $format->set_pattern(); # This is optional when using a solid fill
3977              
3978             $format->set_bg_color( 'green' );
3979             $worksheet->write( 'A1', 'Ray', $format );
3980              
3981             For further examples see the 'Patterns' worksheet created by formats.pl.
3982              
3983              
3984              
3985              
3986             =head2 set_fg_color()
3987              
3988             Default state: Color is off
3989             Default action: Solid fill.
3990             Valid args: See set_color()
3991              
3992              
3993             The C<set_fg_color()> method can be used to set the foreground colour of a pattern.
3994              
3995             For further examples see the 'Patterns' worksheet created by formats.pl.
3996              
3997              
3998              
3999              
4000             =head2 set_border()
4001              
4002             Also applies to: set_bottom()
4003             set_top()
4004             set_left()
4005             set_right()
4006              
4007             Default state: Border is off
4008             Default action: Set border type 1
4009             Valid args: 0-13, See below.
4010              
4011             A cell border is comprised of a border on the bottom, top, left and right. These can be set to the same value using C<set_border()> or individually using the relevant method calls shown above.
4012              
4013             The following shows the border styles sorted by Excel::Writer::XLSX index number:
4014              
4015             Index Name Weight Style
4016             ===== ============= ====== ===========
4017             0 None 0
4018             1 Continuous 1 -----------
4019             2 Continuous 2 -----------
4020             3 Dash 1 - - - - - -
4021             4 Dot 1 . . . . . .
4022             5 Continuous 3 -----------
4023             6 Double 3 ===========
4024             7 Continuous 0 -----------
4025             8 Dash 2 - - - - - -
4026             9 Dash Dot 1 - . - . - .
4027             10 Dash Dot 2 - . - . - .
4028             11 Dash Dot Dot 1 - . . - . .
4029             12 Dash Dot Dot 2 - . . - . .
4030             13 SlantDash Dot 2 / - . / - .
4031              
4032              
4033             The following shows the borders sorted by style:
4034              
4035             Name Weight Style Index
4036             ============= ====== =========== =====
4037             Continuous 0 ----------- 7
4038             Continuous 1 ----------- 1
4039             Continuous 2 ----------- 2
4040             Continuous 3 ----------- 5
4041             Dash 1 - - - - - - 3
4042             Dash 2 - - - - - - 8
4043             Dash Dot 1 - . - . - . 9
4044             Dash Dot 2 - . - . - . 10
4045             Dash Dot Dot 1 - . . - . . 11
4046             Dash Dot Dot 2 - . . - . . 12
4047             Dot 1 . . . . . . 4
4048             Double 3 =========== 6
4049             None 0 0
4050             SlantDash Dot 2 / - . / - . 13
4051              
4052              
4053             The following shows the borders in the order shown in the Excel Dialog.
4054              
4055             Index Style Index Style
4056             ===== ===== ===== =====
4057             0 None 12 - . . - . .
4058             7 ----------- 13 / - . / - .
4059             4 . . . . . . 10 - . - . - .
4060             11 - . . - . . 8 - - - - - -
4061             9 - . - . - . 2 -----------
4062             3 - - - - - - 5 -----------
4063             1 ----------- 6 ===========
4064              
4065              
4066             Examples of the available border styles are shown in the 'Borders' worksheet created by formats.pl.
4067              
4068              
4069              
4070              
4071             =head2 set_border_color()
4072              
4073             Also applies to: set_bottom_color()
4074             set_top_color()
4075             set_left_color()
4076             set_right_color()
4077              
4078             Default state: Color is off
4079             Default action: Undefined
4080             Valid args: See set_color()
4081              
4082              
4083             Set the colour of the cell borders. A cell border is comprised of a border on the bottom, top, left and right. These can be set to the same colour using C<set_border_color()> or individually using the relevant method calls shown above. Examples of the border styles and colours are shown in the 'Borders' worksheet created by formats.pl.
4084              
4085              
4086             =head2 set_diag_type()
4087              
4088             Default state: Diagonal border is off.
4089             Default action: None.
4090             Valid args: 1-3, See below.
4091              
4092             Set the diagonal border type for the cell. Three types of diagonal borders are available in Excel:
4093              
4094             1: From bottom left to top right.
4095             2: From top left to bottom right.
4096             3: Same as 1 and 2 combined.
4097              
4098             For example:
4099              
4100             $format->set_diag_type( 3 );
4101              
4102              
4103              
4104             =head2 set_diag_border()
4105              
4106             Default state: Border is off
4107             Default action: Set border type 1
4108             Valid args: 0-13, See below.
4109              
4110             Set the diagonal border style. Same as the parameter to C<set_border()> above.
4111              
4112              
4113              
4114              
4115             =head2 set_diag_color()
4116              
4117             Default state: Color is off
4118             Default action: Undefined
4119             Valid args: See set_color()
4120              
4121              
4122             Set the colour of the diagonal cell border:
4123              
4124             $format->set_diag_type( 3 );
4125             $format->set_diag_border( 7 );
4126             $format->set_diag_color( 'red' );
4127              
4128              
4129              
4130             =head2 copy( $format )
4131              
4132             This method is used to copy all of the properties from one Format object to another:
4133              
4134             my $lorry1 = $workbook->add_format();
4135             $lorry1->set_bold();
4136             $lorry1->set_italic();
4137             $lorry1->set_color( 'red' ); # lorry1 is bold, italic and red
4138              
4139             my $lorry2 = $workbook->add_format();
4140             $lorry2->copy( $lorry1 );
4141             $lorry2->set_color( 'yellow' ); # lorry2 is bold, italic and yellow
4142              
4143             The C<copy()> method is only useful if you are using the method interface to Format properties. It generally isn't required if you are setting Format properties directly using hashes.
4144              
4145              
4146             Note: this is not a copy constructor, both objects must exist prior to copying.
4147              
4148              
4149              
4150              
4151             =head1 UNICODE IN EXCEL
4152              
4153             The following is a brief introduction to handling Unicode in C<Excel::Writer::XLSX>.
4154              
4155             I<For a more general introduction to Unicode handling in Perl see> L<perlunitut> and L<perluniintro>.
4156              
4157             Excel::Writer::XLSX writer differs from Spreadsheet::WriteExcel in that it only handles Unicode data in C<UTF-8> format and doesn't try to handle legacy UTF-16 Excel formats.
4158              
4159             If the data is in C<UTF-8> format then Excel::Writer::XLSX will handle it automatically.
4160              
4161             If you are dealing with non-ASCII characters that aren't in C<UTF-8> then perl provides useful tools in the guise of the C<Encode> module to help you to convert to the required format. For example:
4162              
4163             use Encode 'decode';
4164              
4165             my $string = 'some string with koi8-r characters';
4166             $string = decode('koi8-r', $string); # koi8-r to utf8
4167              
4168             Alternatively you can read data from an encoded file and convert it to C<UTF-8> as you read it in:
4169              
4170              
4171             my $file = 'unicode_koi8r.txt';
4172             open FH, '<:encoding(koi8-r)', $file or die "Couldn't open $file: $!\n";
4173              
4174             my $row = 0;
4175             while ( <FH> ) {
4176             # Data read in is now in utf8 format.
4177             chomp;
4178             $worksheet->write( $row++, 0, $_ );
4179             }
4180              
4181             These methodologies are explained in more detail in L<perlunitut>, L<perluniintro> and L<perlunicode>.
4182              
4183             If the program contains UTF-8 text then you will also need to add C<use utf8> to the includes:
4184              
4185             use utf8;
4186              
4187             ...
4188              
4189             $worksheet->write( 'A1', 'Some UTF-8 string' );
4190              
4191              
4192             See also the C<unicode_*.pl> programs in the examples directory of the distro.
4193              
4194              
4195              
4196              
4197             =head1 WORKING WITH COLOURS
4198              
4199             Throughout Excel::Writer::XLSX colours can be specified using a Html style C<#RRGGBB> value. For example with a Format object:
4200              
4201             $format->set_color( '#FF0000' );
4202              
4203             For backward compatibility a limited number of color names are supported:
4204              
4205             $format->set_color( 'red' );
4206              
4207             The color names supported are:
4208              
4209             black
4210             blue
4211             brown
4212             cyan
4213             gray
4214             green
4215             lime
4216             magenta
4217             navy
4218             orange
4219             pink
4220             purple
4221             red
4222             silver
4223             white
4224             yellow
4225              
4226             See also C<colors.pl> in the C<examples> directory.
4227              
4228              
4229             =head1 DATES AND TIME IN EXCEL
4230              
4231             There are two important things to understand about dates and times in Excel:
4232              
4233             =over 4
4234              
4235             =item 1 A date/time in Excel is a real number plus an Excel number format.
4236              
4237             =item 2 Excel::Writer::XLSX doesn't automatically convert date/time strings in C<write()> to an Excel date/time.
4238              
4239             =back
4240              
4241             These two points are explained in more detail below along with some suggestions on how to convert times and dates to the required format.
4242              
4243              
4244             =head2 An Excel date/time is a number plus a format
4245              
4246             If you write a date string with C<write()> then all you will get is a string:
4247              
4248             $worksheet->write( 'A1', '02/03/04' ); # !! Writes a string not a date. !!
4249              
4250             Dates and times in Excel are represented by real numbers, for example "Jan 1 2001 12:30 AM" is represented by the number 36892.521.
4251              
4252             The integer part of the number stores the number of days since the epoch and the fractional part stores the percentage of the day.
4253              
4254             A date or time in Excel is just like any other number. To have the number display as a date you must apply an Excel number format to it. Here are some examples.
4255              
4256             #!/usr/bin/perl -w
4257              
4258             use strict;
4259             use Excel::Writer::XLSX;
4260              
4261             my $workbook = Excel::Writer::XLSX->new( 'date_examples.xlsx' );
4262             my $worksheet = $workbook->add_worksheet();
4263              
4264             $worksheet->set_column( 'A:A', 30 ); # For extra visibility.
4265              
4266             my $number = 39506.5;
4267              
4268             $worksheet->write( 'A1', $number ); # 39506.5
4269              
4270             my $format2 = $workbook->add_format( num_format => 'dd/mm/yy' );
4271             $worksheet->write( 'A2', $number, $format2 ); # 28/02/08
4272              
4273             my $format3 = $workbook->add_format( num_format => 'mm/dd/yy' );
4274             $worksheet->write( 'A3', $number, $format3 ); # 02/28/08
4275              
4276             my $format4 = $workbook->add_format( num_format => 'd-m-yyyy' );
4277             $worksheet->write( 'A4', $number, $format4 ); # 28-2-2008
4278              
4279             my $format5 = $workbook->add_format( num_format => 'dd/mm/yy hh:mm' );
4280             $worksheet->write( 'A5', $number, $format5 ); # 28/02/08 12:00
4281              
4282             my $format6 = $workbook->add_format( num_format => 'd mmm yyyy' );
4283             $worksheet->write( 'A6', $number, $format6 ); # 28 Feb 2008
4284              
4285             my $format7 = $workbook->add_format( num_format => 'mmm d yyyy hh:mm AM/PM' );
4286             $worksheet->write('A7', $number , $format7); # Feb 28 2008 12:00 PM
4287              
4288             $workbook->close();
4289              
4290             =head2 Excel::Writer::XLSX doesn't automatically convert date/time strings
4291              
4292             Excel::Writer::XLSX doesn't automatically convert input date strings into Excel's formatted date numbers due to the large number of possible date formats and also due to the possibility of misinterpretation.
4293              
4294             For example, does C<02/03/04> mean March 2 2004, February 3 2004 or even March 4 2002.
4295              
4296             Therefore, in order to handle dates you will have to convert them to numbers and apply an Excel format. Some methods for converting dates are listed in the next section.
4297              
4298             The most direct way is to convert your dates to the ISO8601 C<yyyy-mm-ddThh:mm:ss.sss> date format and use the C<write_date_time()> worksheet method:
4299              
4300             $worksheet->write_date_time( 'A2', '2001-01-01T12:20', $format );
4301              
4302             See the C<write_date_time()> section of the documentation for more details.
4303              
4304             A general methodology for handling date strings with C<write_date_time()> is:
4305              
4306             1. Identify incoming date/time strings with a regex.
4307             2. Extract the component parts of the date/time using the same regex.
4308             3. Convert the date/time to the ISO8601 format.
4309             4. Write the date/time using write_date_time() and a number format.
4310              
4311             Here is an example:
4312              
4313             #!/usr/bin/perl -w
4314              
4315             use strict;
4316             use Excel::Writer::XLSX;
4317              
4318             my $workbook = Excel::Writer::XLSX->new( 'example.xlsx' );
4319             my $worksheet = $workbook->add_worksheet();
4320              
4321             # Set the default format for dates.
4322             my $date_format = $workbook->add_format( num_format => 'mmm d yyyy' );
4323              
4324             # Increase column width to improve visibility of data.
4325             $worksheet->set_column( 'A:C', 20 );
4326              
4327             # Simulate reading from a data source.
4328             my $row = 0;
4329              
4330             while ( <DATA> ) {
4331             chomp;
4332              
4333             my $col = 0;
4334             my @data = split ' ';
4335              
4336             for my $item ( @data ) {
4337              
4338             # Match dates in the following formats: d/m/yy, d/m/yyyy
4339             if ( $item =~ qr[^(\d{1,2})/(\d{1,2})/(\d{4})$] ) {
4340              
4341             # Change to the date format required by write_date_time().
4342             my $date = sprintf "%4d-%02d-%02dT", $3, $2, $1;
4343              
4344             $worksheet->write_date_time( $row, $col++, $date,
4345             $date_format );
4346             }
4347             else {
4348              
4349             # Just plain data
4350             $worksheet->write( $row, $col++, $item );
4351             }
4352             }
4353             $row++;
4354             }
4355              
4356             $workbook->close();
4357              
4358             __DATA__
4359             Item Cost Date
4360             Book 10 1/9/2007
4361             Beer 4 12/9/2007
4362             Bed 500 5/10/2007
4363              
4364             For a slightly more advanced solution you can modify the C<write()> method to handle date formats of your choice via the C<add_write_handler()> method. See the C<add_write_handler()> section of the docs and the write_handler3.pl and write_handler4.pl programs in the examples directory of the distro.
4365              
4366              
4367             =head2 Converting dates and times to an Excel date or time
4368              
4369             The C<write_date_time()> method above is just one way of handling dates and times.
4370              
4371             You can also use the C<convert_date_time()> worksheet method to convert from an ISO8601 style date string to an Excel date and time number.
4372              
4373             The L<Excel::Writer::XLSX::Utility> module which is included in the distro has date/time handling functions:
4374              
4375             use Excel::Writer::XLSX::Utility;
4376              
4377             $date = xl_date_list(2002, 1, 1); # 37257
4378             $date = xl_parse_date("11 July 1997"); # 35622
4379             $time = xl_parse_time('3:21:36 PM'); # 0.64
4380             $date = xl_decode_date_EU("13 May 2002"); # 37389
4381              
4382             Note: some of these functions require additional CPAN modules.
4383              
4384             For date conversions using the CPAN C<DateTime> framework see L<DateTime::Format::Excel> L<http://search.cpan.org/search?dist=DateTime-Format-Excel>.
4385              
4386              
4387              
4388              
4389             =head1 OUTLINES AND GROUPING IN EXCEL
4390              
4391              
4392             Excel allows you to group rows or columns so that they can be hidden or displayed with a single mouse click. This feature is referred to as outlines.
4393              
4394             Outlines can reduce complex data down to a few salient sub-totals or summaries.
4395              
4396             This feature is best viewed in Excel but the following is an ASCII representation of what a worksheet with three outlines might look like. Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at level 1. The lines at the left hand side are called outline level bars.
4397              
4398              
4399             ------------------------------------------
4400             1 2 3 | | A | B | C | D | ...
4401             ------------------------------------------
4402             _ | 1 | A | | | | ...
4403             | _ | 2 | B | | | | ...
4404             | | | 3 | (C) | | | | ...
4405             | | | 4 | (D) | | | | ...
4406             | - | 5 | E | | | | ...
4407             | _ | 6 | F | | | | ...
4408             | | | 7 | (G) | | | | ...
4409             | | | 8 | (H) | | | | ...
4410             | - | 9 | I | | | | ...
4411             - | . | ... | ... | ... | ... | ...
4412              
4413              
4414             Clicking the minus sign on each of the level 2 outlines will collapse and hide the data as shown in the next figure. The minus sign changes to a plus sign to indicate that the data in the outline is hidden.
4415              
4416             ------------------------------------------
4417             1 2 3 | | A | B | C | D | ...
4418             ------------------------------------------
4419             _ | 1 | A | | | | ...
4420             | | 2 | B | | | | ...
4421             | + | 5 | E | | | | ...
4422             | | 6 | F | | | | ...
4423             | + | 9 | I | | | | ...
4424             - | . | ... | ... | ... | ... | ...
4425              
4426              
4427             Clicking on the minus sign on the level 1 outline will collapse the remaining rows as follows:
4428              
4429             ------------------------------------------
4430             1 2 3 | | A | B | C | D | ...
4431             ------------------------------------------
4432             | 1 | A | | | | ...
4433             + | . | ... | ... | ... | ... | ...
4434              
4435              
4436             Grouping in C<Excel::Writer::XLSX> is achieved by setting the outline level via the C<set_row()> and C<set_column()> worksheet methods:
4437              
4438             set_row( $row, $height, $format, $hidden, $level, $collapsed )
4439             set_column( $first_col, $last_col, $width, $format, $hidden, $level, $collapsed )
4440              
4441             The following example sets an outline level of 1 for rows 2 and 3 (zero-indexed) and columns B to G. The parameters C<$height> and C<$XF> are assigned default values since they are undefined:
4442              
4443             $worksheet->set_row( 1, undef, undef, 0, 1 );
4444             $worksheet->set_row( 2, undef, undef, 0, 1 );
4445             $worksheet->set_column( 'B:G', undef, undef, 0, 1 );
4446              
4447             Excel allows up to 7 outline levels. Therefore the C<$level> parameter should be in the range C<0 E<lt>= $level E<lt>= 7>.
4448              
4449             Rows and columns can be collapsed by setting the C<$hidden> flag for the hidden rows/columns and setting the C<$collapsed> flag for the row/column that has the collapsed C<+> symbol:
4450              
4451             $worksheet->set_row( 1, undef, undef, 1, 1 );
4452             $worksheet->set_row( 2, undef, undef, 1, 1 );
4453             $worksheet->set_row( 3, undef, undef, 0, 0, 1 ); # Collapsed flag.
4454              
4455             $worksheet->set_column( 'B:G', undef, undef, 1, 1 );
4456             $worksheet->set_column( 'H:H', undef, undef, 0, 0, 1 ); # Collapsed flag.
4457              
4458             Note: Setting the C<$collapsed> flag is particularly important for compatibility with OpenOffice.org and Gnumeric.
4459              
4460             For a more complete example see the C<outline.pl> and C<outline_collapsed.pl> programs in the examples directory of the distro.
4461              
4462             Some additional outline properties can be set via the C<outline_settings()> worksheet method, see above.
4463              
4464              
4465              
4466              
4467             =head1 DATA VALIDATION IN EXCEL
4468              
4469             Data validation is a feature of Excel which allows you to restrict the data that a users enters in a cell and to display help and warning messages. It also allows you to restrict input to values in a drop down list.
4470              
4471             A typical use case might be to restrict data in a cell to integer values in a certain range, to provide a help message to indicate the required value and to issue a warning if the input data doesn't meet the stated criteria. In Excel::Writer::XLSX we could do that as follows:
4472              
4473             $worksheet->data_validation('B3',
4474             {
4475             validate => 'integer',
4476             criteria => 'between',
4477             minimum => 1,
4478             maximum => 100,
4479             input_title => 'Input an integer:',
4480             input_message => 'Between 1 and 100',
4481             error_message => 'Sorry, try again.',
4482             });
4483              
4484              
4485             =begin html
4486              
4487             <p><center><img src="http://jmcnamara.github.io/excel-writer-xlsx/images/examples/validation_example.jpg" alt="The output from the above example"/></center></p>
4488              
4489             =end html
4490              
4491             For more information on data validation see the following Microsoft support article "Description and examples of data validation in Excel": L<http://support.microsoft.com/kb/211485>.
4492              
4493             The following sections describe how to use the C<data_validation()> method and its various options.
4494              
4495              
4496             =head2 data_validation( $row, $col, { parameter => 'value', ... } )
4497              
4498             The C<data_validation()> method is used to construct an Excel data validation.
4499              
4500             It can be applied to a single cell or a range of cells. You can pass 3 parameters such as C<($row, $col, {...})> or 5 parameters such as C<($first_row, $first_col, $last_row, $last_col, {...})>. You can also use C<A1> style notation. For example:
4501              
4502             $worksheet->data_validation( 0, 0, {...} );
4503             $worksheet->data_validation( 0, 0, 4, 1, {...} );
4504              
4505             # Which are the same as:
4506              
4507             $worksheet->data_validation( 'A1', {...} );
4508             $worksheet->data_validation( 'A1:B5', {...} );
4509              
4510             See also the note about L</Cell notation> for more information.
4511              
4512              
4513             The last parameter in C<data_validation()> must be a hash ref containing the parameters that describe the type and style of the data validation. The allowable parameters are:
4514              
4515             validate
4516             criteria
4517             value | minimum | source
4518             maximum
4519             ignore_blank
4520             dropdown
4521              
4522             input_title
4523             input_message
4524             show_input
4525              
4526             error_title
4527             error_message
4528             error_type
4529             show_error
4530              
4531             These parameters are explained in the following sections. Most of the parameters are optional, however, you will generally require the three main options C<validate>, C<criteria> and C<value>.
4532              
4533             $worksheet->data_validation('B3',
4534             {
4535             validate => 'integer',
4536             criteria => '>',
4537             value => 100,
4538             });
4539              
4540             The C<data_validation> method returns:
4541              
4542             0 for success.
4543             -1 for insufficient number of arguments.
4544             -2 for row or column out of bounds.
4545             -3 for incorrect parameter or value.
4546              
4547              
4548             =head2 validate
4549              
4550             This parameter is passed in a hash ref to C<data_validation()>.
4551              
4552             The C<validate> parameter is used to set the type of data that you wish to validate. It is always required and it has no default value. Allowable values are:
4553              
4554             any
4555             integer
4556             decimal
4557             list
4558             date
4559             time
4560             length
4561             custom
4562              
4563             =over
4564              
4565             =item * B<any> is used to specify that the type of data is unrestricted. This is useful to display an input message without restricting the data that can be entered.
4566              
4567             =item * B<integer> restricts the cell to integer values. Excel refers to this as 'whole number'.
4568              
4569             validate => 'integer',
4570             criteria => '>',
4571             value => 100,
4572              
4573             =item * B<decimal> restricts the cell to decimal values.
4574              
4575             validate => 'decimal',
4576             criteria => '>',
4577             value => 38.6,
4578              
4579             =item * B<list> restricts the cell to a set of user specified values. These can be passed in an array ref or as a cell range (named ranges aren't currently supported):
4580              
4581             validate => 'list',
4582             value => ['open', 'high', 'close'],
4583             # Or like this:
4584             value => 'B1:B3',
4585              
4586             Excel requires that range references are only to cells on the same worksheet.
4587              
4588             =item * B<date> restricts the cell to date values. Dates in Excel are expressed as integer values but you can also pass an ISO8601 style string as used in C<write_date_time()>. See also L</DATES AND TIME IN EXCEL> for more information about working with Excel's dates.
4589              
4590             validate => 'date',
4591             criteria => '>',
4592             value => 39653, # 24 July 2008
4593             # Or like this:
4594             value => '2008-07-24T',
4595              
4596             =item * B<time> restricts the cell to time values. Times in Excel are expressed as decimal values but you can also pass an ISO8601 style string as used in C<write_date_time()>. See also L</DATES AND TIME IN EXCEL> for more information about working with Excel's times.
4597              
4598             validate => 'time',
4599             criteria => '>',
4600             value => 0.5, # Noon
4601             # Or like this:
4602             value => 'T12:00:00',
4603              
4604             =item * B<length> restricts the cell data based on an integer string length. Excel refers to this as 'Text length'.
4605              
4606             validate => 'length',
4607             criteria => '>',
4608             value => 10,
4609              
4610             =item * B<custom> restricts the cell based on an external Excel formula that returns a C<TRUE/FALSE> value.
4611              
4612             validate => 'custom',
4613             value => '=IF(A10>B10,TRUE,FALSE)',
4614              
4615             =back
4616              
4617              
4618             =head2 criteria
4619              
4620             This parameter is passed in a hash ref to C<data_validation()>.
4621              
4622             The C<criteria> parameter is used to set the criteria by which the data in the cell is validated. It is almost always required except for the C<list> and C<custom> validate options. It has no default value. Allowable values are:
4623              
4624             'between'
4625             'not between'
4626             'equal to' | '==' | '='
4627             'not equal to' | '!=' | '<>'
4628             'greater than' | '>'
4629             'less than' | '<'
4630             'greater than or equal to' | '>='
4631             'less than or equal to' | '<='
4632              
4633             You can either use Excel's textual description strings, in the first column above, or the more common symbolic alternatives. The following are equivalent:
4634              
4635             validate => 'integer',
4636             criteria => 'greater than',
4637             value => 100,
4638              
4639             validate => 'integer',
4640             criteria => '>',
4641             value => 100,
4642              
4643             The C<list> and C<custom> validate options don't require a C<criteria>. If you specify one it will be ignored.
4644              
4645             validate => 'list',
4646             value => ['open', 'high', 'close'],
4647              
4648             validate => 'custom',
4649             value => '=IF(A10>B10,TRUE,FALSE)',
4650              
4651              
4652             =head2 value | minimum | source
4653              
4654             This parameter is passed in a hash ref to C<data_validation()>.
4655              
4656             The C<value> parameter is used to set the limiting value to which the C<criteria> is applied. It is always required and it has no default value. You can also use the synonyms C<minimum> or C<source> to make the validation a little clearer and closer to Excel's description of the parameter:
4657              
4658             # Use 'value'
4659             validate => 'integer',
4660             criteria => '>',
4661             value => 100,
4662              
4663             # Use 'minimum'
4664             validate => 'integer',
4665             criteria => 'between',
4666             minimum => 1,
4667             maximum => 100,
4668              
4669             # Use 'source'
4670             validate => 'list',
4671             source => '$B$1:$B$3',
4672              
4673              
4674             =head2 maximum
4675              
4676             This parameter is passed in a hash ref to C<data_validation()>.
4677              
4678             The C<maximum> parameter is used to set the upper limiting value when the C<criteria> is either C<'between'> or C<'not between'>:
4679              
4680             validate => 'integer',
4681             criteria => 'between',
4682             minimum => 1,
4683             maximum => 100,
4684              
4685              
4686             =head2 ignore_blank
4687              
4688             This parameter is passed in a hash ref to C<data_validation()>.
4689              
4690             The C<ignore_blank> parameter is used to toggle on and off the 'Ignore blank' option in the Excel data validation dialog. When the option is on the data validation is not applied to blank data in the cell. It is on by default.
4691              
4692             ignore_blank => 0, # Turn the option off
4693              
4694              
4695             =head2 dropdown
4696              
4697             This parameter is passed in a hash ref to C<data_validation()>.
4698              
4699             The C<dropdown> parameter is used to toggle on and off the 'In-cell dropdown' option in the Excel data validation dialog. When the option is on a dropdown list will be shown for C<list> validations. It is on by default.
4700              
4701             dropdown => 0, # Turn the option off
4702              
4703              
4704             =head2 input_title
4705              
4706             This parameter is passed in a hash ref to C<data_validation()>.
4707              
4708             The C<input_title> parameter is used to set the title of the input message that is displayed when a cell is entered. It has no default value and is only displayed if the input message is displayed. See the C<input_message> parameter below.
4709              
4710             input_title => 'This is the input title',
4711              
4712             The maximum title length is 32 characters.
4713              
4714              
4715             =head2 input_message
4716              
4717             This parameter is passed in a hash ref to C<data_validation()>.
4718              
4719             The C<input_message> parameter is used to set the input message that is displayed when a cell is entered. It has no default value.
4720              
4721             validate => 'integer',
4722             criteria => 'between',
4723             minimum => 1,
4724             maximum => 100,
4725             input_title => 'Enter the applied discount:',
4726             input_message => 'between 1 and 100',
4727              
4728             The message can be split over several lines using newlines, C<"\n"> in double quoted strings.
4729              
4730             input_message => "This is\na test.",
4731              
4732             The maximum message length is 255 characters.
4733              
4734              
4735             =head2 show_input
4736              
4737             This parameter is passed in a hash ref to C<data_validation()>.
4738              
4739             The C<show_input> parameter is used to toggle on and off the 'Show input message when cell is selected' option in the Excel data validation dialog. When the option is off an input message is not displayed even if it has been set using C<input_message>. It is on by default.
4740              
4741             show_input => 0, # Turn the option off
4742              
4743              
4744             =head2 error_title
4745              
4746             This parameter is passed in a hash ref to C<data_validation()>.
4747              
4748             The C<error_title> parameter is used to set the title of the error message that is displayed when the data validation criteria is not met. The default error title is 'Microsoft Excel'.
4749              
4750             error_title => 'Input value is not valid',
4751              
4752             The maximum title length is 32 characters.
4753              
4754              
4755             =head2 error_message
4756              
4757             This parameter is passed in a hash ref to C<data_validation()>.
4758              
4759             The C<error_message> parameter is used to set the error message that is displayed when a cell is entered. The default error message is "The value you entered is not valid.\nA user has restricted values that can be entered into the cell.".
4760              
4761             validate => 'integer',
4762             criteria => 'between',
4763             minimum => 1,
4764             maximum => 100,
4765             error_title => 'Input value is not valid',
4766             error_message => 'It should be an integer between 1 and 100',
4767              
4768             The message can be split over several lines using newlines, C<"\n"> in double quoted strings.
4769              
4770             input_message => "This is\na test.",
4771              
4772             The maximum message length is 255 characters.
4773              
4774              
4775             =head2 error_type
4776              
4777             This parameter is passed in a hash ref to C<data_validation()>.
4778              
4779             The C<error_type> parameter is used to specify the type of error dialog that is displayed. There are 3 options:
4780              
4781             'stop'
4782             'warning'
4783             'information'
4784              
4785             The default is C<'stop'>.
4786              
4787              
4788             =head2 show_error
4789              
4790             This parameter is passed in a hash ref to C<data_validation()>.
4791              
4792             The C<show_error> parameter is used to toggle on and off the 'Show error alert after invalid data is entered' option in the Excel data validation dialog. When the option is off an error message is not displayed even if it has been set using C<error_message>. It is on by default.
4793              
4794             show_error => 0, # Turn the option off
4795              
4796             =head2 Data Validation Examples
4797              
4798             Example 1. Limiting input to an integer greater than a fixed value.
4799              
4800             $worksheet->data_validation('A1',
4801             {
4802             validate => 'integer',
4803             criteria => '>',
4804             value => 0,
4805             });
4806              
4807             Example 2. Limiting input to an integer greater than a fixed value where the value is referenced from a cell.
4808              
4809             $worksheet->data_validation('A2',
4810             {
4811             validate => 'integer',
4812             criteria => '>',
4813             value => '=E3',
4814             });
4815              
4816             Example 3. Limiting input to a decimal in a fixed range.
4817              
4818             $worksheet->data_validation('A3',
4819             {
4820             validate => 'decimal',
4821             criteria => 'between',
4822             minimum => 0.1,
4823             maximum => 0.5,
4824             });
4825              
4826             Example 4. Limiting input to a value in a dropdown list.
4827              
4828             $worksheet->data_validation('A4',
4829             {
4830             validate => 'list',
4831             source => ['open', 'high', 'close'],
4832             });
4833              
4834             Example 5. Limiting input to a value in a dropdown list where the list is specified as a cell range.
4835              
4836             $worksheet->data_validation('A5',
4837             {
4838             validate => 'list',
4839             source => '=$E$4:$G$4',
4840             });
4841              
4842             Example 6. Limiting input to a date in a fixed range.
4843              
4844             $worksheet->data_validation('A6',
4845             {
4846             validate => 'date',
4847             criteria => 'between',
4848             minimum => '2008-01-01T',
4849             maximum => '2008-12-12T',
4850             });
4851              
4852             Example 7. Displaying a message when the cell is selected.
4853              
4854             $worksheet->data_validation('A7',
4855             {
4856             validate => 'integer',
4857             criteria => 'between',
4858             minimum => 1,
4859             maximum => 100,
4860             input_title => 'Enter an integer:',
4861             input_message => 'between 1 and 100',
4862             });
4863              
4864             See also the C<data_validate.pl> program in the examples directory of the distro.
4865              
4866              
4867              
4868              
4869             =head1 CONDITIONAL FORMATTING IN EXCEL
4870              
4871             Conditional formatting is a feature of Excel which allows you to apply a format to a cell or a range of cells based on a certain criteria.
4872              
4873             For example the following criteria is used to highlight cells >= 50 in red in the C<conditional_format.pl> example from the distro:
4874              
4875             # Write a conditional format over a range.
4876             $worksheet->conditional_formatting( 'B3:K12',
4877             {
4878             type => 'cell',
4879             criteria => '>=',
4880             value => 50,
4881             format => $format1,
4882             }
4883             );
4884              
4885             =begin html
4886              
4887             <p><center><img src="http://jmcnamara.github.io/excel-writer-xlsx/images/examples/conditional_example.jpg" alt="The output from the above example"/></center></p>
4888              
4889             =end html
4890              
4891              
4892              
4893             =head2 conditional_formatting( $row, $col, { parameter => 'value', ... } )
4894              
4895             The C<conditional_formatting()> method is used to apply formatting based on user defined criteria to an Excel::Writer::XLSX file.
4896              
4897             It can be applied to a single cell or a range of cells. You can pass 3 parameters such as C<($row, $col, {...})> or 5 parameters such as C<($first_row, $first_col, $last_row, $last_col, {...})>. You can also use C<A1> style notation. For example:
4898              
4899             $worksheet->conditional_formatting( 0, 0, {...} );
4900             $worksheet->conditional_formatting( 0, 0, 4, 1, {...} );
4901              
4902             # Which are the same as:
4903              
4904             $worksheet->conditional_formatting( 'A1', {...} );
4905             $worksheet->conditional_formatting( 'A1:B5', {...} );
4906              
4907             See also the note about L</Cell notation> for more information.
4908              
4909             Using C<A1> style notation is also possible to specify non-contiguous ranges, separated by a comma. For example:
4910              
4911             $worksheet->conditional_formatting( 'A1:D5,A8:D12', {...} );
4912              
4913             The last parameter in C<conditional_formatting()> must be a hash ref containing the parameters that describe the type and style of the data validation. The main parameters are:
4914              
4915             type
4916             format
4917             criteria
4918             value
4919             minimum
4920             maximum
4921              
4922             Other, less commonly used parameters are:
4923              
4924             min_type
4925             mid_type
4926             max_type
4927             min_value
4928             mid_value
4929             max_value
4930             min_color
4931             mid_color
4932             max_color
4933             bar_color
4934             bar_only
4935             bar_solid
4936             bar_negative_color
4937             bar_border_color
4938             bar_negative_border_color
4939             bar_negative_color_same
4940             bar_negative_border_color_same
4941             bar_no_border
4942             bar_direction
4943             bar_axis_position
4944             bar_axis_color
4945             data_bar_2010
4946             icon_style
4947             icons
4948             reverse_icons
4949             icons_only
4950             stop_if_true
4951             multi_range
4952              
4953             Additional parameters which are used for specific conditional format types are shown in the relevant sections below.
4954              
4955             =head2 type
4956              
4957             This parameter is passed in a hash ref to C<conditional_formatting()>.
4958              
4959             The C<type> parameter is used to set the type of conditional formatting that you wish to apply. It is always required and it has no default value. Allowable C<type> values and their associated parameters are:
4960              
4961             Type Parameters
4962             ==== ==========
4963             cell criteria
4964             value
4965             minimum
4966             maximum
4967             format
4968              
4969             date criteria
4970             value
4971             minimum
4972             maximum
4973             format
4974              
4975             time_period criteria
4976             format
4977              
4978             text criteria
4979             value
4980             format
4981              
4982             average criteria
4983             format
4984              
4985             duplicate format
4986              
4987             unique format
4988              
4989             top criteria
4990             value
4991             format
4992              
4993             bottom criteria
4994             value
4995             format
4996              
4997             blanks format
4998              
4999             no_blanks format
5000              
5001             errors format
5002              
5003             no_errors format
5004              
5005             formula criteria
5006             format
5007              
5008             2_color_scale min_type
5009             max_type
5010             min_value
5011             max_value
5012             min_color
5013             max_color
5014              
5015             3_color_scale min_type
5016             mid_type
5017             max_type
5018             min_value
5019             mid_value
5020             max_value
5021             min_color
5022             mid_color
5023             max_color
5024              
5025             data_bar min_type
5026             max_type
5027             min_value
5028             max_value
5029             bar_only
5030             bar_color
5031             bar_solid*
5032             bar_negative_color*
5033             bar_border_color*
5034             bar_negative_border_color*
5035             bar_negative_color_same*
5036             bar_negative_border_color_same*
5037             bar_no_border*
5038             bar_direction*
5039             bar_axis_position*
5040             bar_axis_color*
5041             data_bar_2010*
5042              
5043             icon_set icon_style
5044             reverse_icons
5045             icons
5046             icons_only
5047              
5048             Data bar parameters marked with (*) are only available in Excel 2010 and later. Files that use these properties can still be opened in Excel 2007 but the data bars will be displayed without them.
5049              
5050              
5051             =head2 type => 'cell'
5052              
5053             This is the most common conditional formatting type. It is used when a format is applied to a cell based on a simple criterion. For example:
5054              
5055             $worksheet->conditional_formatting( 'A1',
5056             {
5057             type => 'cell',
5058             criteria => 'greater than',
5059             value => 5,
5060             format => $red_format,
5061             }
5062             );
5063              
5064             Or, using the C<between> criteria:
5065              
5066             $worksheet->conditional_formatting( 'C1:C4',
5067             {
5068             type => 'cell',
5069             criteria => 'between',
5070             minimum => 20,
5071             maximum => 30,
5072             format => $green_format,
5073             }
5074             );
5075              
5076              
5077             =head2 criteria
5078              
5079             The C<criteria> parameter is used to set the criteria by which the cell data will be evaluated. It has no default value. The most common criteria as applied to C<< { type => 'cell' } >> are:
5080              
5081             'between'
5082             'not between'
5083             'equal to' | '==' | '='
5084             'not equal to' | '!=' | '<>'
5085             'greater than' | '>'
5086             'less than' | '<'
5087             'greater than or equal to' | '>='
5088             'less than or equal to' | '<='
5089              
5090             You can either use Excel's textual description strings, in the first column above, or the more common symbolic alternatives.
5091              
5092             Additional criteria which are specific to other conditional format types are shown in the relevant sections below.
5093              
5094              
5095             =head2 value
5096              
5097             The C<value> is generally used along with the C<criteria> parameter to set the rule by which the cell data will be evaluated.
5098              
5099             type => 'cell',
5100             criteria => '>',
5101             value => 5
5102             format => $format,
5103              
5104             The C<value> property can also be an cell reference.
5105              
5106             type => 'cell',
5107             criteria => '>',
5108             value => '$C$1',
5109             format => $format,
5110              
5111              
5112             =head2 format
5113              
5114             The C<format> parameter is used to specify the format that will be applied to the cell when the conditional formatting criterion is met. The format is created using the C<add_format()> method in the same way as cell formats:
5115              
5116             $format = $workbook->add_format( bold => 1, italic => 1 );
5117              
5118             $worksheet->conditional_formatting( 'A1',
5119             {
5120             type => 'cell',
5121             criteria => '>',
5122             value => 5
5123             format => $format,
5124             }
5125             );
5126              
5127             The conditional format follows the same rules as in Excel: it is superimposed over the existing cell format and not all font and border properties can be modified. Font properties that can't be modified are font name, font size, superscript and subscript. The border property that cannot be modified is diagonal borders.
5128              
5129             Excel specifies some default formats to be used with conditional formatting. You can replicate them using the following Excel::Writer::XLSX formats:
5130              
5131             # Light red fill with dark red text.
5132              
5133             my $format1 = $workbook->add_format(
5134             bg_color => '#FFC7CE',
5135             color => '#9C0006',
5136             );
5137              
5138             # Light yellow fill with dark yellow text.
5139              
5140             my $format2 = $workbook->add_format(
5141             bg_color => '#FFEB9C',
5142             color => '#9C6500',
5143             );
5144              
5145             # Green fill with dark green text.
5146              
5147             my $format3 = $workbook->add_format(
5148             bg_color => '#C6EFCE',
5149             color => '#006100',
5150             );
5151              
5152              
5153             =head2 minimum
5154              
5155             The C<minimum> parameter is used to set the lower limiting value when the C<criteria> is either C<'between'> or C<'not between'>:
5156              
5157             validate => 'integer',
5158             criteria => 'between',
5159             minimum => 1,
5160             maximum => 100,
5161              
5162              
5163             =head2 maximum
5164              
5165             The C<maximum> parameter is used to set the upper limiting value when the C<criteria> is either C<'between'> or C<'not between'>. See the previous example.
5166              
5167              
5168             =head2 type => 'date'
5169              
5170             The C<date> type is the same as the C<cell> type and uses the same criteria and values. However it allows the C<value>, C<minimum> and C<maximum> properties to be specified in the ISO8601 C<yyyy-mm-ddThh:mm:ss.sss> date format which is detailed in the C<write_date_time()> method.
5171              
5172             $worksheet->conditional_formatting( 'A1:A4',
5173             {
5174             type => 'date',
5175             criteria => 'greater than',
5176             value => '2011-01-01T',
5177             format => $format,
5178             }
5179             );
5180              
5181              
5182             =head2 type => 'time_period'
5183              
5184             The C<time_period> type is used to specify Excel's "Dates Occurring" style conditional format.
5185              
5186             $worksheet->conditional_formatting( 'A1:A4',
5187             {
5188             type => 'time_period',
5189             criteria => 'yesterday',
5190             format => $format,
5191             }
5192             );
5193              
5194             The period is set in the C<criteria> and can have one of the following values:
5195              
5196             criteria => 'yesterday',
5197             criteria => 'today',
5198             criteria => 'last 7 days',
5199             criteria => 'last week',
5200             criteria => 'this week',
5201             criteria => 'next week',
5202             criteria => 'last month',
5203             criteria => 'this month',
5204             criteria => 'next month'
5205              
5206              
5207             =head2 type => 'text'
5208              
5209             The C<text> type is used to specify Excel's "Specific Text" style conditional format. It is used to do simple string matching using the C<criteria> and C<value> parameters:
5210              
5211             $worksheet->conditional_formatting( 'A1:A4',
5212             {
5213             type => 'text',
5214             criteria => 'containing',
5215             value => 'foo',
5216             format => $format,
5217             }
5218             );
5219              
5220             The C<criteria> can have one of the following values:
5221              
5222             criteria => 'containing',
5223             criteria => 'not containing',
5224             criteria => 'begins with',
5225             criteria => 'ends with',
5226              
5227             The C<value> parameter should be a string or single character.
5228              
5229              
5230             =head2 type => 'average'
5231              
5232             The C<average> type is used to specify Excel's "Average" style conditional format.
5233              
5234             $worksheet->conditional_formatting( 'A1:A4',
5235             {
5236             type => 'average',
5237             criteria => 'above',
5238             format => $format,
5239             }
5240             );
5241              
5242             The type of average for the conditional format range is specified by the C<criteria>:
5243              
5244             criteria => 'above',
5245             criteria => 'below',
5246             criteria => 'equal or above',
5247             criteria => 'equal or below',
5248             criteria => '1 std dev above',
5249             criteria => '1 std dev below',
5250             criteria => '2 std dev above',
5251             criteria => '2 std dev below',
5252             criteria => '3 std dev above',
5253             criteria => '3 std dev below',
5254              
5255              
5256              
5257             =head2 type => 'duplicate'
5258              
5259             The C<duplicate> type is used to highlight duplicate cells in a range:
5260              
5261             $worksheet->conditional_formatting( 'A1:A4',
5262             {
5263             type => 'duplicate',
5264             format => $format,
5265             }
5266             );
5267              
5268              
5269             =head2 type => 'unique'
5270              
5271             The C<unique> type is used to highlight unique cells in a range:
5272              
5273             $worksheet->conditional_formatting( 'A1:A4',
5274             {
5275             type => 'unique',
5276             format => $format,
5277             }
5278             );
5279              
5280              
5281             =head2 type => 'top'
5282              
5283             The C<top> type is used to specify the top C<n> values by number or percentage in a range:
5284              
5285             $worksheet->conditional_formatting( 'A1:A4',
5286             {
5287             type => 'top',
5288             value => 10,
5289             format => $format,
5290             }
5291             );
5292              
5293             The C<criteria> can be used to indicate that a percentage condition is required:
5294              
5295             $worksheet->conditional_formatting( 'A1:A4',
5296             {
5297             type => 'top',
5298             value => 10,
5299             criteria => '%',
5300             format => $format,
5301             }
5302             );
5303              
5304              
5305             =head2 type => 'bottom'
5306              
5307             The C<bottom> type is used to specify the bottom C<n> values by number or percentage in a range.
5308              
5309             It takes the same parameters as C<top>, see above.
5310              
5311              
5312             =head2 type => 'blanks'
5313              
5314             The C<blanks> type is used to highlight blank cells in a range:
5315              
5316             $worksheet->conditional_formatting( 'A1:A4',
5317             {
5318             type => 'blanks',
5319             format => $format,
5320             }
5321             );
5322              
5323              
5324             =head2 type => 'no_blanks'
5325              
5326             The C<no_blanks> type is used to highlight non blank cells in a range:
5327              
5328             $worksheet->conditional_formatting( 'A1:A4',
5329             {
5330             type => 'no_blanks',
5331             format => $format,
5332             }
5333             );
5334              
5335              
5336             =head2 type => 'errors'
5337              
5338             The C<errors> type is used to highlight error cells in a range:
5339              
5340             $worksheet->conditional_formatting( 'A1:A4',
5341             {
5342             type => 'errors',
5343             format => $format,
5344             }
5345             );
5346              
5347              
5348             =head2 type => 'no_errors'
5349              
5350             The C<no_errors> type is used to highlight non error cells in a range:
5351              
5352             $worksheet->conditional_formatting( 'A1:A4',
5353             {
5354             type => 'no_errors',
5355             format => $format,
5356             }
5357             );
5358              
5359              
5360              
5361             =head2 type => 'formula'
5362              
5363             The C<formula> type is used to specify a conditional format based on a user defined formula:
5364              
5365             $worksheet->conditional_formatting( 'A1:A4',
5366             {
5367             type => 'formula',
5368             criteria => '=$A$1 > 5',
5369             format => $format,
5370             }
5371             );
5372              
5373             The formula is specified in the C<criteria>.
5374              
5375              
5376             =head2 type => '2_color_scale'
5377              
5378             The C<2_color_scale> type is used to specify Excel's "2 Color Scale" style conditional format.
5379              
5380             $worksheet->conditional_formatting( 'A1:A12',
5381             {
5382             type => '2_color_scale',
5383             }
5384             );
5385              
5386             This conditional type can be modified with C<min_type>, C<max_type>, C<min_value>, C<max_value>, C<min_color> and C<max_color>, see below.
5387              
5388              
5389             =head2 type => '3_color_scale'
5390              
5391             The C<3_color_scale> type is used to specify Excel's "3 Color Scale" style conditional format.
5392              
5393             $worksheet->conditional_formatting( 'A1:A12',
5394             {
5395             type => '3_color_scale',
5396             }
5397             );
5398              
5399             This conditional type can be modified with C<min_type>, C<mid_type>, C<max_type>, C<min_value>, C<mid_value>, C<max_value>, C<min_color>, C<mid_color> and C<max_color>, see below.
5400              
5401              
5402             =head2 type => 'data_bar'
5403              
5404             The C<data_bar> type is used to specify Excel's "Data Bar" style conditional format.
5405              
5406             $worksheet->conditional_formatting( 'A1:A12',
5407             {
5408             type => 'data_bar',
5409             }
5410             );
5411              
5412             This data bar conditional type can be modified with the following parameters, which are explained in the sections below. These properties were available in the original xlsx file specification used in Excel 2007::
5413              
5414             min_type
5415             max_type
5416             min_value
5417             max_value
5418             bar_color
5419             bar_only
5420              
5421             In Excel 2010 additional data bar properties were added such as solid (non-gradient) bars and control over how negative values are displayed. These properties can be set using the following parameters:
5422              
5423             bar_solid
5424             bar_negative_color
5425             bar_border_color
5426             bar_negative_border_color
5427             bar_negative_color_same
5428             bar_negative_border_color_same
5429             bar_no_border
5430             bar_direction
5431             bar_axis_position
5432             bar_axis_color
5433             data_bar_2010
5434              
5435             Files that use these Excel 2010 properties can still be opened in Excel 2007 but the data bars will be displayed without them.
5436              
5437              
5438              
5439             =head2 type => 'icon_set'
5440              
5441             The C<icon_set> type is used to specify a conditional format with a set of icons such as traffic lights or arrows:
5442              
5443             $worksheet->conditional_formatting( 'A1:C1',
5444             {
5445             type => 'icon_set',
5446             icon_style => '3_traffic_lights',
5447             }
5448             );
5449              
5450             The icon set style is specified by the C<icon_style> parameter. Valid options are:
5451              
5452             3_arrows
5453             3_arrows_gray
5454             3_flags
5455             3_signs
5456             3_symbols
5457             3_symbols_circled
5458             3_traffic_lights
5459             3_traffic_lights_rimmed
5460              
5461             4_arrows
5462             4_arrows_gray
5463             4_ratings
5464             4_red_to_black
5465             4_traffic_lights
5466              
5467             5_arrows
5468             5_arrows_gray
5469             5_quarters
5470             5_ratings
5471              
5472             The criteria, type and value of each icon can be specified using the C<icon> array of hash refs with optional C<criteria>, C<type> and C<value> parameters:
5473              
5474             $worksheet->conditional_formatting( 'A1:D1',
5475             {
5476             type => 'icon_set',
5477             icon_style => '4_red_to_black',
5478             icons => [ {criteria => '>', type => 'number', value => 90},
5479             {criteria => '>=', type => 'percentile', value => 50},
5480             {criteria => '>', type => 'percent', value => 25},
5481             ],
5482             }
5483             );
5484              
5485              
5486             The C<icons criteria> parameter should be either C<< >= >> or C<< > >>. The default C<criteria> is C<< >= >>.
5487              
5488             The C<icons type> parameter should be one of the following values:
5489              
5490             number
5491             percentile
5492             percent
5493             formula
5494              
5495             The default C<type> is C<percent>.
5496              
5497             The C<icons value> parameter can be a value or formula:
5498              
5499             $worksheet->conditional_formatting( 'A1:D1',
5500             {
5501             type => 'icon_set',
5502             icon_style => '4_red_to_black',
5503             icons => [ {value => 90},
5504             {value => 50},
5505             {value => 25},
5506             ],
5507             }
5508             );
5509              
5510             Note: The C<icons> parameters should start with the highest value and with each subsequent one being lower. The default C<value> is C<(n * 100) / number_of_icons>. The lowest number icon in an icon set has properties defined by Excel. Therefore in a C<n> icon set, there is no C<n-1> hash of parameters.
5511              
5512             The order of the icons can be reversed using the C<reverse_icons> parameter:
5513              
5514             $worksheet->conditional_formatting( 'A1:C1',
5515             {
5516             type => 'icon_set',
5517             icon_style => '3_arrows',
5518             reverse_icons => 1,
5519             }
5520             );
5521              
5522             The icons can be displayed without the cell value using the C<icons_only> parameter:
5523              
5524             $worksheet->conditional_formatting( 'A1:C1',
5525             {
5526             type => 'icon_set',
5527             icon_style => '3_flags',
5528             icons_only => 1,
5529             }
5530             );
5531              
5532              
5533              
5534              
5535             =head2 min_type, mid_type, max_type
5536              
5537             The C<min_type> and C<max_type> properties are available when the conditional formatting type is C<2_color_scale>, C<3_color_scale> or C<data_bar>. The C<mid_type> is available for C<3_color_scale>. The properties are used as follows:
5538              
5539             $worksheet->conditional_formatting( 'A1:A12',
5540             {
5541             type => '2_color_scale',
5542             min_type => 'percent',
5543             max_type => 'percent',
5544             }
5545             );
5546              
5547             The available min/mid/max types are:
5548              
5549             min (for min_type only)
5550             num
5551             percent
5552             percentile
5553             formula
5554             max (for max_type only)
5555              
5556              
5557             =head2 min_value, mid_value, max_value
5558              
5559             The C<min_value> and C<max_value> properties are available when the conditional formatting type is C<2_color_scale>, C<3_color_scale> or C<data_bar>. The C<mid_value> is available for C<3_color_scale>. The properties are used as follows:
5560              
5561             $worksheet->conditional_formatting( 'A1:A12',
5562             {
5563             type => '2_color_scale',
5564             min_value => 10,
5565             max_value => 90,
5566             }
5567             );
5568              
5569             =head2 min_color, mid_color, max_color, bar_color
5570              
5571             The C<min_color> and C<max_color> properties are available when the conditional formatting type is C<2_color_scale>, C<3_color_scale> or C<data_bar>. The C<mid_color> is available for C<3_color_scale>. The properties are used as follows:
5572              
5573             $worksheet->conditional_formatting( 'A1:A12',
5574             {
5575             type => '2_color_scale',
5576             min_color => "#C5D9F1",
5577             max_color => "#538ED5",
5578             }
5579             );
5580              
5581             The color can be specified as an Excel::Writer::XLSX color index or, more usefully, as a HTML style RGB hex number, as shown above.
5582              
5583              
5584             =head2 bar_only
5585              
5586             The C<bar_only> parameter property displays a bar data but not the data in the cells:
5587              
5588             $worksheet->conditional_formatting( 'D3:D14',
5589             {
5590             type => 'data_bar',
5591             bar_only => 1
5592             }
5593             );
5594              
5595              
5596             =head2 bar_solid
5597              
5598             The C<bar_solid> parameter turns on a solid (non-gradient) fill for data bars:
5599              
5600              
5601             $worksheet->conditional_formatting( 'H3:H14',
5602             {
5603             type => 'data_bar',
5604             bar_solid => 1
5605             }
5606             );
5607              
5608             Note, this property is only visible in Excel 2010 and later.
5609              
5610              
5611             =head2 bar_negative_color
5612              
5613             The C<bar_negative_color> parameter is used to set the color fill for the negative portion of a data bar.
5614              
5615             The color can be specified as an Excel::Writer::XLSX color index or as a HTML style RGB hex number, as shown in the other examples.
5616              
5617             Note, this property is only visible in Excel 2010 and later.
5618              
5619              
5620             =head2 bar_border_color
5621              
5622             The C<bar_border_color> parameter is used to set the border color of a data bar.
5623              
5624             The color can be specified as an Excel::Writer::XLSX color index or as a HTML style RGB hex number, as shown in the other examples.
5625              
5626             Note, this property is only visible in Excel 2010 and later.
5627              
5628              
5629             =head2 bar_negative_border_color
5630              
5631             The C<bar_negative_border_color> parameter is used to set the border color of the negative portion of a data bar.
5632              
5633             The color can be specified as an Excel::Writer::XLSX color index or as a HTML style RGB hex number, as shown in the other examples.
5634              
5635             Note, this property is only visible in Excel 2010 and later.
5636              
5637              
5638             =head2 bar_negative_color_same
5639              
5640             The C<bar_negative_color_same> parameter sets the fill color for the negative portion of a data bar to be the same as the fill color for the positive portion of the data bar:
5641              
5642             $worksheet->conditional_formatting( 'N3:N14',
5643             {
5644             type => 'data_bar',
5645             bar_negative_color_same => 1,
5646             bar_negative_border_color_same => 1
5647             }
5648             );
5649              
5650             Note, this property is only visible in Excel 2010 and later.
5651              
5652              
5653             =head2 bar_negative_border_color_same
5654              
5655             The C<bar_negative_border_color_same> parameter sets the border color for the negative portion of a data bar to be the same as the border color for the positive portion of the data bar.
5656              
5657             Note, this property is only visible in Excel 2010 and later.
5658              
5659              
5660             =head2 bar_no_border
5661              
5662             The C<bar_no_border> parameter turns off the border of a data bar.
5663              
5664              
5665             Note, this property is only visible in Excel 2010 and later, however the default in Excel 2007 is not to have a border.
5666              
5667              
5668             =head2 bar_direction
5669              
5670             The C<bar_direction> parameter sets the direction for data bars. This property can be either C<left> for left-to-right or C<right> for right-to-left. If the property isn't set then Excel will adjust the position automatically based on the context:
5671              
5672             $worksheet->conditional_formatting( 'J3:J14',
5673             {
5674             type => 'data_bar',
5675             bar_direction => 'right'
5676             }
5677             );
5678              
5679             Note, this property is only visible in Excel 2010 and later.
5680              
5681              
5682             =head2 bar_axis_position
5683              
5684             The C<bar_axis_position> parameter sets the position within the cells for the axis that is shown in data bars when there are negative values to display. The property can be either C<middle> or C<none>. If the property isn't set then Excel will position the axis based on the range of positive and negative values.
5685              
5686             Note, this property is only visible in Excel 2010 and later.
5687              
5688              
5689             =head2 bar_axis_color
5690              
5691             The C<bar_axis_color> parameter sets the color for the axis that is shown in data bars when there are negative values to display.
5692              
5693             The color can be specified as an Excel::Writer::XLSX color index or as a HTML style RGB hex number, as shown in the other examples.
5694              
5695             Note, this property is only visible in Excel 2010 and later.
5696              
5697              
5698             =head2 data_bar_2010
5699              
5700             The C<data_bar_2010> parameter sets Excel 2010 style data bars even when Excel 2010 specific properties aren't used. This can be used to create consistency across all the data bar formatting in a worksheet:
5701              
5702             $worksheet->conditional_formatting( 'L3:L14',
5703             {
5704             type => 'data_bar',
5705             data_bar_2010 => 1
5706             }
5707             );
5708              
5709             Note, this property is only visible in Excel 2010 and later.
5710              
5711              
5712             =head2 stop_if_true
5713              
5714             The C<stop_if_true> parameter, if set to a true value, will enable the "stop if true" feature on the conditional formatting rule, so that subsequent rules are not examined for any cell on which the conditions for this rule are met.
5715              
5716              
5717             =head2 Conditional Formatting Examples
5718              
5719             Example 1. Highlight cells greater than an integer value.
5720              
5721             $worksheet->conditional_formatting( 'A1:F10',
5722             {
5723             type => 'cell',
5724             criteria => 'greater than',
5725             value => 5,
5726             format => $format,
5727             }
5728             );
5729              
5730             Example 2. Highlight cells greater than a value in a reference cell.
5731              
5732             $worksheet->conditional_formatting( 'A1:F10',
5733             {
5734             type => 'cell',
5735             criteria => 'greater than',
5736             value => '$H$1',
5737             format => $format,
5738             }
5739             );
5740              
5741             Example 3. Highlight cells greater than a certain date:
5742              
5743             $worksheet->conditional_formatting( 'A1:F10',
5744             {
5745             type => 'date',
5746             criteria => 'greater than',
5747             value => '2011-01-01T',
5748             format => $format,
5749             }
5750             );
5751              
5752             Example 4. Highlight cells with a date in the last seven days:
5753              
5754             $worksheet->conditional_formatting( 'A1:F10',
5755             {
5756             type => 'time_period',
5757             criteria => 'last 7 days',
5758             format => $format,
5759             }
5760             );
5761              
5762             Example 5. Highlight cells with strings starting with the letter C<b>:
5763              
5764             $worksheet->conditional_formatting( 'A1:F10',
5765             {
5766             type => 'text',
5767             criteria => 'begins with',
5768             value => 'b',
5769             format => $format,
5770             }
5771             );
5772              
5773             Example 6. Highlight cells that are 1 std deviation above the average for the range:
5774              
5775             $worksheet->conditional_formatting( 'A1:F10',
5776             {
5777             type => 'average',
5778             format => $format,
5779             }
5780             );
5781              
5782             Example 7. Highlight duplicate cells in a range:
5783              
5784             $worksheet->conditional_formatting( 'A1:F10',
5785             {
5786             type => 'duplicate',
5787             format => $format,
5788             }
5789             );
5790              
5791             Example 8. Highlight unique cells in a range.
5792              
5793             $worksheet->conditional_formatting( 'A1:F10',
5794             {
5795             type => 'unique',
5796             format => $format,
5797             }
5798             );
5799              
5800             Example 9. Highlight the top 10 cells.
5801              
5802             $worksheet->conditional_formatting( 'A1:F10',
5803             {
5804             type => 'top',
5805             value => 10,
5806             format => $format,
5807             }
5808             );
5809              
5810              
5811             Example 10. Highlight blank cells.
5812              
5813             $worksheet->conditional_formatting( 'A1:F10',
5814             {
5815             type => 'blanks',
5816             format => $format,
5817             }
5818             );
5819              
5820             Example 11. Set traffic light icons in 3 cells:
5821              
5822             $worksheet->conditional_formatting( 'A1:C1',
5823             {
5824             type => 'icon_set',
5825             icon_style => '3_traffic_lights',
5826             }
5827             );
5828              
5829              
5830             See also the C<conditional_format.pl> example program in C<EXAMPLES>.
5831              
5832              
5833              
5834              
5835             =head1 SPARKLINES IN EXCEL
5836              
5837             Sparklines are a feature of Excel 2010+ which allows you to add small charts to worksheet cells. These are useful for showing visual trends in data in a compact format.
5838              
5839             In Excel::Writer::XLSX Sparklines can be added to cells using the C<add_sparkline()> worksheet method:
5840              
5841             $worksheet->add_sparkline(
5842             {
5843             location => 'F2',
5844             range => 'Sheet1!A2:E2',
5845             type => 'column',
5846             style => 12,
5847             }
5848             );
5849              
5850             =begin html
5851              
5852             <p><center><img src="http://jmcnamara.github.io/excel-writer-xlsx/images/examples/sparklines1.jpg" alt="Sparklines example."/></center></p>
5853              
5854             =end html
5855              
5856             B<Note:> Sparklines are a feature of Excel 2010+ only. You can write them to an XLSX file that can be read by Excel 2007 but they won't be displayed.
5857              
5858              
5859             =head2 add_sparkline( { parameter => 'value', ... } )
5860              
5861             The C<add_sparkline()> worksheet method is used to add sparklines to a cell or a range of cells.
5862              
5863             The parameters to C<add_sparkline()> must be passed in a hash ref. The main sparkline parameters are:
5864              
5865             location (required)
5866             range (required)
5867             type
5868             style
5869              
5870             markers
5871             negative_points
5872             axis
5873             reverse
5874              
5875             Other, less commonly used parameters are:
5876              
5877             high_point
5878             low_point
5879             first_point
5880             last_point
5881             max
5882             min
5883             empty_cells
5884             show_hidden
5885             date_axis
5886             weight
5887              
5888             series_color
5889             negative_color
5890             markers_color
5891             first_color
5892             last_color
5893             high_color
5894             low_color
5895              
5896             These parameters are explained in the sections below:
5897              
5898             =head2 location
5899              
5900             This is the cell where the sparkline will be displayed:
5901              
5902             location => 'F1'
5903              
5904             The C<location> should be a single cell. (For multiple cells see L<Grouped Sparklines> below).
5905              
5906             To specify the location in row-column notation use the C<xl_rowcol_to_cell()> function from the L<Excel::Writer::XLSX::Utility> module.
5907              
5908             use Excel::Writer::XLSX::Utility ':rowcol';
5909             ...
5910             location => xl_rowcol_to_cell( 0, 5 ), # F1
5911              
5912              
5913             =head2 range
5914              
5915             This specifies the cell data range that the sparkline will plot:
5916              
5917             $worksheet->add_sparkline(
5918             {
5919             location => 'F1',
5920             range => 'A1:E1',
5921             }
5922             );
5923              
5924             The C<range> should be a 2D array. (For 3D arrays of cells see L<Grouped Sparklines> below).
5925              
5926             If C<range> is not on the same worksheet you can specify its location using the usual Excel notation:
5927              
5928             range => 'Sheet1!A1:E1',
5929              
5930             If the worksheet contains spaces or special characters you should quote the worksheet name in the same way that Excel does:
5931              
5932             range => q('Monthly Data'!A1:E1),
5933              
5934             To specify the location in row-column notation use the C<xl_range()> or C<xl_range_formula()> functions from the L<Excel::Writer::XLSX::Utility> module.
5935              
5936             use Excel::Writer::XLSX::Utility ':rowcol';
5937             ...
5938             range => xl_range( 1, 1, 0, 4 ), # 'A1:E1'
5939             range => xl_range_formula( 'Sheet1', 0, 0, 0, 4 ), # 'Sheet1!A2:E2'
5940              
5941             =head2 type
5942              
5943             Specifies the type of sparkline. There are 3 available sparkline types:
5944              
5945             line (default)
5946             column
5947             win_loss
5948              
5949             For example:
5950              
5951             {
5952             location => 'F1',
5953             range => 'A1:E1',
5954             type => 'column',
5955             }
5956              
5957              
5958             =head2 style
5959              
5960             Excel provides 36 built-in Sparkline styles in 6 groups of 6. The C<style> parameter can be used to replicate these and should be a corresponding number from 1 .. 36.
5961              
5962             {
5963             location => 'A14',
5964             range => 'Sheet2!A2:J2',
5965             style => 3,
5966             }
5967              
5968             The style number starts in the top left of the style grid and runs left to right. The default style is 1. It is possible to override colour elements of the sparklines using the C<*_color> parameters below.
5969              
5970             =head2 markers
5971              
5972             Turn on the markers for C<line> style sparklines.
5973              
5974             {
5975             location => 'A6',
5976             range => 'Sheet2!A1:J1',
5977             markers => 1,
5978             }
5979              
5980             Markers aren't shown in Excel for C<column> and C<win_loss> sparklines.
5981              
5982             =head2 negative_points
5983              
5984             Highlight negative values in a sparkline range. This is usually required with C<win_loss> sparklines.
5985              
5986             {
5987             location => 'A21',
5988             range => 'Sheet2!A3:J3',
5989             type => 'win_loss',
5990             negative_points => 1,
5991             }
5992              
5993             =head2 axis
5994              
5995             Display a horizontal axis in the sparkline:
5996              
5997             {
5998             location => 'A10',
5999             range => 'Sheet2!A1:J1',
6000             axis => 1,
6001             }
6002              
6003             =head2 reverse
6004              
6005             Plot the data from right-to-left instead of the default left-to-right:
6006              
6007             {
6008             location => 'A24',
6009             range => 'Sheet2!A4:J4',
6010             type => 'column',
6011             reverse => 1,
6012             }
6013              
6014             =head2 weight
6015              
6016             Adjust the default line weight (thickness) for C<line> style sparklines.
6017              
6018             weight => 0.25,
6019              
6020             The weight value should be one of the following values allowed by Excel:
6021              
6022             0.25 0.5 0.75
6023             1 1.25
6024             2.25
6025             3
6026             4.25
6027             6
6028              
6029             =head2 high_point, low_point, first_point, last_point
6030              
6031             Highlight points in a sparkline range.
6032              
6033             high_point => 1,
6034             low_point => 1,
6035             first_point => 1,
6036             last_point => 1,
6037              
6038              
6039             =head2 max, min
6040              
6041             Specify the maximum and minimum vertical axis values:
6042              
6043             max => 0.5,
6044             min => -0.5,
6045              
6046             As a special case you can set the maximum and minimum to be for a group of sparklines rather than one:
6047              
6048             max => 'group',
6049              
6050             See L<Grouped Sparklines> below.
6051              
6052             =head2 empty_cells
6053              
6054             Define how empty cells are handled in a sparkline.
6055              
6056             empty_cells => 'zero',
6057              
6058             The available options are:
6059              
6060             gaps : show empty cells as gaps (the default).
6061             zero : plot empty cells as 0.
6062             connect: Connect points with a line ("line" type sparklines only).
6063              
6064             =head2 show_hidden
6065              
6066             Plot data in hidden rows and columns:
6067              
6068             show_hidden => 1,
6069              
6070             Note, this option is off by default.
6071              
6072             =head2 date_axis
6073              
6074             Specify an alternative date axis for the sparkline. This is useful if the data being plotted isn't at fixed width intervals:
6075              
6076             {
6077             location => 'F3',
6078             range => 'A3:E3',
6079             date_axis => 'A4:E4',
6080             }
6081              
6082             The number of cells in the date range should correspond to the number of cells in the data range.
6083              
6084              
6085             =head2 series_color
6086              
6087             It is possible to override the colour of a sparkline style using the following parameters:
6088              
6089             series_color
6090             negative_color
6091             markers_color
6092             first_color
6093             last_color
6094             high_color
6095             low_color
6096              
6097             The color should be specified as a HTML style C<#rrggbb> hex value:
6098              
6099             {
6100             location => 'A18',
6101             range => 'Sheet2!A2:J2',
6102             type => 'column',
6103             series_color => '#E965E0',
6104             }
6105              
6106             =head2 Grouped Sparklines
6107              
6108             The C<add_sparkline()> worksheet method can be used multiple times to write as many sparklines as are required in a worksheet.
6109              
6110             However, it is sometimes necessary to group contiguous sparklines so that changes that are applied to one are applied to all. In Excel this is achieved by selecting a 3D range of cells for the data C<range> and a 2D range of cells for the C<location>.
6111              
6112             In Excel::Writer::XLSX, you can simulate this by passing an array refs of values to C<location> and C<range>:
6113              
6114             {
6115             location => [ 'A27', 'A28', 'A29' ],
6116             range => [ 'Sheet2!A5:J5', 'Sheet2!A6:J6', 'Sheet2!A7:J7' ],
6117             markers => 1,
6118             }
6119              
6120             =head2 Sparkline examples
6121              
6122             See the C<sparklines1.pl> and C<sparklines2.pl> example programs in the C<examples> directory of the distro.
6123              
6124              
6125              
6126              
6127             =head1 TABLES IN EXCEL
6128              
6129             Tables in Excel are a way of grouping a range of cells into a single entity that has common formatting or that can be referenced from formulas. Tables can have column headers, autofilters, total rows, column formulas and default formatting.
6130              
6131             =begin html
6132              
6133             <p><center><img src="http://jmcnamara.github.io/excel-writer-xlsx/images/examples/tables.jpg" width="640" height="420" alt="Output from tables.pl" /></center></p>
6134              
6135             =end html
6136              
6137              
6138             For more information see "An Overview of Excel Tables" L<http://office.microsoft.com/en-us/excel-help/overview-of-excel-tables-HA010048546.aspx>.
6139              
6140             Note, tables don't work in Excel::Writer::XLSX when C<set_optimization()> mode in on.
6141              
6142              
6143             =head2 add_table( $row1, $col1, $row2, $col2, { parameter => 'value', ... })
6144              
6145             Tables are added to a worksheet using the C<add_table()> method:
6146              
6147             $worksheet->add_table( 'B3:F7', { %parameters } );
6148              
6149             The data range can be specified in 'A1' or 'row/col' notation (see also the note about L</Cell notation> for more information):
6150              
6151              
6152             $worksheet->add_table( 'B3:F7' );
6153             # Same as:
6154             $worksheet->add_table( 2, 1, 6, 5 );
6155              
6156             The last parameter in C<add_table()> should be a hash ref containing the parameters that describe the table options and data. The available parameters are:
6157              
6158             data
6159             autofilter
6160             header_row
6161             banded_columns
6162             banded_rows
6163             first_column
6164             last_column
6165             style
6166             total_row
6167             columns
6168             name
6169              
6170             The table parameters are detailed below. There are no required parameters and the hash ref isn't required if no options are specified.
6171              
6172              
6173              
6174             =head2 data
6175              
6176             The C<data> parameter can be used to specify the data in the cells of the table.
6177              
6178             my $data = [
6179             [ 'Apples', 10000, 5000, 8000, 6000 ],
6180             [ 'Pears', 2000, 3000, 4000, 5000 ],
6181             [ 'Bananas', 6000, 6000, 6500, 6000 ],
6182             [ 'Oranges', 500, 300, 200, 700 ],
6183              
6184             ];
6185              
6186             $worksheet->add_table( 'B3:F7', { data => $data } );
6187              
6188             Table data can also be written separately, as an array or individual cells.
6189              
6190             # These two statements are the same as the single statement above.
6191             $worksheet->add_table( 'B3:F7' );
6192             $worksheet->write_col( 'B4', $data );
6193              
6194             Writing the cell data separately is occasionally required when you need to control the C<write_*()> method used to populate the cells or if you wish to tweak the cell formatting.
6195              
6196             The C<data> structure should be an array ref of array refs holding row data as shown above.
6197              
6198             =head2 header_row
6199              
6200             The C<header_row> parameter can be used to turn on or off the header row in the table. It is on by default.
6201              
6202             $worksheet->add_table( 'B4:F7', { header_row => 0 } ); # Turn header off.
6203              
6204             The header row will contain default captions such as C<Column 1>, C<Column 2>, etc. These captions can be overridden using the C<columns> parameter below.
6205              
6206              
6207             =head2 autofilter
6208              
6209             The C<autofilter> parameter can be used to turn on or off the autofilter in the header row. It is on by default.
6210              
6211             $worksheet->add_table( 'B3:F7', { autofilter => 0 } ); # Turn autofilter off.
6212              
6213             The C<autofilter> is only shown if the C<header_row> is on. Filters within the table are not supported.
6214              
6215              
6216             =head2 banded_rows
6217              
6218             The C<banded_rows> parameter can be used to used to create rows of alternating colour in the table. It is on by default.
6219              
6220             $worksheet->add_table( 'B3:F7', { banded_rows => 0 } );
6221              
6222              
6223             =head2 banded_columns
6224              
6225             The C<banded_columns> parameter can be used to used to create columns of alternating colour in the table. It is off by default.
6226              
6227             $worksheet->add_table( 'B3:F7', { banded_columns => 1 } );
6228              
6229              
6230             =head2 first_column
6231              
6232             The C<first_column> parameter can be used to highlight the first column of the table. The type of highlighting will depend on the C<style> of the table. It may be bold text or a different colour. It is off by default.
6233              
6234             $worksheet->add_table( 'B3:F7', { first_column => 1 } );
6235              
6236              
6237             =head2 last_column
6238              
6239             The C<last_column> parameter can be used to highlight the last column of the table. The type of highlighting will depend on the C<style> of the table. It may be bold text or a different colour. It is off by default.
6240              
6241             $worksheet->add_table( 'B3:F7', { last_column => 1 } );
6242              
6243              
6244             =head2 style
6245              
6246             The C<style> parameter can be used to set the style of the table. Standard Excel table format names should be used (with matching capitalisation):
6247              
6248             $worksheet11->add_table(
6249             'B3:F7',
6250             {
6251             data => $data,
6252             style => 'Table Style Light 11',
6253             }
6254             );
6255              
6256             The default table style is 'Table Style Medium 9'.
6257              
6258             You can also turn the table style off by setting it to 'None':
6259              
6260             $worksheet11->add_table( 'B3:F7', { style => 'None' } );
6261              
6262              
6263              
6264             =head2 name
6265              
6266             By default tables are named C<Table1>, C<Table2>, etc. The C<name> parameter can be used to set the name of the table:
6267              
6268             $worksheet->add_table( 'B3:F7', { name => 'SalesData' } );
6269              
6270             If you override the table name you must ensure that it doesn't clash with an existing table name and that it follows Excel's requirements for table names L<http://office.microsoft.com/en-001/excel-help/define-and-use-names-in-formulas-HA010147120.aspx#BMsyntax_rules_for_names>.
6271              
6272             If you need to know the name of the table, for example to use it in a formula, you can get it as follows:
6273              
6274             my $table = $worksheet2->add_table( 'B3:F7' );
6275             my $table_name = $table->{_name};
6276              
6277              
6278             =head2 total_row
6279              
6280             The C<total_row> parameter can be used to turn on the total row in the last row of a table. It is distinguished from the other rows by a different formatting and also with dropdown C<SUBTOTAL> functions.
6281              
6282             $worksheet->add_table( 'B3:F7', { total_row => 1 } );
6283              
6284             The default total row doesn't have any captions or functions. These must by specified via the C<columns> parameter below.
6285              
6286             =head2 columns
6287              
6288             The C<columns> parameter can be used to set properties for columns within the table.
6289              
6290             The sub-properties that can be set are:
6291              
6292             header
6293             formula
6294             total_string
6295             total_function
6296             total_value
6297             format
6298             header_format
6299              
6300             The column data must be specified as an array ref of hash refs. For example to override the default 'Column n' style table headers:
6301              
6302             $worksheet->add_table(
6303             'B3:F7',
6304             {
6305             data => $data,
6306             columns => [
6307             { header => 'Product' },
6308             { header => 'Quarter 1' },
6309             { header => 'Quarter 2' },
6310             { header => 'Quarter 3' },
6311             { header => 'Quarter 4' },
6312             ]
6313             }
6314             );
6315              
6316             If you don't wish to specify properties for a specific column you pass an empty hash ref and the defaults will be applied:
6317              
6318             ...
6319             columns => [
6320             { header => 'Product' },
6321             { header => 'Quarter 1' },
6322             { }, # Defaults to 'Column 3'.
6323             { header => 'Quarter 3' },
6324             { header => 'Quarter 4' },
6325             ]
6326             ...
6327              
6328              
6329             Column formulas can by applied using the C<formula> column property:
6330              
6331             $worksheet8->add_table(
6332             'B3:G7',
6333             {
6334             data => $data,
6335             columns => [
6336             { header => 'Product' },
6337             { header => 'Quarter 1' },
6338             { header => 'Quarter 2' },
6339             { header => 'Quarter 3' },
6340             { header => 'Quarter 4' },
6341             {
6342             header => 'Year',
6343             formula => '=SUM(Table8[@[Quarter 1]:[Quarter 4]])'
6344             },
6345             ]
6346             }
6347             );
6348              
6349             The Excel 2007 C<[#This Row]> and Excel 2010 C<@> structural references are supported within the formula.
6350              
6351             As stated above the C<total_row> table parameter turns on the "Total" row in the table but it doesn't populate it with any defaults. Total captions and functions must be specified via the C<columns> property and the C<total_string>, C<total_function> and C<total_value> sub properties:
6352              
6353             $worksheet10->add_table(
6354             'B3:F8',
6355             {
6356             data => $data,
6357             total_row => 1,
6358             columns => [
6359             { header => 'Product', total_string => 'Totals' },
6360             { header => 'Quarter 1', total_function => 'sum' },
6361             { header => 'Quarter 2', total_function => 'sum' },
6362             { header => 'Quarter 3', total_function => 'sum' },
6363             { header => 'Quarter 4', total_function => 'sum' },
6364             ]
6365             }
6366             );
6367              
6368             The supported totals row C<SUBTOTAL> functions are:
6369              
6370             average
6371             count_nums
6372             count
6373             max
6374             min
6375             std_dev
6376             sum
6377             var
6378              
6379             User defined functions or formulas aren't supported.
6380              
6381             It is also possible to set a calculated value for the C<total_function> using the C<total_value> sub property. This is only necessary when creating workbooks for applications that cannot calculate the value of formulas automatically. This is similar to setting the C<value> optional property in C<write_formula()>:
6382              
6383             $worksheet10->add_table(
6384             'B3:F8',
6385             {
6386             data => $data,
6387             total_row => 1,
6388             columns => [
6389             { total_string => 'Totals' },
6390             { total_function => 'sum', total_value => 100 },
6391             { total_function => 'sum', total_value => 200 },
6392             { total_function => 'sum', total_value => 100 },
6393             { total_function => 'sum', total_value => 400 },
6394             ]
6395             }
6396             );
6397              
6398              
6399              
6400              
6401             Formatting can also be applied to columns, to the column data using C<format> and to the header using C<header_format>:
6402              
6403             my $currency_format = $workbook->add_format( num_format => '$#,##0' );
6404              
6405             $worksheet->add_table(
6406             'B3:D8',
6407             {
6408             data => $data,
6409             total_row => 1,
6410             columns => [
6411             { header => 'Product', total_string => 'Totals' },
6412             {
6413             header => 'Quarter 1',
6414             total_function => 'sum',
6415             format => $currency_format,
6416             },
6417             {
6418             header => 'Quarter 2',
6419             header_format => $bold,
6420             total_function => 'sum',
6421             format => $currency_format,
6422             },
6423             ]
6424             }
6425             );
6426              
6427             Standard Excel::Writer::XLSX format objects can be used. However, they should be limited to numerical formats for the columns and simple formatting like text wrap for the headers. Overriding other table formatting may produce inconsistent results.
6428              
6429              
6430              
6431             =head1 FORMULAS AND FUNCTIONS IN EXCEL
6432              
6433              
6434              
6435              
6436             =head2 Introduction
6437              
6438             The following is a brief introduction to formulas and functions in Excel and Excel::Writer::XLSX.
6439              
6440             A formula is a string that begins with an equals sign:
6441              
6442             '=A1+B1'
6443             '=AVERAGE(1, 2, 3)'
6444              
6445             The formula can contain numbers, strings, boolean values, cell references, cell ranges and functions. Named ranges are not supported. Formulas should be written as they appear in Excel, that is cells and functions must be in uppercase.
6446              
6447             Cells in Excel are referenced using the A1 notation system where the column is designated by a letter and the row by a number. Columns range from A to XFD i.e. 0 to 16384, rows range from 1 to 1048576. The C<Excel::Writer::XLSX::Utility> module that is included in the distro contains helper functions for dealing with A1 notation, for example:
6448              
6449             use Excel::Writer::XLSX::Utility;
6450              
6451             ( $row, $col ) = xl_cell_to_rowcol( 'C2' ); # (1, 2)
6452             $str = xl_rowcol_to_cell( 1, 2 ); # C2
6453              
6454             The Excel C<$> notation in cell references is also supported. This allows you to specify whether a row or column is relative or absolute. This only has an effect if the cell is copied. The following examples show relative and absolute values.
6455              
6456             '=A1' # Column and row are relative
6457             '=$A1' # Column is absolute and row is relative
6458             '=A$1' # Column is relative and row is absolute
6459             '=$A$1' # Column and row are absolute
6460              
6461             Formulas can also refer to cells in other worksheets of the current workbook. For example:
6462              
6463             '=Sheet2!A1'
6464             '=Sheet2!A1:A5'
6465             '=Sheet2:Sheet3!A1'
6466             '=Sheet2:Sheet3!A1:A5'
6467             q{='Test Data'!A1}
6468             q{='Test Data1:Test Data2'!A1}
6469              
6470             The sheet reference and the cell reference are separated by C<!> the exclamation mark symbol. If worksheet names contain spaces, commas or parentheses then Excel requires that the name is enclosed in single quotes as shown in the last two examples above. In order to avoid using a lot of escape characters you can use the quote operator C<q{}> to protect the quotes. See C<perlop> in the main Perl documentation. Only valid sheet names that have been added using the C<add_worksheet()> method can be used in formulas. You cannot reference external workbooks.
6471              
6472              
6473             The following table lists the operators that are available in Excel's formulas. The majority of the operators are the same as Perl's, differences are indicated:
6474              
6475             Arithmetic operators:
6476             =====================
6477             Operator Meaning Example
6478             + Addition 1+2
6479             - Subtraction 2-1
6480             * Multiplication 2*3
6481             / Division 1/4
6482             ^ Exponentiation 2^3 # Equivalent to **
6483             - Unary minus -(1+2)
6484             % Percent (Not modulus) 13%
6485              
6486              
6487             Comparison operators:
6488             =====================
6489             Operator Meaning Example
6490             = Equal to A1 = B1 # Equivalent to ==
6491             <> Not equal to A1 <> B1 # Equivalent to !=
6492             > Greater than A1 > B1
6493             < Less than A1 < B1
6494             >= Greater than or equal to A1 >= B1
6495             <= Less than or equal to A1 <= B1
6496              
6497              
6498             String operator:
6499             ================
6500             Operator Meaning Example
6501             & Concatenation "Hello " & "World!" # [1]
6502              
6503              
6504             Reference operators:
6505             ====================
6506             Operator Meaning Example
6507             : Range operator A1:A4 # [2]
6508             , Union operator SUM(1, 2+2, B3) # [3]
6509              
6510              
6511             Notes:
6512             [1]: Equivalent to "Hello " . "World!" in Perl.
6513             [2]: This range is equivalent to cells A1, A2, A3 and A4.
6514             [3]: The comma behaves like the list separator in Perl.
6515              
6516             The range and comma operators can have different symbols in non-English versions of Excel, see below.
6517              
6518             For a general introduction to Excel's formulas and an explanation of the syntax of the function refer to the Excel help files or the following: L<http://office.microsoft.com/en-us/assistance/CH062528031033.aspx>.
6519              
6520             In most cases a formula in Excel can be used directly in the C<write_formula> method. However, there are a few potential issues and differences that the user should be aware of. These are explained in the following sections.
6521              
6522              
6523             =head2 Non US Excel functions and syntax
6524              
6525              
6526             Excel stores formulas in the format of the US English version, regardless of the language or locale of the end-user's version of Excel. Therefore all formula function names written using Excel::Writer::XLSX must be in English:
6527              
6528             worksheet->write_formula('A1', '=SUM(1, 2, 3)'); # OK
6529             worksheet->write_formula('A2', '=SOMME(1, 2, 3)'); # French. Error on load.
6530              
6531             Also, formulas must be written with the US style separator/range operator which is a comma (not semi-colon). Therefore a formula with multiple values should be written as follows:
6532              
6533             worksheet->write_formula('A1', '=SUM(1, 2, 3)'); # OK
6534             worksheet->write_formula('A2', '=SUM(1; 2; 3)'); # Semi-colon. Error on load.
6535              
6536             If you have a non-English version of Excel you can use the following multi-lingual Formula Translator (L<http://en.excel-translator.de/language/>) to help you convert the formula. It can also replace semi-colons with commas.
6537              
6538              
6539             =head2 Formulas added in Excel 2010 and later
6540              
6541             Excel 2010 and later added functions which weren't defined in the original file specification. These functions are referred to by Microsoft as I<future> functions. Examples of these functions are C<ACOT>, C<CHISQ.DIST.RT> , C<CONFIDENCE.NORM>, C<STDEV.P>, C<STDEV.S> and C<WORKDAY.INTL>.
6542              
6543             When written using C<write_formula()> these functions need to be fully qualified with a C<_xlfn.> (or other) prefix as they are shown the list below. For example:
6544              
6545             worksheet->write_formula('A1', '=_xlfn.STDEV.S(B1:B10)')
6546              
6547             They will appear without the prefix in Excel.
6548              
6549             The following list is taken from the MS XLSX extensions documentation on future functions: L<http://msdn.microsoft.com/en-us/library/dd907480%28v=office.12%29.aspx>:
6550              
6551             _xlfn.ACOT
6552             _xlfn.ACOTH
6553             _xlfn.AGGREGATE
6554             _xlfn.ARABIC
6555             _xlfn.BASE
6556             _xlfn.BETA.DIST
6557             _xlfn.BETA.INV
6558             _xlfn.BINOM.DIST
6559             _xlfn.BINOM.DIST.RANGE
6560             _xlfn.BINOM.INV
6561             _xlfn.BITAND
6562             _xlfn.BITLSHIFT
6563             _xlfn.BITOR
6564             _xlfn.BITRSHIFT
6565             _xlfn.BITXOR
6566             _xlfn.CEILING.MATH
6567             _xlfn.CEILING.PRECISE
6568             _xlfn.CHISQ.DIST
6569             _xlfn.CHISQ.DIST.RT
6570             _xlfn.CHISQ.INV
6571             _xlfn.CHISQ.INV.RT
6572             _xlfn.CHISQ.TEST
6573             _xlfn.COMBINA
6574             _xlfn.CONFIDENCE.NORM
6575             _xlfn.CONFIDENCE.T
6576             _xlfn.COT
6577             _xlfn.COTH
6578             _xlfn.COVARIANCE.P
6579             _xlfn.COVARIANCE.S
6580             _xlfn.CSC
6581             _xlfn.CSCH
6582             _xlfn.DAYS
6583             _xlfn.DECIMAL
6584             ECMA.CEILING
6585             _xlfn.ERF.PRECISE
6586             _xlfn.ERFC.PRECISE
6587             _xlfn.EXPON.DIST
6588             _xlfn.F.DIST
6589             _xlfn.F.DIST.RT
6590             _xlfn.F.INV
6591             _xlfn.F.INV.RT
6592             _xlfn.F.TEST
6593             _xlfn.FILTERXML
6594             _xlfn.FLOOR.MATH
6595             _xlfn.FLOOR.PRECISE
6596             _xlfn.FORECAST.ETS
6597             _xlfn.FORECAST.ETS.CONFINT
6598             _xlfn.FORECAST.ETS.SEASONALITY
6599             _xlfn.FORECAST.ETS.STAT
6600             _xlfn.FORECAST.LINEAR
6601             _xlfn.FORMULATEXT
6602             _xlfn.GAMMA
6603             _xlfn.GAMMA.DIST
6604             _xlfn.GAMMA.INV
6605             _xlfn.GAMMALN.PRECISE
6606             _xlfn.GAUSS
6607             _xlfn.HYPGEOM.DIST
6608             _xlfn.IFNA
6609             _xlfn.IMCOSH
6610             _xlfn.IMCOT
6611             _xlfn.IMCSC
6612             _xlfn.IMCSCH
6613             _xlfn.IMSEC
6614             _xlfn.IMSECH
6615             _xlfn.IMSINH
6616             _xlfn.IMTAN
6617             _xlfn.ISFORMULA
6618             ISO.CEILING
6619             _xlfn.ISOWEEKNUM
6620             _xlfn.LOGNORM.DIST
6621             _xlfn.LOGNORM.INV
6622             _xlfn.MODE.MULT
6623             _xlfn.MODE.SNGL
6624             _xlfn.MUNIT
6625             _xlfn.NEGBINOM.DIST
6626             NETWORKDAYS.INTL
6627             _xlfn.NORM.DIST
6628             _xlfn.NORM.INV
6629             _xlfn.NORM.S.DIST
6630             _xlfn.NORM.S.INV
6631             _xlfn.NUMBERVALUE
6632             _xlfn.PDURATION
6633             _xlfn.PERCENTILE.EXC
6634             _xlfn.PERCENTILE.INC
6635             _xlfn.PERCENTRANK.EXC
6636             _xlfn.PERCENTRANK.INC
6637             _xlfn.PERMUTATIONA
6638             _xlfn.PHI
6639             _xlfn.POISSON.DIST
6640             _xlfn.QUARTILE.EXC
6641             _xlfn.QUARTILE.INC
6642             _xlfn.QUERYSTRING
6643             _xlfn.RANK.AVG
6644             _xlfn.RANK.EQ
6645             _xlfn.RRI
6646             _xlfn.SEC
6647             _xlfn.SECH
6648             _xlfn.SHEET
6649             _xlfn.SHEETS
6650             _xlfn.SKEW.P
6651             _xlfn.STDEV.P
6652             _xlfn.STDEV.S
6653             _xlfn.T.DIST
6654             _xlfn.T.DIST.2T
6655             _xlfn.T.DIST.RT
6656             _xlfn.T.INV
6657             _xlfn.T.INV.2T
6658             _xlfn.T.TEST
6659             _xlfn.UNICHAR
6660             _xlfn.UNICODE
6661             _xlfn.VAR.P
6662             _xlfn.VAR.S
6663             _xlfn.WEBSERVICE
6664             _xlfn.WEIBULL.DIST
6665             WORKDAY.INTL
6666             _xlfn.XOR
6667             _xlfn.Z.TEST
6668              
6669              
6670             =head2 Using Tables in Formulas
6671              
6672             Worksheet tables can be added with Excel::Writer::XLSX using the C<add_table()> method:
6673              
6674             worksheet->add_table('B3:F7', {options});
6675              
6676             By default tables are named C<Table1>, C<Table2>, etc., in the order that they are added. However it can also be set by the user using the C<name> parameter:
6677              
6678             worksheet->add_table('B3:F7', {'name': 'SalesData'});
6679              
6680             If you need to know the name of the table, for example to use it in a formula,
6681             you can get it as follows:
6682              
6683             table = worksheet->add_table('B3:F7');
6684             table_name = table->{_name};
6685              
6686             When used in a formula a table name such as C<TableX> should be referred to as C<TableX[]> (like a Perl array):
6687              
6688             worksheet->write_formula('A5', '=VLOOKUP("Sales", Table1[], 2, FALSE');
6689              
6690              
6691             =head2 Dealing with #NAME? errors
6692              
6693             If there is an error in the syntax of a formula it is usually displayed in
6694             Excel as C<#NAME?>. If you encounter an error like this you can debug it as
6695             follows:
6696              
6697             =over
6698              
6699             =item 1. Ensure the formula is valid in Excel by copying and pasting it into a cell. Note, this should be done in Excel and not other applications such as OpenOffice or LibreOffice since they may have slightly different syntax.
6700              
6701             =item 2. Ensure the formula is using comma separators instead of semi-colons, see L<Non US Excel functions and syntax> above.
6702              
6703             =item 3. Ensure the formula is in English, see L<Non US Excel functions and syntax> above.
6704              
6705             =item 4. Ensure that the formula doesn't contain an Excel 2010+ future function as listed in L<Formulas added in Excel 2010 and later> above. If it does then ensure that the correct prefix is used.
6706              
6707             =back
6708              
6709             Finally if you have completed all the previous steps and still get a C<#NAME?> error you can examine a valid Excel file to see what the correct syntax should be. To do this you should create a valid formula in Excel and save the file. You can then examine the XML in the unzipped file.
6710              
6711             The following shows how to do that using Linux C<unzip> and libxml's xmllint
6712             L<http://xmlsoft.org/xmllint.html> to format the XML for clarity:
6713              
6714             $ unzip myfile.xlsx -d myfile
6715             $ xmllint --format myfile/xl/worksheets/sheet1.xml | grep '<f>'
6716              
6717             <f>SUM(1, 2, 3)</f>
6718              
6719              
6720             =head2 Formula Results
6721              
6722             Excel::Writer::XLSX doesn't calculate the result of a formula and instead stores the value 0 as the formula result. It then sets a global flag in the XLSX file to say that all formulas and functions should be recalculated when the file is opened.
6723              
6724             This is the method recommended in the Excel documentation and in general it works fine with spreadsheet applications. However, applications that don't have a facility to calculate formulas will only display the 0 results. Examples of such applications are Excel Viewer, PDF Converters, and some mobile device applications.
6725              
6726             If required, it is also possible to specify the calculated result of the
6727             formula using the optional last C<value> parameter in C<write_formula>:
6728              
6729             worksheet->write_formula('A1', '=2+2', num_format, 4);
6730              
6731             The C<value> parameter can be a number, a string, a boolean sting (C<'TRUE'> or C<'FALSE'>) or one of the following Excel error codes:
6732              
6733             #DIV/0!
6734             #N/A
6735             #NAME?
6736             #NULL!
6737             #NUM!
6738             #REF!
6739             #VALUE!
6740              
6741             It is also possible to specify the calculated result of an array formula created with C<write_array_formula>:
6742              
6743             # Specify the result for a single cell range.
6744             worksheet->write_array_formula('A1:A1', '{=SUM(B1:C1*B2:C2)}', format, 2005);
6745              
6746             However, using this parameter only writes a single value to the upper left cell in the result array. For a multi-cell array formula where the results are required, the other result values can be specified by using C<write_number()> to write to the appropriate cell:
6747              
6748             # Specify the results for a multi cell range.
6749             worksheet->write_array_formula('A1:A3', '{=TREND(C1:C3,B1:B3)}', format, 15);
6750             worksheet->write_number('A2', 12, format);
6751             worksheet->write_number('A3', 14, format);
6752              
6753              
6754              
6755              
6756             =head1 WORKING WITH VBA MACROS
6757              
6758             An Excel C<xlsm> file is exactly the same as a C<xlsx> file except that is includes an additional C<vbaProject.bin> file which contains functions and/or macros. Excel uses a different extension to differentiate between the two file formats since files containing macros are usually subject to additional security checks.
6759              
6760             The C<vbaProject.bin> file is a binary OLE COM container. This was the format used in older C<xls> versions of Excel prior to Excel 2007. Unlike all of the other components of an xlsx/xlsm file the data isn't stored in XML format. Instead the functions and macros as stored as pre-parsed binary format. As such it wouldn't be feasible to define macros and create a C<vbaProject.bin> file from scratch (at least not in the remaining lifespan and interest levels of the author).
6761              
6762             Instead a workaround is used to extract C<vbaProject.bin> files from existing xlsm files and then add these to Excel::Writer::XLSX files.
6763              
6764              
6765             =head2 The extract_vba utility
6766              
6767             The C<extract_vba> utility is used to extract the C<vbaProject.bin> binary from an Excel 2007+ xlsm file. The utility is included in the Excel::Writer::XLSX bin directory and is also installed as a standalone executable file:
6768              
6769             $ extract_vba macro_file.xlsm
6770             Extracted: vbaProject.bin
6771              
6772              
6773             =head2 Adding the VBA macros to a Excel::Writer::XLSX file
6774              
6775             Once the C<vbaProject.bin> file has been extracted it can be added to the Excel::Writer::XLSX workbook using the C<add_vba_project()> method:
6776              
6777             $workbook->add_vba_project( './vbaProject.bin' );
6778              
6779             If the VBA file contains functions you can then refer to them in calculations using C<write_formula>:
6780              
6781             $worksheet->write_formula( 'A1', '=MyMortgageCalc(200000, 25)' );
6782              
6783             Excel files that contain functions and macros should use an C<xlsm> extension or else Excel will complain and possibly not open the file:
6784              
6785             my $workbook = Excel::Writer::XLSX->new( 'file.xlsm' );
6786              
6787             It is also possible to assign a macro to a button that is inserted into a
6788             worksheet using the C<insert_button()> method:
6789              
6790             my $workbook = Excel::Writer::XLSX->new( 'file.xlsm' );
6791             ...
6792             $workbook->add_vba_project( './vbaProject.bin' );
6793              
6794             $worksheet->insert_button( 'C2', { macro => 'my_macro' } );
6795              
6796              
6797             It may be necessary to specify a more explicit macro name prefixed by the workbook VBA name as follows:
6798              
6799             $worksheet->insert_button( 'C2', { macro => 'ThisWorkbook.my_macro' } );
6800              
6801             See the C<macros.pl> from the examples directory for a working example.
6802              
6803             Note: Button is the only VBA Control supported by Excel::Writer::XLSX. Due to the large effort in implementation (1+ man months) it is unlikely that any other form elements will be added in the future.
6804              
6805              
6806             =head2 Setting the VBA codenames
6807              
6808             VBA macros generally refer to workbook and worksheet objects. If the VBA codenames aren't specified then Excel::Writer::XLSX will use the Excel defaults of C<ThisWorkbook> and C<Sheet1>, C<Sheet2> etc.
6809              
6810             If the macro uses other codenames you can set them using the workbook and worksheet C<set_vba_name()> methods as follows:
6811              
6812             $workbook->set_vba_name( 'MyWorkbook' );
6813             $worksheet->set_vba_name( 'MySheet' );
6814              
6815             You can find the names that are used in the VBA editor or by unzipping the C<xlsm> file and grepping the files. The following shows how to do that using libxml's xmllint L<http://xmlsoft.org/xmllint.html> to format the XML for clarity:
6816              
6817             $ unzip myfile.xlsm -d myfile
6818             $ xmllint --format `find myfile -name "*.xml" | xargs` | grep "Pr.*codeName"
6819              
6820             <workbookPr codeName="MyWorkbook" defaultThemeVersion="124226"/>
6821             <sheetPr codeName="MySheet"/>
6822              
6823              
6824             Note: This step is particularly important for macros created with non-English versions of Excel.
6825              
6826              
6827              
6828             =head2 What to do if it doesn't work
6829              
6830             This feature should be considered experimental and there is no guarantee that it will work in all cases. Some effort may be required and some knowledge of VBA will certainly help. If things don't work out here are some things to try:
6831              
6832             =over
6833              
6834             =item *
6835              
6836             Start with a simple macro file, ensure that it works and then add complexity.
6837              
6838             =item *
6839              
6840             Try to extract the macros from an Excel 2007 file. The method should work with macros from later versions (it was also tested with Excel 2010 macros). However there may be features in the macro files of more recent version of Excel that aren't backward compatible.
6841              
6842             =item *
6843              
6844             Check the code names that macros use to refer to the workbook and worksheets (see the previous section above). In general VBA uses a code name of C<ThisWorkbook> to refer to the current workbook and the sheet name (such as C<Sheet1>) to refer to the worksheets. These are the defaults used by Excel::Writer::XLSX. If the macro uses other names then you can specify these using the workbook and worksheet C<set_vba_name()> methods:
6845              
6846             $workbook>set_vba_name( 'MyWorkbook' );
6847             $worksheet->set_vba_name( 'MySheet' );
6848              
6849             =back
6850              
6851              
6852             =head1 EXAMPLES
6853              
6854             See L<Excel::Writer::XLSX::Examples> for a full list of examples.
6855              
6856              
6857             =head2 Example 1
6858              
6859             The following example shows some of the basic features of Excel::Writer::XLSX.
6860              
6861              
6862             #!/usr/bin/perl -w
6863              
6864             use strict;
6865             use Excel::Writer::XLSX;
6866              
6867             # Create a new workbook called simple.xlsx and add a worksheet
6868             my $workbook = Excel::Writer::XLSX->new( 'simple.xlsx' );
6869             my $worksheet = $workbook->add_worksheet();
6870              
6871             # The general syntax is write($row, $column, $token). Note that row and
6872             # column are zero indexed
6873              
6874             # Write some text
6875             $worksheet->write( 0, 0, 'Hi Excel!' );
6876              
6877              
6878             # Write some numbers
6879             $worksheet->write( 2, 0, 3 );
6880             $worksheet->write( 3, 0, 3.00000 );
6881             $worksheet->write( 4, 0, 3.00001 );
6882             $worksheet->write( 5, 0, 3.14159 );
6883              
6884              
6885             # Write some formulas
6886             $worksheet->write( 7, 0, '=A3 + A6' );
6887             $worksheet->write( 8, 0, '=IF(A5>3,"Yes", "No")' );
6888              
6889              
6890             # Write a hyperlink
6891             my $hyperlink_format = $workbook->add_format(
6892             color => 'blue',
6893             underline => 1,
6894             );
6895              
6896             $worksheet->write( 10, 0, 'http://www.perl.com/', $hyperlink_format );
6897              
6898             $workbook->close();
6899              
6900             =begin html
6901              
6902             <p><center><img src="http://jmcnamara.github.io/excel-writer-xlsx/images/examples/a_simple.jpg" width="640" height="420" alt="Output from a_simple.pl" /></center></p>
6903              
6904             =end html
6905              
6906              
6907              
6908              
6909             =head2 Example 2
6910              
6911             The following is a general example which demonstrates some features of working with multiple worksheets.
6912              
6913             #!/usr/bin/perl -w
6914              
6915             use strict;
6916             use Excel::Writer::XLSX;
6917              
6918             # Create a new Excel workbook
6919             my $workbook = Excel::Writer::XLSX->new( 'regions.xlsx' );
6920              
6921             # Add some worksheets
6922             my $north = $workbook->add_worksheet( 'North' );
6923             my $south = $workbook->add_worksheet( 'South' );
6924             my $east = $workbook->add_worksheet( 'East' );
6925             my $west = $workbook->add_worksheet( 'West' );
6926              
6927             # Add a Format
6928             my $format = $workbook->add_format();
6929             $format->set_bold();
6930             $format->set_color( 'blue' );
6931              
6932             # Add a caption to each worksheet
6933             for my $worksheet ( $workbook->sheets() ) {
6934             $worksheet->write( 0, 0, 'Sales', $format );
6935             }
6936              
6937             # Write some data
6938             $north->write( 0, 1, 200000 );
6939             $south->write( 0, 1, 100000 );
6940             $east->write( 0, 1, 150000 );
6941             $west->write( 0, 1, 100000 );
6942              
6943             # Set the active worksheet
6944             $south->activate();
6945              
6946             # Set the width of the first column
6947             $south->set_column( 0, 0, 20 );
6948              
6949             # Set the active cell
6950             $south->set_selection( 0, 1 );
6951              
6952             $workbook->close();
6953              
6954             =begin html
6955              
6956             <p><center><img src="http://jmcnamara.github.io/excel-writer-xlsx/images/examples/regions.jpg" width="640" height="420" alt="Output from regions.pl" /></center></p>
6957              
6958             =end html
6959              
6960              
6961              
6962              
6963             =head2 Example 3
6964              
6965             Example of how to add conditional formatting to an Excel::Writer::XLSX file. The example below highlights cells that have a value greater than or equal to 50 in red and cells below that value in green.
6966              
6967             #!/usr/bin/perl
6968              
6969             use strict;
6970             use warnings;
6971             use Excel::Writer::XLSX;
6972              
6973             my $workbook = Excel::Writer::XLSX->new( 'conditional_format.xlsx' );
6974             my $worksheet = $workbook->add_worksheet();
6975              
6976              
6977             # This example below highlights cells that have a value greater than or
6978             # equal to 50 in red and cells below that value in green.
6979              
6980             # Light red fill with dark red text.
6981             my $format1 = $workbook->add_format(
6982             bg_color => '#FFC7CE',
6983             color => '#9C0006',
6984              
6985             );
6986              
6987             # Green fill with dark green text.
6988             my $format2 = $workbook->add_format(
6989             bg_color => '#C6EFCE',
6990             color => '#006100',
6991              
6992             );
6993              
6994             # Some sample data to run the conditional formatting against.
6995             my $data = [
6996             [ 34, 72, 38, 30, 75, 48, 75, 66, 84, 86 ],
6997             [ 6, 24, 1, 84, 54, 62, 60, 3, 26, 59 ],
6998             [ 28, 79, 97, 13, 85, 93, 93, 22, 5, 14 ],
6999             [ 27, 71, 40, 17, 18, 79, 90, 93, 29, 47 ],
7000             [ 88, 25, 33, 23, 67, 1, 59, 79, 47, 36 ],
7001             [ 24, 100, 20, 88, 29, 33, 38, 54, 54, 88 ],
7002             [ 6, 57, 88, 28, 10, 26, 37, 7, 41, 48 ],
7003             [ 52, 78, 1, 96, 26, 45, 47, 33, 96, 36 ],
7004             [ 60, 54, 81, 66, 81, 90, 80, 93, 12, 55 ],
7005             [ 70, 5, 46, 14, 71, 19, 66, 36, 41, 21 ],
7006             ];
7007              
7008             my $caption = 'Cells with values >= 50 are in light red. '
7009             . 'Values < 50 are in light green';
7010              
7011             # Write the data.
7012             $worksheet->write( 'A1', $caption );
7013             $worksheet->write_col( 'B3', $data );
7014              
7015             # Write a conditional format over a range.
7016             $worksheet->conditional_formatting( 'B3:K12',
7017             {
7018             type => 'cell',
7019             criteria => '>=',
7020             value => 50,
7021             format => $format1,
7022             }
7023             );
7024              
7025             # Write another conditional format over the same range.
7026             $worksheet->conditional_formatting( 'B3:K12',
7027             {
7028             type => 'cell',
7029             criteria => '<',
7030             value => 50,
7031             format => $format2,
7032             }
7033             );
7034              
7035             $workbook->close();
7036              
7037             =begin html
7038              
7039              
7040             <p><center><img src="http://jmcnamara.github.io/excel-writer-xlsx/images/examples/conditional_format.jpg" width="640" height="420" alt="Output from conditional_format.pl" /></center></p>
7041              
7042              
7043             =end html
7044              
7045              
7046              
7047              
7048             =head2 Example 4
7049              
7050             The following is a simple example of using functions.
7051              
7052             #!/usr/bin/perl -w
7053              
7054             use strict;
7055             use Excel::Writer::XLSX;
7056              
7057             # Create a new workbook and add a worksheet
7058             my $workbook = Excel::Writer::XLSX->new( 'stats.xlsx' );
7059             my $worksheet = $workbook->add_worksheet( 'Test data' );
7060              
7061             # Set the column width for columns 1
7062             $worksheet->set_column( 0, 0, 20 );
7063              
7064              
7065             # Create a format for the headings
7066             my $format = $workbook->add_format();
7067             $format->set_bold();
7068              
7069              
7070             # Write the sample data
7071             $worksheet->write( 0, 0, 'Sample', $format );
7072             $worksheet->write( 0, 1, 1 );
7073             $worksheet->write( 0, 2, 2 );
7074             $worksheet->write( 0, 3, 3 );
7075             $worksheet->write( 0, 4, 4 );
7076             $worksheet->write( 0, 5, 5 );
7077             $worksheet->write( 0, 6, 6 );
7078             $worksheet->write( 0, 7, 7 );
7079             $worksheet->write( 0, 8, 8 );
7080              
7081             $worksheet->write( 1, 0, 'Length', $format );
7082             $worksheet->write( 1, 1, 25.4 );
7083             $worksheet->write( 1, 2, 25.4 );
7084             $worksheet->write( 1, 3, 24.8 );
7085             $worksheet->write( 1, 4, 25.0 );
7086             $worksheet->write( 1, 5, 25.3 );
7087             $worksheet->write( 1, 6, 24.9 );
7088             $worksheet->write( 1, 7, 25.2 );
7089             $worksheet->write( 1, 8, 24.8 );
7090              
7091             # Write some statistical functions
7092             $worksheet->write( 4, 0, 'Count', $format );
7093             $worksheet->write( 4, 1, '=COUNT(B1:I1)' );
7094              
7095             $worksheet->write( 5, 0, 'Sum', $format );
7096             $worksheet->write( 5, 1, '=SUM(B2:I2)' );
7097              
7098             $worksheet->write( 6, 0, 'Average', $format );
7099             $worksheet->write( 6, 1, '=AVERAGE(B2:I2)' );
7100              
7101             $worksheet->write( 7, 0, 'Min', $format );
7102             $worksheet->write( 7, 1, '=MIN(B2:I2)' );
7103              
7104             $worksheet->write( 8, 0, 'Max', $format );
7105             $worksheet->write( 8, 1, '=MAX(B2:I2)' );
7106              
7107             $worksheet->write( 9, 0, 'Standard Deviation', $format );
7108             $worksheet->write( 9, 1, '=STDEV(B2:I2)' );
7109              
7110             $worksheet->write( 10, 0, 'Kurtosis', $format );
7111             $worksheet->write( 10, 1, '=KURT(B2:I2)' );
7112              
7113             $workbook->close();
7114              
7115             =begin html
7116              
7117             <p><center><img src="http://jmcnamara.github.io/excel-writer-xlsx/images/examples/stats.jpg" width="640" height="420" alt="Output from stats.pl" /></center></p>
7118              
7119             =end html
7120              
7121              
7122              
7123              
7124             =head2 Example 5
7125              
7126             The following example converts a tab separated file called C<tab.txt> into an Excel file called C<tab.xlsx>.
7127              
7128             #!/usr/bin/perl -w
7129              
7130             use strict;
7131             use Excel::Writer::XLSX;
7132              
7133             open( TABFILE, 'tab.txt' ) or die "tab.txt: $!";
7134              
7135             my $workbook = Excel::Writer::XLSX->new( 'tab.xlsx' );
7136             my $worksheet = $workbook->add_worksheet();
7137              
7138             # Row and column are zero indexed
7139             my $row = 0;
7140              
7141             while ( <TABFILE> ) {
7142             chomp;
7143              
7144             # Split on single tab
7145             my @fields = split( '\t', $_ );
7146              
7147             my $col = 0;
7148             for my $token ( @fields ) {
7149             $worksheet->write( $row, $col, $token );
7150             $col++;
7151             }
7152             $row++;
7153             }
7154              
7155             $workbook->close();
7156              
7157             NOTE: This is a simple conversion program for illustrative purposes only. For converting a CSV or Tab separated or any other type of delimited text file to Excel I recommend the more rigorous csv2xls program that is part of H.Merijn Brand's L<Text::CSV_XS> module distro.
7158              
7159             See the examples/csv2xls link here: L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>.
7160              
7161              
7162              
7163              
7164             =head2 Additional Examples
7165              
7166             The following is a description of the example files that are provided
7167             in the standard Excel::Writer::XLSX distribution. They demonstrate the
7168             different features and options of the module. See L<Excel::Writer::XLSX::Examples> for more details.
7169              
7170             Getting started
7171             ===============
7172             a_simple.pl A simple demo of some of the features.
7173             bug_report.pl A template for submitting bug reports.
7174             demo.pl A demo of some of the available features.
7175             formats.pl All the available formatting on several worksheets.
7176             regions.pl A simple example of multiple worksheets.
7177             stats.pl Basic formulas and functions.
7178              
7179              
7180             Intermediate
7181             ============
7182             autofilter.pl Examples of worksheet autofilters.
7183             array_formula.pl Examples of how to write array formulas.
7184             cgi.pl A simple CGI program.
7185             chart_area.pl A demo of area style charts.
7186             chart_bar.pl A demo of bar (vertical histogram) style charts.
7187             chart_column.pl A demo of column (histogram) style charts.
7188             chart_line.pl A demo of line style charts.
7189             chart_pie.pl A demo of pie style charts.
7190             chart_doughnut.pl A demo of doughnut style charts.
7191             chart_radar.pl A demo of radar style charts.
7192             chart_scatter.pl A demo of scatter style charts.
7193             chart_secondary_axis.pl A demo of a line chart with a secondary axis.
7194             chart_combined.pl A demo of a combined column and line chart.
7195             chart_pareto.pl A demo of a combined Pareto chart.
7196             chart_stock.pl A demo of stock style charts.
7197             chart_data_table.pl A demo of a chart with a data table on the axis.
7198             chart_data_tools.pl A demo of charts with data highlighting options.
7199             chart_data_labels.pl A demo of standard and custom chart data labels.
7200             chart_clustered.pl A demo of a chart with a clustered axis.
7201             chart_styles.pl A demo of the available chart styles.
7202             chart_gauge.pl A demo of a gauge style chart.
7203             colors.pl A demo of the colour palette and named colours.
7204             comments1.pl Add comments to worksheet cells.
7205             comments2.pl Add comments with advanced options.
7206             conditional_format.pl Add conditional formats to a range of cells.
7207             data_validate.pl An example of data validation and dropdown lists.
7208             date_time.pl Write dates and times with write_date_time().
7209             defined_name.pl Example of how to create defined names.
7210             diag_border.pl A simple example of diagonal cell borders.
7211             filehandle.pl Examples of working with filehandles.
7212             headers.pl Examples of worksheet headers and footers.
7213             hide_row_col.pl Example of hiding rows and columns.
7214             hide_sheet.pl Simple example of hiding a worksheet.
7215             hyperlink1.pl Shows how to create web hyperlinks.
7216             hyperlink2.pl Examples of internal and external hyperlinks.
7217             indent.pl An example of cell indentation.
7218             ignore_errors.pl An example of turning off worksheet cells errors/warnings.
7219             macros.pl An example of adding macros from an existing file.
7220             merge1.pl A simple example of cell merging.
7221             merge2.pl A simple example of cell merging with formatting.
7222             merge3.pl Add hyperlinks to merged cells.
7223             merge4.pl An advanced example of merging with formatting.
7224             merge5.pl An advanced example of merging with formatting.
7225             merge6.pl An example of merging with Unicode strings.
7226             mod_perl1.pl A simple mod_perl 1 program.
7227             mod_perl2.pl A simple mod_perl 2 program.
7228             outline.pl An example of outlines and grouping.
7229             outline_collapsed.pl An example of collapsed outlines.
7230             panes.pl An example of how to create panes.
7231             properties.pl Add document properties to a workbook.
7232             protection.pl Example of cell locking and formula hiding.
7233             rich_strings.pl Example of strings with multiple formats.
7234             right_to_left.pl Change default sheet direction to right to left.
7235             sales.pl An example of a simple sales spreadsheet.
7236             shape1.pl Insert shapes in worksheet.
7237             shape2.pl Insert shapes in worksheet. With properties.
7238             shape3.pl Insert shapes in worksheet. Scaled.
7239             shape4.pl Insert shapes in worksheet. With modification.
7240             shape5.pl Insert shapes in worksheet. With connections.
7241             shape6.pl Insert shapes in worksheet. With connections.
7242             shape7.pl Insert shapes in worksheet. One to many connections.
7243             shape8.pl Insert shapes in worksheet. One to many connections.
7244             shape_all.pl Demo of all the available shape and connector types.
7245             sparklines1.pl Simple sparklines demo.
7246             sparklines2.pl Sparklines demo showing formatting options.
7247             stats_ext.pl Same as stats.pl with external references.
7248             stocks.pl Demonstrates conditional formatting.
7249             background.pl Example of how to set the background image for a worksheet.
7250             tab_colors.pl Example of how to set worksheet tab colours.
7251             tables.pl Add Excel tables to a worksheet.
7252             write_handler1.pl Example of extending the write() method. Step 1.
7253             write_handler2.pl Example of extending the write() method. Step 2.
7254             write_handler3.pl Example of extending the write() method. Step 3.
7255             write_handler4.pl Example of extending the write() method. Step 4.
7256             write_to_scalar.pl Example of writing an Excel file to a Perl scalar.
7257              
7258              
7259             Unicode
7260             =======
7261             unicode_2022_jp.pl Japanese: ISO-2022-JP.
7262             unicode_8859_11.pl Thai: ISO-8859_11.
7263             unicode_8859_7.pl Greek: ISO-8859_7.
7264             unicode_big5.pl Chinese: BIG5.
7265             unicode_cp1251.pl Russian: CP1251.
7266             unicode_cp1256.pl Arabic: CP1256.
7267             unicode_cyrillic.pl Russian: Cyrillic.
7268             unicode_koi8r.pl Russian: KOI8-R.
7269             unicode_polish_utf8.pl Polish : UTF8.
7270             unicode_shift_jis.pl Japanese: Shift JIS.
7271              
7272              
7273              
7274             =head1 LIMITATIONS
7275              
7276             The following limits are imposed by Excel 2007+:
7277              
7278             Description Limit
7279             -------------------------------------- ------
7280             Maximum number of chars in a string 32,767
7281             Maximum number of columns 16,384
7282             Maximum number of rows 1,048,576
7283             Maximum chars in a sheet name 31
7284             Maximum chars in a header/footer 254
7285              
7286             Maximum characters in hyperlink url (1) 2079
7287             Maximum number of unique hyperlinks (2) 65,530
7288              
7289             (1) Versions of Excel prior to Excel 2015 limited hyperlink links and anchor/locations to 255 characters each. Versions after that support urls up to 2079 characters. Excel::Writer::XLSX versions >= 1.0.2 support the new longer limit by default.
7290              
7291             (2) Per worksheet. Excel allows a greater number of non-unique hyperlinks if they are contiguous and can be grouped into a single range. This isn't supported by Excel::Writer::XLSX.
7292              
7293              
7294              
7295              
7296             =head1 REQUIREMENTS
7297              
7298             L<http://search.cpan.org/search?dist=Archive-Zip/>.
7299              
7300             Perl 5.8.2.
7301              
7302              
7303              
7304              
7305             =head1 SPEED AND MEMORY USAGE
7306              
7307             C<Spreadsheet::WriteExcel> was written to optimise speed and reduce memory usage. However, these design goals meant that it wasn't easy to implement features that many users requested such as writing formatting and data separately.
7308              
7309             As a result C<Excel::Writer::XLSX> takes a different design approach and holds a lot more data in memory so that it is functionally more flexible.
7310              
7311             The effect of this is that Excel::Writer::XLSX is about 30% slower than Spreadsheet::WriteExcel and uses 5 times more memory.
7312              
7313             In addition the extended row and column ranges in Excel 2007+ mean that it is possible to run out of memory creating large files. This was almost never an issue with Spreadsheet::WriteExcel.
7314              
7315             This memory usage can be reduced almost completely by using the Workbook C<set_optimization()> method:
7316              
7317             $workbook->set_optimization();
7318              
7319             The trade-off is that you won't be able to take advantage of features that manipulate cell data after it is written. One such feature is Tables.
7320              
7321              
7322              
7323             =head1 DOWNLOADING
7324              
7325             The latest version of this module is always available at: L<http://search.cpan.org/search?dist=Excel-Writer-XLSX/>.
7326              
7327              
7328              
7329             =head1 INSTALLATION
7330              
7331             The module can be installed using the standard Perl procedure:
7332              
7333             perl Makefile.PL
7334             make
7335             make test
7336             make install # You may need to be sudo/root
7337              
7338              
7339              
7340             =head1 DIAGNOSTICS
7341              
7342              
7343             =over 4
7344              
7345             =item Filename required by Excel::Writer::XLSX->new()
7346              
7347             A filename must be given in the constructor.
7348              
7349             =item Can't open filename. It may be in use or protected.
7350              
7351             The file cannot be opened for writing. The directory that you are writing to may be protected or the file may be in use by another program.
7352              
7353              
7354             =item Can't call method "XXX" on an undefined value at someprogram.pl.
7355              
7356             On Windows this is usually caused by the file that you are trying to create clashing with a version that is already open and locked by Excel.
7357              
7358             =item The file you are trying to open 'file.xls' is in a different format than specified by the file extension.
7359              
7360             This warning occurs when you create an XLSX file but give it an xls extension.
7361              
7362             =back
7363              
7364              
7365              
7366              
7367             =head1 WRITING EXCEL FILES
7368              
7369             Depending on your requirements, background and general sensibilities you may prefer one of the following methods of getting data into Excel:
7370              
7371             =over 4
7372              
7373             =item * Spreadsheet::WriteExcel
7374              
7375             This module is the precursor to Excel::Writer::XLSX and uses the same interface. It produces files in the Excel Biff xls format that was used in Excel versions 97-2003. These files can still be read by Excel 2007 but have some limitations in relation to the number of rows and columns that the format supports.
7376              
7377             L<Spreadsheet::WriteExcel>.
7378              
7379             =item * Win32::OLE module and office automation
7380              
7381             This requires a Windows platform and an installed copy of Excel. This is the most powerful and complete method for interfacing with Excel.
7382              
7383             L<Win32::OLE>
7384              
7385             =item * CSV, comma separated variables or text
7386              
7387             Excel will open and automatically convert files with a C<csv> extension.
7388              
7389             To create CSV files refer to the L<Text::CSV_XS> module.
7390              
7391              
7392             =item * DBI with DBD::ADO or DBD::ODBC
7393              
7394             Excel files contain an internal index table that allows them to act like a database file. Using one of the standard Perl database modules you can connect to an Excel file as a database.
7395              
7396              
7397             =back
7398              
7399             For other Perl-Excel modules try the following search: L<http://search.cpan.org/search?mode=module&query=excel>.
7400              
7401              
7402              
7403              
7404             =head1 READING EXCEL FILES
7405              
7406             To read data from Excel files try:
7407              
7408             =over 4
7409              
7410             =item * L<Spreadsheet::ParseXLSX>
7411              
7412             A module for reading data from XLSX files. It also imports most, if
7413             not all, of the metadata to be found in Excel XLSX files. As its
7414             author describes it: "This module is an adaptor for
7415             L<Spreadsheet::ParseExcel> that reads XLSX files. For documentation
7416             about the various data that you can retrieve from these classes,
7417             please see L<Spreadsheet::ParseExcel>,
7418             L<Spreadsheet::ParseExcel::Workbook>,
7419             L<Spreadsheet::ParseExcel::Worksheet>, and
7420             L<Spreadsheet::ParseExcel::Cell>."
7421              
7422             =item * Spreadsheet::XLSX
7423              
7424             A module for reading formatted or unformatted data from XLSX files.
7425              
7426             L<Spreadsheet::XLSX>
7427              
7428             =item * SimpleXlsx
7429              
7430             A lightweight module for reading data from XLSX files.
7431              
7432             L<SimpleXlsx>
7433              
7434             =item * Spreadsheet::ParseExcel
7435              
7436             This module can read data from an Excel XLS file but it doesn't support the XLSX format.
7437              
7438             L<Spreadsheet::ParseExcel>
7439              
7440             =item * Win32::OLE module and office automation (reading)
7441              
7442             See above.
7443              
7444             =item * DBI with DBD::ADO or DBD::ODBC.
7445              
7446             See above.
7447              
7448             =back
7449              
7450              
7451             For other Perl-Excel modules try the following search: L<http://search.cpan.org/search?mode=module&query=excel>.
7452              
7453              
7454             =head1 BUGS
7455              
7456             =over
7457              
7458             =item * Memory usage is very high for large worksheets.
7459              
7460             If you run out of memory creating large worksheets use the C<set_optimization()> method. See L</SPEED AND MEMORY USAGE> for more information.
7461              
7462             =item * Perl packaging programs can't find chart modules.
7463              
7464             When using Excel::Writer::XLSX charts with Perl packagers such as PAR or Cava you should explicitly include the chart that you are trying to create in your C<use> statements. This isn't a bug as such but it might help someone from banging their head off a wall:
7465              
7466             ...
7467             use Excel::Writer::XLSX;
7468             use Excel::Writer::XLSX::Chart::Column;
7469             ...
7470              
7471             =back
7472              
7473              
7474             If you wish to submit a bug report run the C<bug_report.pl> program in the C<examples> directory of the distro.
7475              
7476              
7477              
7478              
7479             The bug tracker is on Github: L<https://github.com/jmcnamara/excel-writer-xlsx/issues>.
7480              
7481              
7482              
7483              
7484             =head1 REPOSITORY
7485              
7486             The Excel::Writer::XLSX source code in host on github: L<http://github.com/jmcnamara/excel-writer-xlsx>.
7487              
7488              
7489              
7490              
7491             =head1 DONATIONS and SPONSORSHIP
7492              
7493             If you'd care to donate to the Excel::Writer::XLSX project or sponsor a new feature, you can do so via PayPal: L<http://tinyurl.com/7ayes>.
7494              
7495              
7496              
7497              
7498             =head1 SEE ALSO
7499              
7500             Spreadsheet::WriteExcel: L<http://search.cpan.org/dist/Spreadsheet-WriteExcel>.
7501              
7502             Spreadsheet::ParseExcel: L<http://search.cpan.org/dist/Spreadsheet-ParseExcel>.
7503              
7504             Spreadsheet::XLSX: L<http://search.cpan.org/dist/Spreadsheet-XLSX>.
7505              
7506              
7507              
7508             =head1 ACKNOWLEDGEMENTS
7509              
7510              
7511             The following people contributed to the debugging, testing or enhancement of Excel::Writer::XLSX:
7512              
7513             Rob Messer of IntelliSurvey gave me the initial prompt to port Spreadsheet::WriteExcel to the XLSX format. IntelliSurvey (L<http://www.intellisurvey.com>) also sponsored large files optimisations and the charting feature.
7514              
7515             Bariatric Advantage (L<http://www.bariatricadvantage.com>) sponsored work on chart formatting.
7516              
7517             Eric Johnson provided the ability to use secondary axes with charts. Thanks to Foxtons (L<http://foxtons.co.uk>) for sponsoring this work.
7518              
7519             BuildFax (L<http://www.buildfax.com>) sponsored the Tables feature and the Chart point formatting feature.
7520              
7521              
7522              
7523              
7524             =head1 DISCLAIMER OF WARRANTY
7525              
7526             Because this software is licensed free of charge, there is no warranty for the software, to the extent permitted by applicable law. Except when otherwise stated in writing the copyright holders and/or other parties provide the software "as is" without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the software is with you. Should the software prove defective, you assume the cost of all necessary servicing, repair, or correction.
7527              
7528             In no event unless required by applicable law or agreed to in writing will any copyright holder, or any other party who may modify and/or redistribute the software as permitted by the above licence, be liable to you for damages, including any general, special, incidental, or consequential damages arising out of the use or inability to use the software (including but not limited to loss of data or data being rendered inaccurate or losses sustained by you or third parties or a failure of the software to operate with any other software), even if such holder or other party has been advised of the possibility of such damages.
7529              
7530              
7531              
7532              
7533             =head1 LICENSE
7534              
7535             The Perl Artistic Licence L<http://dev.perl.org/licenses/artistic.html>.
7536              
7537              
7538              
7539              
7540             =head1 AUTHOR
7541              
7542             John McNamara jmcnamara@cpan.org
7543              
7544              
7545              
7546              
7547             =head1 COPYRIGHT
7548              
7549             Copyright MM-MMXXI, John McNamara.
7550              
7551             All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.