| 1 | package Spreadsheet::DataFromExcel; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | our $VERSION = '1.001003'; | |||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use Spreadsheet::ParseExcel; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | sub new { bless {}, shift } | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | sub load { | ||||||||
|
Subroutine
load: called CC=11 SCAR=25.9
Pod
documented
| |||||||||||
| 13 | 1 | my ( $self, $file, $sheet_name, $start_row, $end_row ) = @_; | |||||||||
| 14 | |||||||||||
| 15 | 1 | $self->error( undef ); | |||||||||
| 16 | |||||||||||
| 17 | 1 | ▶ | return $self->_set_error("File $file not found") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | unless -e $file; | ||||||||||
| 19 | |||||||||||
| 20 | 1 | my $parser = Spreadsheet::ParseExcel->new(); | |||||||||
| 21 | 1 | ▶ | my $workbook = $parser->Parse($file) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | or return $self->_set_error("Could not load Excel file; perhaps it's not a real Excel file or is damaged?"); | ||||||||||
| 23 | |||||||||||
| 24 | 1 | ▶ | my $worksheet = defined $sheet_name | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | ? $workbook->Worksheet($sheet_name) | ||||||||||
| 26 | : ($workbook->worksheets)[0]; | ||||||||||
| 27 | |||||||||||
| 28 | 1 | ▶ | unless ( defined $worksheet ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | ▶ | if ( defined $sheet_name ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | return $self->_set_error("Specified worksheet $sheet_name was not found in the Excel file"); | |||||||||
| 31 | } | ||||||||||
| 32 | else { | ||||||||||
| 33 | 0 | return $self->_set_error("Could not obtain any worksheets"); | |||||||||
| 34 | } | ||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 1 | my @data; | |||||||||
| 38 | |||||||||||
| 39 | 1 | my ( $row_min, $row_max ) = $worksheet->row_range; | |||||||||
| 40 | 1 | ▶ | $row_min = $start_row | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | if defined $start_row; | ||||||||||
| 42 | |||||||||||
| 43 | 1 | ▶ | $row_max = $end_row | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | if defined $end_row; | ||||||||||
| 45 | |||||||||||
| 46 | 1 | my ( $col_min, $col_max ) = $worksheet->col_range; | |||||||||
| 47 | |||||||||||
| 48 | 1 | for my $row ( $row_min .. $row_max ) { | |||||||||
| 49 | 3 | my @row; | |||||||||
| 50 | 3 | for my $col ( $col_min .. $col_max ) { | |||||||||
| 51 | 12 | my $cell = $worksheet->get_cell( $row, $col ); | |||||||||
| 52 | 12 | ▶ | unless ( $cell ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 4 | push @row, undef; | |||||||||
| 54 | 4 | next; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 8 | push @row, $cell->unformatted; | |||||||||
| 58 | } | ||||||||||
| 59 | 3 | push @data, \@row; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 1 | return \@data; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | ▶ | sub _set_error { | ||||||||
|
Subroutine
_set_error: not called CC=1 SCAR=6.9
| |||||||||||
| 66 | 0 | my ( $self, $error ) = @_; | |||||||||
| 67 | 0 | $self->error( $error ); | |||||||||
| 68 | 0 | return; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 1 | ▶ | sub error { | ||||||||
|
Subroutine
error: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 72 | 1 | my $self = shift; | |||||||||
| 73 | |||||||||||
| 74 | 1 | ▶ | @_ and $self->{ERROR} = shift; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | |||||||||||
| 76 | 1 | return $self->{ERROR}; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 2 | 1; | |||||||||
| 80 | __END__ | ||||||||||