File Coverage

blib/lib/Spreadsheet/ParseExcel/Worksheet.pm
Criterion Covered Total %
statement 113 127 88.9
branch 16 32 50.0
condition 6 16 37.5
subroutine 42 44 95.4
pod 36 38 94.7
total 213 257 82.8


line stmt bran cond sub pod time code
1             package 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) 2014 Douglas Wilson
10             # Copyright (c) 2009-2013 John McNamara
11             # Copyright (c) 2006-2008 Gabor Szabo
12             # Copyright (c) 2000-2006 Kawai Takanori
13             #
14             # perltidy with standard settings.
15             #
16             # Documentation after __END__
17             #
18              
19 21     21   116 use strict;
  21         39  
  21         7351  
20 21     21   122 use warnings;
  21         40  
  21         3212  
21 21     21   119 use Scalar::Util qw(weaken);
  21         39  
  21         42183  
22              
23             our $VERSION = '0.65';
24              
25             ###############################################################################
26             #
27             # new()
28             #
29             sub new {
30              
31 87     87 0 645 my ( $class, %properties ) = @_;
32              
33 87         147 my $self = \%properties;
34              
35 87         639 weaken $self->{_Book};
36              
37 87         388 $self->{Cells} = undef;
38 87         254 $self->{DefColWidth} = 8.43;
39              
40 87         550 return bless $self, $class;
41             }
42              
43             ###############################################################################
44             #
45             # get_cell( $row, $col )
46             #
47             # Returns the Cell object at row $row and column $col, if defined.
48             #
49             sub get_cell {
50              
51 267     267 1 105219 my ( $self, $row, $col ) = @_;
52              
53 267 50 33     4124 if ( !defined $row
    50 33        
      33        
      33        
54             || !defined $col
55             || !defined $self->{MaxRow}
56             || !defined $self->{MaxCol} )
57             {
58              
59             # Return undef if no arguments are given or if no cells are defined.
60 0         0 return undef;
61             }
62             elsif ($row < $self->{MinRow}
63             || $row > $self->{MaxRow}
64             || $col < $self->{MinCol}
65             || $col > $self->{MaxCol} )
66             {
67              
68             # Return undef if outside allowable row/col range.
69 0         0 return undef;
70             }
71             else {
72              
73             # Return the Cell object.
74 267         915 return $self->{Cells}->[$row]->[$col];
75             }
76             }
77              
78             ###############################################################################
79             #
80             # row_range()
81             #
82             # Returns a two-element list ($min, $max) containing the minimum and maximum
83             # defined rows in the worksheet.
84             #
85             # If there is no row defined $max is smaller than $min.
86             #
87             sub row_range {
88              
89 15     15 1 4121 my $self = shift;
90              
91 15   50     106 my $min = $self->{MinRow} || 0;
92 15 50       57 my $max = defined( $self->{MaxRow} ) ? $self->{MaxRow} : ( $min - 1 );
93              
94 15         48 return ( $min, $max );
95             }
96              
97             ###############################################################################
98             #
99             # col_range()
100             #
101             # Returns a two-element list ($min, $max) containing the minimum and maximum
102             # defined cols in the worksheet.
103             #
104             # If there is no column defined $max is smaller than $min.
105             #
106             sub col_range {
107              
108 15     15 1 75 my $self = shift;
109              
110 15   50     94 my $min = $self->{MinCol} || 0;
111 15 50       60 my $max = defined( $self->{MaxCol} ) ? $self->{MaxCol} : ( $min - 1 );
112              
113 15         44 return ( $min, $max );
114             }
115              
116             ###############################################################################
117             #
118             # get_name()
119             #
120             # Returns the name of the worksheet.
121             #
122             sub get_name {
123              
124 11     11 1 1091 my $self = shift;
125              
126 11         43 return $self->{Name};
127             }
128              
129             ###############################################################################
130             #
131             # sheet_num()
132             #
133             sub sheet_num {
134              
135 2     2 0 19 my $self = shift;
136              
137 2         7 return $self->{_SheetNo};
138             }
139              
140             ###############################################################################
141             #
142             # get_h_pagebreaks()
143             #
144             # Returns an array ref of row numbers where a horizontal page break occurs.
145             #
146             sub get_h_pagebreaks {
147              
148 2     2 1 19 my $self = shift;
149              
150 2         8 return $self->{HPageBreak};
151             }
152              
153             ###############################################################################
154             #
155             # get_v_pagebreaks()
156             #
157             # Returns an array ref of column numbers where a vertical page break occurs.
158             #
159             sub get_v_pagebreaks {
160              
161 2     2 1 20 my $self = shift;
162              
163 2         7 return $self->{VPageBreak};
164             }
165              
166             ###############################################################################
167             #
168             # get_merged_areas()
169             #
170             # Returns an array ref of cells that are merged.
171             #
172             sub get_merged_areas {
173              
174 2     2 1 17 my $self = shift;
175              
176 2         8 return $self->{MergedArea};
177             }
178              
179             ###############################################################################
180             #
181             # get_row_heights()
182             #
183             # Returns an array of row heights.
184             #
185             sub get_row_heights {
186              
187 3     3 1 24 my $self = shift;
188              
189 3 50       14 if ( wantarray() ) {
190 3 50       31 return unless $self->{RowHeight};
191 3         6 return @{ $self->{RowHeight} };
  3         17  
192             }
193 0         0 return $self->{RowHeight};
194             }
195              
196             ###############################################################################
197             #
198             # get_col_widths()
199             #
200             # Returns an array of column widths.
201             #
202             sub get_col_widths {
203              
204 3     3 1 24 my $self = shift;
205              
206 3 50       14 if ( wantarray() ) {
207 3 50       12 return unless $self->{ColWidth};
208 3         4 return @{ $self->{ColWidth} };
  3         80  
209             }
210 0         0 return $self->{ColWidth};
211             }
212              
213             ###############################################################################
214             #
215             # get_default_row_height()
216             #
217             # Returns the default row height for the worksheet. Generally 12.75.
218             #
219             sub get_default_row_height {
220              
221 2     2 1 17 my $self = shift;
222              
223 2         8 return $self->{DefRowHeight};
224             }
225              
226             ###############################################################################
227             #
228             # get_default_col_width()
229             #
230             # Returns the default column width for the worksheet. Generally 8.43.
231             #
232             sub get_default_col_width {
233              
234 2     2 1 17 my $self = shift;
235              
236 2         8 return $self->{DefColWidth};
237             }
238              
239             ###############################################################################
240             #
241             # _get_row_properties()
242             #
243             # Returns an array_ref of row properties.
244             # TODO. This is a placeholder for a future method.
245             #
246             sub _get_row_properties {
247              
248 0     0   0 my $self = shift;
249              
250 0         0 return $self->{RowProperties};
251             }
252              
253             ###############################################################################
254             #
255             # _get_col_properties()
256             #
257             # Returns an array_ref of column properties.
258             # TODO. This is a placeholder for a future method.
259             #
260             sub _get_col_properties {
261              
262 0     0   0 my $self = shift;
263              
264 0         0 return $self->{ColProperties};
265             }
266              
267             ###############################################################################
268             #
269             # get_header()
270             #
271             # Returns the worksheet header string.
272             #
273             sub get_header {
274              
275 2     2 1 19 my $self = shift;
276              
277 2         7 return $self->{Header};
278             }
279              
280             ###############################################################################
281             #
282             # get_footer()
283             #
284             # Returns the worksheet footer string.
285             #
286             sub get_footer {
287              
288 2     2 1 17 my $self = shift;
289              
290 2         426 return $self->{Footer};
291             }
292              
293             ###############################################################################
294             #
295             # get_margin_left()
296             #
297             # Returns the left margin of the worksheet in inches.
298             #
299             sub get_margin_left {
300              
301 2     2 1 16 my $self = shift;
302              
303 2         8 return $self->{LeftMargin};
304             }
305              
306             ###############################################################################
307             #
308             # get_margin_right()
309             #
310             # Returns the right margin of the worksheet in inches.
311             #
312             sub get_margin_right {
313              
314 2     2 1 17 my $self = shift;
315              
316 2         6 return $self->{RightMargin};
317             }
318              
319             ###############################################################################
320             #
321             # get_margin_top()
322             #
323             # Returns the top margin of the worksheet in inches.
324             #
325             sub get_margin_top {
326              
327 2     2 1 16 my $self = shift;
328              
329 2         8 return $self->{TopMargin};
330             }
331              
332             ###############################################################################
333             #
334             # get_margin_bottom()
335             #
336             # Returns the bottom margin of the worksheet in inches.
337             #
338             sub get_margin_bottom {
339              
340 2     2 1 16 my $self = shift;
341              
342 2         6 return $self->{BottomMargin};
343             }
344              
345             ###############################################################################
346             #
347             # get_margin_header()
348             #
349             # Returns the header margin of the worksheet in inches.
350             #
351             sub get_margin_header {
352              
353 2     2 1 17 my $self = shift;
354              
355 2         8 return $self->{HeaderMargin};
356             }
357              
358             ###############################################################################
359             #
360             # get_margin_footer()
361             #
362             # Returns the footer margin of the worksheet in inches.
363             #
364             sub get_margin_footer {
365              
366 2     2 1 17 my $self = shift;
367              
368 2         6 return $self->{FooterMargin};
369             }
370              
371             ###############################################################################
372             #
373             # get_paper()
374             #
375             # Returns the printer paper size.
376             #
377             sub get_paper {
378              
379 2     2 1 18 my $self = shift;
380              
381 2         10 return $self->{PaperSize};
382             }
383              
384             ###############################################################################
385             #
386             # get_start_page()
387             #
388             # Returns the page number that printing will start from.
389             #
390             sub get_start_page {
391              
392 4     4 1 31 my $self = shift;
393              
394             # Only return the page number if the "First page number" option is set.
395 4 100       14 if ( $self->{UsePage} ) {
396 2         6 return $self->{PageStart};
397             }
398             else {
399 2         6 return 0;
400             }
401             }
402              
403             ###############################################################################
404             #
405             # get_print_order()
406             #
407             # Returns the Worksheet page printing order.
408             #
409             sub get_print_order {
410              
411 2     2 1 16 my $self = shift;
412              
413 2         8 return $self->{LeftToRight};
414             }
415              
416             ###############################################################################
417             #
418             # get_print_scale()
419             #
420             # Returns the workbook scale for printing.
421             #
422             sub get_print_scale {
423              
424 2     2 1 18 my $self = shift;
425              
426 2         7 return $self->{Scale};
427             }
428              
429             ###############################################################################
430             #
431             # get_fit_to_pages()
432             #
433             # Returns the number of pages wide and high that the printed worksheet page
434             # will fit to.
435             #
436             sub get_fit_to_pages {
437              
438 6     6 1 44 my $self = shift;
439              
440 6 100       23 if ( !$self->{PageFit} ) {
441 3         7 return ( 0, 0 );
442             }
443             else {
444 3         14 return ( $self->{FitWidth}, $self->{FitHeight} );
445             }
446             }
447              
448             ###############################################################################
449             #
450             # is_portrait()
451             #
452             # Returns true if the worksheet has been set for printing in portrait mode.
453             #
454             sub is_portrait {
455              
456 2     2 1 20 my $self = shift;
457              
458 2         8 return $self->{Landscape};
459             }
460              
461             ###############################################################################
462             #
463             # is_centered_horizontally()
464             #
465             # Returns true if the worksheet has been centered horizontally for printing.
466             #
467             sub is_centered_horizontally {
468              
469 2     2 1 43 my $self = shift;
470              
471 2         8 return $self->{HCenter};
472             }
473              
474             ###############################################################################
475             #
476             # is_centered_vertically()
477             #
478             # Returns true if the worksheet has been centered vertically for printing.
479             #
480             sub is_centered_vertically {
481              
482 2     2 1 17 my $self = shift;
483              
484 2         8 return $self->{HCenter};
485             }
486              
487             ###############################################################################
488             #
489             # is_print_gridlines()
490             #
491             # Returns true if the worksheet print "gridlines" option is turned on.
492             #
493             sub is_print_gridlines {
494              
495 2     2 1 15 my $self = shift;
496              
497 2         6 return $self->{PrintGrid};
498             }
499              
500             ###############################################################################
501             #
502             # is_print_row_col_headers()
503             #
504             # Returns true if the worksheet print "row and column headings" option is on.
505             #
506             sub is_print_row_col_headers {
507              
508 2     2 1 16 my $self = shift;
509              
510 2         6 return $self->{PrintHeaders};
511             }
512              
513             ###############################################################################
514             #
515             # is_print_black_and_white()
516             #
517             # Returns true if the worksheet print "black and white" option is turned on.
518             #
519             sub is_print_black_and_white {
520              
521 2     2 1 19 my $self = shift;
522              
523 2         8 return $self->{NoColor};
524             }
525              
526             ###############################################################################
527             #
528             # is_print_draft()
529             #
530             # Returns true if the worksheet print "draft" option is turned on.
531             #
532             sub is_print_draft {
533              
534 2     2 1 15 my $self = shift;
535              
536 2         7 return $self->{Draft};
537             }
538              
539             ###############################################################################
540             #
541             # is_print_comments()
542             #
543             # Returns true if the worksheet print "comments" option is turned on.
544             #
545             sub is_print_comments {
546              
547 2     2 1 15 my $self = shift;
548              
549 2         7 return $self->{Notes};
550             }
551              
552             =head2 get_tab_color()
553              
554             Return color index of tab, or undef if not set.
555              
556             =cut
557              
558             sub get_tab_color {
559 1     1 1 6 my $worksheet = shift;
560              
561 1         7 return $worksheet->{TabColor};
562             }
563              
564             =head2 is_sheet_hidden()
565              
566             Return true if sheet is hidden
567              
568             =cut
569              
570             sub is_sheet_hidden {
571 2     2 1 471 my $worksheet = shift;
572              
573 2         6 return $worksheet->{SheetHidden};
574             }
575              
576             =head2 is_row_hidden($row)
577              
578             In scalar context, return true if $row is hidden
579             In array context, return an array whose elements are true
580             if the corresponding row is hidden.
581              
582             =cut
583              
584             sub is_row_hidden {
585 2     2 1 941 my $worksheet = shift;
586              
587 2         5 my ($row) = @_;
588              
589 2 50       10 unless ( $worksheet->{RowHidden} ) {
590 0 0       0 return () if (wantarray);
591 0         0 return 0;
592             }
593              
594 2 50       7 return @{ $worksheet->{RowHidden} } if (wantarray);
  0         0  
595 2         6 return $worksheet->{RowHidden}[$row];
596             }
597              
598             =head2 is_col_hidden($col)
599              
600             In scalar context, return true if $col is hidden
601             In array context, return an array whose elements are true
602             if the corresponding column is hidden.
603              
604             =cut
605              
606             sub is_col_hidden {
607 2     2 1 9445 my $worksheet = shift;
608              
609 2         5 my ($col) = @_;
610              
611 2 50       10 unless ( $worksheet->{ColHidden} ) {
612 0 0       0 return () if (wantarray);
613 0         0 return 0;
614             }
615              
616 2 50       8 return @{ $worksheet->{ColHidden} } if (wantarray);
  0         0  
617 2         9 return $worksheet->{ColHidden}[$col];
618             }
619              
620             ###############################################################################
621             #
622             # Mapping between legacy method names and new names.
623             #
624             {
625 21     21   140 no warnings; # Ignore warnings about variables used only once.
  21         36  
  21         1607  
626             *sheetNo = *sheet_num;
627             *Cell = *get_cell;
628             *RowRange = *row_range;
629             *ColRange = *col_range;
630             }
631              
632             1;
633              
634             __END__