SaveParser.pm 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. # DISCLAIMER OF WARRANTY
  2. # Because this software is licensed free of charge, there is no warranty for the software,
  3. # to the extent permitted by applicable law. Except when otherwise stated in writing
  4. # the copyright holders and/or other parties provide the software "as is" without
  5. # warranty of any kind, either expressed or implied, including, but not limited to,
  6. # the implied warranties of merchantability and fitness for a particular purpose.
  7. # The entire risk as to the quality and performance of the software is with you.
  8. # Should the software prove defective, you assume the cost of all necessary
  9. # servicing, repair, or correction.
  10. # In no event unless required by applicable law or agreed to in writing will any
  11. # copyright holder, or any other party who may modify and/or redistribute the software
  12. # as permitted by the above licence, be liable to you for damages, including any general,
  13. # special, incidental, or consequential damages arising out of the use or inability
  14. # to use the software (including but not limited to loss of data or data being rendered
  15. # inaccurate or losses sustained by you or third parties or a failure of the software
  16. # to operate with any other software), even if such holder or other party
  17. # has been advised of the possibility of such damages.
  18. # AUTHOR
  19. # Current maintainer 0.40+: John McNamara jmcnamara@cpan.org
  20. # Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
  21. # Original author: Kawai Takanori (Hippo2000) kwitknr@cpan.org
  22. # COPYRIGHT
  23. # Copyright (c) 2009-2010 John McNamara
  24. # Copyright (c) 2006-2008 Gabor Szabo
  25. # Copyright (c) 2000-2006 Kawai Takanori
  26. # All rights reserved. This is free software. You may distribute under the terms of
  27. # the Artistic License(full text of the Artistic License http://dev.perl.org/licenses/artistic.html).
  28. package Spreadsheet::ParseExcel::SaveParser;
  29. ###############################################################################
  30. #
  31. # Spreadsheet::ParseExcel::SaveParser - Rewrite an existing Excel file.
  32. #
  33. # Used in conjunction with Spreadsheet::ParseExcel.
  34. #
  35. # Copyright (c) 2009 John McNamara
  36. # Copyright (c) 2006-2008 Gabor Szabo
  37. # Copyright (c) 2000-2006 Kawai Takanori
  38. #
  39. # perltidy with standard settings.
  40. #
  41. # Documentation after __END__
  42. #
  43. use strict;
  44. use warnings;
  45. use Spreadsheet::ParseExcel;
  46. use Spreadsheet::ParseExcel::SaveParser::Workbook;
  47. use Spreadsheet::ParseExcel::SaveParser::Worksheet;
  48. use Spreadsheet::WriteExcel;
  49. use base 'Spreadsheet::ParseExcel';
  50. our $VERSION = '0.57';
  51. ###############################################################################
  52. #
  53. # new()
  54. #
  55. sub new {
  56. my ( $package, %params ) = @_;
  57. $package->SUPER::new(%params);
  58. }
  59. ###############################################################################
  60. #
  61. # Create()
  62. #
  63. sub Create {
  64. my ( $self, $formatter ) = @_;
  65. #0. New $workbook
  66. my $workbook = Spreadsheet::ParseExcel::Workbook->new();
  67. $workbook->{SheetCount} = 0;
  68. # User specified formater class.
  69. if ($formatter) {
  70. $workbook->{FmtClass} = $formatter;
  71. }
  72. else {
  73. $workbook->{FmtClass} = Spreadsheet::ParseExcel::FmtDefault->new();
  74. }
  75. return Spreadsheet::ParseExcel::SaveParser::Workbook->new($workbook);
  76. }
  77. ###############################################################################
  78. #
  79. # Parse()
  80. #
  81. sub Parse {
  82. my ( $self, $sFile, $formatter ) = @_;
  83. my $workbook = $self->SUPER::Parse( $sFile, $formatter );
  84. return undef unless defined $workbook;
  85. return Spreadsheet::ParseExcel::SaveParser::Workbook->new($workbook);
  86. }
  87. ###############################################################################
  88. #
  89. # SaveAs()
  90. #
  91. sub SaveAs {
  92. my ( $self, $workbook, $filename ) = @_;
  93. $workbook->SaveAs($filename);
  94. }
  95. 1;
  96. __END__
  97. =head1 NAME
  98. Spreadsheet::ParseExcel::SaveParser - Rewrite an existing Excel file.
  99. =head1 SYNOPSIS
  100. Say we start with an Excel file that looks like this:
  101. -----------------------------------------------------
  102. | | A | B | C |
  103. -----------------------------------------------------
  104. | 1 | Hello | ... | ... | ...
  105. | 2 | World | ... | ... | ...
  106. | 3 | *Bold text* | ... | ... | ...
  107. | 4 | ... | ... | ... | ...
  108. | 5 | ... | ... | ... | ...
  109. Then we process it with the following program:
  110. #!/usr/bin/perl
  111. use strict;
  112. use warnings;
  113. use Spreadsheet::ParseExcel;
  114. use Spreadsheet::ParseExcel::SaveParser;
  115. # Open an existing file with SaveParser
  116. my $parser = Spreadsheet::ParseExcel::SaveParser->new();
  117. my $template = $parser->Parse('template.xls');
  118. # Get the first worksheet.
  119. my $worksheet = $template->worksheet(0);
  120. my $row = 0;
  121. my $col = 0;
  122. # Overwrite the string in cell A1
  123. $worksheet->AddCell( $row, $col, 'New string' );
  124. # Add a new string in cell B1
  125. $worksheet->AddCell( $row, $col + 1, 'Newer' );
  126. # Add a new string in cell C1 with the format from cell A3.
  127. my $cell = $worksheet->get_cell( $row + 2, $col );
  128. my $format_number = $cell->{FormatNo};
  129. $worksheet->AddCell( $row, $col + 2, 'Newest', $format_number );
  130. # Write over the existing file or write a new file.
  131. $template->SaveAs('newfile.xls');
  132. We should now have an Excel file that looks like this:
  133. -----------------------------------------------------
  134. | | A | B | C |
  135. -----------------------------------------------------
  136. | 1 | New string | Newer | *Newest* | ...
  137. | 2 | World | ... | ... | ...
  138. | 3 | *Bold text* | ... | ... | ...
  139. | 4 | ... | ... | ... | ...
  140. | 5 | ... | ... | ... | ...
  141. =head1 DESCRIPTION
  142. 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>.
  143. =head1 METHODS
  144. =head1 Parser
  145. =head2 new()
  146. $parse = new Spreadsheet::ParseExcel::SaveParser();
  147. Constructor.
  148. =head2 Parse()
  149. $workbook = $parse->Parse($sFileName);
  150. $workbook = $parse->Parse($sFileName , $formatter);
  151. Returns a L</Workbook> object. If an error occurs, returns undef.
  152. The optional C<$formatter> is a Formatter Class to format the value of cells.
  153. =head1 Workbook
  154. The C<Parse()> method returns a C<Spreadsheet::ParseExcel::SaveParser::Workbook> object.
  155. This is a subclass of the L<Spreadsheet::ParseExcel::Workbook> and has the following methods:
  156. =head2 worksheets()
  157. Returns an array of L</Worksheet> objects. This was most commonly used to iterate over the worksheets in a workbook:
  158. for my $worksheet ( $workbook->worksheets() ) {
  159. ...
  160. }
  161. =head2 worksheet()
  162. The C<worksheet()> method returns a single C<Worksheet> object using either its name or index:
  163. $worksheet = $workbook->worksheet('Sheet1');
  164. $worksheet = $workbook->worksheet(0);
  165. Returns C<undef> if the sheet name or index doesn't exist.
  166. =head2 AddWorksheet()
  167. $workbook = $workbook->AddWorksheet($name, %properties);
  168. Create a new Worksheet object of type C<Spreadsheet::ParseExcel::Worksheet>.
  169. The C<%properties> hash contains the properties of new Worksheet.
  170. =head2 AddFont
  171. $workbook = $workbook->AddFont(%properties);
  172. Create new Font object of type C<Spreadsheet::ParseExcel::Font>.
  173. The C<%properties> hash contains the properties of new Font.
  174. =head2 AddFormat
  175. $workbook = $workbook->AddFormat(%properties);
  176. The C<%properties> hash contains the properties of new Font.
  177. =head1 Worksheet
  178. Spreadsheet::ParseExcel::SaveParser::Worksheet
  179. Worksheet is a subclass of Spreadsheet::ParseExcel::Worksheet.
  180. And has these methods :
  181. The C<Worksbook::worksheet()> method returns a C<Spreadsheet::ParseExcel::SaveParser::Worksheet> object.
  182. This is a subclass of the L<Spreadsheet::ParseExcel::Worksheet> and has the following methods:
  183. =head1 AddCell
  184. $workbook = $worksheet->AddCell($row, $col, $value, $format [$encoding]);
  185. Create new Cell object of type C<Spreadsheet::ParseExcel::Cell>.
  186. The C<$format> parameter is the format number rather than a full format object.
  187. To specify just same as another cell,
  188. you can set it like below:
  189. $row = 0;
  190. $col = 0;
  191. $worksheet = $template->worksheet(0);
  192. $cell = $worksheet->get_cell( $row, $col );
  193. $format_number = $cell->{FormatNo};
  194. $worksheet->AddCell($row +1, $coll, 'New data', $format_number);
  195. =head1 TODO
  196. Please note that this module is currently (versions 0.50-0.60) undergoing a major
  197. restructuring and rewriting.
  198. =head1 Known Problems
  199. You can only rewrite the features that Spreadsheet::WriteExcel supports so
  200. macros, graphs and some other features in the original Excel file will be lost.
  201. Also, formulas aren't rewritten, only the result of a formula is written.
  202. Only last print area will remain. (Others will be removed)
  203. =head1 AUTHOR
  204. Maintainer 0.40+: John McNamara jmcnamara@cpan.org
  205. Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
  206. Original author: Kawai Takanori kwitknr@cpan.org
  207. =head1 COPYRIGHT
  208. Copyright (c) 2009-2010 John McNamara
  209. Copyright (c) 2006-2008 Gabor Szabo
  210. Copyright (c) 2000-2002 Kawai Takanori and Nippon-RAD Co. OP Division
  211. All rights reserved.
  212. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
  213. =cut