File Coverage

blib/lib/Data/XLSX/Parser/Workbook.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Data::XLSX::Parser::Workbook;
2 6     6   32 use strict;
  6         9  
  6         243  
3 6     6   34 use warnings;
  6         9  
  6         224  
4              
5 6     6   16134 use XML::Parser::Expat;
  0            
  0            
6             use Archive::Zip ();
7             use File::Temp;
8              
9             sub new {
10             my ($class, $archive) = @_;
11              
12             my $self = bless [], $class;
13              
14             my $fh = File::Temp->new( SUFFIX => '.xml' );
15              
16             my $handle = $archive->workbook;
17             die 'Failed to write temporally file: ', $fh->filename
18             unless $handle->extractToFileNamed($fh->filename) == Archive::Zip::AZ_OK;
19              
20             my $parser = XML::Parser::Expat->new;
21             $parser->setHandlers(
22             Start => sub { $self->_start(@_) },
23             End => sub {},
24             Char => sub {},
25             );
26             $parser->parse($fh);
27              
28             $self;
29             }
30              
31             sub names {
32             my ($self) = @_;
33             map { $_->{name} } @$self;
34             }
35              
36             sub sheet_id {
37             my ($self, $name) = @_;
38              
39             my ($meta) = grep { $_->{name} eq $name } @$self
40             or return;
41              
42             if ($meta->{'r:id'}) {
43             (my $r = $meta->{'r:id'}) =~ s/^rId//;
44             return $r;
45             }
46             else {
47             return $meta->{sheetId};
48             }
49             }
50              
51             sub _start {
52             my ($self, $parser, $el, %attr) = @_;
53             push @$self, \%attr if $el eq 'sheet';
54             }
55              
56             1;