Workbook.pm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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::Workbook;
  29. ###############################################################################
  30. #
  31. # Spreadsheet::ParseExcel::Workbook - A class for Workbooks.
  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. our $VERSION = '0.57';
  46. ###############################################################################
  47. #
  48. # new()
  49. #
  50. # Constructor.
  51. #
  52. sub new {
  53. my ($class) = @_;
  54. my $self = {};
  55. bless $self, $class;
  56. }
  57. ###############################################################################
  58. #
  59. # worksheet()
  60. #
  61. # This method returns a single Worksheet object using either its name or index.
  62. #
  63. sub worksheet {
  64. my ( $oBook, $sName ) = @_;
  65. my $oWkS;
  66. foreach $oWkS ( @{ $oBook->{Worksheet} } ) {
  67. return $oWkS if ( $oWkS->{Name} eq $sName );
  68. }
  69. if ( $sName =~ /^\d+$/ ) {
  70. return $oBook->{Worksheet}->[$sName];
  71. }
  72. return undef;
  73. }
  74. ###############################################################################
  75. #
  76. # worksheets()
  77. #
  78. # Returns an array ofWorksheet objects.
  79. #
  80. sub worksheets {
  81. my $self = shift;
  82. return @{ $self->{Worksheet} };
  83. }
  84. ###############################################################################
  85. #
  86. # worksheet_count()
  87. #
  88. # Returns the number Woksheet objects in the Workbook.
  89. #
  90. sub worksheet_count {
  91. my $self = shift;
  92. return $self->{SheetCount};
  93. }
  94. ###############################################################################
  95. #
  96. # get_filename()
  97. #
  98. # Returns the name of the Excel file of C<undef> if the data was read from a filehandle rather than a file.
  99. #
  100. sub get_filename {
  101. my $self = shift;
  102. return $self->{File};
  103. }
  104. ###############################################################################
  105. #
  106. # get_print_areas()
  107. #
  108. # Returns an array ref of print areas.
  109. #
  110. # TODO. This should really be a Worksheet method.
  111. #
  112. sub get_print_areas {
  113. my $self = shift;
  114. return $self->{PrintArea};
  115. }
  116. ###############################################################################
  117. #
  118. # get_print_titles()
  119. #
  120. # Returns an array ref of print title hash refs.
  121. #
  122. # TODO. This should really be a Worksheet method.
  123. #
  124. sub get_print_titles {
  125. my $self = shift;
  126. return $self->{PrintTitle};
  127. }
  128. ###############################################################################
  129. #
  130. # using_1904_date()
  131. #
  132. # Returns true if the Excel file is using the 1904 date epoch.
  133. #
  134. sub using_1904_date {
  135. my $self = shift;
  136. return $self->{Flg1904};
  137. }
  138. ###############################################################################
  139. #
  140. # ParseAbort()
  141. #
  142. # Todo
  143. #
  144. sub ParseAbort {
  145. my ( $self, $val ) = @_;
  146. $self->{_ParseAbort} = $val;
  147. }
  148. ###############################################################################
  149. #
  150. # Parse(). Deprecated.
  151. #
  152. # Syntactic wrapper around Spreadsheet::ParseExcel::Parse().
  153. # This method is *deprecated* since it doesn't conform to the the current
  154. # error handling in the S::PE Parse() method.
  155. #
  156. sub Parse {
  157. my ( $class, $source, $formatter ) = @_;
  158. my $excel = Spreadsheet::ParseExcel->new();
  159. my $workbook = $excel->Parse( $source, $formatter );
  160. $workbook->{_Excel} = $excel;
  161. return $workbook;
  162. }
  163. ###############################################################################
  164. #
  165. # Mapping between legacy method names and new names.
  166. #
  167. {
  168. no warnings; # Ignore warnings about variables used only once.
  169. *Worksheet = *worksheet;
  170. }
  171. 1;
  172. __END__
  173. =pod
  174. =head1 NAME
  175. Spreadsheet::ParseExcel::Workbook - A class for Workbooks.
  176. =head1 SYNOPSIS
  177. See the documentation for Spreadsheet::ParseExcel.
  178. =head1 DESCRIPTION
  179. This module is used in conjunction with Spreadsheet::ParseExcel. See the documentation for L<Spreadsheet::ParseExcel>.
  180. =head1 Methods
  181. The following Workbook methods are available:
  182. $workbook->worksheets()
  183. $workbook->worksheet()
  184. $workbook->worksheet_count()
  185. $workbook->get_filename()
  186. $workbook->get_print_areas()
  187. $workbook->get_print_titles()
  188. $workbook->using_1904_date()
  189. =head2 worksheets()
  190. The C<worksheets()> method returns an array of Worksheet objects. This was most commonly used to iterate over the worksheets in a workbook:
  191. for my $worksheet ( $workbook->worksheets() ) {
  192. ...
  193. }
  194. =head2 worksheet()
  195. The C<worksheet()> method returns a single C<Worksheet> object using either its name or index:
  196. $worksheet = $workbook->worksheet('Sheet1');
  197. $worksheet = $workbook->worksheet(0);
  198. Returns C<undef> if the sheet name or index doesn't exist.
  199. =head2 worksheet_count()
  200. The C<worksheet_count()> method returns the number of Woksheet objects in the Workbook.
  201. my $worksheet_count = $workbook->worksheet_count();
  202. =head2 get_filename()
  203. The C<get_filename()> method returns the name of the Excel file of C<undef> if the data was read from a filehandle rather than a file.
  204. my $filename = $workbook->get_filename();
  205. =head2 get_print_areas()
  206. The C<get_print_areas()> method returns an array ref of print areas.
  207. my $print_areas = $workbook->get_print_areas();
  208. Each print area is as follows:
  209. [ $start_row, $start_col, $end_row, $end_col ]
  210. Returns undef if there are no print areas.
  211. =head2 get_print_titles()
  212. The C<get_print_titles()> method returns an array ref of print title hash refs.
  213. my $print_titles = $workbook->get_print_titles();
  214. Each print title array ref is as follows:
  215. {
  216. Row => [ $start_row, $end_row ],
  217. Column => [ $start_col, $end_col ],
  218. }
  219. Returns undef if there are no print titles.
  220. =head2 using_1904_date()
  221. The C<using_1904_date()> method returns true if the Excel file is using the 1904 date epoch instead of the 1900 epoch.
  222. my $using_1904_date = $workbook->using_1904_date();
  223. The Windows version of Excel generally uses the 1900 epoch while the Mac version of Excel generally uses the 1904 epoch.
  224. Returns 0 if the 1900 epoch is in use.
  225. =head1 AUTHOR
  226. Maintainer 0.40+: John McNamara jmcnamara@cpan.org
  227. Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
  228. Original author: Kawai Takanori kwitknr@cpan.org
  229. =head1 COPYRIGHT
  230. Copyright (c) 2009-2010 John McNamara
  231. Copyright (c) 2006-2008 Gabor Szabo
  232. Copyright (c) 2000-2006 Kawai Takanori
  233. All rights reserved.
  234. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
  235. =cut