blob: 3d2cf9c54b41603b15aa6ca04a97c10d8320902f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001package Spreadsheet::ParseExcel::SaveParser;
2
3###############################################################################
4#
5# Spreadsheet::ParseExcel::SaveParser - Rewrite an existing Excel file.
6#
7# Used in conjunction with Spreadsheet::ParseExcel.
8#
9# Copyright (c) 2009 John McNamara
10# Copyright (c) 2006-2008 Gabor Szabo
11# Copyright (c) 2000-2006 Kawai Takanori
12#
13# perltidy with standard settings.
14#
15# Documentation after __END__
16#
17
18use strict;
19use warnings;
20
21use Spreadsheet::ParseExcel;
22use Spreadsheet::ParseExcel::SaveParser::Workbook;
23use Spreadsheet::ParseExcel::SaveParser::Worksheet;
24use Spreadsheet::WriteExcel;
25use base 'Spreadsheet::ParseExcel';
26
27our $VERSION = '0.59';
28
29###############################################################################
30#
31# new()
32#
33sub new {
34
35 my ( $package, %params ) = @_;
36 $package->SUPER::new(%params);
37}
38
39###############################################################################
40#
41# Create()
42#
43sub Create {
44
45 my ( $self, $formatter ) = @_;
46
47 #0. New $workbook
48 my $workbook = Spreadsheet::ParseExcel::Workbook->new();
49 $workbook->{SheetCount} = 0;
50
51 # User specified formater class.
52 if ($formatter) {
53 $workbook->{FmtClass} = $formatter;
54 }
55 else {
56 $workbook->{FmtClass} = Spreadsheet::ParseExcel::FmtDefault->new();
57 }
58
59 return Spreadsheet::ParseExcel::SaveParser::Workbook->new($workbook);
60}
61
62###############################################################################
63#
64# Parse()
65#
66sub Parse {
67
68 my ( $self, $sFile, $formatter ) = @_;
69
70 my $workbook = $self->SUPER::Parse( $sFile, $formatter );
71
72 return undef unless defined $workbook;
73 return Spreadsheet::ParseExcel::SaveParser::Workbook->new($workbook);
74}
75
76###############################################################################
77#
78# SaveAs()
79#
80sub SaveAs {
81
82 my ( $self, $workbook, $filename ) = @_;
83
84 $workbook->SaveAs($filename);
85}
86
871;
88
89__END__
90
91=head1 NAME
92
93Spreadsheet::ParseExcel::SaveParser - Rewrite an existing Excel file.
94
95=head1 SYNOPSIS
96
97
98
99Say we start with an Excel file that looks like this:
100
101 -----------------------------------------------------
102 | | A | B | C |
103 -----------------------------------------------------
104 | 1 | Hello | ... | ... | ...
105 | 2 | World | ... | ... | ...
106 | 3 | *Bold text* | ... | ... | ...
107 | 4 | ... | ... | ... | ...
108 | 5 | ... | ... | ... | ...
109
110
111Then we process it with the following program:
112
113 #!/usr/bin/perl
114
115 use strict;
116 use warnings;
117
118 use Spreadsheet::ParseExcel;
119 use Spreadsheet::ParseExcel::SaveParser;
120
121
122 # Open an existing file with SaveParser
123 my $parser = Spreadsheet::ParseExcel::SaveParser->new();
124 my $template = $parser->Parse('template.xls');
125
126
127 # Get the first worksheet.
128 my $worksheet = $template->worksheet(0);
129 my $row = 0;
130 my $col = 0;
131
132
133 # Overwrite the string in cell A1
134 $worksheet->AddCell( $row, $col, 'New string' );
135
136
137 # Add a new string in cell B1
138 $worksheet->AddCell( $row, $col + 1, 'Newer' );
139
140
141 # Add a new string in cell C1 with the format from cell A3.
142 my $cell = $worksheet->get_cell( $row + 2, $col );
143 my $format_number = $cell->{FormatNo};
144
145 $worksheet->AddCell( $row, $col + 2, 'Newest', $format_number );
146
147
148 # Write over the existing file or write a new file.
149 $template->SaveAs('newfile.xls');
150
151
152We should now have an Excel file that looks like this:
153
154 -----------------------------------------------------
155 | | A | B | C |
156 -----------------------------------------------------
157 | 1 | New string | Newer | *Newest* | ...
158 | 2 | World | ... | ... | ...
159 | 3 | *Bold text* | ... | ... | ...
160 | 4 | ... | ... | ... | ...
161 | 5 | ... | ... | ... | ...
162
163
164
165=head1 DESCRIPTION
166
167The C<Spreadsheet::ParseExcel::SaveParser> module rewrite an existing Excel file by reading it with C<Spreadsheet::ParseExcel> and rewriting it with C<Spreadsheet::WriteExcel>.
168
169=head1 METHODS
170
171=head1 Parser
172
173=head2 new()
174
175 $parse = new Spreadsheet::ParseExcel::SaveParser();
176
177Constructor.
178
179=head2 Parse()
180
181 $workbook = $parse->Parse($sFileName);
182
183 $workbook = $parse->Parse($sFileName , $formatter);
184
185Returns a L</Workbook> object. If an error occurs, returns undef.
186
187The optional C<$formatter> is a Formatter Class to format the value of cells.
188
189
190=head1 Workbook
191
192The C<Parse()> method returns a C<Spreadsheet::ParseExcel::SaveParser::Workbook> object.
193
194This is a subclass of the L<Spreadsheet::ParseExcel::Workbook> and has the following methods:
195
196=head2 worksheets()
197
198Returns an array of L</Worksheet> objects. This was most commonly used to iterate over the worksheets in a workbook:
199
200 for my $worksheet ( $workbook->worksheets() ) {
201 ...
202 }
203
204=head2 worksheet()
205
206The C<worksheet()> method returns a single C<Worksheet> object using either its name or index:
207
208 $worksheet = $workbook->worksheet('Sheet1');
209 $worksheet = $workbook->worksheet(0);
210
211Returns C<undef> if the sheet name or index doesn't exist.
212
213
214=head2 AddWorksheet()
215
216 $workbook = $workbook->AddWorksheet($name, %properties);
217
218Create a new Worksheet object of type C<Spreadsheet::ParseExcel::Worksheet>.
219
220The C<%properties> hash contains the properties of new Worksheet.
221
222
223=head2 AddFont
224
225 $workbook = $workbook->AddFont(%properties);
226
227Create new Font object of type C<Spreadsheet::ParseExcel::Font>.
228
229The C<%properties> hash contains the properties of new Font.
230
231
232=head2 AddFormat
233
234 $workbook = $workbook->AddFormat(%properties);
235
236The C<%properties> hash contains the properties of new Font.
237
238
239=head1 Worksheet
240
241Spreadsheet::ParseExcel::SaveParser::Worksheet
242
243Worksheet is a subclass of Spreadsheet::ParseExcel::Worksheet.
244And has these methods :
245
246
247The C<Worksbook::worksheet()> method returns a C<Spreadsheet::ParseExcel::SaveParser::Worksheet> object.
248
249This is a subclass of the L<Spreadsheet::ParseExcel::Worksheet> and has the following methods:
250
251
252=head1 AddCell
253
254 $workbook = $worksheet->AddCell($row, $col, $value, $format [$encoding]);
255
256Create new Cell object of type C<Spreadsheet::ParseExcel::Cell>.
257
258The C<$format> parameter is the format number rather than a full format object.
259
260To specify just same as another cell,
261you can set it like below:
262
263 $row = 0;
264 $col = 0;
265 $worksheet = $template->worksheet(0);
266 $cell = $worksheet->get_cell( $row, $col );
267 $format_number = $cell->{FormatNo};
268
269 $worksheet->AddCell($row +1, $coll, 'New data', $format_number);
270
271
272
273
274=head1 TODO
275
276Please note that this module is currently (versions 0.50-0.60) undergoing a major
277restructuring and rewriting.
278
279=head1 Known Problems
280
281
282You can only rewrite the features that Spreadsheet::WriteExcel supports so
283macros, graphs and some other features in the original Excel file will be lost.
284Also, formulas aren't rewritten, only the result of a formula is written.
285
286Only last print area will remain. (Others will be removed)
287
288
289=head1 AUTHOR
290
291Maintainer 0.40+: John McNamara jmcnamara@cpan.org
292
293Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
294
295Original author: Kawai Takanori kwitknr@cpan.org
296
297=head1 COPYRIGHT
298
299Copyright (c) 2009-2010 John McNamara
300
301Copyright (c) 2006-2008 Gabor Szabo
302
303Copyright (c) 2000-2002 Kawai Takanori and Nippon-RAD Co. OP Division
304
305All rights reserved.
306
307You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
308
309
310=cut