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