1: package Spreadsheet::WriteExcel;
2: 
3: ###############################################################################
4: #
5: # WriteExcel.
6: #
7: # Spreadsheet::WriteExcel - Write to a cross-platform Excel binary file.
8: #
9: # Copyright 2000-2012, John McNamara, jmcnamara@cpan.org
10: #
11: # Documentation after __END__
12: #
13: 
14: use Exporter;
15: 
16: use strict;
17: use Spreadsheet::WriteExcel::Workbook;
18: 
19: 
20: 
21: use vars qw($VERSION @ISA);
22: @ISA = qw(Spreadsheet::WriteExcel::Workbook Exporter);
23: 
24: $VERSION = '2.38'; # A farewell to arms.
25: 
26: 
27: 
28: ###############################################################################
29: #
30: # new()
31: #
32: # Constructor. Wrapper for a Workbook object.
33: # uses: Spreadsheet::WriteExcel::BIFFwriter
34: #       Spreadsheet::WriteExcel::Chart
35: #       Spreadsheet::WriteExcel::OLEwriter
36: #       Spreadsheet::WriteExcel::Workbook
37: #       Spreadsheet::WriteExcel::Worksheet
38: #       Spreadsheet::WriteExcel::Format
39: #       Spreadsheet::WriteExcel::Formula
40: #       Spreadsheet::WriteExcel::Properties
41: #
42: sub new {
43: 
44:     my $class = shift;
45:     my $self  = Spreadsheet::WriteExcel::Workbook->new(@_);
46: 
47:     # Check for file creation failures before re-blessing
48:     bless  $self, $class if defined $self;
49: 
50:     return $self;
51: }
52: 
53: 
54: 1;
55: 
56: 
57: __END__
58: 
59: 
60: 
61: =head1 NAME
62: 
63: Spreadsheet::WriteExcel - Write to a cross-platform Excel binary file.
64: 
65: =head1 VERSION
66: 
67: This document refers to version 2.38 of Spreadsheet::WriteExcel, released November 22, 2012.
68: 
69: 
70: 
71: 
72: =head1 SYNOPSIS
73: 
74: To write a string, a formatted string, a number and a formula to the first worksheet in an Excel workbook called perl.xls:
75: 
76:     use Spreadsheet::WriteExcel;
77: 
78:     # Create a new Excel workbook
79:     my $workbook = Spreadsheet::WriteExcel->new('perl.xls');
80: 
81:     # Add a worksheet
82:     $worksheet = $workbook->add_worksheet();
83: 
84:     #  Add and define a format
85:     $format = $workbook->add_format(); # Add a format
86:     $format->set_bold();
87:     $format->set_color('red');
88:     $format->set_align('center');
89: 
90:     # Write a formatted and unformatted string, row and column notation.
91:     $col = $row = 0;
92:     $worksheet->write($row, $col, 'Hi Excel!', $format);
93:     $worksheet->write(1,    $col, 'Hi Excel!');
94: 
95:     # Write a number and a formula using A1 notation
96:     $worksheet->write('A3', 1.2345);
97:     $worksheet->write('A4', '=SIN(PI()/4)');
98: 
99: 
100: 
101: 
102: =head1 DESCRIPTION
103: 
104: The Spreadsheet::WriteExcel Perl module can be used to create a cross-platform Excel binary file. Multiple worksheets can be added to a workbook and formatting can be applied to cells. Text, numbers, formulas, hyperlinks, images and charts can be written to the cells.
105: 
106: The file produced by this module is compatible with Excel 97, 2000, 2002, 2003 and 2007.
107: 
108: The module will work on the majority of Windows, UNIX and Mac platforms. Generated files are also compatible with the Linux/UNIX spreadsheet applications Gnumeric and OpenOffice.org.
109: 
110: This module cannot be used to write to an existing Excel file (See L</MODIFYING AND REWRITING EXCEL FILES>).
111: 
112: For the Excel 2007+ xlsx file format see the L<Excel::Writer::XLSX> module.
113: 
114: 
115: 
116: =head1 QUICK START
117: 
118: Spreadsheet::WriteExcel 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 three easy steps:
119: 
120: 1. Create a new Excel I<workbook> (i.e. file) using C<new()>.
121: 
122: 2. Add a I<worksheet> to the new workbook using C<add_worksheet()>.
123: 
124: 3. Write to the worksheet using C<write()>.
125: 
126: Like this:
127: 
128:     use Spreadsheet::WriteExcel;                             # Step 0
129: 
130:     my $workbook = Spreadsheet::WriteExcel->new('perl.xls'); # Step 1
131:     $worksheet   = $workbook->add_worksheet();               # Step 2
132:     $worksheet->write('A1', 'Hi Excel!');                    # Step 3
133: 
134: This will create an Excel file called C<perl.xls> 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 also more than 80 examples that come with the distribution and which you can use to get you started. See L</EXAMPLES>.
135: 
136: Those of you who read the instructions first and assemble the furniture afterwards will know how to proceed. ;-)
137: 
138: 
139: 
140: 
141: =head1 WORKBOOK METHODS
142: 
143: The Spreadsheet::WriteExcel module provides an object oriented interface to a new Excel workbook. The following methods are available through a new workbook.
144: 
145:     new()
146:     add_worksheet()
147:     add_format()
148:     add_chart()
149:     add_chart_ext()
150:     close()
151:     compatibility_mode()
152:     set_properties()
153:     define_name()
154:     set_tempdir()
155:     set_custom_color()
156:     sheets()
157:     set_1904()
158:     set_codepage()
159: 
160: 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.
161: 
162: 
163: 
164: 
165: =head2 new()
166: 
167: 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:
168: 
169:     my $workbook  = Spreadsheet::WriteExcel->new('filename.xls');
170:     my $worksheet = $workbook->add_worksheet();
171:     $worksheet->write(0, 0, 'Hi Excel!');
172: 
173: Here are some other examples of using C<new()> with filenames:
174: 
175:     my $workbook1 = Spreadsheet::WriteExcel->new($filename);
176:     my $workbook2 = Spreadsheet::WriteExcel->new('/tmp/filename.xls');
177:     my $workbook3 = Spreadsheet::WriteExcel->new("c:\\tmp\\filename.xls");
178:     my $workbook4 = Spreadsheet::WriteExcel->new('c:\tmp\filename.xls');
179: 
180: 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?>.
181: 
182: The C<new()> constructor returns a Spreadsheet::WriteExcel 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.
183: 
184: 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>:
185: 
186:     my $workbook  = Spreadsheet::WriteExcel->new('protected.xls');
187:     die "Problems creating new Excel file: $!" unless defined $workbook;
188: 
189: You can also pass a valid filehandle to the C<new()> constructor. For example in a CGI program you could do something like this:
190: 
191:     binmode(STDOUT);
192:     my $workbook  = Spreadsheet::WriteExcel->new(\*STDOUT);
193: 
194: The requirement for C<binmode()> is explained below.
195: 
196: See also, the C<cgi.pl> program in the C<examples> directory of the distro.
197: 
198: However, this special case will not work in C<mod_perl> programs where you will have to do something like the following:
199: 
200:     # mod_perl 1
201:     ...
202:     tie *XLS, 'Apache';
203:     binmode(XLS);
204:     my $workbook  = Spreadsheet::WriteExcel->new(\*XLS);
205:     ...
206: 
207:     # mod_perl 2
208:     ...
209:     tie *XLS => $r;  # Tie to the Apache::RequestRec object
210:     binmode(*XLS);
211:     my $workbook  = Spreadsheet::WriteExcel->new(\*XLS);
212:     ...
213: 
214: See also, the C<mod_perl1.pl> and C<mod_perl2.pl> programs in the C<examples> directory of the distro.
215: 
216: 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.
217: 
218: For example here is a way to write an Excel file to a scalar with C<perl 5.8>:
219: 
220:     #!/usr/bin/perl -w
221: 
222:     use strict;
223:     use Spreadsheet::WriteExcel;
224: 
225:     # Requires perl 5.8 or later
226:     open my $fh, '>', \my $str or die "Failed to open filehandle: $!";
227: 
228:     my $workbook  = Spreadsheet::WriteExcel->new($fh);
229:     my $worksheet = $workbook->add_worksheet();
230: 
231:     $worksheet->write(0, 0,  'Hi Excel!');
232: 
233:     $workbook->close();
234: 
235:     # The Excel file in now in $str. Remember to binmode() the output
236:     # filehandle before printing it.
237:     binmode STDOUT;
238:     print $str;
239: 
240: See also the C<write_to_scalar.pl> and C<filehandle.pl> programs in the C<examples> directory of the distro.
241: 
242: 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. This applies especially to users of perl 5.8 on systems where C<UTF-8> is likely to be in operation such as RedHat Linux 9. If your program, either intentionally or not, writes C<UTF-8> data to a filehandle that is passed to C<new()> it will corrupt the Excel file that is created.
243: 
244: You don't have to worry about C<binmode()> if you are using filenames instead of filehandles. Spreadsheet::WriteExcel 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.
245: 
246: 
247: 
248: 
249: 
250: =head2 add_worksheet($sheetname, $utf_16_be)
251: 
252: At least one worksheet should be added to a new workbook. A worksheet is used to write data into cells:
253: 
254:     $worksheet1 = $workbook->add_worksheet();           # Sheet1
255:     $worksheet2 = $workbook->add_worksheet('Foglio2');  # Foglio2
256:     $worksheet3 = $workbook->add_worksheet('Data');     # Data
257:     $worksheet4 = $workbook->add_worksheet();           # Sheet4
258: 
259: If C<$sheetname> is not specified the default Excel convention will be followed, i.e. Sheet1, Sheet2, etc. The C<$utf_16_be> parameter is optional, see below.
260: 
261: The worksheet name must be a valid Excel worksheet name, i.e. it cannot contain any of the following characters, C<[ ] : * ? / \> and it must be less than 32 characters. In addition, you cannot use the same, case insensitive, C<$sheetname> for more than one worksheet.
262: 
263: On systems with C<perl 5.8> and later the C<add_worksheet()> method will also handle strings in C<UTF-8> format.
264: 
265:     $worksheet = $workbook->add_worksheet("\x{263a}"); # Smiley
266: 
267: On earlier Perl systems your can specify C<UTF-16BE> worksheet names using an additional optional parameter:
268: 
269:     my $name = pack 'n', 0x263a;
270:     $worksheet = $workbook->add_worksheet($name, 1);   # Smiley
271: 
272: 
273: 
274: 
275: =head2 add_format(%properties)
276: 
277: 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.
278: 
279:     $format1 = $workbook->add_format(%props); # Set properties at creation
280:     $format2 = $workbook->add_format();       # Set properties later
281: 
282: See the L</CELL FORMATTING> section for more details about Format properties and how to set them.
283: 
284: 
285: 
286: 
287: =head2 add_chart(%properties)
288: 
289: 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.
290: 
291:     my $chart = $workbook->add_chart( type => 'column' );
292: 
293: The properties that can be set are:
294: 
295:     type     (required)
296:     name     (optional)
297:     embedded (optional)
298: 
299: =over
300: 
301: =item * C<type>
302: 
303: This is a required parameter. It defines the type of chart that will be created.
304: 
305:     my $chart = $workbook->add_chart( type => 'line' );
306: 
307: The available types are:
308: 
309:     area
310:     bar
311:     column
312:     line
313:     pie
314:     scatter
315:     stock
316: 
317: =item * C<name>
318: 
319: 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.
320: 
321:     my $chart = $workbook->add_chart( type => 'line', name => 'Results Chart' );
322: 
323: =item * C<embedded>
324: 
325: 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.
326: 
327:     my $chart = $workbook->add_chart( type => 'line', embedded => 1 );
328: 
329:     # Configure the chart.
330:     ...
331: 
332:     # Insert the chart into the a worksheet.
333:     $worksheet->insert_chart( 'E2', $chart );
334: 
335: =back
336: 
337: See L<Spreadsheet::WriteExcel::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.
338: 
339: 
340: 
341: 
342: =head2 add_chart_ext($chart_data, $chartname)
343: 
344: This method is use to include externally generated charts in a Spreadsheet::WriteExcel file.
345: 
346:     my $chart = $workbook->add_chart_ext('chart01.bin', 'Chart1');
347: 
348: This feature is semi-deprecated in favour of the "native" charts created using C<add_chart()>. Read C<external_charts.txt> (or C<.pod>) in the external_charts directory of the distro for a full explanation.
349: 
350: 
351: 
352: 
353: =head2 close()
354: 
355: In general your Excel file will be closed automatically when your program ends or when the Workbook object goes out of scope, however the C<close()> method can be used to explicitly close an Excel file.
356: 
357:     $workbook->close();
358: 
359: 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.
360: 
361: 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:
362: 
363: =over 4
364: 
365: =item *
366: 
367: If C<my()> was not used to declare the scope of a workbook variable created using C<new()>.
368: 
369: =item *
370: 
371: If the C<new()>, C<add_worksheet()> or C<add_format()> methods are called in subroutines.
372: 
373: =back
374: 
375: The reason for this is that Spreadsheet::WriteExcel 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.
376: 
377: In general, if you create a file with a size of 0 bytes or you fail to create a file you need to call C<close()>.
378: 
379: 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:
380: 
381:     $workbook->close() or die "Error closing file: $!";
382: 
383: 
384: 
385: 
386: =head2 compatibility_mode()
387: 
388: This method is used to improve compatibility with third party applications that read Excel files.
389: 
390:     $workbook->compatibility_mode();
391: 
392: An Excel file is comprised of binary records that describe properties of a spreadsheet. Excel is reasonably liberal about this and, outside of a core subset, it doesn't require every possible record to be present when it reads a file. This is also true of Gnumeric and OpenOffice.Org Calc.
393: 
394: Spreadsheet::WriteExcel takes advantage of this fact to omit some records in order to minimise the amount of data stored in memory and to simplify and speed up the writing of files. However, some third party applications that read Excel files often expect certain records to be present. In "compatibility mode" Spreadsheet::WriteExcel writes these records and tries to be as close to an Excel generated file as possible.
395: 
396: Applications that require C<compatibility_mode()> are Apache POI, Apple Numbers, and Quickoffice on Nokia, Palm and other devices. You should also use C<compatibility_mode()> if your Excel file will be used as an external data source by another Excel file.
397: 
398: If you encounter other situations that require C<compatibility_mode()>, please let me know.
399: 
400: It should be noted that C<compatibility_mode()> requires additional data to be stored in memory and additional processing. This incurs a memory and speed penalty and may not be suitable for very large files (>20MB).
401: 
402: You must call C<compatibility_mode()> before calling C<add_worksheet()>.
403: 
404: 
405: 
406: 
407: =head2 set_properties()
408: 
409: The C<set_properties> method can be used to set the document properties of the Excel file created by C<Spreadsheet::WriteExcel>. These properties are visible when you use the C<< File->Properties >> menu option in Excel and are also available to external applications that read or index windows files.
410: 
411: The properties should be passed as a hash of values as follows:
412: 
413:     $workbook->set_properties(
414:         title    => 'This is an example spreadsheet',
415:         author   => 'John McNamara',
416:         comments => 'Created with Perl and Spreadsheet::WriteExcel',
417:     );
418: 
419: The properties that can be set are:
420: 
421:     title
422:     subject
423:     author
424:     manager
425:     company
426:     category
427:     keywords
428:     comments
429: 
430: User defined properties are not supported due to effort required.
431: 
432: In perl 5.8+ you can also pass UTF-8 strings as properties. See L</UNICODE IN EXCEL>.
433: 
434:     my $smiley = chr 0x263A;
435: 
436:     $workbook->set_properties(
437:         subject => "Happy now? $smiley",
438:     );
439: 
440: With older versions of perl you can use a module to convert a non-ASCII string to a binary representation of UTF-8 and then pass an additional C<utf8> flag to C<set_properties()>:
441: 
442:     my $smiley = pack 'H*', 'E298BA';
443: 
444:     $workbook->set_properties(
445:         subject => "Happy now? $smiley",
446:         utf8    => 1,
447:     );
448: 
449: Usually Spreadsheet::WriteExcel allows you to use UTF-16 with pre 5.8 versions of perl. However, document properties don't support UTF-16 for these type of strings.
450: 
451: In order to promote the usefulness of Perl and the Spreadsheet::WriteExcel module consider adding a comment such as the following when using document properties:
452: 
453:     $workbook->set_properties(
454:         ...,
455:         comments => 'Created with Perl and Spreadsheet::WriteExcel',
456:         ...,
457:     );
458: 
459: This feature requires that the C<OLE::Storage_Lite> module is installed (which is usually the case for a standard Spreadsheet::WriteExcel installation). However, this also means that the resulting OLE document may B<possibly> be buggy for files less than 7MB since it hasn't been as rigorously tested in that domain. As a result of this C<set_properties> is currently incompatible with Gnumeric for files less than 7MB. This is being investigated. If you encounter any problems with this features let me know.
460: 
461: For convenience it is possible to pass either a hash or hash ref of arguments to this method.
462: 
463: See also the C<properties.pl> program in the examples directory of the distro.
464: 
465: 
466: 
467: 
468: =head2 define_name()
469: 
470: 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.
471: 
472:     $workbook->define_name('Exchange_rate', '=0.96');
473:     $workbook->define_name('Sales',         '=Sheet1!$G$1:$H$10');
474:     $workbook->define_name('Sheet2!Sales',  '=Sheet2!$G$1:$G$10');
475: 
476: See the defined_name.pl program in the examples dir of the distro.
477: 
478: Note: This currently a beta feature. More documentation and examples will be added.
479: 
480: 
481: 
482: 
483: =head2 set_tempdir()
484: 
485: For speed and efficiency C<Spreadsheet::WriteExcel> stores worksheet data in temporary files prior to assembling the final workbook.
486: 
487: If Spreadsheet::WriteExcel is unable to create these temporary files it will store the required data in memory. This can be slow for large files.
488: 
489: The problem occurs mainly with IIS on Windows although it could feasibly occur on Unix systems as well. The problem generally occurs because the default temp file directory is defined as C<C:/> or some other directory that IIS doesn't provide write access to.
490: 
491: To check if this might be a problem on a particular system you can run a simple test program with C<-w> or C<use warnings>. This will generate a warning if the module cannot create the required temporary files:
492: 
493:     #!/usr/bin/perl -w
494: 
495:     use Spreadsheet::WriteExcel;
496: 
497:     my $workbook  = Spreadsheet::WriteExcel->new('test.xls');
498:     my $worksheet = $workbook->add_worksheet();
499: 
500: To avoid this problem the C<set_tempdir()> method can be used to specify a directory that is accessible for the creation of temporary files.
501: 
502: The C<File::Temp> module is used to create the 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:
503: 
504:     perl -MFile::Spec -le "print File::Spec->tmpdir"
505: 
506: Even if the default temporary file directory is accessible you may wish to specify an alternative location for security or maintenance reasons:
507: 
508:     $workbook->set_tempdir('/tmp/writeexcel');
509:     $workbook->set_tempdir('c:\windows\temp\writeexcel');
510: 
511: The directory for the temporary file must exist, C<set_tempdir()> will not create a new directory.
512: 
513: One disadvantage of using the C<set_tempdir()> method is that on some Windows systems it will limit you to approximately 800 concurrent tempfiles. This means that a single program running on one of these systems will be limited to creating a total of 800 workbook and worksheet objects. You can run multiple, non-concurrent programs to work around this if necessary.
514: 
515: 
516: 
517: 
518: =head2 set_custom_color($index, $red, $green, $blue)
519: 
520: The C<set_custom_color()> method can be used to override one of the built-in palette values with a more suitable colour.
521: 
522: The value for C<$index> should be in the range 8..63, see L</COLOURS IN EXCEL>.
523: 
524: The default named colours use the following indices:
525: 
526:      8   =>   black
527:      9   =>   white
528:     10   =>   red
529:     11   =>   lime
530:     12   =>   blue
531:     13   =>   yellow
532:     14   =>   magenta
533:     15   =>   cyan
534:     16   =>   brown
535:     17   =>   green
536:     18   =>   navy
537:     20   =>   purple
538:     22   =>   silver
539:     23   =>   gray
540:     33   =>   pink
541:     53   =>   orange
542: 
543: A new colour is set using its RGB (red green blue) components. The C<$red>, C<$green> and C<$blue> values must be in the range 0..255. You can determine the required values in Excel using the C<Tools-E<gt>Options-E<gt>Colors-E<gt>Modify> dialog.
544: 
545: The C<set_custom_color()> workbook method can also be used with a HTML style C<#rrggbb> hex value:
546: 
547:     $workbook->set_custom_color(40, 255,  102,  0   ); # Orange
548:     $workbook->set_custom_color(40, 0xFF, 0x66, 0x00); # Same thing
549:     $workbook->set_custom_color(40, '#FF6600'       ); # Same thing
550: 
551:     my $font = $workbook->add_format(color => 40); # Use the modified colour
552: 
553: The return value from C<set_custom_color()> is the index of the colour that was changed:
554: 
555:     my $ferrari = $workbook->set_custom_color(40, 216, 12, 12);
556: 
557:     my $format  = $workbook->add_format(
558:                                         bg_color => $ferrari,
559:                                         pattern  => 1,
560:                                         border   => 1
561:                                       );
562: 
563: 
564: 
565: 
566: =head2 sheets(0, 1, ...)
567: 
568: The C<sheets()> method returns a list, or a sliced list, of the worksheets in a workbook.
569: 
570: 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:
571: 
572:     foreach $worksheet ($workbook->sheets()) {
573:        print $worksheet->get_name();
574:     }
575: 
576: 
577: You can also specify a slice list to return one or more worksheet objects:
578: 
579:     $worksheet = $workbook->sheets(0);
580:     $worksheet->write('A1', 'Hello');
581: 
582: 
583: Or since return value from C<sheets()> is a reference to a worksheet object you can write the above example as:
584: 
585:     $workbook->sheets(0)->write('A1', 'Hello');
586: 
587: 
588: The following example returns the first and last worksheet in a workbook:
589: 
590:     foreach $worksheet ($workbook->sheets(0, -1)) {
591:        # Do something
592:     }
593: 
594: 
595: Array slices are explained in the perldata manpage.
596: 
597: 
598: 
599: 
600: =head2 set_1904()
601: 
602: 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.
603: 
604: Spreadsheet::WriteExcel 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.
605: 
606: See also L</DATES AND TIME IN EXCEL> for more information about working with Excel's date system.
607: 
608: In general you probably won't need to use C<set_1904()>.
609: 
610: 
611: 
612: 
613: =head2 set_codepage($codepage)
614: 
615: The default code page or character set used by Spreadsheet::WriteExcel is ANSI. This is also the default used by Excel for Windows. Occasionally however it may be necessary to change the code page via the C<set_codepage()> method.
616: 
617: Changing the code page may be required if your are using Spreadsheet::WriteExcel on the Macintosh and you are using characters outside the ASCII 128 character set:
618: 
619:     $workbook->set_codepage(1); # ANSI, MS Windows
620:     $workbook->set_codepage(2); # Apple Macintosh
621: 
622: The C<set_codepage()> method is rarely required.
623: 
624: 
625: 
626: 
627: =head1 WORKSHEET METHODS
628: 
629: A new worksheet is created by calling the C<add_worksheet()> method from a workbook object:
630: 
631:     $worksheet1 = $workbook->add_worksheet();
632:     $worksheet2 = $workbook->add_worksheet();
633: 
634: The following methods are available through a new worksheet:
635: 
636:     write()
637:     write_number()
638:     write_string()
639:     write_utf16be_string()
640:     write_utf16le_string()
641:     keep_leading_zeros()
642:     write_blank()
643:     write_row()
644:     write_col()
645:     write_date_time()
646:     write_url()
647:     write_url_range()
648:     write_formula()
649:     store_formula()
650:     repeat_formula()
651:     write_comment()
652:     show_comments()
653:     add_write_handler()
654:     insert_image()
655:     insert_chart()
656:     data_validation()
657:     get_name()
658:     activate()
659:     select()
660:     hide()
661:     set_first_sheet()
662:     protect()
663:     set_selection()
664:     set_row()
665:     set_column()
666:     outline_settings()
667:     freeze_panes()
668:     split_panes()
669:     merge_range()
670:     set_zoom()
671:     right_to_left()
672:     hide_zero()
673:     set_tab_color()
674:     autofilter()
675: 
676: 
677: 
678: 
679: =head2 Cell notation
680: 
681: Spreadsheet::WriteExcel supports two forms of notation to designate the position of cells: Row-column notation and A1 notation.
682: 
683: 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:
684: 
685:     (0, 0)      # The top left cell in row-column notation.
686:     ('A1')      # The top left cell in A1 notation.
687: 
688:     (1999, 29)  # Row-column notation.
689:     ('AD2000')  # The same cell in A1 notation.
690: 
691: Row-column notation is useful if you are referring to cells programmatically:
692: 
693:     for my $i (0 .. 9) {
694:         $worksheet->write($i, 0, 'Hello'); # Cells A1 to A10
695:     }
696: 
697: A1 notation is useful for setting up a worksheet manually and for working with formulas:
698: 
699:     $worksheet->write('H1', 200);
700:     $worksheet->write('H2', '=H1+1');
701: 
702: In formulas and applicable methods you can also use the C<A:A> column notation:
703: 
704:     $worksheet->write('A1', '=SUM(B:B)');
705: 
706: The C<Spreadsheet::WriteExcel::Utility> module that is included in the distro contains helper functions for dealing with A1 notation, for example:
707: 
708:     use Spreadsheet::WriteExcel::Utility;
709: 
710:     ($row, $col)    = xl_cell_to_rowcol('C2');  # (1, 2)
711:     $str            = xl_rowcol_to_cell(1, 2);  # C2
712: 
713: 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.
714: 
715: Note: in Excel it is also possible to use a R1C1 notation. This is not supported by Spreadsheet::WriteExcel.
716: 
717: 
718: 
719: 
720: =head2 write($row, $column, $token, $format)
721: 
722: 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:
723: 
724:     write_string()
725:     write_number()
726:     write_blank()
727:     write_formula()
728:     write_url()
729:     write_row()
730:     write_col()
731: 
732: 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:
733: 
734:                                                       # Same as:
735:     $worksheet->write(0, 0, 'Hello'                ); # write_string()
736:     $worksheet->write(1, 0, 'One'                  ); # write_string()
737:     $worksheet->write(2, 0,  2                     ); # write_number()
738:     $worksheet->write(3, 0,  3.00001               ); # write_number()
739:     $worksheet->write(4, 0,  ""                    ); # write_blank()
740:     $worksheet->write(5, 0,  ''                    ); # write_blank()
741:     $worksheet->write(6, 0,  undef                 ); # write_blank()
742:     $worksheet->write(7, 0                         ); # write_blank()
743:     $worksheet->write(8, 0,  'http://www.perl.com/'); # write_url()
744:     $worksheet->write('A9',  'ftp://ftp.cpan.org/' ); # write_url()
745:     $worksheet->write('A10', 'internal:Sheet1!A1'  ); # write_url()
746:     $worksheet->write('A11', 'external:c:\foo.xls' ); # write_url()
747:     $worksheet->write('A12', '=A3 + 3*A4'          ); # write_formula()
748:     $worksheet->write('A13', '=SIN(PI()/4)'        ); # write_formula()
749:     $worksheet->write('A14', \@array               ); # write_row()
750:     $worksheet->write('A15', [\@array]             ); # write_col()
751: 
752:     # And if the keep_leading_zeros property is set:
753:     $worksheet->write('A16', '2'                   ); # write_number()
754:     $worksheet->write('A17', '02'                  ); # write_string()
755:     $worksheet->write('A18', '00002'               ); # write_string()
756: 
757: 
758: The "looks like" rule is defined by regular expressions:
759: 
760: C<write_number()> if C<$token> is a number based on the following regex: C<$token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/>.
761: 
762: 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+$/>.
763: 
764: C<write_blank()> if C<$token> is undef or a blank string: C<undef>, C<""> or C<''>.
765: 
766: 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:|>.
767: 
768: C<write_url()> if C<$token> is an internal or external sheet reference based on the following regex: C<$token =~ m[^(in|ex)ternal:]>.
769: 
770: C<write_formula()> if the first character of C<$token> is C<"=">.
771: 
772: C<write_row()> if C<$token> is an array ref.
773: 
774: C<write_col()> if C<$token> is an array ref of array refs.
775: 
776: C<write_string()> if none of the previous conditions apply.
777: 
778: The C<$format> parameter is optional. It should be a valid Format object, see L</CELL FORMATTING>:
779: 
780:     my $format = $workbook->add_format();
781:     $format->set_bold();
782:     $format->set_color('red');
783:     $format->set_align('center');
784: 
785:     $worksheet->write(4, 0, 'Hello', $format); # Formatted string
786: 
787: 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.
788: 
789: 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.
790: 
791: You can also add your own data handlers to the C<write()> method using C<add_write_handler()>.
792: 
793: On systems with C<perl 5.8> and later the C<write()> method will also handle Unicode strings in C<UTF-8> format.
794: 
795: The C<write> methods return:
796: 
797:     0 for success.
798:    -1 for insufficient number of arguments.
799:    -2 for row or column out of bounds.
800:    -3 for string too long.
801: 
802: 
803: 
804: 
805: =head2 write_number($row, $column, $number, $format)
806: 
807: Write an integer or a float to the cell specified by C<$row> and C<$column>:
808: 
809:     $worksheet->write_number(0, 0,  123456);
810:     $worksheet->write_number('A2',  2.3451);
811: 
812: See the note about L</Cell notation>. The C<$format> parameter is optional.
813: 
814: In general it is sufficient to use the C<write()> method.
815: 
816: 
817: 
818: 
819: =head2 write_string($row, $column, $string, $format)
820: 
821: Write a string to the cell specified by C<$row> and C<$column>:
822: 
823:     $worksheet->write_string(0, 0, 'Your text here' );
824:     $worksheet->write_string('A2', 'or here' );
825: 
826: 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.
827: 
828: The C<$format> parameter is optional.
829: 
830: On systems with C<perl 5.8> and later the C<write()> method will also handle strings in C<UTF-8> format. With older perls you can also write Unicode in C<UTF16> format via the C<write_utf16be_string()> method. See also the C<unicode_*.pl> programs in the examples directory of the distro.
831: 
832: 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:
833: 
834:     # Write as a plain string
835:     $worksheet->write_string('A1', '01209');
836: 
837: 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<@>:
838: 
839:     # Format as a string. Doesn't change to a number when edited
840:     my $format1 = $workbook->add_format(num_format => '@');
841:     $worksheet->write_string('A2', '01209', $format1);
842: 
843: See also the note about L</Cell notation>.
844: 
845: 
846: 
847: 
848: =head2 write_utf16be_string($row, $column, $string, $format)
849: 
850: This method is used to write C<UTF-16BE> strings to a cell in Excel. It is functionally the same as the C<write_string()> method except that the string should be in C<UTF-16BE> Unicode format. It is generally easier, when using Spreadsheet::WriteExcel, to write unicode strings in C<UTF-8> format, see L</UNICODE IN EXCEL>. The C<write_utf16be_string()> method is mainly of use in versions of perl prior to 5.8.
851: 
852: 
853: The following is a simple example showing how to write some Unicode strings in C<UTF-16BE> format:
854: 
855:     #!/usr/bin/perl -w
856: 
857: 
858:     use strict;
859:     use Spreadsheet::WriteExcel;
860:     use Unicode::Map();
861: 
862:     my $workbook  = Spreadsheet::WriteExcel->new('utf_16_be.xls');
863:     my $worksheet = $workbook->add_worksheet();
864: 
865:     # Increase the column width for clarity
866:     $worksheet->set_column('A:A', 25);
867: 
868: 
869:     # Write a Unicode character
870:     #
871:     my $smiley = pack 'n', 0x263a;
872: 
873:     # Increase the font size for legibility.
874:     my $big_font = $workbook->add_format(size => 72);
875: 
876:     $worksheet->write_utf16be_string('A3', $smiley, $big_font);
877: 
878: 
879: 
880:     # Write a phrase in Cyrillic using a hex-encoded string
881:     #
882:     my $str = pack 'H*', '042d0442043e0020044404400430043704300020043d' .
883:                          '043000200440044304410441043a043e043c0021';
884: 
885:     $worksheet->write_utf16be_string('A5', $str);
886: 
887: 
888: 
889:     # Map a string to UTF-16BE using an external module.
890:     #
891:     my $map   = Unicode::Map->new('ISO-8859-1');
892:     my $utf16 = $map->to_unicode('Hello world!');
893: 
894:     $worksheet->write_utf16be_string('A7', $utf16);
895: 
896: You can convert ASCII encodings to the required C<UTF-16BE> format using one of the many Unicode modules on CPAN. For example C<Unicode::Map> and C<Unicode::String>: L<http://search.cpan.org/author/MSCHWARTZ/Unicode-Map/Map.pm> and L<http://search.cpan.org/author/GAAS/Unicode-String/String.pm>.
897: 
898: For a full list of the Perl Unicode modules see: L<http://search.cpan.org/search?query=unicode&mode=all>.
899: 
900: C<UTF-16BE> is the format most often returned by C<Perl> modules that generate C<UTF-16>. To write C<UTF-16> strings in little-endian format use the C<write_utf16be_string_le()> method below.
901: 
902: The C<write_utf16be_string()> method was previously called C<write_unicode()>. That, overly general, name is still supported but deprecated.
903: 
904: See also the C<unicode_*.pl> programs in the examples directory of the distro.
905: 
906: 
907: 
908: 
909: =head2 write_utf16le_string($row, $column, $string, $format)
910: 
911: This method is the same as C<write_utf16be()> except that the string should be 16-bit characters in little-endian format. This is generally referred to as C<UTF-16LE>. See L</UNICODE IN EXCEL>.
912: 
913: C<UTF-16> data can be changed from little-endian to big-endian format (and vice-versa) as follows:
914: 
915:     $utf16be = pack 'n*', unpack 'v*', $utf16le;
916: 
917: 
918: 
919: 
920: =head2 keep_leading_zeros()
921: 
922: This method changes the default handling of integers with leading zeros when using the C<write()> method.
923: 
924: 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.
925: 
926: 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.
927: 
928: 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()>:
929: 
930:     # Implicitly write a number, the leading zero is removed: 1209
931:     $worksheet->write('A1', '01209');
932: 
933:     # Write a zero padded number using a format: 01209
934:     my $format1 = $workbook->add_format(num_format => '00000');
935:     $worksheet->write('A2', '01209', $format1);
936: 
937:     # Write explicitly as a string: 01209
938:     $worksheet->write_string('A3', '01209');
939: 
940:     # Write implicitly as a string: 01209
941:     $worksheet->keep_leading_zeros();
942:     $worksheet->write('A4', '01209');
943: 
944: 
945: The above code would generate a worksheet that looked like the following:
946: 
947:      -----------------------------------------------------------
948:     |   |     A     |     B     |     C     |     D     | ...
949:      -----------------------------------------------------------
950:     | 1 |      1209 |           |           |           | ...
951:     | 2 |     01209 |           |           |           | ...
952:     | 3 | 01209     |           |           |           | ...
953:     | 4 | 01209     |           |           |           | ...
954: 
955: 
956: 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>.
957: 
958: 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<@>:
959: 
960:     # Format as a string (01209)
961:     my $format2 = $workbook->add_format(num_format => '@');
962:     $worksheet->write_string('A5', '01209', $format2);
963: 
964: 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:
965: 
966:     $worksheet->keep_leading_zeros();  # Set on
967:     $worksheet->keep_leading_zeros(1); # Set on
968:     $worksheet->keep_leading_zeros(0); # Set off
969: 
970: See also the C<add_write_handler()> method.
971: 
972: 
973: =head2 write_blank($row, $column, $format)
974: 
975: Write a blank cell specified by C<$row> and C<$column>:
976: 
977:     $worksheet->write_blank(0, 0, $format);
978: 
979: This method is used to add formatting to a cell which doesn't contain a string or number value.
980: 
981: 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.
982: 
983: As such, if you write an empty cell without formatting it is ignored:
984: 
985:     $worksheet->write('A1',  undef, $format); # write_blank()
986:     $worksheet->write('A2',  undef         ); # Ignored
987: 
988: This seemingly uninteresting fact means that you can write arrays of data without special treatment for undef or empty string values.
989: 
990: See the note about L</Cell notation>.
991: 
992: 
993: 
994: 
995: =head2 write_row($row, $column, $array_ref, $format)
996: 
997: 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:
998: 
999:     @array      = ('awk', 'gawk', 'mawk');
1000:     $array_ref  = \@array;
1001: 
1002:     $worksheet->write_row(0, 0, $array_ref);
1003: 
1004:     # The above example is equivalent to:
1005:     $worksheet->write(0, 0, $array[0]);
1006:     $worksheet->write(0, 1, $array[1]);
1007:     $worksheet->write(0, 2, $array[2]);
1008: 
1009: 
1010: 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:
1011: 
1012:     $worksheet->write_row('A1', $array_ref); # Write a row of data
1013:     $worksheet->write(    'A1', $array_ref); # Same thing
1014: 
1015: 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.
1016: 
1017: Array references within the data will be treated as columns. This allows you to write 2D arrays of data in one go. For example:
1018: 
1019:     @eec =  (
1020:                 ['maggie', 'milly', 'molly', 'may'  ],
1021:                 [13,       14,      15,      16     ],
1022:                 ['shell',  'star',  'crab',  'stone']
1023:             );
1024: 
1025:     $worksheet->write_row('A1', \@eec);
1026: 
1027: 
1028: Would produce a worksheet as follows:
1029: 
1030:      -----------------------------------------------------------
1031:     |   |    A    |    B    |    C    |    D    |    E    | ...
1032:      -----------------------------------------------------------
1033:     | 1 | maggie  | 13      | shell   | ...     |  ...    | ...
1034:     | 2 | milly   | 14      | star    | ...     |  ...    | ...
1035:     | 3 | molly   | 15      | crab    | ...     |  ...    | ...
1036:     | 4 | may     | 16      | stone   | ...     |  ...    | ...
1037:     | 5 | ...     | ...     | ...     | ...     |  ...    | ...
1038:     | 6 | ...     | ...     | ...     | ...     |  ...    | ...
1039: 
1040: 
1041: To write the data in a row-column order refer to the C<write_col()> method below.
1042: 
1043: 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.
1044: 
1045: 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>.
1046: 
1047: 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.
1048: 
1049: See also the C<write_arrays.pl> program in the C<examples> directory of the distro.
1050: 
1051: The C<write_row()> method allows the following idiomatic conversion of a text file to an Excel file:
1052: 
1053:     #!/usr/bin/perl -w
1054: 
1055:     use strict;
1056:     use Spreadsheet::WriteExcel;
1057: 
1058:     my $workbook  = Spreadsheet::WriteExcel->new('file.xls');
1059:     my $worksheet = $workbook->add_worksheet();
1060: 
1061:     open INPUT, 'file.txt' or die "Couldn't open file: $!";
1062: 
1063:     $worksheet->write($.-1, 0, [split]) while <INPUT>;
1064: 
1065: 
1066: 
1067: 
1068: =head2 write_col($row, $column, $array_ref, $format)
1069: 
1070: 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:
1071: 
1072:     @array      = ('awk', 'gawk', 'mawk');
1073:     $array_ref  = \@array;
1074: 
1075:     $worksheet->write_col(0, 0, $array_ref);
1076: 
1077:     # The above example is equivalent to:
1078:     $worksheet->write(0, 0, $array[0]);
1079:     $worksheet->write(1, 0, $array[1]);
1080:     $worksheet->write(2, 0, $array[2]);
1081: 
1082: 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.
1083: 
1084: Array references within the data will be treated as rows. This allows you to write 2D arrays of data in one go. For example:
1085: 
1086:     @eec =  (
1087:                 ['maggie', 'milly', 'molly', 'may'  ],
1088:                 [13,       14,      15,      16     ],
1089:                 ['shell',  'star',  'crab',  'stone']
1090:             );
1091: 
1092:     $worksheet->write_col('A1', \@eec);
1093: 
1094: 
1095: Would produce a worksheet as follows:
1096: 
1097:      -----------------------------------------------------------
1098:     |   |    A    |    B    |    C    |    D    |    E    | ...
1099:      -----------------------------------------------------------
1100:     | 1 | maggie  | milly   | molly   | may     |  ...    | ...
1101:     | 2 | 13      | 14      | 15      | 16      |  ...    | ...
1102:     | 3 | shell   | star    | crab    | stone   |  ...    | ...
1103:     | 4 | ...     | ...     | ...     | ...     |  ...    | ...
1104:     | 5 | ...     | ...     | ...     | ...     |  ...    | ...
1105:     | 6 | ...     | ...     | ...     | ...     |  ...    | ...
1106: 
1107: 
1108: To write the data in a column-row order refer to the C<write_row()> method above.
1109: 
1110: 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.
1111: 
1112: 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:
1113: 
1114:     $worksheet->write_col('A1', $array_ref    ); # Write a column of data
1115:     $worksheet->write(    'A1', [ $array_ref ]); # Same thing
1116: 
1117: 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>.
1118: 
1119: 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.
1120: 
1121: See also the C<write_arrays.pl> program in the C<examples> directory of the distro.
1122: 
1123: 
1124: 
1125: 
1126: =head2 write_date_time($row, $col, $date_string, $format)
1127: 
1128: 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>:
1129: 
1130:     $worksheet->write_date_time('A1', '2004-05-13T23:20', $date_format);
1131: 
1132: The C<$date_string> should be in the following format:
1133: 
1134:     yyyy-mm-ddThh:mm:ss.sss
1135: 
1136: This conforms to an ISO8601 date but it should be noted that the full range of ISO8601 formats are not supported.
1137: 
1138: The following variations on the C<$date_string> parameter are permitted:
1139: 
1140:     yyyy-mm-ddThh:mm:ss.sss         # Standard format
1141:     yyyy-mm-ddT                     # No time
1142:               Thh:mm:ss.sss         # No date
1143:     yyyy-mm-ddThh:mm:ss.sssZ        # Additional Z (but not time zones)
1144:     yyyy-mm-ddThh:mm:ss             # No fractional seconds
1145:     yyyy-mm-ddThh:mm                # No seconds
1146: 
1147: Note that the C<T> is required in all cases.
1148: 
1149: 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:
1150: 
1151:     my $date_format = $workbook->add_format(num_format => 'mm/dd/yy');
1152:     $worksheet->write_date_time('A1', '2004-05-13T23:20', $date_format);
1153: 
1154: 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.
1155: 
1156: See also the date_time.pl program in the C<examples> directory of the distro.
1157: 
1158: 
1159: 
1160: 
1161: =head2 write_url($row, $col, $url, $label, $format)
1162: 
1163: 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 parameters C<$label> and the C<$format> are optional and their position is interchangeable.
1164: 
1165: The label is written using the C<write()> method. Therefore it is possible to write strings, numbers or formulas as labels.
1166: 
1167: There are four web style URI's supported: C<http://>, C<https://>, C<ftp://> and  C<mailto:>:
1168: 
1169:     $worksheet->write_url(0, 0,  'ftp://www.perl.org/'                  );
1170:     $worksheet->write_url(1, 0,  'http://www.perl.com/', 'Perl home'    );
1171:     $worksheet->write_url('A3',  'http://www.perl.com/', $format        );
1172:     $worksheet->write_url('A4',  'http://www.perl.com/', 'Perl', $format);
1173:     $worksheet->write_url('A5',  'mailto:jmcnamara@cpan.org'            );
1174: 
1175: 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:
1176: 
1177:     $worksheet->write_url('A6',  'internal:Sheet2!A1'                   );
1178:     $worksheet->write_url('A7',  'internal:Sheet2!A1',   $format        );
1179:     $worksheet->write_url('A8',  'internal:Sheet2!A1:B2'                );
1180:     $worksheet->write_url('A9',  q{internal:'Sales Data'!A1}            );
1181:     $worksheet->write_url('A10', 'external:c:\temp\foo.xls'             );
1182:     $worksheet->write_url('A11', 'external:c:\temp\foo.xls#Sheet2!A1'   );
1183:     $worksheet->write_url('A12', 'external:..\..\..\foo.xls'            );
1184:     $worksheet->write_url('A13', 'external:..\..\..\foo.xls#Sheet2!A1'  );
1185:     $worksheet->write_url('A13', 'external:\\\\NETWORK\share\foo.xls'   );
1186: 
1187: All of the these URI types are recognised by the C<write()> method, see above.
1188: 
1189: 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>.
1190: 
1191: In external links the workbook and worksheet name must be separated by the C<#> character: C<external:Workbook.xls#Sheet1!A1'>.
1192: 
1193: 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.xls> you could link to it as follows:
1194: 
1195:     $worksheet->write_url('A14', 'external:c:\temp\foo.xls#my_name');
1196: 
1197: Note, you cannot currently create named ranges with C<Spreadsheet::WriteExcel>.
1198: 
1199: 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.
1200: 
1201: 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'>.
1202: 
1203: 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?>.
1204: 
1205: Finally, you can avoid most of these quoting problems by using forward slashes. These are translated internally to backslashes:
1206: 
1207:     $worksheet->write_url('A14', "external:c:/temp/foo.xls"             );
1208:     $worksheet->write_url('A15', 'external://NETWORK/share/foo.xls'     );
1209: 
1210: See also, the note about L</Cell notation>.
1211: 
1212: 
1213: 
1214: 
1215: =head2 write_url_range($row1, $col1, $row2, $col2, $url, $string, $format)
1216: 
1217: This method is essentially the same as the C<write_url()> method described above. The main difference is that you can specify a link for a range of cells:
1218: 
1219:     $worksheet->write_url(0, 0, 0, 3, 'ftp://www.perl.org/'              );
1220:     $worksheet->write_url(1, 0, 0, 3, 'http://www.perl.com/', 'Perl home');
1221:     $worksheet->write_url('A3:D3',    'internal:Sheet2!A1'               );
1222:     $worksheet->write_url('A4:D4',    'external:c:\temp\foo.xls'         );
1223: 
1224: 
1225: This method is generally only required when used in conjunction with merged cells. See the C<merge_range()> method and the C<merge> property of a Format object, L</CELL FORMATTING>.
1226: 
1227: There is no way to force this behaviour through the C<write()> method.
1228: 
1229: The parameters C<$string> and the C<$format> are optional and their position is interchangeable. However, they are applied only to the first cell in the range.
1230: 
1231: See also, the note about L</Cell notation>.
1232: 
1233: 
1234: 
1235: 
1236: =head2 write_formula($row, $column, $formula, $format, $value)
1237: 
1238: Write a formula or function to the cell specified by C<$row> and C<$column>:
1239: 
1240:     $worksheet->write_formula(0, 0, '=$B$3 + B4'  );
1241:     $worksheet->write_formula(1, 0, '=SIN(PI()/4)');
1242:     $worksheet->write_formula(2, 0, '=SUM(B1:B5)' );
1243:     $worksheet->write_formula('A4', '=IF(A3>1,"Yes", "No")'   );
1244:     $worksheet->write_formula('A5', '=AVERAGE(1, 2, 3, 4)'    );
1245:     $worksheet->write_formula('A6', '=DATEVALUE("1-Jan-2001")');
1246: 
1247: See the note about L</Cell notation>. For more information about writing Excel formulas see L</FORMULAS AND FUNCTIONS IN EXCEL>
1248: 
1249: See also the section "Improving performance when working with formulas" and the C<store_formula()> and C<repeat_formula()> methods.
1250: 
1251: 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:
1252: 
1253:     $worksheet->write('A1', '=2+2', $format, 4);
1254: 
1255: However, this probably isn't something that will ever need to do. If you do use this feature then do so with care.
1256: 
1257: 
1258: 
1259: 
1260: =head2 store_formula($formula)
1261: 
1262: The C<store_formula()> method is used in conjunction with C<repeat_formula()> to speed up the generation of repeated formulas. See "Improving performance when working with formulas" in L</FORMULAS AND FUNCTIONS IN EXCEL>.
1263: 
1264: The C<store_formula()> method pre-parses a textual representation of a formula and stores it for use at a later stage by the C<repeat_formula()> method.
1265: 
1266: C<store_formula()> carries the same speed penalty as C<write_formula()>. However, in practice it will be used less frequently.
1267: 
1268: The return value of this method is a scalar that can be thought of as a reference to a formula.
1269: 
1270:     my $sin = $worksheet->store_formula('=SIN(A1)');
1271:     my $cos = $worksheet->store_formula('=COS(A1)');
1272: 
1273:     $worksheet->repeat_formula('B1', $sin, $format, 'A1', 'A2');
1274:     $worksheet->repeat_formula('C1', $cos, $format, 'A1', 'A2');
1275: 
1276: Although C<store_formula()> is a worksheet method the return value can be used in any worksheet:
1277: 
1278:     my $now = $worksheet->store_formula('=NOW()');
1279: 
1280:     $worksheet1->repeat_formula('B1', $now);
1281:     $worksheet2->repeat_formula('B1', $now);
1282:     $worksheet3->repeat_formula('B1', $now);
1283: 
1284: 
1285: 
1286: =head2 repeat_formula($row, $col, $formula, $format, ($pattern => $replace, ...))
1287: 
1288: 
1289: The C<repeat_formula()> method is used in conjunction with C<store_formula()> to speed up the generation of repeated formulas.  See "Improving performance when working with formulas" in L</FORMULAS AND FUNCTIONS IN EXCEL>.
1290: 
1291: In many respects C<repeat_formula()> behaves like C<write_formula()> except that it is significantly faster.
1292: 
1293: The C<repeat_formula()> method creates a new formula based on the pre-parsed tokens returned by C<store_formula()>. The new formula is generated by substituting C<$pattern>, C<$replace> pairs in the stored formula:
1294: 
1295:     my $formula = $worksheet->store_formula('=A1 * 3 + 50');
1296: 
1297:     for my $row (0..99) {
1298:         $worksheet->repeat_formula($row, 1, $formula, $format, 'A1', 'A'.($row +1));
1299:     }
1300: 
1301: It should be noted that C<repeat_formula()> doesn't modify the tokens. In the above example the substitution is always made against the original token, C<A1>, which doesn't change.
1302: 
1303: As usual, you can use C<undef> if you don't wish to specify a C<$format>:
1304: 
1305:     $worksheet->repeat_formula('B2', $formula, $format, 'A1', 'A2');
1306:     $worksheet->repeat_formula('B3', $formula, undef,   'A1', 'A3');
1307: 
1308: The substitutions are made from left to right and you can use as many C<$pattern>, C<$replace> pairs as you need. However, each substitution is made only once:
1309: 
1310:     my $formula = $worksheet->store_formula('=A1 + A1');
1311: 
1312:     # Gives '=B1 + A1'
1313:     $worksheet->repeat_formula('B1', $formula, undef, 'A1', 'B1');
1314: 
1315:     # Gives '=B1 + B1'
1316:     $worksheet->repeat_formula('B2', $formula, undef, ('A1', 'B1') x 2);
1317: 
1318: Since the C<$pattern> is interpolated each time that it is used it is worth using the C<qr> operator to quote the pattern. The C<qr> operator is explained in the C<perlop> man page.
1319: 
1320:     $worksheet->repeat_formula('B1', $formula, $format, qr/A1/, 'A2');
1321: 
1322: Care should be taken with the values that are substituted. The formula returned by C<repeat_formula()> contains several other tokens in addition to those in the formula and these might also match the  pattern that you are trying to replace. In particular you should avoid substituting a single 0, 1, 2 or 3.
1323: 
1324: You should also be careful to avoid false matches. For example the following snippet is meant to change the stored formula in steps from C<=A1 + SIN(A1)> to C<=A10 + SIN(A10)>.
1325: 
1326:     my $formula = $worksheet->store_formula('=A1 + SIN(A1)');
1327: 
1328:     for my $row (1 .. 10) {
1329:         $worksheet->repeat_formula($row -1, 1, $formula, undef,
1330:                                     qw/A1/, 'A' . $row,   #! Bad.
1331:                                     qw/A1/, 'A' . $row    #! Bad.
1332:                                   );
1333:     }
1334: 
1335: However it contains a bug. In the last iteration of the loop when C<$row> is 10 the following substitutions will occur:
1336: 
1337:     s/A1/A10/;    changes    =A1 + SIN(A1)     to    =A10 + SIN(A1)
1338:     s/A1/A10/;    changes    =A10 + SIN(A1)    to    =A100 + SIN(A1) # !!
1339: 
1340: The solution in this case is to use a more explicit match such as C<qw/^A1$/>:
1341: 
1342:         $worksheet->repeat_formula($row -1, 1, $formula, undef,
1343:                                     qw/^A1$/, 'A' . $row,
1344:                                     qw/^A1$/, 'A' . $row
1345:                                   );
1346: 
1347: Another similar problem occurs due to the fact that substitutions are made in order. For example the following snippet is meant to change the stored formula from C<=A10 + A11>  to C<=A11 + A12>:
1348: 
1349:     my $formula = $worksheet->store_formula('=A10 + A11');
1350: 
1351:     $worksheet->repeat_formula('A1', $formula, undef,
1352:                                 qw/A10/, 'A11',   #! Bad.
1353:                                 qw/A11/, 'A12'    #! Bad.
1354:                               );
1355: 
1356: However, the actual substitution yields C<=A12 + A11>:
1357: 
1358:     s/A10/A11/;    changes    =A10 + A11    to    =A11 + A11
1359:     s/A11/A12/;    changes    =A11 + A11    to    =A12 + A11 # !!
1360: 
1361: The solution here would be to reverse the order of the substitutions or to start with a stored formula that won't yield a false match such as C<=X10 + Y11>:
1362: 
1363:     my $formula = $worksheet->store_formula('=X10 + Y11');
1364: 
1365:     $worksheet->repeat_formula('A1', $formula, undef,
1366:                                 qw/X10/, 'A11',
1367:                                 qw/Y11/, 'A12'
1368:                               );
1369: 
1370: 
1371: If you think that you have a problem related to a false match you can check the tokens that you are substituting against as follows.
1372: 
1373:     my $formula = $worksheet->store_formula('=A1*5+4');
1374:     print "@$formula\n";
1375: 
1376: See also the C<repeat.pl> program in the C<examples> directory of the distro.
1377: 
1378: 
1379: 
1380: 
1381: =head2 write_comment($row, $column, $string, ...)
1382: 
1383: 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.
1384: 
1385: The following example shows how to add a comment to a cell:
1386: 
1387:     $worksheet->write        (2, 2, 'Hello');
1388:     $worksheet->write_comment(2, 2, 'This is a comment.');
1389: 
1390: 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>.
1391: 
1392:     $worksheet->write        ('C3', 'Hello');
1393:     $worksheet->write_comment('C3', 'This is a comment.');
1394: 
1395: On systems with C<perl 5.8> and later the C<write_comment()> method will also handle strings in C<UTF-8> format.
1396: 
1397:     $worksheet->write_comment('C3', "\x{263a}");       # Smiley
1398:     $worksheet->write_comment('C4', 'Comment ca va?');
1399: 
1400: 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:
1401: 
1402:     $worksheet->write_comment('C3', 'Hello', visible => 1, author => 'Perl');
1403: 
1404: Most of these options are quite specific and in general the default comment behaviour will be all that you need. However, should you need greater control over the format of the cell comment the following options are available:
1405: 
1406:     encoding
1407:     author
1408:     author_encoding
1409:     visible
1410:     x_scale
1411:     width
1412:     y_scale
1413:     height
1414:     color
1415:     start_cell
1416:     start_row
1417:     start_col
1418:     x_offset
1419:     y_offset
1420: 
1421: 
1422: =over 4
1423: 
1424: =item Option: encoding
1425: 
1426: This option is used to indicate that the comment string is encoded as C<UTF-16BE>.
1427: 
1428:     my $comment = pack 'n', 0x263a; # UTF-16BE Smiley symbol
1429: 
1430:     $worksheet->write_comment('C3', $comment, encoding => 1);
1431: 
1432: If you wish to use Unicode characters in the comment string then the preferred method is to use perl 5.8 and C<UTF-8> strings, see L</UNICODE IN EXCEL>.
1433: 
1434: 
1435: =item Option: author
1436: 
1437: This option is used to indicate who the author of the comment is. 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.
1438: 
1439:     $worksheet->write_comment('C3', 'Atonement', author => 'Ian McEwan');
1440: 
1441: 
1442: =item Option: author_encoding
1443: 
1444: This option is used to indicate that the author string is encoded as C<UTF-16BE>.
1445: 
1446: 
1447: =item Option: visible
1448: 
1449: 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 Spreadsheet::WriteExcel individual comments can be made visible as follows:
1450: 
1451:     $worksheet->write_comment('C3', 'Hello', visible => 1);
1452: 
1453: 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:
1454: 
1455:     $worksheet->write_comment('C3', 'Hello', visible => 0);
1456: 
1457: 
1458: =item Option: x_scale
1459: 
1460: This option is used to set the width of the cell comment box as a factor of the default width.
1461: 
1462:     $worksheet->write_comment('C3', 'Hello', x_scale => 2);
1463:     $worksheet->write_comment('C4', 'Hello', x_scale => 4.2);
1464: 
1465: 
1466: =item Option: width
1467: 
1468: This option is used to set the width of the cell comment box explicitly in pixels.
1469: 
1470:     $worksheet->write_comment('C3', 'Hello', width => 200);
1471: 
1472: 
1473: =item Option: y_scale
1474: 
1475: This option is used to set the height of the cell comment box as a factor of the default height.
1476: 
1477:     $worksheet->write_comment('C3', 'Hello', y_scale => 2);
1478:     $worksheet->write_comment('C4', 'Hello', y_scale => 4.2);
1479: 
1480: 
1481: =item Option: height
1482: 
1483: This option is used to set the height of the cell comment box explicitly in pixels.
1484: 
1485:     $worksheet->write_comment('C3', 'Hello', height => 200);
1486: 
1487: 
1488: =item Option: color
1489: 
1490: This option is used to set the background colour of cell comment box. You can use one of the named colours recognised by Spreadsheet::WriteExcel or a colour index. See L</COLOURS IN EXCEL>.
1491: 
1492:     $worksheet->write_comment('C3', 'Hello', color => 'green');
1493:     $worksheet->write_comment('C4', 'Hello', color => 0x35);    # Orange
1494: 
1495: 
1496: =item Option: start_cell
1497: 
1498: 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>.
1499: 
1500:     $worksheet->write_comment('C3', 'Hello', start_cell => 'E2');
1501: 
1502: 
1503: =item Option: start_row
1504: 
1505: 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.
1506: 
1507:     $worksheet->write_comment('C3', 'Hello', start_row => 0);
1508: 
1509: 
1510: =item Option: start_col
1511: 
1512: 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.
1513: 
1514:     $worksheet->write_comment('C3', 'Hello', start_col => 4);
1515: 
1516: 
1517: =item Option: x_offset
1518: 
1519: This option is used to change the x offset, in pixels, of a comment within a cell:
1520: 
1521:     $worksheet->write_comment('C3', $comment, x_offset => 30);
1522: 
1523: 
1524: =item Option: y_offset
1525: 
1526: This option is used to change the y offset, in pixels, of a comment within a cell:
1527: 
1528:     $worksheet->write_comment('C3', $comment, x_offset => 30);
1529: 
1530: 
1531: =back
1532: 
1533: You can apply as many of these options as you require.
1534: 
1535: See also L</ROW HEIGHTS AND WORKSHEET OBJECTS>.
1536: 
1537: 
1538: 
1539: 
1540: =head2 show_comments()
1541: 
1542: This method is used to make all cell comments visible when a worksheet is opened.
1543: 
1544: Individual comments can be made visible using the C<visible> parameter of the C<write_comment> method (see above):
1545: 
1546:     $worksheet->write_comment('C3', 'Hello', visible => 1);
1547: 
1548: If all of the cell comments have been made visible you can hide individual comments as follows:
1549: 
1550:     $worksheet->write_comment('C3', 'Hello', visible => 0);
1551: 
1552: 
1553: 
1554: 
1555: =head2 add_write_handler($re, $code_ref)
1556: 
1557: This method is used to extend the Spreadsheet::WriteExcel write() method to handle user defined data.
1558: 
1559: 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.
1560: 
1561: 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()>.
1562: 
1563: 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:
1564: 
1565:     $worksheet->add_write_handler(qr/^\d\d\d\d$/, \&my_write);
1566: 
1567: (In the these examples the C<qr> operator is used to quote the regular expression strings, see L<perlop> for more details).
1568: 
1569: 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:
1570: 
1571:     $worksheet->add_write_handler(qr/^\d{7}$/, \&write_my_id);
1572: 
1573: 
1574:     sub write_my_id {
1575:         my $worksheet = shift;
1576:         return $worksheet->write_string(@_);
1577:     }
1578: 
1579: * You could also use the C<keep_leading_zeros()> method for this.
1580: 
1581: Then if you call C<write()> with an appropriate string it will be handled automatically:
1582: 
1583:     # Writes 0000000. It would normally be written as a number; 0.
1584:     $worksheet->write('A1', '0000000');
1585: 
1586: 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:
1587: 
1588:     $_[0]   A ref to the calling worksheet. *
1589:     $_[1]   Zero based row number.
1590:     $_[2]   Zero based column number.
1591:     $_[3]   A number or string or token.
1592:     $_[4]   A format ref if any.
1593:     $_[5]   Any other arguments.
1594:     ...
1595: 
1596:     *  It is good style to shift this off the list so the @_ is the same
1597:        as the argument list seen by write().
1598: 
1599: 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.
1600: 
1601: 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:
1602: 
1603: 
1604:     sub write_my_id {
1605:         my $worksheet = shift;
1606:         my $col       = $_[1];
1607: 
1608:         if ($col == 0) {
1609:             return $worksheet->write_string(@_);
1610:         }
1611:         else {
1612:             # Reject the match and return control to write()
1613:             return undef;
1614:         }
1615:     }
1616: 
1617: Now, you will get different behaviour for the first column and other columns:
1618: 
1619:     $worksheet->write('A1', '0000000'); # Writes 0000000
1620:     $worksheet->write('B1', '0000000'); # Writes 0
1621: 
1622: 
1623: You may add more than one handler in which case they will be called in the order that they were added.
1624: 
1625: Note, the C<add_write_handler()> method is particularly suited for handling dates.
1626: 
1627: See the C<write_handler 1-4> programs in the C<examples> directory for further examples.
1628: 
1629: 
1630: 
1631: 
1632: =head2 insert_image($row, $col, $filename, $x, $y, $scale_x, $scale_y)
1633: 
1634: This method can be used to insert a image into a worksheet. The image can be in PNG, JPEG or BMP format. The C<$x>, C<$y>, C<$scale_x> and C<$scale_y> parameters are optional.
1635: 
1636:     $worksheet1->insert_image('A1', 'perl.bmp');
1637:     $worksheet2->insert_image('A1', '../images/perl.bmp');
1638:     $worksheet3->insert_image('A1', '.c:\images\perl.bmp');
1639: 
1640: 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.
1641: 
1642:     $worksheet1->insert_image('A1', 'perl.bmp', 32, 10);
1643: 
1644: The default width of a cell is 63 pixels. The default height of a cell is 17 pixels. The pixels offsets can be calculated using the following relationships:
1645: 
1646:     Wp = int(12We)   if We <  1
1647:     Wp = int(7We +5) if We >= 1
1648:     Hp = int(4/3He)
1649: 
1650:     where:
1651:     We is the cell width in Excels units
1652:     Wp is width in pixels
1653:     He is the cell height in Excels units
1654:     Hp is height in pixels
1655: 
1656: 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.
1657: 
1658: The parameters C<$scale_x> and C<$scale_y> can be used to scale the inserted image horizontally and vertically:
1659: 
1660:     # Scale the inserted image: width x 2.0, height x 0.8
1661:     $worksheet->insert_image('A1', 'perl.bmp', 0, 0, 2, 0.8);
1662: 
1663: See also the C<images.pl> program in the C<examples> directory of the distro.
1664: 
1665: BMP images must be 24 bit, true colour, bitmaps. In general it is best to avoid BMP images since they aren't compressed. The older C<insert_bitmap()> method is still supported but deprecated.
1666: 
1667: See also L</ROW HEIGHTS AND WORKSHEET OBJECTS>.
1668: 
1669: 
1670: 
1671: 
1672: =head2 insert_chart($row, $col, $chart, $x, $y, $scale_x, $scale_y)
1673: 
1674: 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.
1675: 
1676:     my $chart = $workbook->add_chart( type => 'line', embedded => 1 );
1677: 
1678:     # Configure the chart.
1679:     ...
1680: 
1681:     # Insert the chart into the a worksheet.
1682:     $worksheet->insert_chart('E2', $chart);
1683: 
1684: See C<add_chart()> for details on how to create the Chart object and L<Spreadsheet::WriteExcel::Chart> for details on how to configure it. See also the C<chart_*.pl> programs in the examples directory of the distro.
1685: 
1686: The C<$x>, C<$y>, C<$scale_x> and C<$scale_y> parameters are optional.
1687: 
1688: 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. See the C<insert_image> method above for more information on sizes.
1689: 
1690:     $worksheet1->insert_chart('E2', $chart, 3, 3);
1691: 
1692: The parameters C<$scale_x> and C<$scale_y> can be used to scale the inserted image horizontally and vertically:
1693: 
1694:     # Scale the width by 120% and the height by 150%
1695:     $worksheet->insert_chart('E2', $chart, 0, 0, 1.2, 1.5);
1696: 
1697: The easiest way to calculate the required scaling is to create a test chart worksheet with Spreadsheet::WriteExcel. Then open the file, select the chart and drag the corner to get the required size. While holding down the mouse the scale of the resized chart is shown to the left of the formula bar.
1698: 
1699: See also L</ROW HEIGHTS AND WORKSHEET OBJECTS>.
1700: 
1701: 
1702: 
1703: 
1704: =head2 embed_chart($row, $col, $filename, $x, $y, $scale_x, $scale_y)
1705: 
1706: This method can be used to insert a externally generated chart into a worksheet. The chart must first be extracted from an existing Excel file. This feature is semi-deprecated in favour of the "native" charts created using C<add_chart()>. Read C<external_charts.txt> (or C<.pod>) in the external_charts directory of the distro for a full explanation.
1707: 
1708: Here is an example:
1709: 
1710:     $worksheet->embed_chart('B2', 'sales_chart.bin');
1711: 
1712: The C<$x>, C<$y>, C<$scale_x> and C<$scale_y> parameters are optional. See C<insert_chart()> above for details.
1713: 
1714: 
1715: 
1716: 
1717: =head2 data_validation()
1718: 
1719: 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.
1720: 
1721: 
1722:     $worksheet->data_validation('B3',
1723:         {
1724:             validate => 'integer',
1725:             criteria => '>',
1726:             value    => 100,
1727:         });
1728: 
1729:     $worksheet->data_validation('B5:B9',
1730:         {
1731:             validate => 'list',
1732:             value    => ['open', 'high', 'close'],
1733:         });
1734: 
1735: This method contains a lot of parameters and is described in detail in a separate section L</DATA VALIDATION IN EXCEL>.
1736: 
1737: 
1738: See also the C<data_validate.pl> program in the examples directory of the distro
1739: 
1740: 
1741: 
1742: 
1743: =head2 get_name()
1744: 
1745: The C<get_name()> method is used to retrieve the name of a worksheet. For example:
1746: 
1747:     foreach my $sheet ($workbook->sheets()) {
1748:         print $sheet->get_name();
1749:     }
1750: 
1751: For reasons related to the design of Spreadsheet::WriteExcel 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.
1752: 
1753: 
1754: 
1755: 
1756: =head2 activate()
1757: 
1758: The C<activate()> method is used to specify which worksheet is initially visible in a multi-sheet workbook:
1759: 
1760:     $worksheet1 = $workbook->add_worksheet('To');
1761:     $worksheet2 = $workbook->add_worksheet('the');
1762:     $worksheet3 = $workbook->add_worksheet('wind');
1763: 
1764:     $worksheet3->activate();
1765: 
1766: 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.
1767: 
1768: The default active worksheet is the first worksheet.
1769: 
1770: 
1771: 
1772: 
1773: =head2 select()
1774: 
1775: The C<select()> method is used to indicate that a worksheet is selected in a multi-sheet workbook:
1776: 
1777:     $worksheet1->activate();
1778:     $worksheet2->select();
1779:     $worksheet3->select();
1780: 
1781: 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.
1782: 
1783: 
1784: 
1785: 
1786: =head2 hide()
1787: 
1788: The C<hide()> method is used to hide a worksheet:
1789: 
1790:     $worksheet2->hide();
1791: 
1792: You may wish to hide a worksheet in order to avoid confusing a user with intermediate data or calculations.
1793: 
1794: 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:
1795: 
1796:     $worksheet2->activate();
1797:     $worksheet1->hide();
1798: 
1799: 
1800: 
1801: 
1802: =head2 set_first_sheet()
1803: 
1804: 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()>:
1805: 
1806:     for (1..20) {
1807:         $workbook->add_worksheet;
1808:     }
1809: 
1810:     $worksheet21 = $workbook->add_worksheet();
1811:     $worksheet22 = $workbook->add_worksheet();
1812: 
1813:     $worksheet21->set_first_sheet();
1814:     $worksheet22->activate();
1815: 
1816: This method is not required very often. The default value is the first worksheet.
1817: 
1818: 
1819: 
1820: 
1821: =head2 protect($password)
1822: 
1823: The C<protect()> method is used to protect a worksheet from modification:
1824: 
1825:     $worksheet->protect();
1826: 
1827: It can be turned off in Excel via the C<Tools-E<gt>Protection-E<gt>Unprotect Sheet> menu command.
1828: 
1829: 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 "locked" cell cannot be edited. A "hidden" cell will display the results of a formula but not the formula itself. In Excel a cell's locked property is on by default.
1830: 
1831:     # Set some format properties
1832:     my $unlocked  = $workbook->add_format(locked => 0);
1833:     my $hidden    = $workbook->add_format(hidden => 1);
1834: 
1835:     # Enable worksheet protection
1836:     $worksheet->protect();
1837: 
1838:     # This cell cannot be edited, it is locked by default
1839:     $worksheet->write('A1', '=1+2');
1840: 
1841:     # This cell can be edited
1842:     $worksheet->write('A2', '=1+2', $unlocked);
1843: 
1844:     # The formula in this cell isn't visible
1845:     $worksheet->write('A3', '=1+2', $hidden);
1846: 
1847: See also the C<set_locked> and C<set_hidden> format methods in L</CELL FORMATTING>.
1848: 
1849: You can optionally add a password to the worksheet protection:
1850: 
1851:     $worksheet->protect('drowssap');
1852: 
1853: Note, the worksheet level password in Excel provides very weak protection. It does not encrypt your data in any way and it is very easy to deactivate. Therefore, do not use the above method if you wish to protect sensitive data or calculations. However, before you get worried, Excel's own workbook level password protection does provide strong encryption in Excel 97+. For technical reasons this will never be supported by C<Spreadsheet::WriteExcel>.
1854: 
1855: 
1856: 
1857: 
1858: =head2 set_selection($first_row, $first_col, $last_row, $last_col)
1859: 
1860: 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>.
1861: 
1862: Examples:
1863: 
1864:     $worksheet1->set_selection(3, 3);       # 1. Cell D4.
1865:     $worksheet2->set_selection(3, 3, 6, 6); # 2. Cells D4 to G7.
1866:     $worksheet3->set_selection(6, 6, 3, 3); # 3. Cells G7 to D4.
1867:     $worksheet4->set_selection('D4');       # Same as 1.
1868:     $worksheet5->set_selection('D4:G7');    # Same as 2.
1869:     $worksheet6->set_selection('G7:D4');    # Same as 3.
1870: 
1871: The default cell selections is (0, 0), 'A1'.
1872: 
1873: 
1874: 
1875: 
1876: =head2 set_row($row, $height, $format, $hidden, $level, $collapsed)
1877: 
1878: This method can be used to change the default properties of a row. All parameters apart from C<$row> are optional.
1879: 
1880: The most common use for this method is to change the height of a row:
1881: 
1882:     $worksheet->set_row(0, 20); # Row 1 height set to 20
1883: 
1884: If you wish to set the format without changing the height you can pass C<undef> as the height parameter:
1885: 
1886:     $worksheet->set_row(0, undef, $format);
1887: 
1888: The C<$format> parameter will be applied to any cells in the row that don't  have a format. For example
1889: 
1890:     $worksheet->set_row(0, undef, $format1);    # Set the format for row 1
1891:     $worksheet->write('A1', 'Hello');           # Defaults to $format1
1892:     $worksheet->write('B1', 'Hello', $format2); # Keeps $format2
1893: 
1894: 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.
1895: 
1896: 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:
1897: 
1898:     $worksheet->set_row(0, 20,    $format, 1);
1899:     $worksheet->set_row(1, undef, undef,   1);
1900: 
1901: 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.
1902: 
1903: The following example sets an outline level of 1 for rows 1 and 2 (zero-indexed):
1904: 
1905:     $worksheet->set_row(1, undef, undef, 0, 1);
1906:     $worksheet->set_row(2, undef, undef, 0, 1);
1907: 
1908: The C<$hidden> parameter can also be used to hide collapsed outlined rows when used in conjunction with the C<$level> parameter.
1909: 
1910:     $worksheet->set_row(1, undef, undef, 1, 1);
1911:     $worksheet->set_row(2, undef, undef, 1, 1);
1912: 
1913: For collapsed outlines you should also indicate which row has the collapsed C<+> symbol using the optional C<$collapsed> parameter.
1914: 
1915:     $worksheet->set_row(3, undef, undef, 0, 0, 1);
1916: 
1917: For a more complete example see the C<outline.pl> and C<outline_collapsed.pl> programs in the examples directory of the distro.
1918: 
1919: 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>.
1920: 
1921: 
1922: 
1923: 
1924: =head2 set_column($first_col, $last_col, $width, $format, $hidden, $level, $collapsed)
1925: 
1926: 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.
1927: 
1928: 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>.
1929: 
1930: 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>.
1931: 
1932: Examples:
1933: 
1934:     $worksheet->set_column(0, 0,  20); # Column  A   width set to 20
1935:     $worksheet->set_column(1, 3,  30); # Columns B-D width set to 30
1936:     $worksheet->set_column('E:E', 20); # Column  E   width set to 20
1937:     $worksheet->set_column('F:H', 30); # Columns F-H width set to 30
1938: 
1939: 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 Arial 10. 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.
1940: 
1941: 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:
1942: 
1943:     $worksheet->set_column(0, 0, undef, $format);
1944: 
1945: The C<$format> parameter will be applied to any cells in the column that don't  have a format. For example
1946: 
1947:     $worksheet->set_column('A:A', undef, $format1); # Set format for col 1
1948:     $worksheet->write('A1', 'Hello');               # Defaults to $format1
1949:     $worksheet->write('A2', 'Hello', $format2);     # Keeps $format2
1950: 
1951: 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.
1952: 
1953: A default row format takes precedence over a default column format
1954: 
1955:     $worksheet->set_row(0, undef,        $format1); # Set format for row 1
1956:     $worksheet->set_column('A:A', undef, $format2); # Set format for col 1
1957:     $worksheet->write('A1', 'Hello');               # Defaults to $format1
1958:     $worksheet->write('A2', 'Hello');               # Defaults to $format2
1959: 
1960: 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:
1961: 
1962:     $worksheet->set_column('D:D', 20,    $format, 1);
1963:     $worksheet->set_column('E:E', undef, undef,   1);
1964: 
1965: 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.
1966: 
1967: The following example sets an outline level of 1 for columns B to G:
1968: 
1969:     $worksheet->set_column('B:G', undef, undef, 0, 1);
1970: 
1971: The C<$hidden> parameter can also be used to hide collapsed outlined columns when used in conjunction with the C<$level> parameter.
1972: 
1973:     $worksheet->set_column('B:G', undef, undef, 1, 1);
1974: 
1975: For collapsed outlines you should also indicate which row has the collapsed C<+> symbol using the optional C<$collapsed> parameter.
1976: 
1977:     $worksheet->set_column('H:H', undef, undef, 0, 0, 1);
1978: 
1979: For a more complete example see the C<outline.pl> and C<outline_collapsed.pl> programs in the examples directory of the distro.
1980: 
1981: 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>.
1982: 
1983: 
1984: 
1985: 
1986: =head2 outline_settings($visible, $symbols_below, $symbols_right, $auto_style)
1987: 
1988: 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>.
1989: 
1990: 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.
1991: 
1992:     $worksheet->outline_settings(0);
1993: 
1994: 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.
1995: 
1996: 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.
1997: 
1998: 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<Spreadsheet::WriteExcel> 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.
1999: 
2000: The default settings for all of these parameters correspond to Excel's default parameters.
2001: 
2002: 
2003: The worksheet parameters controlled by C<outline_settings()> are rarely used.
2004: 
2005: 
2006: 
2007: 
2008: =head2 freeze_panes($row, $col, $top_row, $left_col)
2009: 
2010: 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
2011: 
2012: 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.
2013: 
2014: 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.
2015: 
2016: Examples:
2017: 
2018:     $worksheet->freeze_panes(1, 0); # Freeze the first row
2019:     $worksheet->freeze_panes('A2'); # Same using A1 notation
2020:     $worksheet->freeze_panes(0, 1); # Freeze the first column
2021:     $worksheet->freeze_panes('B1'); # Same using A1 notation
2022:     $worksheet->freeze_panes(1, 2); # Freeze first row and first 2 columns
2023:     $worksheet->freeze_panes('C2'); # Same using A1 notation
2024: 
2025: 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:
2026: 
2027:     $worksheet->freeze_panes(1, 0, 20, 0);
2028: 
2029: You cannot use A1 notation for the C<$top_row> and C<$left_col> parameters.
2030: 
2031: 
2032: See also the C<panes.pl> program in the C<examples> directory of the distribution.
2033: 
2034: 
2035: 
2036: 
2037: =head2 split_panes($y, $x, $top_row, $left_col)
2038: 
2039: 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.
2040: 
2041: 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<12.75> for a row and  C<8.43> for a column.
2042: 
2043: 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.
2044: 
2045: Example:
2046: 
2047:     $worksheet->split_panes(12.75, 0,    1, 0); # First row
2048:     $worksheet->split_panes(0,     8.43, 0, 1); # First column
2049:     $worksheet->split_panes(12.75, 8.43, 1, 1); # First row and column
2050: 
2051: You cannot use A1 notation with this method.
2052: 
2053: See also the C<freeze_panes()> method and the C<panes.pl> program in the C<examples> directory of the distribution.
2054: 
2055: 
2056: Note: This C<split_panes()> method was called C<thaw_panes()> in older versions. The older name is still available for backwards compatibility.
2057: 
2058: 
2059: 
2060: 
2061: =head2 merge_range($first_row, $first_col, $last_row, $last_col, $token, $format, $utf_16_be)
2062: 
2063: Merging cells can be achieved by setting the C<merge> property of a Format object, see L</CELL FORMATTING>. However, this only allows simple Excel5 style horizontal merging which Excel refers to as "center across selection".
2064: 
2065: The C<merge_range()> method allows you to do Excel97+ style formatting where the cells can contain other types of alignment in addition to the merging:
2066: 
2067:     my $format = $workbook->add_format(
2068:                                         border  => 6,
2069:                                         valign  => 'vcenter',
2070:                                         align   => 'center',
2071:                                       );
2072: 
2073:     $worksheet->merge_range('B3:D4', 'Vertical and horizontal', $format);
2074: 
2075: B<WARNING>. The format object that is used with a C<merge_range()> method call is marked internally as being associated with a merged range. It is a fatal error to use a merged format in a non-merged cell. Instead you should use separate formats for merged and non-merged cells. This restriction will be removed in a future release.
2076: 
2077: The C<$utf_16_be> parameter is optional, see below.
2078: 
2079: 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.
2080: 
2081: Setting the C<merge> property of the format isn't required when you are using C<merge_range()>. In fact using it will exclude the use of any other horizontal alignment option.
2082: 
2083: On systems with C<perl 5.8> and later the C<merge_range()> method will also handle strings in C<UTF-8> format.
2084: 
2085:     $worksheet->merge_range('B3:D4', "\x{263a}", $format); # Smiley
2086: 
2087: On earlier Perl systems your can specify C<UTF-16BE> worksheet names using an additional optional parameter:
2088: 
2089:     my $str = pack 'n', 0x263a;
2090:     $worksheet->merge_range('B3:D4', $str, $format, 1); # Smiley
2091: 
2092: 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.
2093: 
2094: 
2095: 
2096: 
2097: =head2 set_zoom($scale)
2098: 
2099: Set the worksheet zoom factor in the range C<10 E<lt>= $scale E<lt>= 400>:
2100: 
2101:     $worksheet1->set_zoom(50);
2102:     $worksheet2->set_zoom(75);
2103:     $worksheet3->set_zoom(300);
2104:     $worksheet4->set_zoom(400);
2105: 
2106: The default zoom factor is 100. You cannot zoom to "Selection" because it is calculated by Excel at run-time.
2107: 
2108: Note, C<set_zoom()> does not affect the scale of the printed page. For that you should use C<set_print_scale()>.
2109: 
2110: 
2111: 
2112: 
2113: =head2 right_to_left()
2114: 
2115: 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 he A1 cell in the top right.
2116: 
2117:     $worksheet->right_to_left();
2118: 
2119: This is useful when creating Arabic, Hebrew or other near or far eastern worksheets that use right-to-left as the default direction.
2120: 
2121: 
2122: 
2123: 
2124: =head2 hide_zero()
2125: 
2126: The C<hide_zero()> method is used to hide any zero values that appear in cells.
2127: 
2128:     $worksheet->hide_zero();
2129: 
2130: In Excel this option is found under Tools->Options->View.
2131: 
2132: 
2133: 
2134: 
2135: =head2 set_tab_color()
2136: 
2137: The C<set_tab_color()> method is used to change the colour of the worksheet tab. This feature is only available in Excel 2002 and later. You can use one of the standard colour names provided by the Format object or a colour index. See L</COLOURS IN EXCEL> and the C<set_custom_color()> method.
2138: 
2139:     $worksheet1->set_tab_color('red');
2140:     $worksheet2->set_tab_color(0x0C);
2141: 
2142: See the C<tab_colors.pl> program in the examples directory of the distro.
2143: 
2144: 
2145: 
2146: 
2147: =head2 autofilter($first_row, $first_col, $last_row, $last_col)
2148: 
2149: 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 in turn allow users to filter the data based on simple criteria so that some data is shown and some is hidden.
2150: 
2151: To add an autofilter to a worksheet:
2152: 
2153:     $worksheet->autofilter(0, 0, 10, 3);
2154:     $worksheet->autofilter('A1:D11');    # Same as above in A1 notation.
2155: 
2156: Filter conditions can be applied using the C<filter_column()> method.
2157: 
2158: See the C<autofilter.pl> program in the examples directory of the distro for a more detailed example.
2159: 
2160: 
2161: 
2162: 
2163: =head2 filter_column($column, $expression)
2164: 
2165: 
2166: The C<filter_column> method can be used to filter columns in a autofilter range based on simple conditions.
2167: 
2168: 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<Spreadsheet::WriteExcel> 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.
2169: 
2170: The conditions for the filter are specified using simple expressions:
2171: 
2172:     $worksheet->filter_column('A', 'x > 2000');
2173:     $worksheet->filter_column('B', 'x > 2000 and x < 5000');
2174: 
2175: The C<$column> parameter can either be a zero indexed column number or a string column name.
2176: 
2177: The following operators are available:
2178: 
2179:     Operator        Synonyms
2180:        ==           =   eq  =~
2181:        !=           <>  ne  !=
2182:        >
2183:        <
2184:        >=
2185:        <=
2186: 
2187:        and          &&
2188:        or           ||
2189: 
2190: 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.
2191: 
2192: An expression can comprise a single statement or two statements separated by the C<and> and C<or> operators. For example:
2193: 
2194:     'x <  2000'
2195:     'x >  2000'
2196:     'x == 2000'
2197:     'x >  2000 and x <  5000'
2198:     'x == 2000 or  x == 5000'
2199: 
2200: Filtering of blank or non-blank data can be achieved by using a value of C<Blanks> or C<NonBlanks> in the expression:
2201: 
2202:     'x == Blanks'
2203:     'x == NonBlanks'
2204: 
2205: Top 10 style filters can be specified using a expression like the following:
2206: 
2207:     Top|Bottom 1-500 Items|%
2208: 
2209: For example:
2210: 
2211:     'Top    10 Items'
2212:     'Bottom  5 Items'
2213:     'Top    25 %'
2214:     'Bottom 50 %'
2215: 
2216: Excel also allows some simple string matching operations:
2217: 
2218:     'x =~ b*'   # begins with b
2219:     'x !~ b*'   # doesn't begin with b
2220:     'x =~ *b'   # ends with b
2221:     'x !~ *b'   # doesn't end with b
2222:     'x =~ *b*'  # contains b
2223:     'x !~ *b*'  # doesn't contains b
2224: 
2225: 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<~>.
2226: 
2227: 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:
2228: 
2229:     'x     < 2000'
2230:     'col   < 2000'
2231:     'Price < 2000'
2232: 
2233: Also, note that a filter condition can only be applied to a column in a range specified by the C<autofilter()> Worksheet method.
2234: 
2235: See the C<autofilter.pl> program in the examples directory of the distro for a more detailed example.
2236: 
2237: 
2238: 
2239: 
2240: =head1 PAGE SET-UP METHODS
2241: 
2242: 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.
2243: 
2244: The following methods are available for page set-up:
2245: 
2246:     set_landscape()
2247:     set_portrait()
2248:     set_page_view()
2249:     set_paper()
2250:     center_horizontally()
2251:     center_vertically()
2252:     set_margins()
2253:     set_header()
2254:     set_footer()
2255:     repeat_rows()
2256:     repeat_columns()
2257:     hide_gridlines()
2258:     print_row_col_headers()
2259:     print_area()
2260:     print_across()
2261:     fit_to_pages()
2262:     set_start_page()
2263:     set_print_scale()
2264:     set_h_pagebreaks()
2265:     set_v_pagebreaks()
2266: 
2267: 
2268: A common requirement when working with Spreadsheet::WriteExcel 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:
2269: 
2270:     foreach $worksheet ($workbook->sheets()) {
2271:        $worksheet->set_landscape();
2272:     }
2273: 
2274: 
2275: 
2276: 
2277: =head2 set_landscape()
2278: 
2279: This method is used to set the orientation of a worksheet's printed page to landscape:
2280: 
2281:     $worksheet->set_landscape(); # Landscape mode
2282: 
2283: 
2284: 
2285: 
2286: =head2 set_portrait()
2287: 
2288: 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.
2289: 
2290:     $worksheet->set_portrait(); # Portrait mode
2291: 
2292: 
2293: 
2294: =head2 set_page_view()
2295: 
2296: This method is used to display the worksheet in "Page View" mode. This is currently only supported by Mac Excel, where it is the default.
2297: 
2298:     $worksheet->set_page_view();
2299: 
2300: 
2301: 
2302: =head2 set_paper($index)
2303: 
2304: This method is used to set the paper format for the printed output of a worksheet. The following paper styles are available:
2305: 
2306:     Index   Paper format            Paper size
2307:     =====   ============            ==========
2308:       0     Printer default         -
2309:       1     Letter                  8 1/2 x 11 in
2310:       2     Letter Small            8 1/2 x 11 in
2311:       3     Tabloid                 11 x 17 in
2312:       4     Ledger                  17 x 11 in
2313:       5     Legal                   8 1/2 x 14 in
2314:       6     Statement               5 1/2 x 8 1/2 in
2315:       7     Executive               7 1/4 x 10 1/2 in
2316:       8     A3                      297 x 420 mm
2317:       9     A4                      210 x 297 mm
2318:      10     A4 Small                210 x 297 mm
2319:      11     A5                      148 x 210 mm
2320:      12     B4                      250 x 354 mm
2321:      13     B5                      182 x 257 mm
2322:      14     Folio                   8 1/2 x 13 in
2323:      15     Quarto                  215 x 275 mm
2324:      16     -                       10x14 in
2325:      17     -                       11x17 in
2326:      18     Note                    8 1/2 x 11 in
2327:      19     Envelope  9             3 7/8 x 8 7/8
2328:      20     Envelope 10             4 1/8 x 9 1/2
2329:      21     Envelope 11             4 1/2 x 10 3/8
2330:      22     Envelope 12             4 3/4 x 11
2331:      23     Envelope 14             5 x 11 1/2
2332:      24     C size sheet            -
2333:      25     D size sheet            -
2334:      26     E size sheet            -
2335:      27     Envelope DL             110 x 220 mm
2336:      28     Envelope C3             324 x 458 mm
2337:      29     Envelope C4             229 x 324 mm
2338:      30     Envelope C5             162 x 229 mm
2339:      31     Envelope C6             114 x 162 mm
2340:      32     Envelope C65            114 x 229 mm
2341:      33     Envelope B4             250 x 353 mm
2342:      34     Envelope B5             176 x 250 mm
2343:      35     Envelope B6             176 x 125 mm
2344:      36     Envelope                110 x 230 mm
2345:      37     Monarch                 3.875 x 7.5 in
2346:      38     Envelope                3 5/8 x 6 1/2 in
2347:      39     Fanfold                 14 7/8 x 11 in
2348:      40     German Std Fanfold      8 1/2 x 12 in
2349:      41     German Legal Fanfold    8 1/2 x 13 in
2350: 
2351: 
2352: 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.
2353: 
2354:     $worksheet->set_paper(1); # US Letter
2355:     $worksheet->set_paper(9); # A4
2356: 
2357: If you do not specify a paper type the worksheet will print using the printer's default paper.
2358: 
2359: 
2360: 
2361: 
2362: =head2 center_horizontally()
2363: 
2364: Center the worksheet data horizontally between the margins on the printed page:
2365: 
2366:     $worksheet->center_horizontally();
2367: 
2368: 
2369: 
2370: 
2371: =head2 center_vertically()
2372: 
2373: Center the worksheet data vertically between the margins on the printed page:
2374: 
2375:     $worksheet->center_vertically();
2376: 
2377: 
2378: 
2379: 
2380: =head2 set_margins($inches)
2381: 
2382: There are several methods available for setting the worksheet margins on the printed page:
2383: 
2384:     set_margins()        # Set all margins to the same value
2385:     set_margins_LR()     # Set left and right margins to the same value
2386:     set_margins_TB()     # Set top and bottom margins to the same value
2387:     set_margin_left();   # Set left margin
2388:     set_margin_right();  # Set right margin
2389:     set_margin_top();    # Set top margin
2390:     set_margin_bottom(); # Set bottom margin
2391: 
2392: All of these methods take a distance in inches as a parameter. Note: 1 inch = 25.4mm. ;-) The default left and right margin is 0.75 inch. The default top and bottom margin is 1.00 inch.
2393: 
2394: 
2395: 
2396: =head2 set_header($string, $margin)
2397: 
2398: 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.
2399: 
2400: The available control character are:
2401: 
2402:     Control             Category            Description
2403:     =======             ========            ===========
2404:     &L                  Justification       Left
2405:     &C                                      Center
2406:     &R                                      Right
2407: 
2408:     &P                  Information         Page number
2409:     &N                                      Total number of pages
2410:     &D                                      Date
2411:     &T                                      Time
2412:     &F                                      File name
2413:     &A                                      Worksheet name
2414:     &Z                                      Workbook path
2415: 
2416:     &fontsize           Font                Font size
2417:     &"font,style"                           Font name and style
2418:     &U                                      Single underline
2419:     &E                                      Double underline
2420:     &S                                      Strikethrough
2421:     &X                                      Superscript
2422:     &Y                                      Subscript
2423: 
2424:     &&                  Miscellaneous       Literal ampersand &
2425: 
2426: 
2427: 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>.
2428: 
2429: For example (with ASCII art representation of the results):
2430: 
2431:     $worksheet->set_header('&LHello');
2432: 
2433:      ---------------------------------------------------------------
2434:     |                                                               |
2435:     | Hello                                                         |
2436:     |                                                               |
2437: 
2438: 
2439:     $worksheet->set_header('&CHello');
2440: 
2441:      ---------------------------------------------------------------
2442:     |                                                               |
2443:     |                          Hello                                |
2444:     |                                                               |
2445: 
2446: 
2447:     $worksheet->set_header('&RHello');
2448: 
2449:      ---------------------------------------------------------------
2450:     |                                                               |
2451:     |                                                         Hello |
2452:     |                                                               |
2453: 
2454: 
2455: 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:
2456: 
2457:     $worksheet->set_header('Hello');
2458: 
2459:      ---------------------------------------------------------------
2460:     |                                                               |
2461:     |                          Hello                                |
2462:     |                                                               |
2463: 
2464: 
2465: You can have text in each of the justification regions:
2466: 
2467:     $worksheet->set_header('&LCiao&CBello&RCielo');
2468: 
2469:      ---------------------------------------------------------------
2470:     |                                                               |
2471:     | Ciao                     Bello                          Cielo |
2472:     |                                                               |
2473: 
2474: 
2475: 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:
2476: 
2477:     $worksheet->set_header('&CPage &P of &N');
2478: 
2479:      ---------------------------------------------------------------
2480:     |                                                               |
2481:     |                        Page 1 of 6                            |
2482:     |                                                               |
2483: 
2484: 
2485:     $worksheet->set_header('&CUpdated at &T');
2486: 
2487:      ---------------------------------------------------------------
2488:     |                                                               |
2489:     |                    Updated at 12:30 PM                        |
2490:     |                                                               |
2491: 
2492: 
2493: 
2494: 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:
2495: 
2496:     $worksheet1->set_header('&C&30Hello Big'  );
2497:     $worksheet2->set_header('&C&10Hello Small');
2498: 
2499: 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":
2500: 
2501:     $worksheet1->set_header('&C&"Courier New,Italic"Hello');
2502:     $worksheet2->set_header('&C&"Courier New,Bold Italic"Hello');
2503:     $worksheet3->set_header('&C&"Times New Roman,Regular"Hello');
2504: 
2505: 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:
2506: 
2507:     .LeftHeader   = ""
2508:     .CenterHeader = "&""Times New Roman,Regular""Hello"
2509:     .RightHeader  = ""
2510: 
2511: 
2512: To include a single literal ampersand C<&> in a header or footer you should use a double ampersand C<&&>:
2513: 
2514:     $worksheet1->set_header('&CCuriouser && Curiouser - Attorneys at Law');
2515: 
2516: 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.50 inch. The header and footer margin size can be set as follows:
2517: 
2518:     $worksheet->set_header('&CHello', 0.75);
2519: 
2520: The header and footer margins are independent of the top and bottom margins.
2521: 
2522: 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.
2523: 
2524: On systems with C<perl 5.8> and later the C<set_header()> method can also handle Unicode strings in C<UTF-8> format.
2525: 
2526:     $worksheet->set_header("&C\x{263a}")
2527: 
2528: 
2529: See, also the C<headers.pl> program in the C<examples> directory of the distribution.
2530: 
2531: 
2532: 
2533: 
2534: =head2 set_footer()
2535: 
2536: The syntax of the C<set_footer()> method is the same as C<set_header()>,  see above.
2537: 
2538: 
2539: 
2540: 
2541: =head2 repeat_rows($first_row, $last_row)
2542: 
2543: Set the number of rows to repeat at the top of each printed page.
2544: 
2545: 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:
2546: 
2547:     $worksheet1->repeat_rows(0);    # Repeat the first row
2548:     $worksheet2->repeat_rows(0, 1); # Repeat the first two rows
2549: 
2550: 
2551: 
2552: 
2553: =head2 repeat_columns($first_col, $last_col)
2554: 
2555: Set the columns to repeat at the left hand side of each printed page.
2556: 
2557: 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>.
2558: 
2559:     $worksheet1->repeat_columns(0);     # Repeat the first column
2560:     $worksheet2->repeat_columns(0, 1);  # Repeat the first two columns
2561:     $worksheet3->repeat_columns('A:A'); # Repeat the first column
2562:     $worksheet4->repeat_columns('A:B'); # Repeat the first two columns
2563: 
2564: 
2565: 
2566: 
2567: =head2 hide_gridlines($option)
2568: 
2569: 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.
2570: 
2571:     $worksheet->hide_gridlines();
2572: 
2573: The following values of C<$option> are valid:
2574: 
2575:     0 : Don't hide gridlines
2576:     1 : Hide printed gridlines only
2577:     2 : Hide screen and printed gridlines
2578: 
2579: If you don't supply an argument or use C<undef> the default option is 1, i.e. only the printed gridlines are hidden.
2580: 
2581: 
2582: 
2583: 
2584: =head2 print_row_col_headers()
2585: 
2586: Set the option to print the row and column headers on the printed page.
2587: 
2588: An Excel worksheet looks something like the following;
2589: 
2590:      ------------------------------------------
2591:     |   |   A   |   B   |   C   |   D   |  ...
2592:      ------------------------------------------
2593:     | 1 |       |       |       |       |  ...
2594:     | 2 |       |       |       |       |  ...
2595:     | 3 |       |       |       |       |  ...
2596:     | 4 |       |       |       |       |  ...
2597:     |...|  ...  |  ...  |  ...  |  ...  |  ...
2598: 
2599: 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 :
2600: 
2601:     $worksheet->print_row_col_headers();
2602: 
2603: Do not confuse these headers with page headers as described in the C<set_header()> section above.
2604: 
2605: 
2606: 
2607: 
2608: =head2 print_area($first_row, $first_col, $last_row, $last_col)
2609: 
2610: 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>.
2611: 
2612: 
2613:     $worksheet1->print_area('A1:H20');    # Cells A1 to H20
2614:     $worksheet2->print_area(0, 0, 19, 7); # The same
2615:     $worksheet2->print_area('A:H');       # Columns A to H if rows have data
2616: 
2617: 
2618: 
2619: 
2620: =head2 print_across()
2621: 
2622: The C<print_across> method is used to change the default print direction. This is referred to by Excel as the sheet "page order".
2623: 
2624:     $worksheet->print_across();
2625: 
2626: The default page order is shown below for a worksheet that extends over 4 pages. The order is called "down then across":
2627: 
2628:     [1] [3]
2629:     [2] [4]
2630: 
2631: However, by using the C<print_across> method the print order will be changed to "across then down":
2632: 
2633:     [1] [2]
2634:     [3] [4]
2635: 
2636: 
2637: 
2638: 
2639: =head2 fit_to_pages($width, $height)
2640: 
2641: 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.
2642: 
2643:     $worksheet1->fit_to_pages(1, 1); # Fit to 1x1 pages
2644:     $worksheet2->fit_to_pages(2, 1); # Fit to 2x1 pages
2645:     $worksheet3->fit_to_pages(1, 2); # Fit to 1x2 pages
2646: 
2647: The print area can be defined using the C<print_area()> method as described above.
2648: 
2649: 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 or leave it blank:
2650: 
2651:     $worksheet1->fit_to_pages(1, 0); # 1 page wide and as long as necessary
2652:     $worksheet2->fit_to_pages(1);    # The same
2653: 
2654: 
2655: 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.
2656: 
2657: Note that C<fit_to_pages()> will override any manual page breaks that are defined in the worksheet.
2658: 
2659: 
2660: 
2661: 
2662: =head2 set_start_page($start_page)
2663: 
2664: 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.
2665: 
2666:     $worksheet->set_start_page(2);
2667: 
2668: 
2669: 
2670: 
2671: =head2 set_print_scale($scale)
2672: 
2673: Set the scale factor of the printed page. Scale factors in the range C<10 E<lt>= $scale E<lt>= 400> are valid:
2674: 
2675:     $worksheet1->set_print_scale(50);
2676:     $worksheet2->set_print_scale(75);
2677:     $worksheet3->set_print_scale(300);
2678:     $worksheet4->set_print_scale(400);
2679: 
2680: 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()>.
2681: 
2682: 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.
2683: 
2684: 
2685: 
2686: 
2687: =head2 set_h_pagebreaks(@breaks)
2688: 
2689: 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:
2690: 
2691:     $worksheet1->set_h_pagebreaks(20); # Break between row 20 and 21
2692: 
2693: The C<set_h_pagebreaks()> method will accept a list of page breaks and you can call it more than once:
2694: 
2695:     $worksheet2->set_h_pagebreaks( 20,  40,  60,  80, 100); # Add breaks
2696:     $worksheet2->set_h_pagebreaks(120, 140, 160, 180, 200); # Add some more
2697: 
2698: Note: If you specify the "fit to page" option via the C<fit_to_pages()> method it will override all manual page breaks.
2699: 
2700: There is a silent limitation of about 1000 horizontal page breaks per worksheet in line with an Excel internal limitation.
2701: 
2702: 
2703: 
2704: 
2705: =head2 set_v_pagebreaks(@breaks)
2706: 
2707: 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:
2708: 
2709:     $worksheet1->set_v_pagebreaks(20); # Break between column 20 and 21
2710: 
2711: The C<set_v_pagebreaks()> method will accept a list of page breaks and you can call it more than once:
2712: 
2713:     $worksheet2->set_v_pagebreaks( 20,  40,  60,  80, 100); # Add breaks
2714:     $worksheet2->set_v_pagebreaks(120, 140, 160, 180, 200); # Add some more
2715: 
2716: Note: If you specify the "fit to page" option via the C<fit_to_pages()> method it will override all manual page breaks.
2717: 
2718: 
2719: 
2720: 
2721: =head1 CELL FORMATTING
2722: 
2723: 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.
2724: 
2725: 
2726: =head2 Creating and using a Format object
2727: 
2728: Cell formatting is defined through a Format object. Format objects are created by calling the workbook C<add_format()> method as follows:
2729: 
2730:     my $format1 = $workbook->add_format();       # Set properties later
2731:     my $format2 = $workbook->add_format(%props); # Set at creation
2732: 
2733: 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.
2734: 
2735: 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:
2736: 
2737:     $worksheet->write(0, 0, 'One', $format);
2738:     $worksheet->write_string(1, 0, 'Two', $format);
2739:     $worksheet->write_number(2, 0, 3, $format);
2740:     $worksheet->write_blank(3, 0, $format);
2741: 
2742: 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.
2743: 
2744:     $worksheet->set_row(0, 15, $format);
2745:     $worksheet->set_column(0, 0, 15, $format);
2746: 
2747: 
2748: 
2749: 
2750: =head2 Format methods and Format properties
2751: 
2752: The following table shows the Excel format categories, the formatting properties that can be applied and the equivalent object method:
2753: 
2754: 
2755:     Category   Description       Property        Method Name
2756:     --------   -----------       --------        -----------
2757:     Font       Font type         font            set_font()
2758:                Font size         size            set_size()
2759:                Font color        color           set_color()
2760:                Bold              bold            set_bold()
2761:                Italic            italic          set_italic()
2762:                Underline         underline       set_underline()
2763:                Strikeout         font_strikeout  set_font_strikeout()
2764:                Super/Subscript   font_script     set_font_script()
2765:                Outline           font_outline    set_font_outline()
2766:                Shadow            font_shadow     set_font_shadow()
2767: 
2768:     Number     Numeric format    num_format      set_num_format()
2769: 
2770:     Protection Lock cells        locked          set_locked()
2771:                Hide formulas     hidden          set_hidden()
2772: 
2773:     Alignment  Horizontal align  align           set_align()
2774:                Vertical align    valign          set_align()
2775:                Rotation          rotation        set_rotation()
2776:                Text wrap         text_wrap       set_text_wrap()
2777:                Justify last      text_justlast   set_text_justlast()
2778:                Center across     center_across   set_center_across()
2779:                Indentation       indent          set_indent()
2780:                Shrink to fit     shrink          set_shrink()
2781: 
2782:     Pattern    Cell pattern      pattern         set_pattern()
2783:                Background color  bg_color        set_bg_color()
2784:                Foreground color  fg_color        set_fg_color()
2785: 
2786:     Border     Cell border       border          set_border()
2787:                Bottom border     bottom          set_bottom()
2788:                Top border        top             set_top()
2789:                Left border       left            set_left()
2790:                Right border      right           set_right()
2791:                Border color      border_color    set_border_color()
2792:                Bottom color      bottom_color    set_bottom_color()
2793:                Top color         top_color       set_top_color()
2794:                Left color        left_color      set_left_color()
2795:                Right color       right_color     set_right_color()
2796: 
2797: 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:
2798: 
2799:     my $format = $workbook->add_format();
2800:     $format->set_bold();
2801:     $format->set_color('red');
2802: 
2803: By comparison the properties can be set directly by passing a hash of properties to the Format constructor:
2804: 
2805:     my $format = $workbook->add_format(bold => 1, color => 'red');
2806: 
2807: or after the Format has been constructed by means of the C<set_format_properties()> method as follows:
2808: 
2809:     my $format = $workbook->add_format();
2810:     $format->set_format_properties(bold => 1, color => 'red');
2811: 
2812: You can also store the properties in one or more named hashes and pass them to the required method:
2813: 
2814:     my %font    = (
2815:                     font  => 'Arial',
2816:                     size  => 12,
2817:                     color => 'blue',
2818:                     bold  => 1,
2819:                   );
2820: 
2821:     my %shading = (
2822:                     bg_color => 'green',
2823:                     pattern  => 1,
2824:                   );
2825: 
2826: 
2827:     my $format1 = $workbook->add_format(%font);           # Font only
2828:     my $format2 = $workbook->add_format(%font, %shading); # Font and shading
2829: 
2830: 
2831: The provision of two ways of setting properties might lead you to wonder which is the best way. The method mechanism may be better is 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.
2832: 
2833: The Perl/Tk style of adding properties is also supported:
2834: 
2835:     my %font    = (
2836:                     -font      => 'Arial',
2837:                     -size      => 12,
2838:                     -color     => 'blue',
2839:                     -bold      => 1,
2840:                   );
2841: 
2842: 
2843: 
2844: 
2845: =head2 Working with formats
2846: 
2847: The default format is Arial 10 with all other properties off.
2848: 
2849: Each unique format in Spreadsheet::WriteExcel 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:
2850: 
2851:     my $format = $workbook->add_format();
2852:     $format->set_bold();
2853:     $format->set_color('red');
2854:     $worksheet->write('A1', 'Cell A1', $format);
2855:     $format->set_color('green');
2856:     $worksheet->write('B1', 'Cell B1', $format);
2857: 
2858: 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.
2859: 
2860: In general a method call without an argument will turn a property on, for example:
2861: 
2862:     my $format1 = $workbook->add_format();
2863:     $format1->set_bold();  # Turns bold on
2864:     $format1->set_bold(1); # Also turns bold on
2865:     $format1->set_bold(0); # Turns bold off
2866: 
2867: 
2868: 
2869: 
2870: =head1 FORMAT METHODS
2871: 
2872: 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.xls> which contains examples of almost all the format types.
2873: 
2874: The following Format methods are available:
2875: 
2876:     set_font()
2877:     set_size()
2878:     set_color()
2879:     set_bold()
2880:     set_italic()
2881:     set_underline()
2882:     set_font_strikeout()
2883:     set_font_script()
2884:     set_font_outline()
2885:     set_font_shadow()
2886:     set_num_format()
2887:     set_locked()
2888:     set_hidden()
2889:     set_align()
2890:     set_rotation()
2891:     set_text_wrap()
2892:     set_text_justlast()
2893:     set_center_across()
2894:     set_indent()
2895:     set_shrink()
2896:     set_pattern()
2897:     set_bg_color()
2898:     set_fg_color()
2899:     set_border()
2900:     set_bottom()
2901:     set_top()
2902:     set_left()
2903:     set_right()
2904:     set_border_color()
2905:     set_bottom_color()
2906:     set_top_color()
2907:     set_left_color()
2908:     set_right_color()
2909: 
2910: 
2911: 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) >>.
2912: 
2913: 
2914: =head2 set_format_properties(%properties)
2915: 
2916: The properties of an existing Format object can be also be set by means of C<set_format_properties()>:
2917: 
2918:     my $format = $workbook->add_format();
2919:     $format->set_format_properties(bold => 1, color => 'red');
2920: 
2921: However, this method is here mainly for legacy reasons. It is preferable to set the properties in the format constructor:
2922: 
2923:     my $format = $workbook->add_format(bold => 1, color => 'red');
2924: 
2925: 
2926: =head2 set_font($fontname)
2927: 
2928:     Default state:      Font is Arial
2929:     Default action:     None
2930:     Valid args:         Any valid font name
2931: 
2932: Specify the font used:
2933: 
2934:     $format->set_font('Times New Roman');
2935: 
2936: 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 'Arial', 'Times New Roman' and 'Courier New'. See also the Fonts worksheet created by formats.pl
2937: 
2938: 
2939: 
2940: 
2941: =head2 set_size()
2942: 
2943:     Default state:      Font size is 10
2944:     Default action:     Set font size to 1
2945:     Valid args:         Integer values from 1 to as big as your screen.
2946: 
2947: 
2948: 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.
2949: 
2950:     my $format = $workbook->add_format();
2951:     $format->set_size(30);
2952: 
2953: 
2954: 
2955: 
2956: 
2957: =head2 set_color()
2958: 
2959:     Default state:      Excels default color, usually black
2960:     Default action:     Set the default color
2961:     Valid args:         Integers from 8..63 or the following strings:
2962:                         'black'
2963:                         'blue'
2964:                         'brown'
2965:                         'cyan'
2966:                         'gray'
2967:                         'green'
2968:                         'lime'
2969:                         'magenta'
2970:                         'navy'
2971:                         'orange'
2972:                         'pink'
2973:                         'purple'
2974:                         'red'
2975:                         'silver'
2976:                         'white'
2977:                         'yellow'
2978: 
2979: Set the font colour. The C<set_color()> method is used as follows:
2980: 
2981:     my $format = $workbook->add_format();
2982:     $format->set_color('red');
2983:     $worksheet->write(0, 0, 'wheelbarrow', $format);
2984: 
2985: 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.
2986: 
2987: For additional examples see the 'Named colors' and 'Standard colors' worksheets created by formats.pl in the examples directory.
2988: 
2989: See also L</COLOURS IN EXCEL>.
2990: 
2991: 
2992: 
2993: 
2994: =head2 set_bold()
2995: 
2996:     Default state:      bold is off
2997:     Default action:     Turn bold on
2998:     Valid args:         0, 1 [1]
2999: 
3000: Set the bold property of the font:
3001: 
3002:     $format->set_bold();  # Turn bold on
3003: 
3004: [1] Actually, values in the range 100..1000 are also valid. 400 is normal, 700 is bold and 1000 is very bold indeed. It is probably best to set the value to 1 and use normal bold.
3005: 
3006: 
3007: 
3008: 
3009: =head2 set_italic()
3010: 
3011:     Default state:      Italic is off
3012:     Default action:     Turn italic on
3013:     Valid args:         0, 1
3014: 
3015: Set the italic property of the font:
3016: 
3017:     $format->set_italic();  # Turn italic on
3018: 
3019: 
3020: 
3021: 
3022: =head2 set_underline()
3023: 
3024:     Default state:      Underline is off
3025:     Default action:     Turn on single underline
3026:     Valid args:         0  = No underline
3027:                         1  = Single underline
3028:                         2  = Double underline
3029:                         33 = Single accounting underline
3030:                         34 = Double accounting underline
3031: 
3032: Set the underline property of the font.
3033: 
3034:     $format->set_underline();   # Single underline
3035: 
3036: 
3037: 
3038: 
3039: =head2 set_font_strikeout()
3040: 
3041:     Default state:      Strikeout is off
3042:     Default action:     Turn strikeout on
3043:     Valid args:         0, 1
3044: 
3045: Set the strikeout property of the font.
3046: 
3047: 
3048: 
3049: 
3050: =head2 set_font_script()
3051: 
3052:     Default state:      Super/Subscript is off
3053:     Default action:     Turn Superscript on
3054:     Valid args:         0  = Normal
3055:                         1  = Superscript
3056:                         2  = Subscript
3057: 
3058: Set the superscript/subscript property of the font. This format is currently not very useful.
3059: 
3060: 
3061: 
3062: 
3063: =head2 set_font_outline()
3064: 
3065:     Default state:      Outline is off
3066:     Default action:     Turn outline on
3067:     Valid args:         0, 1
3068: 
3069: Macintosh only.
3070: 
3071: 
3072: 
3073: 
3074: =head2 set_font_shadow()
3075: 
3076:     Default state:      Shadow is off
3077:     Default action:     Turn shadow on
3078:     Valid args:         0, 1
3079: 
3080: Macintosh only.
3081: 
3082: 
3083: 
3084: 
3085: =head2 set_num_format()
3086: 
3087:     Default state:      General format
3088:     Default action:     Format index 1
3089:     Valid args:         See the following table
3090: 
3091: 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.
3092: 
3093: 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:
3094: 
3095:     my $format1 = $workbook->add_format();
3096:     my $format2 = $workbook->add_format();
3097:     $format1->set_num_format('d mmm yyyy'); # Format string
3098:     $format2->set_num_format(0x0f);         # Format index
3099: 
3100:     $worksheet->write(0, 0, 36892.521, $format1);      # 1 Jan 2001
3101:     $worksheet->write(0, 0, 36892.521, $format2);      # 1-Jan-01
3102: 
3103: 
3104: Using format strings you can define very sophisticated formatting of numbers.
3105: 
3106:     $format01->set_num_format('0.000');
3107:     $worksheet->write(0,  0, 3.1415926, $format01);    # 3.142
3108: 
3109:     $format02->set_num_format('#,##0');
3110:     $worksheet->write(1,  0, 1234.56,   $format02);    # 1,235
3111: 
3112:     $format03->set_num_format('#,##0.00');
3113:     $worksheet->write(2,  0, 1234.56,   $format03);    # 1,234.56
3114: 
3115:     $format04->set_num_format('$0.00');
3116:     $worksheet->write(3,  0, 49.99,     $format04);    # $49.99
3117: 
3118:     # Note you can use other currency symbols such as the pound or yen as well.
3119:     # Other currencies may require the use of Unicode.
3120: 
3121:     $format07->set_num_format('mm/dd/yy');
3122:     $worksheet->write(6,  0, 36892.521, $format07);    # 01/01/01
3123: 
3124:     $format08->set_num_format('mmm d yyyy');
3125:     $worksheet->write(7,  0, 36892.521, $format08);    # Jan 1 2001
3126: 
3127:     $format09->set_num_format('d mmmm yyyy');
3128:     $worksheet->write(8,  0, 36892.521, $format09);    # 1 January 2001
3129: 
3130:     $format10->set_num_format('dd/mm/yyyy hh:mm AM/PM');
3131:     $worksheet->write(9,  0, 36892.521, $format10);    # 01/01/2001 12:30 AM
3132: 
3133:     $format11->set_num_format('0 "dollar and" .00 "cents"');
3134:     $worksheet->write(10, 0, 1.87,      $format11);    # 1 dollar and .87 cents
3135: 
3136:     # Conditional formatting
3137:     $format12->set_num_format('[Green]General;[Red]-General;General');
3138:     $worksheet->write(11, 0, 123,       $format12);    # > 0 Green
3139:     $worksheet->write(12, 0, -45,       $format12);    # < 0 Red
3140:     $worksheet->write(13, 0, 0,         $format12);    # = 0 Default colour
3141: 
3142:     # Zip code
3143:     $format13->set_num_format('00000');
3144:     $worksheet->write(14, 0, '01209',   $format13);
3145: 
3146: 
3147: The number system used for dates is described in L</DATES AND TIME IN EXCEL>.
3148: 
3149: The colour format should have one of the following values:
3150: 
3151:     [Black] [Blue] [Cyan] [Green] [Magenta] [Red] [White] [Yellow]
3152: 
3153: 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.
3154: 
3155: For more information refer to the documentation on formatting in the C<docs> directory of the Spreadsheet::WriteExcel distro, the Excel on-line help or L<http://office.microsoft.com/en-gb/assistance/HP051995001033.aspx>.
3156: 
3157: You should ensure that the format string is valid in Excel prior to using it in WriteExcel.
3158: 
3159: Excel's built-in formats are shown in the following table:
3160: 
3161:     Index   Index   Format String
3162:     0       0x00    General
3163:     1       0x01    0
3164:     2       0x02    0.00
3165:     3       0x03    #,##0
3166:     4       0x04    #,##0.00
3167:     5       0x05    ($#,##0_);($#,##0)
3168:     6       0x06    ($#,##0_);[Red]($#,##0)
3169:     7       0x07    ($#,##0.00_);($#,##0.00)
3170:     8       0x08    ($#,##0.00_);[Red]($#,##0.00)
3171:     9       0x09    0%
3172:     10      0x0a    0.00%
3173:     11      0x0b    0.00E+00
3174:     12      0x0c    # ?/?
3175:     13      0x0d    # ??/??
3176:     14      0x0e    m/d/yy
3177:     15      0x0f    d-mmm-yy
3178:     16      0x10    d-mmm
3179:     17      0x11    mmm-yy
3180:     18      0x12    h:mm AM/PM
3181:     19      0x13    h:mm:ss AM/PM
3182:     20      0x14    h:mm
3183:     21      0x15    h:mm:ss
3184:     22      0x16    m/d/yy h:mm
3185:     ..      ....    ...........
3186:     37      0x25    (#,##0_);(#,##0)
3187:     38      0x26    (#,##0_);[Red](#,##0)
3188:     39      0x27    (#,##0.00_);(#,##0.00)
3189:     40      0x28    (#,##0.00_);[Red](#,##0.00)
3190:     41      0x29    _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)
3191:     42      0x2a    _($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)
3192:     43      0x2b    _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)
3193:     44      0x2c    _($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)
3194:     45      0x2d    mm:ss
3195:     46      0x2e    [h]:mm:ss
3196:     47      0x2f    mm:ss.0
3197:     48      0x30    ##0.0E+0
3198:     49      0x31    @
3199: 
3200: 
3201: 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.
3202: 
3203: Note 1. Numeric formats 23 to 36 are not documented by Microsoft and may differ in international versions.
3204: 
3205: Note 2. In Excel 5 the dollar sign appears as a dollar sign. In Excel 97-2000 it appears as the defined local currency symbol.
3206: 
3207: Note 3. The red negative numeric formats display slightly differently in Excel 5 and Excel 97-2000.
3208: 
3209: 
3210: 
3211: 
3212: =head2 set_locked()
3213: 
3214:     Default state:      Cell locking is on
3215:     Default action:     Turn locking on
3216:     Valid args:         0, 1
3217: 
3218: 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.
3219: 
3220:     my $locked  = $workbook->add_format();
3221:     $locked->set_locked(1); # A non-op
3222: 
3223:     my $unlocked = $workbook->add_format();
3224:     $locked->set_locked(0);
3225: 
3226:     # Enable worksheet protection
3227:     $worksheet->protect();
3228: 
3229:     # This cell cannot be edited.
3230:     $worksheet->write('A1', '=1+2', $locked);
3231: 
3232:     # This cell can be edited.
3233:     $worksheet->write('A2', '=1+2', $unlocked);
3234: 
3235: Note: This offers weak protection even with a password, see the note in relation to the C<protect()> method.
3236: 
3237: 
3238: 
3239: 
3240: =head2 set_hidden()
3241: 
3242:     Default state:      Formula hiding is off
3243:     Default action:     Turn hiding on
3244:     Valid args:         0, 1
3245: 
3246: 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.
3247: 
3248:     my $hidden = $workbook->add_format();
3249:     $hidden->set_hidden();
3250: 
3251:     # Enable worksheet protection
3252:     $worksheet->protect();
3253: 
3254:     # The formula in this cell isn't visible
3255:     $worksheet->write('A1', '=1+2', $hidden);
3256: 
3257: 
3258: Note: This offers weak protection even with a password, see the note in relation to the C<protect()> method.
3259: 
3260: 
3261: 
3262: 
3263: =head2 set_align()
3264: 
3265:     Default state:      Alignment is off
3266:     Default action:     Left alignment
3267:     Valid args:         'left'              Horizontal
3268:                         'center'
3269:                         'right'
3270:                         'fill'
3271:                         'justify'
3272:                         'center_across'
3273: 
3274:                         'top'               Vertical
3275:                         'vcenter'
3276:                         'bottom'
3277:                         'vjustify'
3278: 
3279: 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:
3280: 
3281:     my $format = $workbook->add_format();
3282:     $format->set_align('center');
3283:     $format->set_align('vcenter');
3284:     $worksheet->set_row(0, 30);
3285:     $worksheet->write(0, 0, 'X', $format);
3286: 
3287: 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.
3288: 
3289: 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.
3290: 
3291: 
3292: For further examples see the 'Alignment' worksheet created by formats.pl.
3293: 
3294: 
3295: 
3296: 
3297: =head2 set_center_across()
3298: 
3299:     Default state:      Center across selection is off
3300:     Default action:     Turn center across on
3301:     Valid args:         1
3302: 
3303: 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.
3304: 
3305: Only one cell should contain the text, the other cells should be blank:
3306: 
3307:     my $format = $workbook->add_format();
3308:     $format->set_center_across();
3309: 
3310:     $worksheet->write(1, 1, 'Center across selection', $format);
3311:     $worksheet->write_blank(1, 2, $format);
3312: 
3313: See also the C<merge1.pl> to C<merge6.pl> programs in the C<examples> directory and the C<merge_range()> method.
3314: 
3315: 
3316: 
3317: =head2 set_text_wrap()
3318: 
3319:     Default state:      Text wrap is off
3320:     Default action:     Turn text wrap on
3321:     Valid args:         0, 1
3322: 
3323: 
3324: Here is an example using the text wrap property, the escape character C<\n> is used to indicate the end of line:
3325: 
3326:     my $format = $workbook->add_format();
3327:     $format->set_text_wrap();
3328:     $worksheet->write(0, 0, "It's\na bum\nwrap", $format);
3329: 
3330: 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.
3331: 
3332: 
3333: 
3334: 
3335: =head2 set_rotation()
3336: 
3337:     Default state:      Text rotation is off
3338:     Default action:     None
3339:     Valid args:         Integers in the range -90 to 90 and 270
3340: 
3341: Set the rotation of the text in a cell. The rotation can be any angle in the range -90 to 90 degrees.
3342: 
3343:     my $format = $workbook->add_format();
3344:     $format->set_rotation(30);
3345:     $worksheet->write(0, 0, 'This text is rotated', $format);
3346: 
3347: 
3348: The angle 270 is also supported. This indicates text where the letters run from top to bottom.
3349: 
3350: 
3351: 
3352: =head2 set_indent()
3353: 
3354: 
3355:     Default state:      Text indentation is off
3356:     Default action:     Indent text 1 level
3357:     Valid args:         Positive integers
3358: 
3359: 
3360: This method can be used to indent text. The argument, which should be an integer, is taken as the level of indentation:
3361: 
3362: 
3363:     my $format = $workbook->add_format();
3364:     $format->set_indent(2);
3365:     $worksheet->write(0, 0, 'This text is indented', $format);
3366: 
3367: 
3368: Indentation is a horizontal alignment property. It will override any other horizontal properties but it can be used in conjunction with vertical properties.
3369: 
3370: 
3371: 
3372: 
3373: =head2 set_shrink()
3374: 
3375: 
3376:     Default state:      Text shrinking is off
3377:     Default action:     Turn "shrink to fit" on
3378:     Valid args:         1
3379: 
3380: 
3381: This method can be used to shrink text so that it fits in a cell.
3382: 
3383: 
3384:     my $format = $workbook->add_format();
3385:     $format->set_shrink();
3386:     $worksheet->write(0, 0, 'Honey, I shrunk the text!', $format);
3387: 
3388: 
3389: 
3390: 
3391: =head2 set_text_justlast()
3392: 
3393:     Default state:      Justify last is off
3394:     Default action:     Turn justify last on
3395:     Valid args:         0, 1
3396: 
3397: 
3398: Only applies to Far Eastern versions of Excel.
3399: 
3400: 
3401: 
3402: 
3403: =head2 set_pattern()
3404: 
3405:     Default state:      Pattern is off
3406:     Default action:     Solid fill is on
3407:     Valid args:         0 .. 18
3408: 
3409: Set the background pattern of a cell.
3410: 
3411: 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.
3412: 
3413: 
3414: 
3415: 
3416: =head2 set_bg_color()
3417: 
3418:     Default state:      Color is off
3419:     Default action:     Solid fill.
3420:     Valid args:         See set_color()
3421: 
3422: 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.
3423: 
3424: Here is an example of how to set up a solid fill in a cell:
3425: 
3426:     my $format = $workbook->add_format();
3427: 
3428:     $format->set_pattern(); # This is optional when using a solid fill
3429: 
3430:     $format->set_bg_color('green');
3431:     $worksheet->write('A1', 'Ray', $format);
3432: 
3433: For further examples see the 'Patterns' worksheet created by formats.pl.
3434: 
3435: 
3436: 
3437: 
3438: =head2 set_fg_color()
3439: 
3440:     Default state:      Color is off
3441:     Default action:     Solid fill.
3442:     Valid args:         See set_color()
3443: 
3444: 
3445: The C<set_fg_color()> method can be used to set the foreground colour of a pattern.
3446: 
3447: For further examples see the 'Patterns' worksheet created by formats.pl.
3448: 
3449: 
3450: 
3451: 
3452: =head2 set_border()
3453: 
3454:     Also applies to:    set_bottom()
3455:                         set_top()
3456:                         set_left()
3457:                         set_right()
3458: 
3459:     Default state:      Border is off
3460:     Default action:     Set border type 1
3461:     Valid args:         0-13, See below.
3462: 
3463: 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.
3464: 
3465: The following shows the border styles sorted by Spreadsheet::WriteExcel index number:
3466: 
3467:     Index   Name            Weight   Style
3468:     =====   =============   ======   ===========
3469:     0       None            0
3470:     1       Continuous      1        -----------
3471:     2       Continuous      2        -----------
3472:     3       Dash            1        - - - - - -
3473:     4       Dot             1        . . . . . .
3474:     5       Continuous      3        -----------
3475:     6       Double          3        ===========
3476:     7       Continuous      0        -----------
3477:     8       Dash            2        - - - - - -
3478:     9       Dash Dot        1        - . - . - .
3479:     10      Dash Dot        2        - . - . - .
3480:     11      Dash Dot Dot    1        - . . - . .
3481:     12      Dash Dot Dot    2        - . . - . .
3482:     13      SlantDash Dot   2        / - . / - .
3483: 
3484: 
3485: The following shows the borders sorted by style:
3486: 
3487:     Name            Weight   Style         Index
3488:     =============   ======   ===========   =====
3489:     Continuous      0        -----------   7
3490:     Continuous      1        -----------   1
3491:     Continuous      2        -----------   2
3492:     Continuous      3        -----------   5
3493:     Dash            1        - - - - - -   3
3494:     Dash            2        - - - - - -   8
3495:     Dash Dot        1        - . - . - .   9
3496:     Dash Dot        2        - . - . - .   10
3497:     Dash Dot Dot    1        - . . - . .   11
3498:     Dash Dot Dot    2        - . . - . .   12
3499:     Dot             1        . . . . . .   4
3500:     Double          3        ===========   6
3501:     None            0                      0
3502:     SlantDash Dot   2        / - . / - .   13
3503: 
3504: 
3505: The following shows the borders in the order shown in the Excel Dialog.
3506: 
3507:     Index   Style             Index   Style
3508:     =====   =====             =====   =====
3509:     0       None              12      - . . - . .
3510:     7       -----------       13      / - . / - .
3511:     4       . . . . . .       10      - . - . - .
3512:     11      - . . - . .       8       - - - - - -
3513:     9       - . - . - .       2       -----------
3514:     3       - - - - - -       5       -----------
3515:     1       -----------       6       ===========
3516: 
3517: 
3518: Examples of the available border styles are shown in the 'Borders' worksheet created by formats.pl.
3519: 
3520: 
3521: 
3522: 
3523: =head2 set_border_color()
3524: 
3525:     Also applies to:    set_bottom_color()
3526:                         set_top_color()
3527:                         set_left_color()
3528:                         set_right_color()
3529: 
3530:     Default state:      Color is off
3531:     Default action:     Undefined
3532:     Valid args:         See set_color()
3533: 
3534: 
3535: 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.
3536: 
3537: 
3538: 
3539: 
3540: 
3541: =head2 copy($format)
3542: 
3543: 
3544: This method is used to copy all of the properties from one Format object to another:
3545: 
3546:     my $lorry1 = $workbook->add_format();
3547:     $lorry1->set_bold();
3548:     $lorry1->set_italic();
3549:     $lorry1->set_color('red');    # lorry1 is bold, italic and red
3550: 
3551:     my $lorry2 = $workbook->add_format();
3552:     $lorry2->copy($lorry1);
3553:     $lorry2->set_color('yellow'); # lorry2 is bold, italic and yellow
3554: 
3555: 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.
3556: 
3557: 
3558: Note: this is not a copy constructor, both objects must exist prior to copying.
3559: 
3560: 
3561: 
3562: 
3563: =head1 UNICODE IN EXCEL
3564: 
3565: The following is a brief introduction to handling Unicode in C<Spreadsheet::WriteExcel>.
3566: 
3567: I<For a more general introduction to Unicode handling in Perl see> L<perlunitut> and L<perluniintro>.
3568: 
3569: When using Spreadsheet::WriteExcel the best and easiest way to write unicode strings to an Excel file is to use C<UTF-8> encoded strings and perl 5.8 (or later). Spreadsheet::WriteExcel also allows you to write unicode strings using older perls but it generally requires more work, as explained below.
3570: 
3571: Internally, Excel encodes unicode data as C<UTF-16LE> (where LE means little-endian). If you are using perl 5.8+ then Spreadsheet::WriteExcel will convert C<UTF-8> strings to C<UTF-16LE> when required. No further intervention is required from the programmer, for example:
3572: 
3573:     # perl 5.8+ example:
3574:     my $smiley = "\x{263A}";
3575: 
3576:     $worksheet->write('A1', 'Hello world'); # ASCII
3577:     $worksheet->write('A2', $smiley);       # UTF-8
3578: 
3579: Spreadsheet::WriteExcel also lets you write unicode data as C<UTF-16>. Since the majority of CPAN modules default to C<UTF-16BE> (big-endian) Spreadsheet::WriteExcel also uses C<UTF-16BE> and converts it internally to C<UTF-16LE>:
3580: 
3581:     # perl 5.005 example:
3582:     my $smiley = pack 'n', 0x263A;
3583: 
3584:     $worksheet->write               ('A3', 'Hello world'); # ASCII
3585:     $worksheet->write_utf16be_string('A4', $smiley);       # UTF-16
3586: 
3587: Although the above examples look similar there is an important difference. With C<uft8> and perl 5.8+ Spreadsheet::WriteExcel treats C<UTF-8> strings in exactly the same way as any other string. However, with C<UTF16> data we need to distinguish it from other strings either by calling a separate function or by passing an additional flag to indicate the data type.
3588: 
3589: If you are dealing with non-ASCII characters that aren't in C<UTF-8> then perl 5.8+ provides useful tools in the guise of the C<Encode> module to help you to convert to the required format. For example:
3590: 
3591:     use Encode 'decode';
3592: 
3593:     my $string = 'some string with koi8-r characters';
3594:        $string = decode('koi8-r', $string); # koi8-r to utf8
3595: 
3596: Alternatively you can read data from an encoded file and convert it to C<UTF-8> as you read it in:
3597: 
3598: 
3599:     my $file = 'unicode_koi8r.txt';
3600:     open FH, '<:encoding(koi8-r)', $file  or die "Couldn't open $file: $!\n";
3601: 
3602:     my $row = 0;
3603:     while (<FH>) {
3604:         # Data read in is now in utf8 format.
3605:         chomp;
3606:         $worksheet->write($row++, 0,  $_);
3607:     }
3608: 
3609: These methodologies are explained in more detail in L<perlunitut>, L<perluniintro> and L<perlunicode>.
3610: 
3611: See also the C<unicode_*.pl> programs in the examples directory of the distro.
3612: 
3613: 
3614: 
3615: 
3616: =head1 COLOURS IN EXCEL
3617: 
3618: Excel provides a colour palette of 56 colours. In Spreadsheet::WriteExcel these colours are accessed via their palette index in the range 8..63. This index is used to set the colour of fonts, cell patterns and cell borders. For example:
3619: 
3620:     my $format = $workbook->add_format(
3621:                                         color => 12, # index for blue
3622:                                         font  => 'Arial',
3623:                                         size  => 12,
3624:                                         bold  => 1,
3625:                                      );
3626: 
3627: The most commonly used colours can also be accessed by name. The name acts as a simple alias for the colour index:
3628: 
3629:     black     =>    8
3630:     blue      =>   12
3631:     brown     =>   16
3632:     cyan      =>   15
3633:     gray      =>   23
3634:     green     =>   17
3635:     lime      =>   11
3636:     magenta   =>   14
3637:     navy      =>   18
3638:     orange    =>   53
3639:     pink      =>   33
3640:     purple    =>   20
3641:     red       =>   10
3642:     silver    =>   22
3643:     white     =>    9
3644:     yellow    =>   13
3645: 
3646: For example:
3647: 
3648:     my $font = $workbook->add_format(color => 'red');
3649: 
3650: Users of VBA in Excel should note that the equivalent colour indices are in the range 1..56 instead of 8..63.
3651: 
3652: If the default palette does not provide a required colour you can override one of the built-in values. This is achieved by using the C<set_custom_color()> workbook method to adjust the RGB (red green blue) components of the colour:
3653: 
3654:     my $ferrari = $workbook->set_custom_color(40, 216, 12, 12);
3655: 
3656:     my $format  = $workbook->add_format(
3657:                                         bg_color => $ferrari,
3658:                                         pattern  => 1,
3659:                                         border   => 1
3660:                                       );
3661: 
3662:     $worksheet->write_blank('A1', $format);
3663: 
3664: The default Excel colour palette is shown in C<palette.html> in the C<docs> directory  of the distro. You can generate an Excel version of the palette using C<colors.pl> in the C<examples> directory.
3665: 
3666: 
3667: 
3668: 
3669: =head1 DATES AND TIME IN EXCEL
3670: 
3671: There are two important things to understand about dates and times in Excel:
3672: 
3673: =over 4
3674: 
3675: =item 1 A date/time in Excel is a real number plus an Excel number format.
3676: 
3677: =item 2 Spreadsheet::WriteExcel doesn't automatically convert date/time strings in C<write()> to an Excel date/time.
3678: 
3679: =back
3680: 
3681: These two points are explained in more detail below along with some suggestions on how to convert times and dates to the required format.
3682: 
3683: 
3684: =head2 An Excel date/time is a number plus a format
3685: 
3686: If you write a date string with C<write()> then all you will get is a string:
3687: 
3688:     $worksheet->write('A1', '02/03/04'); # !! Writes a string not a date. !!
3689: 
3690: 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.
3691: 
3692: The integer part of the number stores the number of days since the epoch and the fractional part stores the percentage of the day.
3693: 
3694: 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.
3695: 
3696:     #!/usr/bin/perl -w
3697: 
3698:     use strict;
3699:     use Spreadsheet::WriteExcel;
3700: 
3701:     my $workbook  = Spreadsheet::WriteExcel->new('date_examples.xls');
3702:     my $worksheet = $workbook->add_worksheet();
3703: 
3704:     $worksheet->set_column('A:A', 30); # For extra visibility.
3705: 
3706:     my $number    = 39506.5;
3707: 
3708:     $worksheet->write('A1', $number);            #     39506.5
3709: 
3710:     my $format2 = $workbook->add_format(num_format => 'dd/mm/yy');
3711:     $worksheet->write('A2', $number , $format2); #     28/02/08
3712: 
3713:     my $format3 = $workbook->add_format(num_format => 'mm/dd/yy');
3714:     $worksheet->write('A3', $number , $format3); #     02/28/08
3715: 
3716:     my $format4 = $workbook->add_format(num_format => 'd-m-yyyy');
3717:     $worksheet->write('A4', $number , $format4); #     28-2-2008
3718: 
3719:     my $format5 = $workbook->add_format(num_format => 'dd/mm/yy hh:mm');
3720:     $worksheet->write('A5', $number , $format5); #     28/02/08 12:00
3721: 
3722:     my $format6 = $workbook->add_format(num_format => 'd mmm yyyy');
3723:     $worksheet->write('A6', $number , $format6); #     28 Feb 2008
3724: 
3725:     my $format7 = $workbook->add_format(num_format => 'mmm d yyyy hh:mm AM/PM');
3726:     $worksheet->write('A7', $number , $format7); #     Feb 28 2008 12:00 PM
3727: 
3728: 
3729: =head2 Spreadsheet::WriteExcel doesn't automatically convert date/time strings
3730: 
3731: Spreadsheet::WriteExcel 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.
3732: 
3733: For example, does C<02/03/04> mean March 2 2004, February 3 2004 or even March 4 2002.
3734: 
3735: 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.
3736: 
3737: 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:
3738: 
3739:     $worksheet->write_date_time('A2', '2001-01-01T12:20', $format);
3740: 
3741: See the C<write_date_time()> section of the documentation for more details.
3742: 
3743: A general methodology for handling date strings with C<write_date_time()> is:
3744: 
3745:     1. Identify incoming date/time strings with a regex.
3746:     2. Extract the component parts of the date/time using the same regex.
3747:     3. Convert the date/time to the ISO8601 format.
3748:     4. Write the date/time using write_date_time() and a number format.
3749: 
3750: Here is an example:
3751: 
3752:     #!/usr/bin/perl -w
3753: 
3754:     use strict;
3755:     use Spreadsheet::WriteExcel;
3756: 
3757:     my $workbook    = Spreadsheet::WriteExcel->new('example.xls');
3758:     my $worksheet   = $workbook->add_worksheet();
3759: 
3760:     # Set the default format for dates.
3761:     my $date_format = $workbook->add_format(num_format => 'mmm d yyyy');
3762: 
3763:     # Increase column width to improve visibility of data.
3764:     $worksheet->set_column('A:C', 20);
3765: 
3766:     # Simulate reading from a data source.
3767:     my $row = 0;
3768: 
3769:     while (<DATA>) {
3770:         chomp;
3771: 
3772:         my $col  = 0;
3773:         my @data = split ' ';
3774: 
3775:         for my $item (@data) {
3776: 
3777:             # Match dates in the following formats: d/m/yy, d/m/yyyy
3778:             if ($item =~ qr[^(\d{1,2})/(\d{1,2})/(\d{4})$]) {
3779: 
3780:                 # Change to the date format required by write_date_time().
3781:                 my $date = sprintf "%4d-%02d-%02dT", $3, $2, $1;
3782: 
3783:                 $worksheet->write_date_time($row, $col++, $date, $date_format);
3784:             }
3785:             else {
3786:                 # Just plain data
3787:                 $worksheet->write($row, $col++, $item);
3788:             }
3789:         }
3790:         $row++;
3791:     }
3792: 
3793:     __DATA__
3794:     Item    Cost    Date
3795:     Book    10      1/9/2007
3796:     Beer    4       12/9/2007
3797:     Bed     500     5/10/2007
3798: 
3799: 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.
3800: 
3801: 
3802: =head2 Converting dates and times to an Excel date or time
3803: 
3804: The C<write_date_time()> method above is just one way of handling dates and times.
3805: 
3806: The L<Spreadsheet::WriteExcel::Utility> module which is included in the distro has date/time handling functions:
3807: 
3808:     use Spreadsheet::WriteExcel::Utility;
3809: 
3810:     $date           = xl_date_list(2002, 1, 1);         # 37257
3811:     $date           = xl_parse_date("11 July 1997");    # 35622
3812:     $time           = xl_parse_time('3:21:36 PM');      # 0.64
3813:     $date           = xl_decode_date_EU("13 May 2002"); # 37389
3814: 
3815: Note: some of these functions require additional CPAN modules.
3816: 
3817: For date conversions using the CPAN C<DateTime> framework see L<DateTime::Format::Excel> L<http://search.cpan.org/search?dist=DateTime-Format-Excel>.
3818: 
3819: 
3820: 
3821: 
3822: =head1 OUTLINES AND GROUPING IN EXCEL
3823: 
3824: 
3825: 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.
3826: 
3827: Outlines can reduce complex data down to a few salient sub-totals or summaries.
3828: 
3829: 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.
3830: 
3831: 
3832:             ------------------------------------------
3833:      1 2 3 |   |   A   |   B   |   C   |   D   |  ...
3834:             ------------------------------------------
3835:       _    | 1 |   A   |       |       |       |  ...
3836:      |  _  | 2 |   B   |       |       |       |  ...
3837:      | |   | 3 |  (C)  |       |       |       |  ...
3838:      | |   | 4 |  (D)  |       |       |       |  ...
3839:      | -   | 5 |   E   |       |       |       |  ...
3840:      |  _  | 6 |   F   |       |       |       |  ...
3841:      | |   | 7 |  (G)  |       |       |       |  ...
3842:      | |   | 8 |  (H)  |       |       |       |  ...
3843:      | -   | 9 |   I   |       |       |       |  ...
3844:      -     | . |  ...  |  ...  |  ...  |  ...  |  ...
3845: 
3846: 
3847: 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.
3848: 
3849:             ------------------------------------------
3850:      1 2 3 |   |   A   |   B   |   C   |   D   |  ...
3851:             ------------------------------------------
3852:       _    | 1 |   A   |       |       |       |  ...
3853:      |     | 2 |   B   |       |       |       |  ...
3854:      | +   | 5 |   E   |       |       |       |  ...
3855:      |     | 6 |   F   |       |       |       |  ...
3856:      | +   | 9 |   I   |       |       |       |  ...
3857:      -     | . |  ...  |  ...  |  ...  |  ...  |  ...
3858: 
3859: 
3860: Clicking on the minus sign on the level 1 outline will collapse the remaining rows as follows:
3861: 
3862:             ------------------------------------------
3863:      1 2 3 |   |   A   |   B   |   C   |   D   |  ...
3864:             ------------------------------------------
3865:            | 1 |   A   |       |       |       |  ...
3866:      +     | . |  ...  |  ...  |  ...  |  ...  |  ...
3867: 
3868: 
3869: Grouping in C<Spreadsheet::WriteExcel> is achieved by setting the outline level via the C<set_row()> and C<set_column()> worksheet methods:
3870: 
3871:     set_row($row, $height, $format, $hidden, $level, $collapsed)
3872:     set_column($first_col, $last_col, $width, $format, $hidden, $level, $collapsed)
3873: 
3874: The following example sets an outline level of 1 for rows 1 and 2 (zero-indexed) and columns B to G. The parameters C<$height> and C<$XF> are assigned default values since they are undefined:
3875: 
3876:     $worksheet->set_row(1, undef, undef, 0, 1);
3877:     $worksheet->set_row(2, undef, undef, 0, 1);
3878:     $worksheet->set_column('B:G', undef, undef, 0, 1);
3879: 
3880: 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>.
3881: 
3882: 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:
3883: 
3884:     $worksheet->set_row(1, undef, undef, 1, 1);
3885:     $worksheet->set_row(2, undef, undef, 1, 1);
3886:     $worksheet->set_row(3, undef, undef, 0, 0, 1);        # Collapsed flag.
3887: 
3888:     $worksheet->set_column('B:G', undef, undef, 1, 1);
3889:     $worksheet->set_column('H:H', undef, undef, 0, 0, 1); # Collapsed flag.
3890: 
3891: Note: Setting the C<$collapsed> flag is particularly important for compatibility with OpenOffice.org and Gnumeric.
3892: 
3893: For a more complete example see the C<outline.pl> and C<outline_collapsed.pl> programs in the examples directory of the distro.
3894: 
3895: Some additional outline properties can be set via the C<outline_settings()> worksheet method, see above.
3896: 
3897: 
3898: 
3899: 
3900: =head1 DATA VALIDATION IN EXCEL
3901: 
3902: 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.
3903: 
3904: 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 Spreadsheet::WriteExcel we could do that as follows:
3905: 
3906:     $worksheet->data_validation('B3',
3907:         {
3908:             validate        => 'integer',
3909:             criteria        => 'between',
3910:             minimum         => 1,
3911:             maximum         => 100,
3912:             input_title     => 'Input an integer:',
3913:             input_message   => 'Between 1 and 100',
3914:             error_message   => 'Sorry, try again.',
3915:         });
3916: 
3917: The above example would look like this in Excel: L<http://homepage.eircom.net/~jmcnamara/perl/data_validation.jpg>.
3918: 
3919: =begin html
3920: 
3921: <center>
3922: <img src="http://homepage.eircom.net/~jmcnamara/perl/data_validation.jpg" alt="The output from the above example"/>
3923: </center>
3924: 
3925: =end html
3926: 
3927: 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>.
3928: 
3929: The following sections describe how to use the C<data_validation()> method and its various options.
3930: 
3931: 
3932: =head2 data_validation($row, $col, { parameter => 'value', ... })
3933: 
3934: The C<data_validation()> method is used to construct an Excel data validation.
3935: 
3936: 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:
3937: 
3938:     $worksheet->data_validation(0, 0,       {...});
3939:     $worksheet->data_validation(0, 0, 4, 1, {...});
3940: 
3941:     # Which are the same as:
3942: 
3943:     $worksheet->data_validation('A1',       {...});
3944:     $worksheet->data_validation('A1:B5',    {...});
3945: 
3946: See also the note about L</Cell notation> for more information.
3947: 
3948: 
3949: 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:
3950: 
3951:     validate
3952:     criteria
3953:     value | minimum | source
3954:     maximum
3955:     ignore_blank
3956:     dropdown
3957: 
3958:     input_title
3959:     input_message
3960:     show_input
3961: 
3962:     error_title
3963:     error_message
3964:     error_type
3965:     show_error
3966: 
3967: 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>.
3968: 
3969:     $worksheet->data_validation('B3',
3970:         {
3971:             validate => 'integer',
3972:             criteria => '>',
3973:             value    => 100,
3974:         });
3975: 
3976: The C<data_validation> method returns:
3977: 
3978:      0 for success.
3979:     -1 for insufficient number of arguments.
3980:     -2 for row or column out of bounds.
3981:     -3 for incorrect parameter or value.
3982: 
3983: 
3984: =head2 validate
3985: 
3986: This parameter is passed in a hash ref to C<data_validation()>.
3987: 
3988: 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:
3989: 
3990:     any
3991:     integer
3992:     decimal
3993:     list
3994:     date
3995:     time
3996:     length
3997:     custom
3998: 
3999: =over
4000: 
4001: =item * B<any> is used to specify that the type of data is unrestricted. This is the same as not applying a data validation. It is only provided for completeness and isn't used very often in the context of Spreadsheet::WriteExcel.
4002: 
4003: =item * B<integer> restricts the cell to integer values. Excel refers to this as 'whole number'.
4004: 
4005:     validate => 'integer',
4006:     criteria => '>',
4007:     value    => 100,
4008: 
4009: =item * B<decimal> restricts the cell to decimal values.
4010: 
4011:     validate => 'decimal',
4012:     criteria => '>',
4013:     value    => 38.6,
4014: 
4015: =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):
4016: 
4017:     validate => 'list',
4018:     value    => ['open', 'high', 'close'],
4019:     # Or like this:
4020:     value    => 'B1:B3',
4021: 
4022: Excel requires that range references are only to cells on the same worksheet.
4023: 
4024: =item * B<date> restricts the cell to date values. Dates in Excel are expressed as integer values but you can also pass an ISO860 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.
4025: 
4026:     validate => 'date',
4027:     criteria => '>',
4028:     value    => 39653, # 24 July 2008
4029:     # Or like this:
4030:     value    => '2008-07-24T',
4031: 
4032: =item * B<time> restricts the cell to time values. Times in Excel are expressed as decimal values but you can also pass an ISO860 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.
4033: 
4034:     validate => 'time',
4035:     criteria => '>',
4036:     value    => 0.5, # Noon
4037:     # Or like this:
4038:     value    => 'T12:00:00',
4039: 
4040: =item * B<length> restricts the cell data based on an integer string length. Excel refers to this as 'Text length'.
4041: 
4042:     validate => 'length',
4043:     criteria => '>',
4044:     value    => 10,
4045: 
4046: =item * B<custom> restricts the cell based on an external Excel formula that returns a C<TRUE/FALSE> value.
4047: 
4048:     validate => 'custom',
4049:     value    => '=IF(A10>B10,TRUE,FALSE)',
4050: 
4051: =back
4052: 
4053: 
4054: =head2 criteria
4055: 
4056: This parameter is passed in a hash ref to C<data_validation()>.
4057: 
4058: 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:
4059: 
4060:     'between'
4061:     'not between'
4062:     'equal to'                  |  '=='  |  '='
4063:     'not equal to'              |  '!='  |  '<>'
4064:     'greater than'              |  '>'
4065:     'less than'                 |  '<'
4066:     'greater than or equal to'  |  '>='
4067:     'less than or equal to'     |  '<='
4068: 
4069: You can either use Excel's textual description strings, in the first column above, or the more common operator alternatives. The following are equivalent:
4070: 
4071:     validate => 'integer',
4072:     criteria => 'greater than',
4073:     value    => 100,
4074: 
4075:     validate => 'integer',
4076:     criteria => '>',
4077:     value    => 100,
4078: 
4079: The C<list> and C<custom> validate options don't require a C<criteria>. If you specify one it will be ignored.
4080: 
4081:     validate => 'list',
4082:     value    => ['open', 'high', 'close'],
4083: 
4084:     validate => 'custom',
4085:     value    => '=IF(A10>B10,TRUE,FALSE)',
4086: 
4087: 
4088: =head2 value | minimum | source
4089: 
4090: This parameter is passed in a hash ref to C<data_validation()>.
4091: 
4092: 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:
4093: 
4094:     # Use 'value'
4095:     validate => 'integer',
4096:     criteria => '>',
4097:     value    => 100,
4098: 
4099:     # Use 'minimum'
4100:     validate => 'integer',
4101:     criteria => 'between',
4102:     minimum  => 1,
4103:     maximum  => 100,
4104: 
4105:     # Use 'source'
4106:     validate => 'list',
4107:     source   => '$B$1:$B$3',
4108: 
4109: 
4110: =head2 maximum
4111: 
4112: This parameter is passed in a hash ref to C<data_validation()>.
4113: 
4114: The C<maximum> parameter is used to set the upper limiting value when the C<criteria> is either C<'between'> or C<'not between'>:
4115: 
4116:     validate => 'integer',
4117:     criteria => 'between',
4118:     minimum  => 1,
4119:     maximum  => 100,
4120: 
4121: 
4122: =head2 ignore_blank
4123: 
4124: This parameter is passed in a hash ref to C<data_validation()>.
4125: 
4126: 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.
4127: 
4128:     ignore_blank => 0,  # Turn the option off
4129: 
4130: 
4131: =head2 dropdown
4132: 
4133: This parameter is passed in a hash ref to C<data_validation()>.
4134: 
4135: 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.
4136: 
4137:     dropdown => 0,      # Turn the option off
4138: 
4139: 
4140: =head2 input_title
4141: 
4142: This parameter is passed in a hash ref to C<data_validation()>.
4143: 
4144: 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.
4145: 
4146:     input_title   => 'This is the input title',
4147: 
4148: The maximum title length is 32 characters. UTF8 strings are handled automatically in perl 5.8 and later.
4149: 
4150: 
4151: =head2 input_message
4152: 
4153: This parameter is passed in a hash ref to C<data_validation()>.
4154: 
4155: 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.
4156: 
4157:     validate      => 'integer',
4158:     criteria      => 'between',
4159:     minimum       => 1,
4160:     maximum       => 100,
4161:     input_title   => 'Enter the applied discount:',
4162:     input_message => 'between 1 and 100',
4163: 
4164: The message can be split over several lines using newlines, C<"\n"> in double quoted strings.
4165: 
4166:     input_message => "This is\na test.",
4167: 
4168: The maximum message length is 255 characters. UTF8 strings are handled automatically in perl 5.8 and later.
4169: 
4170: 
4171: =head2 show_input
4172: 
4173: This parameter is passed in a hash ref to C<data_validation()>.
4174: 
4175: 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.
4176: 
4177:     show_input => 0,      # Turn the option off
4178: 
4179: 
4180: =head2 error_title
4181: 
4182: This parameter is passed in a hash ref to C<data_validation()>.
4183: 
4184: 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'.
4185: 
4186:     error_title   => 'Input value is not valid',
4187: 
4188: The maximum title length is 32 characters. UTF8 strings are handled automatically in perl 5.8 and later.
4189: 
4190: 
4191: =head2 error_message
4192: 
4193: This parameter is passed in a hash ref to C<data_validation()>.
4194: 
4195: 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.".
4196: 
4197:     validate      => 'integer',
4198:     criteria      => 'between',
4199:     minimum       => 1,
4200:     maximum       => 100,
4201:     error_title   => 'Input value is not valid',
4202:     error_message => 'It should be an integer between 1 and 100',
4203: 
4204: The message can be split over several lines using newlines, C<"\n"> in double quoted strings.
4205: 
4206:     input_message => "This is\na test.",
4207: 
4208: The maximum message length is 255 characters. UTF8 strings are handled automatically in perl 5.8 and later.
4209: 
4210: 
4211: =head2 error_type
4212: 
4213: This parameter is passed in a hash ref to C<data_validation()>.
4214: 
4215: The C<error_type> parameter is used to specify the type of error dialog that is displayed. There are 3 options:
4216: 
4217:     'stop'
4218:     'warning'
4219:     'information'
4220: 
4221: The default is C<'stop'>.
4222: 
4223: 
4224: =head2 show_error
4225: 
4226: This parameter is passed in a hash ref to C<data_validation()>.
4227: 
4228: 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.
4229: 
4230:     show_error => 0,      # Turn the option off
4231: 
4232: =head2 Data Validation Examples
4233: 
4234: Example 1. Limiting input to an integer greater than a fixed value.
4235: 
4236:     $worksheet->data_validation('A1',
4237:         {
4238:             validate        => 'integer',
4239:             criteria        => '>',
4240:             value           => 0,
4241:         });
4242: 
4243: Example 2. Limiting input to an integer greater than a fixed value where the value is referenced from a cell.
4244: 
4245:     $worksheet->data_validation('A2',
4246:         {
4247:             validate        => 'integer',
4248:             criteria        => '>',
4249:             value           => '=E3',
4250:         });
4251: 
4252: Example 3. Limiting input to a decimal in a fixed range.
4253: 
4254:     $worksheet->data_validation('A3',
4255:         {
4256:             validate        => 'decimal',
4257:             criteria        => 'between',
4258:             minimum         => 0.1,
4259:             maximum         => 0.5,
4260:         });
4261: 
4262: Example 4. Limiting input to a value in a dropdown list.
4263: 
4264:     $worksheet->data_validation('A4',
4265:         {
4266:             validate        => 'list',
4267:             source          => ['open', 'high', 'close'],
4268:         });
4269: 
4270: Example 5. Limiting input to a value in a dropdown list where the list is specified as a cell range.
4271: 
4272:     $worksheet->data_validation('A5',
4273:         {
4274:             validate        => 'list',
4275:             source          => '=E4:G4',
4276:         });
4277: 
4278: Example 6. Limiting input to a date in a fixed range.
4279: 
4280:     $worksheet->data_validation('A6',
4281:         {
4282:             validate        => 'date',
4283:             criteria        => 'between',
4284:             minimum         => '2008-01-01T',
4285:             maximum         => '2008-12-12T',
4286:         });
4287: 
4288: Example 7. Displaying a message when the cell is selected.
4289: 
4290:     $worksheet->data_validation('A7',
4291:         {
4292:             validate      => 'integer',
4293:             criteria      => 'between',
4294:             minimum       => 1,
4295:             maximum       => 100,
4296:             input_title   => 'Enter an integer:',
4297:             input_message => 'between 1 and 100',
4298:         });
4299: 
4300: See also the C<data_validate.pl> program in the examples directory of the distro.
4301: 
4302: 
4303: 
4304: =head1 ROW HEIGHTS AND WORKSHEET OBJECTS
4305: 
4306: The following relates to worksheet objects such as images, comments and charts.
4307: 
4308: If you specify the height of a row that contains a worksheet object then Spreadsheet::WriteExcel will adjust the height of the object to maintain its default or user specified dimensions. In this way the object won't appear stretched or compressed in Excel.
4309: 
4310: However, Excel can also adjust the height of a row automatically if it contains cells that have the text wrap property set or contain large fonts. In these cases the height of the row is unknown to Spreadsheet::WriteExcel at execution time and the scaling calculations it performs are incorrect. The effect of this is that the  object is stretched with the row when it is displayed in Excel.
4311: 
4312: In order to avoid this issue you should use the C<set_row()> method to explicitly specify the height of any row that may otherwise be changed automatically by Excel.
4313: 
4314: 
4315: 
4316: 
4317: =head1 FORMULAS AND FUNCTIONS IN EXCEL
4318: 
4319: 
4320: =head2 Caveats
4321: 
4322: The first thing to note is that there are still some outstanding issues with the implementation of formulas and functions:
4323: 
4324:     1. Writing a formula is much slower than writing the equivalent string.
4325:     2. You cannot use array constants, i.e. {1;2;3}, in functions.
4326:     3. Unary minus isn't supported.
4327:     4. Whitespace is not preserved around operators.
4328:     5. Named ranges are not supported.
4329:     6. Array formulas are not supported.
4330: 
4331: However, these constraints will be removed in future versions. They are here because of a trade-off between features and time. Also, it is possible to work around issue 1 using the C<store_formula()> and C<repeat_formula()> methods as described later in this section.
4332: 
4333: 
4334: 
4335: =head2 Introduction
4336: 
4337: The following is a brief introduction to formulas and functions in Excel and Spreadsheet::WriteExcel.
4338: 
4339: A formula is a string that begins with an equals sign:
4340: 
4341:     '=A1+B1'
4342:     '=AVERAGE(1, 2, 3)'
4343: 
4344: 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.
4345: 
4346: 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 IV i.e. 0 to 255, rows range from 1 to 65536. The C<Spreadsheet::WriteExcel::Utility> module that is included in the distro contains helper functions for dealing with A1 notation, for example:
4347: 
4348:     use Spreadsheet::WriteExcel::Utility;
4349: 
4350:     ($row, $col) = xl_cell_to_rowcol('C2');  # (1, 2)
4351:     $str         = xl_rowcol_to_cell(1, 2);  # C2
4352: 
4353: 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.
4354: 
4355:     '=A1'   # Column and row are relative
4356:     '=$A1'  # Column is absolute and row is relative
4357:     '=A$1'  # Column is relative and row is absolute
4358:     '=$A$1' # Column and row are absolute
4359: 
4360: Formulas can also refer to cells in other worksheets of the current workbook. For example:
4361: 
4362:     '=Sheet2!A1'
4363:     '=Sheet2!A1:A5'
4364:     '=Sheet2:Sheet3!A1'
4365:     '=Sheet2:Sheet3!A1:A5'
4366:     q{='Test Data'!A1}
4367:     q{='Test Data1:Test Data2'!A1}
4368: 
4369: 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.
4370: 
4371: 
4372: 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:
4373: 
4374:     Arithmetic operators:
4375:     =====================
4376:     Operator  Meaning                   Example
4377:        +      Addition                  1+2
4378:        -      Subtraction               2-1
4379:        *      Multiplication            2*3
4380:        /      Division                  1/4
4381:        ^      Exponentiation            2^3      # Equivalent to **
4382:        -      Unary minus               -(1+2)   # Not yet supported
4383:        %      Percent (Not modulus)     13%      # Not supported, [1]
4384: 
4385: 
4386:     Comparison operators:
4387:     =====================
4388:     Operator  Meaning                   Example
4389:         =     Equal to                  A1 =  B1 # Equivalent to ==
4390:         <>    Not equal to              A1 <> B1 # Equivalent to !=
4391:         >     Greater than              A1 >  B1
4392:         <     Less than                 A1 <  B1
4393:         >=    Greater than or equal to  A1 >= B1
4394:         <=    Less than or equal to     A1 <= B1
4395: 
4396: 
4397:     String operator:
4398:     ================
4399:     Operator  Meaning                   Example
4400:         &     Concatenation             "Hello " & "World!" # [2]
4401: 
4402: 
4403:     Reference operators:
4404:     ====================
4405:     Operator  Meaning                   Example
4406:         :     Range operator            A1:A4               # [3]
4407:         ,     Union operator            SUM(1, 2+2, B3)     # [4]
4408: 
4409: 
4410:     Notes:
4411:     [1]: You can get a percentage with formatting and modulus with MOD().
4412:     [2]: Equivalent to ("Hello " . "World!") in Perl.
4413:     [3]: This range is equivalent to cells A1, A2, A3 and A4.
4414:     [4]: The comma behaves like the list separator in Perl.
4415: 
4416: The range and comma operators can have different symbols in non-English versions of Excel. These will be supported in a later version of Spreadsheet::WriteExcel. European users of Excel take note:
4417: 
4418:     $worksheet->write('A1', '=SUM(1; 2; 3)'); # Wrong!!
4419:     $worksheet->write('A1', '=SUM(1, 2, 3)'); # Okay
4420: 
4421: The following table lists all of the core functions supported by Excel 5 and Spreadsheet::WriteExcel. Any additional functions that are available through the "Analysis ToolPak" or other add-ins are not supported. These functions have all been tested to verify that they work.
4422: 
4423:     ABS           DB            INDIRECT      NORMINV       SLN
4424:     ACOS          DCOUNT        INFO          NORMSDIST     SLOPE
4425:     ACOSH         DCOUNTA       INT           NORMSINV      SMALL
4426:     ADDRESS       DDB           INTERCEPT     NOT           SQRT
4427:     AND           DEGREES       IPMT          NOW           STANDARDIZE
4428:     AREAS         DEVSQ         IRR           NPER          STDEV
4429:     ASIN          DGET          ISBLANK       NPV           STDEVP
4430:     ASINH         DMAX          ISERR         ODD           STEYX
4431:     ATAN          DMIN          ISERROR       OFFSET        SUBSTITUTE
4432:     ATAN2         DOLLAR        ISLOGICAL     OR            SUBTOTAL
4433:     ATANH         DPRODUCT      ISNA          PEARSON       SUM
4434:     AVEDEV        DSTDEV        ISNONTEXT     PERCENTILE    SUMIF
4435:     AVERAGE       DSTDEVP       ISNUMBER      PERCENTRANK   SUMPRODUCT
4436:     BETADIST      DSUM          ISREF         PERMUT        SUMSQ
4437:     BETAINV       DVAR          ISTEXT        PI            SUMX2MY2
4438:     BINOMDIST     DVARP         KURT          PMT           SUMX2PY2
4439:     CALL          ERROR.TYPE    LARGE         POISSON       SUMXMY2
4440:     CEILING       EVEN          LEFT          POWER         SYD
4441:     CELL          EXACT         LEN           PPMT          T
4442:     CHAR          EXP           LINEST        PROB          TAN
4443:     CHIDIST       EXPONDIST     LN            PRODUCT       TANH
4444:     CHIINV        FACT          LOG           PROPER        TDIST
4445:     CHITEST       FALSE         LOG10         PV            TEXT
4446:     CHOOSE        FDIST         LOGEST        QUARTILE      TIME
4447:     CLEAN         FIND          LOGINV        RADIANS       TIMEVALUE
4448:     CODE          FINV          LOGNORMDIST   RAND          TINV
4449:     COLUMN        FISHER        LOOKUP        RANK          TODAY
4450:     COLUMNS       FISHERINV     LOWER         RATE          TRANSPOSE
4451:     COMBIN        FIXED         MATCH         REGISTER.ID   TREND
4452:     CONCATENATE   FLOOR         MAX           REPLACE       TRIM
4453:     CONFIDENCE    FORECAST      MDETERM       REPT          TRIMMEAN
4454:     CORREL        FREQUENCY     MEDIAN        RIGHT         TRUE
4455:     COS           FTEST         MID           ROMAN         TRUNC
4456:     COSH          FV            MIN           ROUND         TTEST
4457:     COUNT         GAMMADIST     MINUTE        ROUNDDOWN     TYPE
4458:     COUNTA        GAMMAINV      MINVERSE      ROUNDUP       UPPER
4459:     COUNTBLANK    GAMMALN       MIRR          ROW           VALUE
4460:     COUNTIF       GEOMEAN       MMULT         ROWS          VAR
4461:     COVAR         GROWTH        MOD           RSQ           VARP
4462:     CRITBINOM     HARMEAN       MODE          SEARCH        VDB
4463:     DATE          HLOOKUP       MONTH         SECOND        VLOOKUP
4464:     DATEVALUE     HOUR          N             SIGN          WEEKDAY
4465:     DAVERAGE      HYPGEOMDIST   NA            SIN           WEIBULL
4466:     DAY           IF            NEGBINOMDIST  SINH          YEAR
4467:     DAYS360       INDEX         NORMDIST      SKEW          ZTEST
4468: 
4469: You can also modify the module to support function names in the following languages: German, French, Spanish, Portuguese, Dutch, Finnish, Italian and Swedish. See the C<function_locale.pl> program in the C<examples> directory of the distro.
4470: 
4471: 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>.
4472: 
4473: If your formula doesn't work in Spreadsheet::WriteExcel try the following:
4474: 
4475:     1. Verify that the formula works in Excel (or Gnumeric or OpenOffice.org).
4476:     2. Ensure that it isn't on the Caveats list shown above.
4477:     3. Ensure that cell references and formula names are in uppercase.
4478:     4. Ensure that you are using ':' as the range operator, A1:A4.
4479:     5. Ensure that you are using ',' as the union operator, SUM(1,2,3).
4480:     6. Ensure that the function is in the above table.
4481: 
4482: If you go through steps 1-6 and you still have a problem, mail me.
4483: 
4484: 
4485: 
4486: 
4487: =head2 Improving performance when working with formulas
4488: 
4489: Writing a large number of formulas with Spreadsheet::WriteExcel can be slow. This is due to the fact that each formula has to be parsed and with the current implementation this is computationally expensive.
4490: 
4491: However, in a lot of cases the formulas that you write will be quite similar, for example:
4492: 
4493:     $worksheet->write_formula('B1',    '=A1 * 3 + 50',    $format);
4494:     $worksheet->write_formula('B2',    '=A2 * 3 + 50',    $format);
4495:     ...
4496:     ...
4497:     $worksheet->write_formula('B99',   '=A999 * 3 + 50',  $format);
4498:     $worksheet->write_formula('B1000', '=A1000 * 3 + 50', $format);
4499: 
4500: In this example the cell reference changes in iterations from C<A1> to C<A1000>. The parser treats this variable as a I<token> and arranges it according to predefined rules. However, since the parser is oblivious to the value of the token, it is essentially performing the same calculation 1000 times. This is inefficient.
4501: 
4502: The way to avoid this inefficiency and thereby speed up the writing of formulas is to parse the formula once and then repeatedly substitute similar tokens.
4503: 
4504: A formula can be parsed and stored via the C<store_formula()> worksheet method. You can then use the C<repeat_formula()> method to substitute C<$pattern>, C<$replace> pairs in the stored formula:
4505: 
4506:     my $formula = $worksheet->store_formula('=A1 * 3 + 50');
4507: 
4508:     for my $row (0..999) {
4509:         $worksheet->repeat_formula($row, 1, $formula, $format, 'A1', 'A'.($row +1));
4510:     }
4511: 
4512: On an arbitrary test machine this method was 10 times faster than the brute force method shown above.
4513: 
4514: For more information about how Spreadsheet::WriteExcel parses and stores formulas see the C<Spreadsheet::WriteExcel::Formula> man page.
4515: 
4516: It should be noted however that the overall speed of direct formula parsing will be improved in a future version.
4517: 
4518: 
4519: 
4520: 
4521: =head1 EXAMPLES
4522: 
4523: See L<Spreadsheet::WriteExcel::Examples> for a full list of examples.
4524: 
4525: 
4526: =head2 Example 1
4527: 
4528: The following example shows some of the basic features of Spreadsheet::WriteExcel.
4529: 
4530: 
4531:     #!/usr/bin/perl -w
4532: 
4533:     use strict;
4534:     use Spreadsheet::WriteExcel;
4535: 
4536:     # Create a new workbook called simple.xls and add a worksheet
4537:     my $workbook  = Spreadsheet::WriteExcel->new('simple.xls');
4538:     my $worksheet = $workbook->add_worksheet();
4539: 
4540:     # The general syntax is write($row, $column, $token). Note that row and
4541:     # column are zero indexed
4542: 
4543:     # Write some text
4544:     $worksheet->write(0, 0,  'Hi Excel!');
4545: 
4546: 
4547:     # Write some numbers
4548:     $worksheet->write(2, 0,  3);          # Writes 3
4549:     $worksheet->write(3, 0,  3.00000);    # Writes 3
4550:     $worksheet->write(4, 0,  3.00001);    # Writes 3.00001
4551:     $worksheet->write(5, 0,  3.14159);    # TeX revision no.?
4552: 
4553: 
4554:     # Write some formulas
4555:     $worksheet->write(7, 0,  '=A3 + A6');
4556:     $worksheet->write(8, 0,  '=IF(A5>3,"Yes", "No")');
4557: 
4558: 
4559:     # Write a hyperlink
4560:     $worksheet->write(10, 0, 'http://www.perl.com/');
4561: 
4562: 
4563: =begin html
4564: 
4565: <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/a_simple.jpg" width="640" height="420" alt="Output from a_simple.pl" /></center></p>
4566: 
4567: =end html
4568: 
4569: 
4570: 
4571: 
4572: =head2 Example 2
4573: 
4574: The following is a general example which demonstrates some features of working with multiple worksheets.
4575: 
4576:     #!/usr/bin/perl -w
4577: 
4578:     use strict;
4579:     use Spreadsheet::WriteExcel;
4580: 
4581:     # Create a new Excel workbook
4582:     my $workbook = Spreadsheet::WriteExcel->new('regions.xls');
4583: 
4584:     # Add some worksheets
4585:     my $north = $workbook->add_worksheet('North');
4586:     my $south = $workbook->add_worksheet('South');
4587:     my $east  = $workbook->add_worksheet('East');
4588:     my $west  = $workbook->add_worksheet('West');
4589: 
4590:     # Add a Format
4591:     my $format = $workbook->add_format();
4592:     $format->set_bold();
4593:     $format->set_color('blue');
4594: 
4595:     # Add a caption to each worksheet
4596:     foreach my $worksheet ($workbook->sheets()) {
4597:         $worksheet->write(0, 0, 'Sales', $format);
4598:     }
4599: 
4600:     # Write some data
4601:     $north->write(0, 1, 200000);
4602:     $south->write(0, 1, 100000);
4603:     $east->write (0, 1, 150000);
4604:     $west->write (0, 1, 100000);
4605: 
4606:     # Set the active worksheet
4607:     $south->activate();
4608: 
4609:     # Set the width of the first column
4610:     $south->set_column(0, 0, 20);
4611: 
4612:     # Set the active cell
4613:     $south->set_selection(0, 1);
4614: 
4615: 
4616: =begin html
4617: 
4618: <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/regions.jpg" width="640" height="420" alt="Output from regions.pl" /></center></p>
4619: 
4620: =end html
4621: 
4622: 
4623: 
4624: 
4625: =head2 Example 3
4626: 
4627: This example shows how to use a conditional numerical format with colours to indicate if a share price has gone up or down.
4628: 
4629:     use strict;
4630:     use Spreadsheet::WriteExcel;
4631: 
4632:     # Create a new workbook and add a worksheet
4633:     my $workbook  = Spreadsheet::WriteExcel->new('stocks.xls');
4634:     my $worksheet = $workbook->add_worksheet();
4635: 
4636:     # Set the column width for columns 1, 2, 3 and 4
4637:     $worksheet->set_column(0, 3, 15);
4638: 
4639: 
4640:     # Create a format for the column headings
4641:     my $header = $workbook->add_format();
4642:     $header->set_bold();
4643:     $header->set_size(12);
4644:     $header->set_color('blue');
4645: 
4646: 
4647:     # Create a format for the stock price
4648:     my $f_price = $workbook->add_format();
4649:     $f_price->set_align('left');
4650:     $f_price->set_num_format('$0.00');
4651: 
4652: 
4653:     # Create a format for the stock volume
4654:     my $f_volume = $workbook->add_format();
4655:     $f_volume->set_align('left');
4656:     $f_volume->set_num_format('#,##0');
4657: 
4658: 
4659:     # Create a format for the price change. This is an example of a
4660:     # conditional format. The number is formatted as a percentage. If it is
4661:     # positive it is formatted in green, if it is negative it is formatted
4662:     # in red and if it is zero it is formatted as the default font colour
4663:     # (in this case black). Note: the [Green] format produces an unappealing
4664:     # lime green. Try [Color 10] instead for a dark green.
4665:     #
4666:     my $f_change = $workbook->add_format();
4667:     $f_change->set_align('left');
4668:     $f_change->set_num_format('[Green]0.0%;[Red]-0.0%;0.0%');
4669: 
4670: 
4671:     # Write out the data
4672:     $worksheet->write(0, 0, 'Company',$header);
4673:     $worksheet->write(0, 1, 'Price',  $header);
4674:     $worksheet->write(0, 2, 'Volume', $header);
4675:     $worksheet->write(0, 3, 'Change', $header);
4676: 
4677:     $worksheet->write(1, 0, 'Damage Inc.'       );
4678:     $worksheet->write(1, 1, 30.25,    $f_price ); # $30.25
4679:     $worksheet->write(1, 2, 1234567,  $f_volume); # 1,234,567
4680:     $worksheet->write(1, 3, 0.085,    $f_change); # 8.5% in green
4681: 
4682:     $worksheet->write(2, 0, 'Dump Corp.'        );
4683:     $worksheet->write(2, 1, 1.56,     $f_price ); # $1.56
4684:     $worksheet->write(2, 2, 7564,     $f_volume); # 7,564
4685:     $worksheet->write(2, 3, -0.015,   $f_change); # -1.5% in red
4686: 
4687:     $worksheet->write(3, 0, 'Rev Ltd.'          );
4688:     $worksheet->write(3, 1, 0.13,     $f_price ); # $0.13
4689:     $worksheet->write(3, 2, 321,      $f_volume); # 321
4690:     $worksheet->write(3, 3, 0,        $f_change); # 0 in the font color (black)
4691: 
4692: 
4693: =begin html
4694: 
4695: <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/stocks.jpg" width="640" height="420" alt="Output from stocks.pl" /></center></p>
4696: 
4697: =end html
4698: 
4699: 
4700: 
4701: 
4702: =head2 Example 4
4703: 
4704: The following is a simple example of using functions.
4705: 
4706:     #!/usr/bin/perl -w
4707: 
4708:     use strict;
4709:     use Spreadsheet::WriteExcel;
4710: 
4711:     # Create a new workbook and add a worksheet
4712:     my $workbook  = Spreadsheet::WriteExcel->new('stats.xls');
4713:     my $worksheet = $workbook->add_worksheet('Test data');
4714: 
4715:     # Set the column width for columns 1
4716:     $worksheet->set_column(0, 0, 20);
4717: 
4718: 
4719:     # Create a format for the headings
4720:     my $format = $workbook->add_format();
4721:     $format->set_bold();
4722: 
4723: 
4724:     # Write the sample data
4725:     $worksheet->write(0, 0, 'Sample', $format);
4726:     $worksheet->write(0, 1, 1);
4727:     $worksheet->write(0, 2, 2);
4728:     $worksheet->write(0, 3, 3);
4729:     $worksheet->write(0, 4, 4);
4730:     $worksheet->write(0, 5, 5);
4731:     $worksheet->write(0, 6, 6);
4732:     $worksheet->write(0, 7, 7);
4733:     $worksheet->write(0, 8, 8);
4734: 
4735:     $worksheet->write(1, 0, 'Length', $format);
4736:     $worksheet->write(1, 1, 25.4);
4737:     $worksheet->write(1, 2, 25.4);
4738:     $worksheet->write(1, 3, 24.8);
4739:     $worksheet->write(1, 4, 25.0);
4740:     $worksheet->write(1, 5, 25.3);
4741:     $worksheet->write(1, 6, 24.9);
4742:     $worksheet->write(1, 7, 25.2);
4743:     $worksheet->write(1, 8, 24.8);
4744: 
4745:     # Write some statistical functions
4746:     $worksheet->write(4,  0, 'Count', $format);
4747:     $worksheet->write(4,  1, '=COUNT(B1:I1)');
4748: 
4749:     $worksheet->write(5,  0, 'Sum', $format);
4750:     $worksheet->write(5,  1, '=SUM(B2:I2)');
4751: 
4752:     $worksheet->write(6,  0, 'Average', $format);
4753:     $worksheet->write(6,  1, '=AVERAGE(B2:I2)');
4754: 
4755:     $worksheet->write(7,  0, 'Min', $format);
4756:     $worksheet->write(7,  1, '=MIN(B2:I2)');
4757: 
4758:     $worksheet->write(8,  0, 'Max', $format);
4759:     $worksheet->write(8,  1, '=MAX(B2:I2)');
4760: 
4761:     $worksheet->write(9,  0, 'Standard Deviation', $format);
4762:     $worksheet->write(9,  1, '=STDEV(B2:I2)');
4763: 
4764:     $worksheet->write(10, 0, 'Kurtosis', $format);
4765:     $worksheet->write(10, 1, '=KURT(B2:I2)');
4766: 
4767: 
4768: =begin html
4769: 
4770: <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/stats.jpg" width="640" height="420" alt="Output from stats.pl" /></center></p>
4771: 
4772: =end html
4773: 
4774: 
4775: 
4776: 
4777: =head2 Example 5
4778: 
4779: The following example converts a tab separated file called C<tab.txt> into an Excel file called C<tab.xls>.
4780: 
4781:     #!/usr/bin/perl -w
4782: 
4783:     use strict;
4784:     use Spreadsheet::WriteExcel;
4785: 
4786:     open (TABFILE, 'tab.txt') or die "tab.txt: $!";
4787: 
4788:     my $workbook  = Spreadsheet::WriteExcel->new('tab.xls');
4789:     my $worksheet = $workbook->add_worksheet();
4790: 
4791:     # Row and column are zero indexed
4792:     my $row = 0;
4793: 
4794:     while (<TABFILE>) {
4795:         chomp;
4796:         # Split on single tab
4797:         my @Fld = split('\t', $_);
4798: 
4799:         my $col = 0;
4800:         foreach my $token (@Fld) {
4801:             $worksheet->write($row, $col, $token);
4802:             $col++;
4803:         }
4804:         $row++;
4805:     }
4806: 
4807: 
4808: 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 Text::CSV_XS module distro.
4809: 
4810: See the examples/csv2xls link here: L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>.
4811: 
4812: 
4813: 
4814: 
4815: =head2 Additional Examples
4816: 
4817: The following is a description of the example files that are provided
4818: in the standard Spreadsheet::WriteExcel distribution. They demonstrate the
4819: different features and options of the module.
4820: See L<Spreadsheet::WriteExcel::Examples> for more details.
4821: 
4822:     Getting started
4823:     ===============
4824:     a_simple.pl             A get started example with some basic features.
4825:     demo.pl                 A demo of some of the available features.
4826:     regions.pl              A simple example of multiple worksheets.
4827:     stats.pl                Basic formulas and functions.
4828:     formats.pl              All the available formatting on several worksheets.
4829:     bug_report.pl           A template for submitting bug reports.
4830: 
4831: 
4832:     Advanced
4833:     ========
4834:     autofilter.pl           Examples of worksheet autofilters.
4835:     autofit.pl              Simulate Excel's autofit for column widths.
4836:     bigfile.pl              Write past the 7MB limit with OLE::Storage_Lite.
4837:     cgi.pl                  A simple CGI program.
4838:     chart_area.pl           A demo of area style charts.
4839:     chart_bar.pl            A demo of bar (vertical histogram) style charts.
4840:     chart_column.pl         A demo of column (histogram) style charts.
4841:     chart_line.pl           A demo of line style charts.
4842:     chart_pie.pl            A demo of pie style charts.
4843:     chart_scatter.pl        A demo of scatter style charts.
4844:     chart_stock.pl          A demo of stock style charts.
4845:     chess.pl                An example of reusing formatting via properties.
4846:     colors.pl               A demo of the colour palette and named colours.
4847:     comments1.pl            Add comments to worksheet cells.
4848:     comments2.pl            Add comments with advanced options.
4849:     copyformat.pl           Example of copying a cell format.
4850:     data_validate.pl        An example of data validation and dropdown lists.
4851:     date_time.pl            Write dates and times with write_date_time().
4852:     defined_name.pl         Example of how to create defined names.
4853:     diag_border.pl          A simple example of diagonal cell borders.
4854:     easter_egg.pl           Expose the Excel97 flight simulator.
4855:     filehandle.pl           Examples of working with filehandles.
4856:     formula_result.pl       Formulas with user specified results.
4857:     headers.pl              Examples of worksheet headers and footers.
4858:     hide_sheet.pl           Simple example of hiding a worksheet.
4859:     hyperlink1.pl           Shows how to create web hyperlinks.
4860:     hyperlink2.pl           Examples of internal and external hyperlinks.
4861:     images.pl               Adding images to worksheets.
4862:     indent.pl               An example of cell indentation.
4863:     merge1.pl               A simple example of cell merging.
4864:     merge2.pl               A simple example of cell merging with formatting.
4865:     merge3.pl               Add hyperlinks to merged cells.
4866:     merge4.pl               An advanced example of merging with formatting.
4867:     merge5.pl               An advanced example of merging with formatting.
4868:     merge6.pl               An example of merging with Unicode strings.
4869:     mod_perl1.pl            A simple mod_perl 1 program.
4870:     mod_perl2.pl            A simple mod_perl 2 program.
4871:     outline.pl              An example of outlines and grouping.
4872:     outline_collapsed.pl    An example of collapsed outlines.
4873:     panes.pl                An examples of how to create panes.
4874:     properties.pl           Add document properties to a workbook.
4875:     protection.pl           Example of cell locking and formula hiding.
4876:     repeat.pl               Example of writing repeated formulas.
4877:     right_to_left.pl        Change default sheet direction to right to left.
4878:     row_wrap.pl             How to wrap data from one worksheet onto another.
4879:     sales.pl                An example of a simple sales spreadsheet.
4880:     sendmail.pl             Send an Excel email attachment using Mail::Sender.
4881:     stats_ext.pl            Same as stats.pl with external references.
4882:     stocks.pl               Demonstrates conditional formatting.
4883:     tab_colors.pl           Example of how to set worksheet tab colours.
4884:     textwrap.pl             Demonstrates text wrapping options.
4885:     win32ole.pl             A sample Win32::OLE example for comparison.
4886:     write_arrays.pl         Example of writing 1D or 2D arrays of data.
4887:     write_handler1.pl       Example of extending the write() method. Step 1.
4888:     write_handler2.pl       Example of extending the write() method. Step 2.
4889:     write_handler3.pl       Example of extending the write() method. Step 3.
4890:     write_handler4.pl       Example of extending the write() method. Step 4.
4891:     write_to_scalar.pl      Example of writing an Excel file to a Perl scalar.
4892: 
4893: 
4894:     Unicode
4895:     =======
4896:     unicode_utf16.pl        Simple example of using Unicode UTF16 strings.
4897:     unicode_utf16_japan.pl  Write Japanese Unicode strings using UTF-16.
4898:     unicode_cyrillic.pl     Write Russian Cyrillic strings using UTF-8.
4899:     unicode_list.pl         List the chars in a Unicode font.
4900:     unicode_2022_jp.pl      Japanese: ISO-2022-JP to utf8 in perl 5.8.
4901:     unicode_8859_11.pl      Thai:     ISO-8859_11 to utf8 in perl 5.8.
4902:     unicode_8859_7.pl       Greek:    ISO-8859_7  to utf8 in perl 5.8.
4903:     unicode_big5.pl         Chinese:  BIG5        to utf8 in perl 5.8.
4904:     unicode_cp1251.pl       Russian:  CP1251      to utf8 in perl 5.8.
4905:     unicode_cp1256.pl       Arabic:   CP1256      to utf8 in perl 5.8.
4906:     unicode_koi8r.pl        Russian:  KOI8-R      to utf8 in perl 5.8.
4907:     unicode_polish_utf8.pl  Polish :  UTF8        to utf8 in perl 5.8.
4908:     unicode_shift_jis.pl    Japanese: Shift JIS   to utf8 in perl 5.8.
4909: 
4910: 
4911:     Utility
4912:     =======
4913:     csv2xls.pl              Program to convert a CSV file to an Excel file.
4914:     tab2xls.pl              Program to convert a tab separated file to xls.
4915:     datecalc1.pl            Convert Unix/Perl time to Excel time.
4916:     datecalc2.pl            Calculate an Excel date using Date::Calc.
4917:     lecxe.pl                Convert Excel to WriteExcel using Win32::OLE.
4918: 
4919: 
4920:     Developer
4921:     =========
4922:     convertA1.pl            Helper functions for dealing with A1 notation.
4923:     function_locale.pl      Add non-English function names to Formula.pm.
4924:     writeA1.pl              Example of how to extend the module.
4925: 
4926: 
4927: 
4928: =head1 LIMITATIONS
4929: 
4930: The following limits are imposed by Excel:
4931: 
4932:     Description                          Limit
4933:     -----------------------------------  ------
4934:     Maximum number of chars in a string  32767
4935:     Maximum number of columns            256
4936:     Maximum number of rows               65536
4937:     Maximum chars in a sheet name        31
4938:     Maximum chars in a header/footer     254
4939: 
4940: For Excel 2007+ file limits see the L<Excel::Writer::XLSX> module.
4941: 
4942: The minimum file size is 6K due to the OLE overhead. The maximum file size is approximately 7MB (7087104 bytes) of BIFF data. This can be extended by installing Takanori Kawai's OLE::Storage_Lite module L<http://search.cpan.org/search?dist=OLE-Storage_Lite> see the C<bigfile.pl> example in the C<examples> directory of the distro.
4943: 
4944: 
4945: 
4946: 
4947: =head1 DOWNLOADING
4948: 
4949: The latest version of this module is always available at: L<http://search.cpan.org/search?dist=Spreadsheet-WriteExcel/>.
4950: 
4951: 
4952: 
4953: 
4954: =head1 REQUIREMENTS
4955: 
4956: This module requires Perl >= 5.005, Parse::RecDescent, File::Temp and OLE::Storage_Lite:
4957: 
4958:     http://search.cpan.org/search?dist=Parse-RecDescent/ # For formulas.
4959:     http://search.cpan.org/search?dist=File-Temp/        # For set_tempdir().
4960:     http://search.cpan.org/search?dist=OLE-Storage_Lite/ # For files > 7MB.
4961: 
4962: Note, these aren't strict requirements. Spreadsheet::WriteExcel will work without these modules if you don't use write_formula(), set_tempdir() or create files greater than 7MB. However, it is best to install them if possible and they will be installed automatically if you use a tool such as CPAN.pm or ppm.
4963: 
4964: 
4965: =head1 INSTALLATION
4966: 
4967: See the INSTALL or install.html docs that come with the distribution or: L<http://search.cpan.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.31/INSTALL>.
4968: 
4969: 
4970: 
4971: 
4972: =head1 PORTABILITY
4973: 
4974: Spreadsheet::WriteExcel will work on the majority of Windows, UNIX and Macintosh platforms. Specifically, the module will work on any system where perl packs floats in the 64 bit IEEE format. The float must also be in little-endian format but it will be reversed if necessary. Thus:
4975: 
4976:     print join(' ', map { sprintf '%#02x', $_ } unpack('C*', pack 'd', 1.2345)), "\n";
4977: 
4978: should give (or in reverse order):
4979: 
4980:     0x8d 0x97 0x6e 0x12 0x83 0xc0 0xf3 0x3f
4981: 
4982: In general, if you don't know whether your system supports a 64 bit IEEE float or not, it probably does. If your system doesn't, WriteExcel will C<croak()> with the message given in the L</DIAGNOSTICS> section. You can check which platforms the module has been tested on at the CPAN testers site: L<http://testers.cpan.org/search?request=dist&dist=Spreadsheet-WriteExcel>.
4983: 
4984: 
4985: 
4986: 
4987: =head1 DIAGNOSTICS
4988: 
4989: 
4990: =over 4
4991: 
4992: =item Filename required by Spreadsheet::WriteExcel->new()
4993: 
4994: A filename must be given in the constructor.
4995: 
4996: =item Can't open filename. It may be in use or protected.
4997: 
4998: 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.
4999: 
5000: =item Unable to create tmp files via File::Temp::tempfile()...
5001: 
5002: This is a C<-w> warning. You will see it if you are using Spreadsheet::WriteExcel in an environment where temporary files cannot be created, in which case all data will be stored in memory. The warning is for information only: it does not affect creation but it will affect the speed of execution for large files. See the C<set_tempdir> workbook method.
5003: 
5004: 
5005: =item Maximum file size, 7087104, exceeded.
5006: 
5007: The current OLE implementation only supports a maximum BIFF file of this size. This limit can be extended, see the L</LIMITATIONS> section.
5008: 
5009: =item Can't locate Parse/RecDescent.pm in @INC ...
5010: 
5011: Spreadsheet::WriteExcel requires the Parse::RecDescent module. Download it from CPAN: L<http://search.cpan.org/search?dist=Parse-RecDescent>
5012: 
5013: =item Couldn't parse formula ...
5014: 
5015: There are a large number of warnings which relate to badly formed formulas and functions. See the L</FORMULAS AND FUNCTIONS IN EXCEL> section for suggestions on how to avoid these errors. You should also check the formula in Excel to ensure that it is valid.
5016: 
5017: =item Required floating point format not supported on this platform.
5018: 
5019: Operating system doesn't support 64 bit IEEE float or it is byte-ordered in a way unknown to WriteExcel.
5020: 
5021: 
5022: =item 'file.xls' cannot be accessed. The file may be read-only ...
5023: 
5024: You may sometimes encounter the following error when trying to open a file in Excel: "file.xls cannot be accessed. The file may be read-only, or you may be trying to access a read-only location. Or, the server the document is stored on may not be responding."
5025: 
5026: This error generally means that the Excel file has been corrupted. There are two likely causes of this: the file was FTPed in ASCII mode instead of binary mode or else the file was created with C<UTF-8> data returned by an XML parser. See L</Warning about XML::Parser and perl 5.6> for further details.
5027: 
5028: =back
5029: 
5030: 
5031: 
5032: 
5033: =head1 THE EXCEL BINARY FORMAT
5034: 
5035: The following is some general information about the Excel binary format for anyone who may be interested.
5036: 
5037: Excel data is stored in the "Binary Interchange File Format" (BIFF) file format. Details of this format are given in "Excel 97-2007 Binary File Format Specification" L<http://www.microsoft.com/interop/docs/OfficeBinaryFormats.mspx>.
5038: 
5039: Daniel Rentz of OpenOffice.org has also written a detailed description of the Excel workbook records, see L<http://sc.openoffice.org/excelfileformat.pdf>.
5040: 
5041: Charles Wybble has collected together additional information about the Excel file format. See "The Chicago Project" at L<http://chicago.sourceforge.net/devel/>.
5042: 
5043: The BIFF data is stored along with other data in an OLE Compound File. This is a structured storage which acts like a file system within a file. A Compound File is comprised of storages and streams which, to follow the file system analogy, are like directories and files.
5044: 
5045: The OLE format is explained in the "Windows Compound Binary File Format Specification" L<http://www.microsoft.com/interop/docs/supportingtechnologies.mspx>
5046: 
5047: The Digital Imaging Group have also detailed the OLE format in the JPEG2000 specification: see Appendix A of L<http://www.i3a.org/pdf/wg1n1017.pdf>.
5048: 
5049: Please note that the provision of this information does not constitute an invitation to start hacking at the BIFF or OLE file formats. There are more interesting ways to waste your time. ;-)
5050: 
5051: 
5052: 
5053: 
5054: =head1 WRITING EXCEL FILES
5055: 
5056: Depending on your requirements, background and general sensibilities you may prefer one of the following methods of getting data into Excel:
5057: 
5058: =over 4
5059: 
5060: =item * Win32::OLE module and office automation
5061: 
5062: This requires a Windows platform and an installed copy of Excel. This is the most powerful and complete method for interfacing with Excel. See L<http://www.activestate.com/ASPN/Reference/Products/ActivePerl-5.6/faq/Windows/ActivePerl-Winfaq12.html> and L<http://www.activestate.com/ASPN/Reference/Products/ActivePerl-5.6/site/lib/Win32/OLE.html>. If your main platform is UNIX but you have the resources to set up a separate Win32/MSOffice server, you can convert office documents to text, postscript or PDF using Win32::OLE. For a demonstration of how to do this using Perl see Docserver: L<http://search.cpan.org/search?mode=module&query=docserver>.
5063: 
5064: =item * CSV, comma separated variables or text
5065: 
5066: If the file extension is C<csv>, Excel will open and convert this format automatically. Generating a valid CSV file isn't as easy as it seems. Have a look at the DBD::RAM, DBD::CSV, Text::xSV and Text::CSV_XS modules.
5067: 
5068: =item * DBI with DBD::ADO or DBD::ODBC
5069: 
5070: 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.
5071: 
5072: =item * DBD::Excel
5073: 
5074: You can also access Spreadsheet::WriteExcel using the standard DBI interface via Takanori Kawai's DBD::Excel module L<http://search.cpan.org/dist/DBD-Excel>
5075: 
5076: =item * Spreadsheet::WriteExcelXML
5077: 
5078: This module allows you to create an Excel XML file using the same interface as Spreadsheet::WriteExcel. See: L<http://search.cpan.org/dist/Spreadsheet-WriteExcelXML>
5079: 
5080: =item * Excel::Template
5081: 
5082: This module allows you to create an Excel file from an XML template in a manner similar to HTML::Template. See L<http://search.cpan.org/dist/Excel-Template/>.
5083: 
5084: =item * Spreadsheet::WriteExcel::FromXML
5085: 
5086: This module allows you to turn a simple XML file into an Excel file using Spreadsheet::WriteExcel as a back-end. The format of the XML file is defined by a supplied DTD: L<http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromXML>.
5087: 
5088: =item * Spreadsheet::WriteExcel::Simple
5089: 
5090: This provides an easier interface to Spreadsheet::WriteExcel: L<http://search.cpan.org/dist/Spreadsheet-WriteExcel-Simple>.
5091: 
5092: =item * Spreadsheet::WriteExcel::FromDB
5093: 
5094: This is a useful module for creating Excel files directly from a DB table: L<http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromDB>.
5095: 
5096: =item * HTML tables
5097: 
5098: This is an easy way of adding formatting via a text based format.
5099: 
5100: =item * XML or HTML
5101: 
5102: The Excel XML and HTML file specification are available from L<http://msdn.microsoft.com/library/officedev/ofxml2k/ofxml2k.htm>.
5103: 
5104: =back
5105: 
5106: For other Perl-Excel modules try the following search: L<http://search.cpan.org/search?mode=module&query=excel>.
5107: 
5108: 
5109: 
5110: 
5111: =head1 READING EXCEL FILES
5112: 
5113: To read data from Excel files try:
5114: 
5115: =over 4
5116: 
5117: =item * Spreadsheet::ParseExcel
5118: 
5119: This uses the OLE::Storage-Lite module to extract data from an Excel file. L<http://search.cpan.org/dist/Spreadsheet-ParseExcel>.
5120: 
5121: =item * Spreadsheet::ParseExcel_XLHTML
5122: 
5123: This module uses Spreadsheet::ParseExcel's interface but uses xlHtml (see below) to do the conversion: L<http://search.cpan.org/dist/Spreadsheet-ParseExcel_XLHTML>
5124: Spreadsheet::ParseExcel_XLHTML
5125: 
5126: =item * xlHtml
5127: 
5128: This is an open source "Excel to HTML Converter" C/C++ project at L<http://chicago.sourceforge.net/xlhtml/>.
5129: 
5130: =item * DBD::Excel (reading)
5131: 
5132: You can also access Spreadsheet::ParseExcel using the standard DBI interface via  Takanori Kawai's DBD::Excel module L<http://search.cpan.org/dist/DBD-Excel>.
5133: 
5134: =item * Win32::OLE module and office automation (reading)
5135: 
5136: See, the section L<WRITING EXCEL FILES>.
5137: 
5138: =item * HTML tables (reading)
5139: 
5140: If the files are saved from Excel in a HTML format the data can be accessed using HTML::TableExtract L<http://search.cpan.org/dist/HTML-TableExtract>.
5141: 
5142: =item * DBI with DBD::ADO or DBD::ODBC.
5143: 
5144: See, the section L<WRITING EXCEL FILES>.
5145: 
5146: =item * XML::Excel
5147: 
5148: Converts Excel files to XML using Spreadsheet::ParseExcel L<http://search.cpan.org/dist/XML-Excel>.
5149: 
5150: =item * OLE::Storage, aka LAOLA
5151: 
5152: This is a Perl interface to OLE file formats. In particular, the distro contains an Excel to HTML converter called Herbert, L<http://user.cs.tu-berlin.de/~schwartz/pmh/>. This has been superseded by the Spreadsheet::ParseExcel module.
5153: 
5154: =back
5155: 
5156: 
5157: For other Perl-Excel modules try the following search: L<http://search.cpan.org/search?mode=module&query=excel>.
5158: 
5159: If you wish to view Excel files on a UNIX/Linux platform check out the excellent Gnumeric spreadsheet application at L<http://www.gnome.org/projects/gnumeric/> or OpenOffice.org at L<http://www.openoffice.org/>.
5160: 
5161: If you wish to view Excel files on a Windows platform which doesn't have Excel installed you can use the free Microsoft Excel Viewer L<http://office.microsoft.com/downloads/2000/xlviewer.aspx>.
5162: 
5163: 
5164: 
5165: 
5166: =head1 MODIFYING AND REWRITING EXCEL FILES
5167: 
5168: An Excel file is a binary file within a binary file. It contains several interlinked checksums and changing even one byte can cause it to become corrupted.
5169: 
5170: As such you cannot simply append or update an Excel file. The only way to achieve this is to read the entire file into memory, make the required changes or additions and then write the file out again.
5171: 
5172: You can read and rewrite an Excel file using the Spreadsheet::ParseExcel::SaveParser module which is a wrapper around Spreadsheet::ParseExcel and Spreadsheet::WriteExcel. It is part of the Spreadsheet::ParseExcel package: L<http://search.cpan.org/search?dist=Spreadsheet-ParseExcel>.
5173: 
5174: However, you can only rewrite the features that Spreadsheet::WriteExcel supports so macros, graphs and some other features in the original Excel file will be lost. Also, formulas aren't rewritten, only the result of a formula is written.
5175: 
5176: Here is an example:
5177: 
5178:     #!/usr/bin/perl -w
5179: 
5180:     use strict;
5181:     use Spreadsheet::ParseExcel;
5182:     use Spreadsheet::ParseExcel::SaveParser;
5183: 
5184:     # Open the template with SaveParser
5185:     my $parser   = new Spreadsheet::ParseExcel::SaveParser;
5186:     my $template = $parser->Parse('template.xls');
5187: 
5188:     my $sheet    = 0;
5189:     my $row      = 0;
5190:     my $col      = 0;
5191: 
5192:     # Get the format from the cell
5193:     my $format   = $template->{Worksheet}[$sheet]
5194:                             ->{Cells}[$row][$col]
5195:                             ->{FormatNo};
5196: 
5197:     # Write data to some cells
5198:     $template->AddCell(0, $row,   $col,   1,     $format);
5199:     $template->AddCell(0, $row+1, $col, "Hello", $format);
5200: 
5201:     # Add a new worksheet
5202:     $template->AddWorksheet('New Data');
5203: 
5204:     # The SaveParser SaveAs() method returns a reference to a
5205:     # Spreadsheet::WriteExcel object. If you wish you can then
5206:     # use this to access any of the methods that aren't
5207:     # available from the SaveParser object. If you don't need
5208:     # to do this just use SaveAs().
5209:     #
5210:     my $workbook;
5211: 
5212:     {
5213:         # SaveAs generates a lot of harmless warnings about unset
5214:         # Worksheet properties. You can ignore them if you wish.
5215:         local $^W = 0;
5216: 
5217:         # Rewrite the file or save as a new file
5218:         $workbook = $template->SaveAs('new.xls');
5219:     }
5220: 
5221:     # Use Spreadsheet::WriteExcel methods
5222:     my $worksheet  = $workbook->sheets(0);
5223: 
5224:     $worksheet->write($row+2, $col, "World2");
5225: 
5226:     $workbook->close();
5227: 
5228: 
5229: 
5230: 
5231: =head1 Warning about XML::Parser and perl 5.6
5232: 
5233: You must be careful when using Spreadsheet::WriteExcel in conjunction with perl 5.6 and XML::Parser (and other XML parsers) due to the fact that the data returned by the parser is generally in C<UTF-8> format.
5234: 
5235: When C<UTF-8> strings are added to Spreadsheet::WriteExcel's internal data it causes the generated Excel file to become corrupt.
5236: 
5237: Note, this doesn't affect perl 5.005 (which doesn't try to handle C<UTF-8>) or 5.8 (which handles it correctly).
5238: 
5239: To avoid this problem you should upgrade to perl 5.8, if possible, or else you should convert the output data from XML::Parser to ASCII or ISO-8859-1 using one of the following methods:
5240: 
5241:     $new_str = pack 'C*', unpack 'U*', $utf8_str;
5242: 
5243: 
5244:     use Unicode::MapUTF8 'from_utf8';
5245:     $new_str = from_utf8({-str => $utf8_str, -charset => 'ISO-8859-1'});
5246: 
5247: 
5248: 
5249: 
5250: =head1 Warning about Office Service Pack 3
5251: 
5252: If you have Office Service Pack 3 (SP3) installed you may see the following warning when you open a file created by Spreadsheet::WriteExcel:
5253: 
5254:     "File Error: data may have been lost".
5255: 
5256: This is usually caused by multiple instances of data in a cell.
5257: 
5258: SP3 changed Excel's default behaviour when it encounters multiple data in a cell so that it issues a warning when the file is opened and it displays the first data that was written. Prior to SP3 it didn't issue a warning and displayed the last data written.
5259: 
5260: For a longer discussion and some workarounds see the following: L<http://groups.google.com/group/spreadsheet-writeexcel/browse_thread/thread/3dcea40e6620af3a>.
5261: 
5262: 
5263: 
5264: 
5265: =head1 BUGS
5266: 
5267: Formulas are formulae.
5268: 
5269: XML and C<UTF-8> data on perl 5.6 can cause Excel files created by Spreadsheet::WriteExcel to become corrupt. See L</Warning about XML::Parser and perl 5.6> for further details.
5270: 
5271: The format object that is used with a C<merge_range()> method call is marked internally as being associated with a merged range. It is a fatal error to use a merged format in a non-merged cell. The current workaround is to use separate formats for merged and non-merged cell. This restriction will be removed in a future release.
5272: 
5273: Nested formulas sometimes aren't parsed correctly and give a result of "#VALUE". If you come across a formula that parses like this, let me know.
5274: 
5275: Spreadsheet::ParseExcel: All formulas created by Spreadsheet::WriteExcel are read as having a value of zero. This is because Spreadsheet::WriteExcel only stores the formula and not the calculated result.
5276: 
5277: OpenOffice.org: No known issues in this release.
5278: 
5279: Gnumeric: No known issues in this release.
5280: 
5281: If you wish to submit a bug report run the C<bug_report.pl> program in the C<examples> directory of the distro.
5282: 
5283: 
5284: 
5285: 
5286: =head1 TO DO
5287: 
5288: Spreadsheet::WriteExcel has been superseded by L<Excel::Writer::XLSX>. All new feature and enhancements will be added to that module.
5289: 
5290: 
5291: =head1 REPOSITORY
5292: 
5293: The Spreadsheet::WriteExcel source code in host on github: L<http://github.com/jmcnamara/spreadsheet-writeexcel>.
5294: 
5295: 
5296: 
5297: 
5298: =head1 MAILING LIST
5299: 
5300: There is a Google group for discussing and asking questions about Spreadsheet::WriteExcel. This is a good place to search to see if your question has been asked before:  L<http://groups.google.com/group/spreadsheet-writeexcel>.
5301: 
5302: =begin html
5303: 
5304: <center>
5305: <table style="background-color: #fff; padding: 5px;" cellspacing="0">
5306:   <tr><td>
5307:   <img src="http://groups.google.com/intl/en/images/logos/groups_logo_sm.gif"
5308:          height="30" width="140" alt="Google Groups"/>
5309:   </td></tr>
5310:   <tr><td>
5311:   <a href="http://groups.google.com/group/spreadsheet-writeexcel">Spreadsheet::WriteExcel</a>
5312:   </td></tr>
5313: </table>
5314: </center>
5315: 
5316: =end html
5317: 
5318: 
5319: 
5320: Alternatively you can keep up to date with future releases by subscribing at:
5321: L<http://freshmeat.net/projects/writeexcel/>.
5322: 
5323: 
5324: 
5325: 
5326: =head1 DONATIONS
5327: 
5328: If you'd care to donate to the Spreadsheet::WriteExcel project, you can do so via PayPal: L<http://tinyurl.com/7ayes>.
5329: 
5330: 
5331: 
5332: 
5333: =head1 SEE ALSO
5334: 
5335: Spreadsheet::ParseExcel: L<http://search.cpan.org/dist/Spreadsheet-ParseExcel>.
5336: 
5337: Spreadsheet-WriteExcel-FromXML: L<http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromXML>.
5338: 
5339: Spreadsheet::WriteExcel::FromDB: L<http://search.cpan.org/dist/Spreadsheet-WriteExcel-FromDB>.
5340: 
5341: Excel::Template: L<http://search.cpan.org/~rkinyon/Excel-Template/>.
5342: 
5343: DateTime::Format::Excel: L<http://search.cpan.org/dist/DateTime-Format-Excel>.
5344: 
5345: "Reading and writing Excel files with Perl" by Teodor Zlatanov, at IBM developerWorks: L<http://www-106.ibm.com/developerworks/library/l-pexcel/>.
5346: 
5347: "Excel-Dateien mit Perl erstellen - Controller im Gluck" by Peter Dintelmann and Christian Kirsch in the German Unix/web journal iX: L<http://www.heise.de/ix/artikel/2001/06/175/>.
5348: 
5349: Spreadsheet::WriteExcel documentation in Japanese by Takanori Kawai. L<http://member.nifty.ne.jp/hippo2000/perltips/Spreadsheet/WriteExcel.htm>.
5350: 
5351: Oesterly user brushes with fame:
5352: L<http://oesterly.com/releases/12102000.html>.
5353: 
5354: The csv2xls program that is part of Text::CSV_XS:
5355: L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>.
5356: 
5357: 
5358: 
5359: =head1 ACKNOWLEDGMENTS
5360: 
5361: 
5362: The following people contributed to the debugging and testing of Spreadsheet::WriteExcel:
5363: 
5364: Alexander Farber, Andre de Bruin, Arthur@ais, Artur Silveira da Cunha, Bob Rose, Borgar Olsen, Brian Foley, Brian White, Bob Mackay, Cedric Bouvier, Chad Johnson, CPAN testers, Damyan Ivanov, Daniel Berger, Daniel Gardner, Dmitry Kochurov, Eric Frazier, Ernesto Baschny, Felipe Perez Galiana, Gordon Simpson, Hanc Pavel, Harold Bamford, James Holmes, James Wilkinson, Johan Ekenberg, Johann Hanne, Jonathan Scott Duff, J.C. Wren, Kenneth Stacey, Keith Miller, Kyle Krom, Marc Rosenthal, Markus Schmitz, Michael Braig, Michael Buschauer, Mike Blazer, Michael Erickson, Michael W J West, Ning Xie, Paul J. Falbe, Paul Medynski, Peter Dintelmann, Pierre Laplante, Praveen Kotha, Reto Badertscher, Rich Sorden, Shane Ashby, Sharron McKenzie, Shenyu Zheng, Stephan Loescher, Steve Sapovits, Sven Passig, Svetoslav Marinov, Tamas Gulacsi, Troy Daniels, Vahe Sarkissian.
5365: 
5366: The following people contributed patches, examples or Excel information:
5367: 
5368: Andrew Benham, Bill Young, Cedric Bouvier, Charles Wybble, Daniel Rentz, David Robins, Franco Venturi, Guy Albertelli, Ian Penman, John Heitmann, Jon Guy, Kyle R. Burton, Pierre-Jean Vouette, Rubio, Marco Geri, Mark Fowler, Matisse Enzer, Sam Kington, Takanori Kawai, Tom O'Sullivan.
5369: 
5370: Many thanks to Ron McKelvey, Ronzo Consulting for Siemens, who sponsored the development of the formula caching routines.
5371: 
5372: Many thanks to Cassens Transport who sponsored the development of the embedded charts and autofilters.
5373: 
5374: Additional thanks to Takanori Kawai for translating the documentation into Japanese.
5375: 
5376: Gunnar Wolf maintains the Debian distro.
5377: 
5378: Thanks to Damian Conway for the excellent Parse::RecDescent.
5379: 
5380: Thanks to Tim Jenness for File::Temp.
5381: 
5382: Thanks to Michael Meeks and Jody Goldberg for their work on Gnumeric.
5383: 
5384: 
5385: 
5386: 
5387: =head1 DISCLAIMER OF WARRANTY
5388: 
5389: 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.
5390: 
5391: 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.
5392: 
5393: 
5394: 
5395: 
5396: =head1 LICENSE
5397: 
5398: Either the Perl Artistic Licence L<http://dev.perl.org/licenses/artistic.html> or the GPL L<http://www.opensource.org/licenses/gpl-license.php>.
5399: 
5400: 
5401: 
5402: 
5403: =head1 AUTHOR
5404: 
5405: John McNamara jmcnamara@cpan.org
5406: 
5407:     The ashtray says
5408:     You were up all night.
5409:     When you went to bed
5410:     With your darkest mind.
5411:     Your pillow wept
5412:     And covered your eyes.
5413:     And you finally slept
5414:     While the sun caught fire.
5415: 
5416:     You've changed.
5417:       -- Jeff Tweedy
5418: 
5419: 
5420: 
5421: 
5422: =head1 COPYRIGHT
5423: 
5424: Copyright MM-MMXII, John McNamara.
5425: 
5426: All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.