File Coverage

blib/lib/Spreadsheet/ParseExcel/Simple.pm
Criterion Covered Total %
statement 19 21 90.4
branch 3 6 50.0
condition 1 2 50.0
subroutine 7 9 77.7
pod 3 3 100.0
total 33 41 80.4


line stmt bran cond sub pod time code
1             package Spreadsheet::ParseExcel::Simple;
2              
3             $VERSION = '1.04';
4              
5 2     2   34115 use strict;
  2         5  
  2         67  
6 2     2   2889 use Spreadsheet::ParseExcel;
  2         184618  
  2         686  
7              
8             =head1 NAME
9              
10             Spreadsheet::ParseExcel::Simple - A simple interface to Excel data
11              
12             =head1 SYNOPSIS
13              
14             my $xls = Spreadsheet::ParseExcel::Simple->read('spreadsheet.xls');
15             foreach my $sheet ($xls->sheets) {
16             while ($sheet->has_data) {
17             my @data = $sheet->next_row;
18             }
19             }
20              
21             =head1 DESCRIPTION
22              
23             This provides an abstraction to the Spreadsheet::ParseExcel module for
24             simple reading of values.
25              
26             You simply loop over the sheets, and fetch rows to arrays.
27              
28             For anything more complex, you probably want to use
29             Spreadsheet::ParseExcel directly.
30              
31             =head1 BOOK METHODS
32              
33             =head2 read
34              
35             my $xls = Spreadsheet::ParseExcel::Simple->read('spreadsheet.xls');
36              
37             This opens the spreadsheet specified for you. Returns undef if we cannot
38             read the book.
39              
40             =head2 sheets
41              
42             @sheets = $xls->sheets;
43              
44             Each spreadsheet can contain one or more worksheets. This fetches them
45             all back. You can then iterate over them, or jump straight to the one
46             you wish to play with.
47              
48             =head2 book
49              
50             my $book = $xls->book;
51              
52             The Spreadsheet::ParseExcel object we are working with. You can use this
53             if you need to manipulate it in ways that this interface doesn't allow.
54              
55             =head1 SHEET METHODS
56              
57             These methods can be called on each sheet returned from $xls->sheets:
58              
59             =head2 has_data
60              
61             if ($sheet->has_data) { ... }
62              
63             This lets us know if there are more rows in this sheet that we haven't
64             read yet. This allows us to differentiate between an empty row, and
65             the end of the sheet.
66              
67             =head2 next_row
68              
69             my @data = $sheet->next_row;
70              
71             Fetch the next row of data back.
72              
73             =head2 sheet
74              
75             my $obj = $sheet->sheet;
76              
77             The underlying Spreadsheet::ParseExcel object for the worksheet. You can
78             use this if you need to manipulate it in ways that this interface
79             doesn't allow (e.g. asking it for the sheet's name).
80              
81             =head1 AUTHOR
82              
83             Tony Bowden
84              
85             =head1 BUGS and QUERIES
86              
87             Please direct all correspondence regarding this module to:
88             bug-Spreadsheet-ParseExcel-Simple@rt.cpan.org
89              
90             =head1 COPYRIGHT AND LICENSE
91              
92             Copyright (C) 2001-2005 Tony Bowden.
93              
94             This program is free software; you can redistribute it and/or modify it under
95             the terms of the GNU General Public License; either version 2 of the License,
96             or (at your option) any later version.
97              
98             This program is distributed in the hope that it will be useful, but WITHOUT
99             ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
100             FOR A PARTICULAR PURPOSE.
101              
102             =head1 SEE ALSO
103              
104             L.
105              
106             =cut
107              
108             sub read {
109 2     2 1 147839 my $class = shift;
110 2 50       23 my $book = Spreadsheet::ParseExcel->new->Parse(shift) or return;
111 2         65345 bless { book => $book }, $class;
112             }
113              
114 0     0 1 0 sub book { shift->{book} }
115              
116             sub sheets {
117 2         37 map Spreadsheet::ParseExcel::Simple::_Sheet->new($_),
118 2     2 1 16 @{shift->{book}->{Worksheet}};
119             }
120              
121             package Spreadsheet::ParseExcel::Simple::_Sheet;
122              
123             sub new {
124 3     3   7 my $class = shift;
125 3         7 my $sheet = shift;
126 3   50     42 bless {
127             sheet => $sheet,
128             row => $sheet->{MinRow} || 0,
129             }, $class;
130             }
131              
132 0     0   0 sub sheet { shift->{sheet} }
133              
134             sub has_data {
135 7     7   4341 my $self = shift;
136 7 50       117 defined $self->{sheet}->{MaxRow} and ($self->{row} <= $self->{sheet}->{MaxRow});
137             }
138              
139             sub next_row {
140 5 50   5   9 map { $_ ? $_->Value : "" } @{$_[0]->{sheet}->{Cells}[$_[0]->{row}++]};
  9         56  
  5         26  
141             }
142              
143             1;
144