| 1 | package Data::XLSX::Parser::Workbook; | ||
| 2 | 9 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 9 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 0 | ▶ | use XML::Parser::Expat; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Archive::Zip (); | ||
| 7 | use File::Temp; | ||
| 8 | use Carp; | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | my ($class, $archive) = @_; | ||
| 12 | |||
| 13 | my $self = bless [], $class; | ||
| 14 | |||
| 15 | my $fh = File::Temp->new( SUFFIX => '.xml' ) or confess "couldn't create temporary file: $!"; | ||
| 16 | |||
| 17 | my $handle = $archive->workbook or confess "couldn't get handle to workbook archive: $!";; | ||
| 18 | confess 'Failed to write temporary file: ', $fh->filename | ||
| 19 | unless $handle->extractToFileNamed($fh->filename) == Archive::Zip::AZ_OK; | ||
| 20 | |||
| 21 | my $parser = XML::Parser::Expat->new(Namespaces=>1); | ||
| 22 | $parser->setHandlers( | ||
| 23 | Start => sub { $self->_start(@_) }, | ||
| 24 | End => sub {}, | ||
| 25 | Char => sub {}, | ||
| 26 | ); | ||
| 27 | $parser->parse($fh); | ||
| 28 | |||
| 29 | $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub names { | ||
| 33 | my ($self) = @_; | ||
| 34 | map { $_->{name} } @$self; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub sheet_rid { | ||
| 38 | my ($self, $name) = @_; | ||
| 39 | |||
| 40 | my ($meta) = grep { $_->{name} eq $name } @$self | ||
| 41 | or do {carp ("didn't find $name in workbook's sheet names"); return;}; | ||
| 42 | |||
| 43 | return $meta->{'id'}; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub sheet_id { | ||
| 47 | my ($self, $name) = @_; | ||
| 48 | |||
| 49 | my ($meta) = grep { $_->{name} eq $name } @$self | ||
| 50 | or do {carp ("didn't find $name in workbook's sheet names"); return;}; | ||
| 51 | |||
| 52 | return $meta->{sheetId}; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _start { | ||
| 56 | my ($self, $parser, $el, %attr) = @_; | ||
| 57 | push @$self, \%attr if $el eq 'sheet'; | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; | ||
| 61 | __END__ | ||