1#!/usr/bin/perl -w
2
3###############################################################################
4#
5# Example of how to add a user defined data handler to the Spreadsheet::
6# WriteExcelXML write() method.
7#
8# The following example shows how to add a handler for a 7 digit ID number.
9#
10#
11# reverse('�'), September 2004, John McNamara, jmcnamara@cpan.org
12#
13
14use strict;
15use Spreadsheet::WriteExcelXML;
16
17
18my $workbook    = Spreadsheet::WriteExcelXML->new("write_handler1.xls");
19my $worksheet   = $workbook->add_worksheet();
20
21
22###############################################################################
23#
24# Add a handler for 7 digit id numbers. This is useful when you want a string
25# such as 0000001 written as a string instead of a number and thus preserve
26# the leading zeroes.
27#
28# Note: you can get the same effect using the keep_leading_zeros() method but
29# this serves as a simple example.
30#
31$worksheet->add_write_handler(qr[^\d{7}$], \&write_my_id);
32
33
34###############################################################################
35#
36# The following function processes the data when a match is found.
37#
38sub write_my_id {
39
40    my $worksheet = shift;
41
42    return $worksheet->write_string(@_);
43}
44
45
46# This format maintains the cell as text even if it is edited.
47my $id_format   = $workbook->add_format(num_format => '@');
48
49
50# Write some numbers in the user defined format
51$worksheet->write('A1', '0000000', $id_format);
52$worksheet->write('A2', '0000001', $id_format);
53$worksheet->write('A3', '0004000', $id_format);
54$worksheet->write('A4', '1234567', $id_format);
55
56# Write some numbers that don't match the defined format
57$worksheet->write('A6', '000000',  $id_format);
58$worksheet->write('A7', '000001',  $id_format);
59$worksheet->write('A8', '004000',  $id_format);
60$worksheet->write('A9', '123456',  $id_format);
61
62
63__END__
64
65