blob: b9d32d7d6cc60e7917a980697f40fa5dd17fa6ed [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001package Spreadsheet::ParseExcel::Worksheet;
2
3###############################################################################
4#
5# Spreadsheet::ParseExcel::Worksheet - A class for Worksheets.
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;
20use Scalar::Util qw(weaken);
21
22our $VERSION = '0.59';
23
24###############################################################################
25#
26# new()
27#
28sub new {
29
30 my ( $class, %properties ) = @_;
31
32 my $self = \%properties;
33
34 weaken $self->{_Book};
35
36 $self->{Cells} = undef;
37 $self->{DefColWidth} = 8.43;
38
39 return bless $self, $class;
40}
41
42###############################################################################
43#
44# get_cell( $row, $col )
45#
46# Returns the Cell object at row $row and column $col, if defined.
47#
48sub get_cell {
49
50 my ( $self, $row, $col ) = @_;
51
52 if ( !defined $row
53 || !defined $col
54 || !defined $self->{MaxRow}
55 || !defined $self->{MaxCol} )
56 {
57
58 # Return undef if no arguments are given or if no cells are defined.
59 return undef;
60 }
61 elsif ($row < $self->{MinRow}
62 || $row > $self->{MaxRow}
63 || $col < $self->{MinCol}
64 || $col > $self->{MaxCol} )
65 {
66
67 # Return undef if outside allowable row/col range.
68 return undef;
69 }
70 else {
71
72 # Return the Cell object.
73 return $self->{Cells}->[$row]->[$col];
74 }
75}
76
77###############################################################################
78#
79# row_range()
80#
81# Returns a two-element list ($min, $max) containing the minimum and maximum
82# defined rows in the worksheet.
83#
84# If there is no row defined $max is smaller than $min.
85#
86sub row_range {
87
88 my $self = shift;
89
90 my $min = $self->{MinRow} || 0;
91 my $max = defined( $self->{MaxRow} ) ? $self->{MaxRow} : ( $min - 1 );
92
93 return ( $min, $max );
94}
95
96###############################################################################
97#
98# col_range()
99#
100# Returns a two-element list ($min, $max) containing the minimum and maximum
101# defined cols in the worksheet.
102#
103# If there is no column defined $max is smaller than $min.
104#
105sub col_range {
106
107 my $self = shift;
108
109 my $min = $self->{MinCol} || 0;
110 my $max = defined( $self->{MaxCol} ) ? $self->{MaxCol} : ( $min - 1 );
111
112 return ( $min, $max );
113}
114
115###############################################################################
116#
117# get_name()
118#
119# Returns the name of the worksheet.
120#
121sub get_name {
122
123 my $self = shift;
124
125 return $self->{Name};
126}
127
128###############################################################################
129#
130# sheet_num()
131#
132sub sheet_num {
133
134 my $self = shift;
135
136 return $self->{_SheetNo};
137}
138
139###############################################################################
140#
141# get_h_pagebreaks()
142#
143# Returns an array ref of row numbers where a horizontal page break occurs.
144#
145sub get_h_pagebreaks {
146
147 my $self = shift;
148
149 return $self->{HPageBreak};
150}
151
152###############################################################################
153#
154# get_v_pagebreaks()
155#
156# Returns an array ref of column numbers where a vertical page break occurs.
157#
158sub get_v_pagebreaks {
159
160 my $self = shift;
161
162 return $self->{VPageBreak};
163}
164
165###############################################################################
166#
167# get_merged_areas()
168#
169# Returns an array ref of cells that are merged.
170#
171sub get_merged_areas {
172
173 my $self = shift;
174
175 return $self->{MergedArea};
176}
177
178###############################################################################
179#
180# get_row_heights()
181#
182# Returns an array_ref of row heights.
183#
184sub get_row_heights {
185
186 my $self = shift;
187
188 return @{ $self->{RowHeight} };
189}
190
191###############################################################################
192#
193# get_col_widths()
194#
195# Returns an array_ref of column widths.
196#
197sub get_col_widths {
198
199 my $self = shift;
200
201 return @{ $self->{ColWidth} };
202}
203
204###############################################################################
205#
206# get_default_row_height()
207#
208# Returns the default row height for the worksheet. Generally 12.75.
209#
210sub get_default_row_height {
211
212 my $self = shift;
213
214 return $self->{DefRowHeight};
215}
216
217###############################################################################
218#
219# get_default_col_width()
220#
221# Returns the default column width for the worksheet. Generally 8.43.
222#
223sub get_default_col_width {
224
225 my $self = shift;
226
227 return $self->{DefColWidth};
228}
229
230###############################################################################
231#
232# _get_row_properties()
233#
234# Returns an array_ref of row properties.
235# TODO. This is a placeholder for a future method.
236#
237sub _get_row_properties {
238
239 my $self = shift;
240
241 return $self->{RowProperties};
242}
243
244###############################################################################
245#
246# _get_col_properties()
247#
248# Returns an array_ref of column properties.
249# TODO. This is a placeholder for a future method.
250#
251sub _get_col_properties {
252
253 my $self = shift;
254
255 return $self->{ColProperties};
256}
257
258###############################################################################
259#
260# get_header()
261#
262# Returns the worksheet header string.
263#
264sub get_header {
265
266 my $self = shift;
267
268 return $self->{Header};
269}
270
271###############################################################################
272#
273# get_footer()
274#
275# Returns the worksheet footer string.
276#
277sub get_footer {
278
279 my $self = shift;
280
281 return $self->{Footer};
282}
283
284###############################################################################
285#
286# get_margin_left()
287#
288# Returns the left margin of the worksheet in inches.
289#
290sub get_margin_left {
291
292 my $self = shift;
293
294 return $self->{LeftMargin};
295}
296
297###############################################################################
298#
299# get_margin_right()
300#
301# Returns the right margin of the worksheet in inches.
302#
303sub get_margin_right {
304
305 my $self = shift;
306
307 return $self->{RightMargin};
308}
309
310###############################################################################
311#
312# get_margin_top()
313#
314# Returns the top margin of the worksheet in inches.
315#
316sub get_margin_top {
317
318 my $self = shift;
319
320 return $self->{TopMargin};
321}
322
323###############################################################################
324#
325# get_margin_bottom()
326#
327# Returns the bottom margin of the worksheet in inches.
328#
329sub get_margin_bottom {
330
331 my $self = shift;
332
333 return $self->{BottomMargin};
334}
335
336###############################################################################
337#
338# get_margin_header()
339#
340# Returns the header margin of the worksheet in inches.
341#
342sub get_margin_header {
343
344 my $self = shift;
345
346 return $self->{HeaderMargin};
347}
348
349###############################################################################
350#
351# get_margin_footer()
352#
353# Returns the footer margin of the worksheet in inches.
354#
355sub get_margin_footer {
356
357 my $self = shift;
358
359 return $self->{FooterMargin};
360}
361
362###############################################################################
363#
364# get_paper()
365#
366# Returns the printer paper size.
367#
368sub get_paper {
369
370 my $self = shift;
371
372 return $self->{PaperSize};
373}
374
375###############################################################################
376#
377# get_start_page()
378#
379# Returns the page number that printing will start from.
380#
381sub get_start_page {
382
383 my $self = shift;
384
385 # Only return the page number if the "First page number" option is set.
386 if ( $self->{UsePage} ) {
387 return $self->{PageStart};
388 }
389 else {
390 return 0;
391 }
392}
393
394###############################################################################
395#
396# get_print_order()
397#
398# Returns the Worksheet page printing order.
399#
400sub get_print_order {
401
402 my $self = shift;
403
404 return $self->{LeftToRight};
405}
406
407###############################################################################
408#
409# get_print_scale()
410#
411# Returns the workbook scale for printing.
412#
413sub get_print_scale {
414
415 my $self = shift;
416
417 return $self->{Scale};
418}
419
420###############################################################################
421#
422# get_fit_to_pages()
423#
424# Returns the number of pages wide and high that the printed worksheet page
425# will fit to.
426#
427sub get_fit_to_pages {
428
429 my $self = shift;
430
431 if ( !$self->{PageFit} ) {
432 return ( 0, 0 );
433 }
434 else {
435 return ( $self->{FitWidth}, $self->{FitHeight} );
436 }
437}
438
439###############################################################################
440#
441# is_portrait()
442#
443# Returns true if the worksheet has been set for printing in portrait mode.
444#
445sub is_portrait {
446
447 my $self = shift;
448
449 return $self->{Landscape};
450}
451
452###############################################################################
453#
454# is_centered_horizontally()
455#
456# Returns true if the worksheet has been centered horizontally for printing.
457#
458sub is_centered_horizontally {
459
460 my $self = shift;
461
462 return $self->{HCenter};
463}
464
465###############################################################################
466#
467# is_centered_vertically()
468#
469# Returns true if the worksheet has been centered vertically for printing.
470#
471sub is_centered_vertically {
472
473 my $self = shift;
474
475 return $self->{HCenter};
476}
477
478###############################################################################
479#
480# is_print_gridlines()
481#
482# Returns true if the worksheet print "gridlines" option is turned on.
483#
484sub is_print_gridlines {
485
486 my $self = shift;
487
488 return $self->{PrintGrid};
489}
490
491###############################################################################
492#
493# is_print_row_col_headers()
494#
495# Returns true if the worksheet print "row and column headings" option is on.
496#
497sub is_print_row_col_headers {
498
499 my $self = shift;
500
501 return $self->{PrintHeaders};
502}
503
504###############################################################################
505#
506# is_print_black_and_white()
507#
508# Returns true if the worksheet print "black and white" option is turned on.
509#
510sub is_print_black_and_white {
511
512 my $self = shift;
513
514 return $self->{NoColor};
515}
516
517###############################################################################
518#
519# is_print_draft()
520#
521# Returns true if the worksheet print "draft" option is turned on.
522#
523sub is_print_draft {
524
525 my $self = shift;
526
527 return $self->{Draft};
528}
529
530###############################################################################
531#
532# is_print_comments()
533#
534# Returns true if the worksheet print "comments" option is turned on.
535#
536sub is_print_comments {
537
538 my $self = shift;
539
540 return $self->{Notes};
541}
542
543###############################################################################
544#
545# Mapping between legacy method names and new names.
546#
547{
548 no warnings; # Ignore warnings about variables used only once.
549 *sheetNo = *sheet_num;
550 *Cell = *get_cell;
551 *RowRange = *row_range;
552 *ColRange = *col_range;
553}
554
5551;
556
557__END__
558
559=pod
560
561=head1 NAME
562
563Spreadsheet::ParseExcel::Worksheet - A class for Worksheets.
564
565=head1 SYNOPSIS
566
567See the documentation for L<Spreadsheet::ParseExcel>.
568
569=head1 DESCRIPTION
570
571This module is used in conjunction with Spreadsheet::ParseExcel. See the documentation for Spreadsheet::ParseExcel.
572
573=head1 Methods
574
575The C<Spreadsheet::ParseExcel::Worksheet> class encapsulates the properties of an Excel worksheet. It has the following methods:
576
577 $worksheet->get_cell()
578 $worksheet->row_range()
579 $worksheet->col_range()
580 $worksheet->get_name()
581 $worksheet->get_h_pagebreaks()
582 $worksheet->get_v_pagebreaks()
583 $worksheet->get_merged_areas()
584 $worksheet->get_row_heights()
585 $worksheet->get_col_widths()
586 $worksheet->get_default_row_height()
587 $worksheet->get_default_col_width()
588 $worksheet->get_header()
589 $worksheet->get_footer()
590 $worksheet->get_margin_left()
591 $worksheet->get_margin_right()
592 $worksheet->get_margin_top()
593 $worksheet->get_margin_bottom()
594 $worksheet->get_margin_header()
595 $worksheet->get_margin_footer()
596 $worksheet->get_paper()
597 $worksheet->get_start_page()
598 $worksheet->get_print_order()
599 $worksheet->get_print_scale()
600 $worksheet->get_fit_to_pages()
601 $worksheet->is_portrait()
602 $worksheet->is_centered_horizontally()
603 $worksheet->is_centered_vertically()
604 $worksheet->is_print_gridlines()
605 $worksheet->is_print_row_col_headers()
606 $worksheet->is_print_black_and_white()
607 $worksheet->is_print_draft()
608 $worksheet->is_print_comments()
609
610
611=head2 get_cell($row, $col)
612
613Return the L</Cell> object at row C<$row> and column C<$col> if it is defined. Otherwise returns undef.
614
615 my $cell = $worksheet->get_cell($row, $col);
616
617=head2 row_range()
618
619Returns a two-element list C<($min, $max)> containing the minimum and maximum defined rows in the worksheet. If there is no row defined C<$max> is smaller than C<$min>.
620
621 my ( $row_min, $row_max ) = $worksheet->row_range();
622
623=head2 col_range()
624
625Returns a two-element list C<($min, $max)> containing the minimum and maximum of defined columns in the worksheet. If there is no column defined C<$max> is smaller than C<$min>.
626
627 my ( $col_min, $col_max ) = $worksheet->col_range();
628
629
630=head2 get_name()
631
632The C<get_name()> method returns the name of the worksheet.
633
634 my $name = $worksheet->get_name();
635
636
637=head2 get_h_pagebreaks()
638
639The C<get_h_pagebreaks()> method returns an array ref of row numbers where a horizontal page break occurs.
640
641 my $h_pagebreaks = $worksheet->get_h_pagebreaks();
642
643Returns C<undef> if there are no pagebreaks.
644
645
646=head2 get_v_pagebreaks()
647
648The C<get_v_pagebreaks()> method returns an array ref of column numbers where a vertical page break occurs.
649
650 my $v_pagebreaks = $worksheet->get_v_pagebreaks();
651
652Returns C<undef> if there are no pagebreaks.
653
654
655=head2 get_merged_areas()
656
657The C<get_merged_areas()> method returns an array ref of cells that are merged.
658
659 my $merged_areas = $worksheet->get_merged_areas();
660
661Each merged area is represented as follows:
662
663 [ $start_row, $start_col, $end_row, $end_col]
664
665Returns C<undef> if there are no merged areas.
666
667
668=head2 get_row_heights()
669
670The C<get_row_heights()> method returns an array_ref of row heights.
671
672 my $row_heights = $worksheet->get_row_heights();
673
674Returns C<undef> if the property isn't set.
675
676
677=head2 get_col_widths()
678
679The C<get_col_widths()> method returns an array_ref of column widths.
680
681 my $col_widths = $worksheet->get_col_widths();
682
683Returns C<undef> if the property isn't set.
684
685
686=head2 get_default_row_height()
687
688The C<get_default_row_height()> method returns the default row height for the worksheet. Generally 12.75.
689
690 my $default_row_height = $worksheet->get_default_row_height();
691
692
693=head2 get_default_col_width()
694
695The C<get_default_col_width()> method returns the default column width for the worksheet. Generally 8.43.
696
697 my $default_col_width = $worksheet->get_default_col_width();
698
699
700=head2 get_header()
701
702The C<get_header()> method returns the worksheet header string. This string can contain control codes for alignment and font properties. Refer to the Excel on-line help on headers and footers or to the Spreadsheet::WriteExcel documentation for set_header().
703
704 my $header = $worksheet->get_header();
705
706Returns C<undef> if the property isn't set.
707
708
709=head2 get_footer()
710
711The C<get_footer()> method returns the worksheet footer string. This string can contain control codes for alignment and font properties. Refer to the Excel on-line help on headers and footers or to the Spreadsheet::WriteExcel documentation for set_header().
712
713 my $footer = $worksheet->get_footer();
714
715Returns C<undef> if the property isn't set.
716
717
718=head2 get_margin_left()
719
720The C<get_margin_left()> method returns the left margin of the worksheet in inches.
721
722 my $margin_left = $worksheet->get_margin_left();
723
724Returns C<undef> if the property isn't set.
725
726
727=head2 get_margin_right()
728
729The C<get_margin_right()> method returns the right margin of the worksheet in inches.
730
731 my $margin_right = $worksheet->get_margin_right();
732
733Returns C<undef> if the property isn't set.
734
735
736=head2 get_margin_top()
737
738The C<get_margin_top()> method returns the top margin of the worksheet in inches.
739
740 my $margin_top = $worksheet->get_margin_top();
741
742Returns C<undef> if the property isn't set.
743
744
745=head2 get_margin_bottom()
746
747The C<get_margin_bottom()> method returns the bottom margin of the worksheet in inches.
748
749 my $margin_bottom = $worksheet->get_margin_bottom();
750
751Returns C<undef> if the property isn't set.
752
753
754=head2 get_margin_header()
755
756The C<get_margin_header()> method returns the header margin of the worksheet in inches.
757
758 my $margin_header = $worksheet->get_margin_header();
759
760Returns a default value of 0.5 if not set.
761
762
763=head2 get_margin_footer()
764
765The C<get_margin_footer()> method returns the footer margin of the worksheet in inches.
766
767 my $margin_footer = $worksheet->get_margin_footer();
768
769Returns a default value of 0.5 if not set.
770
771
772=head2 get_paper()
773
774The C<get_paper()> method returns the printer paper size.
775
776 my $paper = $worksheet->get_paper();
777
778The value corresponds to the formats shown below:
779
780 Index Paper format Paper size
781 ===== ============ ==========
782 0 Printer default -
783 1 Letter 8 1/2 x 11 in
784 2 Letter Small 8 1/2 x 11 in
785 3 Tabloid 11 x 17 in
786 4 Ledger 17 x 11 in
787 5 Legal 8 1/2 x 14 in
788 6 Statement 5 1/2 x 8 1/2 in
789 7 Executive 7 1/4 x 10 1/2 in
790 8 A3 297 x 420 mm
791 9 A4 210 x 297 mm
792 10 A4 Small 210 x 297 mm
793 11 A5 148 x 210 mm
794 12 B4 250 x 354 mm
795 13 B5 182 x 257 mm
796 14 Folio 8 1/2 x 13 in
797 15 Quarto 215 x 275 mm
798 16 - 10x14 in
799 17 - 11x17 in
800 18 Note 8 1/2 x 11 in
801 19 Envelope 9 3 7/8 x 8 7/8
802 20 Envelope 10 4 1/8 x 9 1/2
803 21 Envelope 11 4 1/2 x 10 3/8
804 22 Envelope 12 4 3/4 x 11
805 23 Envelope 14 5 x 11 1/2
806 24 C size sheet -
807 25 D size sheet -
808 26 E size sheet -
809 27 Envelope DL 110 x 220 mm
810 28 Envelope C3 324 x 458 mm
811 29 Envelope C4 229 x 324 mm
812 30 Envelope C5 162 x 229 mm
813 31 Envelope C6 114 x 162 mm
814 32 Envelope C65 114 x 229 mm
815 33 Envelope B4 250 x 353 mm
816 34 Envelope B5 176 x 250 mm
817 35 Envelope B6 176 x 125 mm
818 36 Envelope 110 x 230 mm
819 37 Monarch 3.875 x 7.5 in
820 38 Envelope 3 5/8 x 6 1/2 in
821 39 Fanfold 14 7/8 x 11 in
822 40 German Std Fanfold 8 1/2 x 12 in
823 41 German Legal Fanfold 8 1/2 x 13 in
824 256 User defined
825
826The two most common paper sizes are C<1 = "US Letter"> and C<9 = A4>. Returns 9 by default.
827
828
829=head2 get_start_page()
830
831The C<get_start_page()> method returns the page number that printing will start from.
832
833 my $start_page = $worksheet->get_start_page();
834
835Returns 0 if the property isn't set.
836
837
838=head2 get_print_order()
839
840The C<get_print_order()> method returns 0 if the worksheet print "page order" is "Down then over" (the default) or 1 if it is "Over then down".
841
842 my $print_order = $worksheet->get_print_order();
843
844
845=head2 get_print_scale()
846
847The C<get_print_scale()> method returns the workbook scale for printing. The print scale fctor can be in the range 10 .. 400.
848
849 my $print_scale = $worksheet->get_print_scale();
850
851Returns 100 by default.
852
853
854=head2 get_fit_to_pages()
855
856The C<get_fit_to_pages()> method returns the number of pages wide and high that the printed worksheet page will fit to.
857
858 my ($pages_wide, $pages_high) = $worksheet->get_fit_to_pages();
859
860Returns (0, 0) if the property isn't set.
861
862
863=head2 is_portrait()
864
865The C<is_portrait()> method returns true if the worksheet has been set for printing in portrait mode.
866
867 my $is_portrait = $worksheet->is_portrait();
868
869Returns 0 if the worksheet has been set for printing in horizontal mode.
870
871
872=head2 is_centered_horizontally()
873
874The C<is_centered_horizontally()> method returns true if the worksheet has been centered horizontally for printing.
875
876 my $is_centered_horizontally = $worksheet->is_centered_horizontally();
877
878Returns 0 if the property isn't set.
879
880
881=head2 is_centered_vertically()
882
883The C<is_centered_vertically()> method returns true if the worksheet has been centered vertically for printing.
884
885 my $is_centered_vertically = $worksheet->is_centered_vertically();
886
887Returns 0 if the property isn't set.
888
889
890=head2 is_print_gridlines()
891
892The C<is_print_gridlines()> method returns true if the worksheet print "gridlines" option is turned on.
893
894 my $is_print_gridlines = $worksheet->is_print_gridlines();
895
896Returns 0 if the property isn't set.
897
898
899=head2 is_print_row_col_headers()
900
901The C<is_print_row_col_headers()> method returns true if the worksheet print "row and column headings" option is turned on.
902
903 my $is_print_row_col_headers = $worksheet->is_print_row_col_headers();
904
905Returns 0 if the property isn't set.
906
907
908=head2 is_print_black_and_white()
909
910The C<is_print_black_and_white()> method returns true if the worksheet print "black and white" option is turned on.
911
912 my $is_print_black_and_white = $worksheet->is_print_black_and_white();
913
914Returns 0 if the property isn't set.
915
916
917=head2 is_print_draft()
918
919The C<is_print_draft()> method returns true if the worksheet print "draft" option is turned on.
920
921 my $is_print_draft = $worksheet->is_print_draft();
922
923Returns 0 if the property isn't set.
924
925
926=head2 is_print_comments()
927
928The C<is_print_comments()> method returns true if the worksheet print "comments" option is turned on.
929
930 my $is_print_comments = $worksheet->is_print_comments();
931
932Returns 0 if the property isn't set.
933
934
935=head1 AUTHOR
936
937Maintainer 0.40+: John McNamara jmcnamara@cpan.org
938
939Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org
940
941Original author: Kawai Takanori kwitknr@cpan.org
942
943=head1 COPYRIGHT
944
945Copyright (c) 2009-2010 John McNamara
946
947Copyright (c) 2006-2008 Gabor Szabo
948
949Copyright (c) 2000-2006 Kawai Takanori
950
951All rights reserved.
952
953You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
954
955=cut