rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | package 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 | |
| 18 | use strict; |
| 19 | use warnings; |
| 20 | |
| 21 | use Spreadsheet::ParseExcel; |
| 22 | use Spreadsheet::ParseExcel::SaveParser::Workbook; |
| 23 | use Spreadsheet::ParseExcel::SaveParser::Worksheet; |
| 24 | use Spreadsheet::WriteExcel; |
| 25 | use base 'Spreadsheet::ParseExcel'; |
| 26 | |
| 27 | our $VERSION = '0.59'; |
| 28 | |
| 29 | ############################################################################### |
| 30 | # |
| 31 | # new() |
| 32 | # |
| 33 | sub new { |
| 34 | |
| 35 | my ( $package, %params ) = @_; |
| 36 | $package->SUPER::new(%params); |
| 37 | } |
| 38 | |
| 39 | ############################################################################### |
| 40 | # |
| 41 | # Create() |
| 42 | # |
| 43 | sub 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 | # |
| 66 | sub 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 | # |
| 80 | sub SaveAs { |
| 81 | |
| 82 | my ( $self, $workbook, $filename ) = @_; |
| 83 | |
| 84 | $workbook->SaveAs($filename); |
| 85 | } |
| 86 | |
| 87 | 1; |
| 88 | |
| 89 | __END__ |
| 90 | |
| 91 | =head1 NAME |
| 92 | |
| 93 | Spreadsheet::ParseExcel::SaveParser - Rewrite an existing Excel file. |
| 94 | |
| 95 | =head1 SYNOPSIS |
| 96 | |
| 97 | |
| 98 | |
| 99 | Say 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 | |
| 111 | Then 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 | |
| 152 | We 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 | |
| 167 | The 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 | |
| 177 | Constructor. |
| 178 | |
| 179 | =head2 Parse() |
| 180 | |
| 181 | $workbook = $parse->Parse($sFileName); |
| 182 | |
| 183 | $workbook = $parse->Parse($sFileName , $formatter); |
| 184 | |
| 185 | Returns a L</Workbook> object. If an error occurs, returns undef. |
| 186 | |
| 187 | The optional C<$formatter> is a Formatter Class to format the value of cells. |
| 188 | |
| 189 | |
| 190 | =head1 Workbook |
| 191 | |
| 192 | The C<Parse()> method returns a C<Spreadsheet::ParseExcel::SaveParser::Workbook> object. |
| 193 | |
| 194 | This is a subclass of the L<Spreadsheet::ParseExcel::Workbook> and has the following methods: |
| 195 | |
| 196 | =head2 worksheets() |
| 197 | |
| 198 | Returns 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 | |
| 206 | The 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 | |
| 211 | Returns C<undef> if the sheet name or index doesn't exist. |
| 212 | |
| 213 | |
| 214 | =head2 AddWorksheet() |
| 215 | |
| 216 | $workbook = $workbook->AddWorksheet($name, %properties); |
| 217 | |
| 218 | Create a new Worksheet object of type C<Spreadsheet::ParseExcel::Worksheet>. |
| 219 | |
| 220 | The C<%properties> hash contains the properties of new Worksheet. |
| 221 | |
| 222 | |
| 223 | =head2 AddFont |
| 224 | |
| 225 | $workbook = $workbook->AddFont(%properties); |
| 226 | |
| 227 | Create new Font object of type C<Spreadsheet::ParseExcel::Font>. |
| 228 | |
| 229 | The C<%properties> hash contains the properties of new Font. |
| 230 | |
| 231 | |
| 232 | =head2 AddFormat |
| 233 | |
| 234 | $workbook = $workbook->AddFormat(%properties); |
| 235 | |
| 236 | The C<%properties> hash contains the properties of new Font. |
| 237 | |
| 238 | |
| 239 | =head1 Worksheet |
| 240 | |
| 241 | Spreadsheet::ParseExcel::SaveParser::Worksheet |
| 242 | |
| 243 | Worksheet is a subclass of Spreadsheet::ParseExcel::Worksheet. |
| 244 | And has these methods : |
| 245 | |
| 246 | |
| 247 | The C<Worksbook::worksheet()> method returns a C<Spreadsheet::ParseExcel::SaveParser::Worksheet> object. |
| 248 | |
| 249 | This 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 | |
| 256 | Create new Cell object of type C<Spreadsheet::ParseExcel::Cell>. |
| 257 | |
| 258 | The C<$format> parameter is the format number rather than a full format object. |
| 259 | |
| 260 | To specify just same as another cell, |
| 261 | you 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 | |
| 276 | Please note that this module is currently (versions 0.50-0.60) undergoing a major |
| 277 | restructuring and rewriting. |
| 278 | |
| 279 | =head1 Known Problems |
| 280 | |
| 281 | |
| 282 | You can only rewrite the features that Spreadsheet::WriteExcel supports so |
| 283 | macros, graphs and some other features in the original Excel file will be lost. |
| 284 | Also, formulas aren't rewritten, only the result of a formula is written. |
| 285 | |
| 286 | Only last print area will remain. (Others will be removed) |
| 287 | |
| 288 | |
| 289 | =head1 AUTHOR |
| 290 | |
| 291 | Maintainer 0.40+: John McNamara jmcnamara@cpan.org |
| 292 | |
| 293 | Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org |
| 294 | |
| 295 | Original author: Kawai Takanori kwitknr@cpan.org |
| 296 | |
| 297 | =head1 COPYRIGHT |
| 298 | |
| 299 | Copyright (c) 2009-2010 John McNamara |
| 300 | |
| 301 | Copyright (c) 2006-2008 Gabor Szabo |
| 302 | |
| 303 | Copyright (c) 2000-2002 Kawai Takanori and Nippon-RAD Co. OP Division |
| 304 | |
| 305 | All rights reserved. |
| 306 | |
| 307 | You 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 |