File Coverage

lib/Spreadsheet/Reader/ExcelXML/ZipReader/WorkbookRels.pm
Criterion Covered Total %
statement 48 49 97.9
branch 12 16 75.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 1 1 100.0
total 70 76 92.1


line stmt bran cond sub pod time code
1             package Spreadsheet::Reader::ExcelXML::ZipReader::WorkbookRels;
2             our $AUTHORITY = 'cpan:JANDREW';
3 14     14   65883 use version; our $VERSION = version->declare('v0.16.8');
  14         22  
  14         99  
4             ###LogSD warn "You uncovered internal logging statements for Spreadsheet::Reader::ExcelXML::ZipReader::WorkbookRels-$VERSION";
5              
6 14     14   1513 use Moose::Role;
  14         23  
  14         116  
7             requires qw(
8             current_node_parsed advance_element_position parse_element
9             squash_node get_rel_info get_sheet_info
10             get_sheet_names good_load
11             );
12             ###LogSD requires 'get_log_space', 'get_all_space';
13 14     14   56255 use Types::Standard qw( Enum ArrayRef HashRef Bool );
  14         21  
  14         128  
14 14     14   10261 use Data::Dumper;
  14         23  
  14         864  
15 14     14   65 use lib '../../../../../lib',;
  14         17  
  14         108  
16             ###LogSD use Log::Shiras::Telephone;
17              
18             #########1 Dispatch Tables 3#########4#########5#########6#########7#########8#########9
19              
20              
21              
22             #########1 Public Attributes 3#########4#########5#########6#########7#########8#########9
23              
24              
25              
26             #########1 Public Methods 3#########4#########5#########6#########7#########8#########9
27              
28             sub load_unique_bits{
29 17     17 1 34 my( $self, ) = @_;
30             ###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
31             ###LogSD $self->get_all_space . '::load_unique_bits', );
32             ###LogSD $phone->talk( level => 'debug', message => [
33             ###LogSD "Setting the WorkbookRelsInterface unique bits" ] );
34              
35             # Build the list
36 17         91 $self->start_the_file_over;
37 17         25 my ( $found_member_names, $worksheet_list, $chartsheet_list );
38              
39             # Handle a zip based file
40 17         131 my( $result, $node_name, $node_level, $result_ref ) = $self->advance_element_position( 'Relationship' );
41 17   66     136 while( $result and $node_name eq 'Relationship' ){
42 109         274 my $relationship_ref = $self->squash_node( $self->parse_element );# Cleans up end nodes without other work
43             ###LogSD $phone->talk( level => 'debug', message => [
44             ###LogSD "parsed sheet ref is:", $relationship_ref ] );
45 109         423 my $rel_ref = $self->get_rel_info( $relationship_ref->{Id} ) ;
46             ###LogSD $phone->talk( level => 'debug', message => [
47             ###LogSD "Current rel ref:", $rel_ref ] );
48 109 100       311 if( $rel_ref ){
49 41         101 my $target = 'xl/' . $relationship_ref->{Target};
50 41         198 my $sheet_ref = $self->get_sheet_info( $rel_ref );
51             ###LogSD $phone->talk( level => 'debug', message => [
52             ###LogSD "Building relationship for: $relationship_ref->{Id}",
53             ###LogSD "With target: $target",
54             ###LogSD "For sheet -$rel_ref- with info:", $sheet_ref ] );
55 41         107 $target =~ s/\\/\//g;
56 41 100       161 if( $target =~ /worksheets(\\|\/)/ ){
    50          
57             ###LogSD $phone->talk( level => 'debug', message => [
58             ###LogSD "Found a worksheet" ] );
59 40         72 $sheet_ref->{file} = $target;
60 40         76 $sheet_ref->{sheet_type} = 'worksheet';
61 40         1093 $self->_set_sheet_info( $rel_ref => $sheet_ref );
62 40         67 $worksheet_list->[$sheet_ref->{sheet_position}] = $rel_ref;
63             ###LogSD $phone->talk( level => 'debug', message => [
64             ###LogSD "Updating sheet member key -$rel_ref- with:", $sheet_ref,
65             ###LogSD "..and updated worksheet list:", $worksheet_list ] );
66             ###LogSD $phone->talk( level => 'trace', message => [
67             ###LogSD "..resulting new sheet lookup for sheet: $rel_ref", $self->get_sheet_info( $rel_ref ), ] );
68 40         51 $found_member_names = 1;
69             }elsif( $target =~ /chartsheets(\\|\/)/ ){
70             ###LogSD $phone->talk( level => 'debug', message => [
71             ###LogSD "Found a chartsheet" ] );
72 1         3 $sheet_ref->{file} = $target;
73 1         3 $sheet_ref->{sheet_type} = 'chartsheet';
74 1         26 $self->_set_sheet_info( $rel_ref => $sheet_ref );
75 1         2 $chartsheet_list->[$sheet_ref->{sheet_position}] = $rel_ref;
76             ###LogSD $phone->talk( level => 'debug', message => [
77             ###LogSD "Updating sheet member key -$rel_ref- with:", $sheet_ref,
78             ###LogSD "..and updated chartsheet list:", $chartsheet_list ] );
79 1         2 $found_member_names = 1;
80             }else{# Add method for pivot table lookup later
81             ###LogSD $phone->talk( level => 'debug', message => [
82             ###LogSD "Not a worksheet or a chartsheet - possible pivot table lookup" ] );
83             #~ $pivot_lookup->{$rel_ID} = $target;
84             }
85             }
86 109         233 ( $result, $node_name, $node_level, $result_ref ) = $self->advance_element_position( 'Relationship' );# Advance to the next start
87             }
88 17 50       61 if( !$found_member_names ){# Handle and xml based file
89 0         0 confess "Couldn't find any zip member (file) names for the sheets - is the workbook empty?";
90             }
91             ###LogSD $phone->talk( level => 'debug', message => [
92             ###LogSD "Loading the worksheet list with:", $worksheet_list ] );
93 17 50       66 map{ $self->_add_worksheet( $_ ) if $_ } @$worksheet_list if $worksheet_list;
  40 50       1149  
94             ###LogSD $phone->talk( level => 'debug', message => [
95             ###LogSD "Loading the chartsheet list with:", $chartsheet_list ] );
96 17 100       58 map{ $self->_add_chartsheet( $_ ) if $_ } @$chartsheet_list if $chartsheet_list;
  2 100       30  
97              
98             ###LogSD $phone->talk( level => 'debug', message => [
99             ###LogSD "Closing out the xml file" ] );
100 17         107 $self->close_the_file;
101 17         454 $self->good_load( 1 );
102             }
103              
104             #########1 Private Attributes 3#########4#########5#########6#########7#########8#########9
105              
106             has _sheet_lookup =>(
107             isa => HashRef,
108             traits => ['Hash'],
109             reader => 'get_sheet_lookup',
110             handles =>{
111             _set_sheet_info => 'set',
112             },
113             default => sub{ {} },
114             );
115              
116             has _worksheet_list =>(
117             isa => ArrayRef,
118             traits => ['Array'],
119             reader => 'get_worksheet_list',
120             handles =>{
121             _add_worksheet => 'push',
122             },
123             default => sub{ [] },
124             );
125              
126             has _chartsheet_list =>(
127             isa => ArrayRef,
128             traits => ['Array'],
129             reader => 'get_chartsheet_list',
130             handles =>{
131             _add_chartsheet => 'push',
132             },
133             default => sub{ [] },
134             );
135              
136             #########1 Private Methods 3#########4#########5#########6#########7#########8#########9
137              
138              
139              
140             #########1 Phinish 3#########4#########5#########6#########7#########8#########9
141              
142 14     14   7868 no Moose::Role;
  14         23  
  14         67  
143             1;
144              
145             #########1 Documentation 3#########4#########5#########6#########7#########8#########9
146             __END__
147              
148             =head1 NAME
149              
150             Spreadsheet::Reader::ExcelXML::ZipReader::WorkbookRels - Workbook Rels Zip file unique reader
151              
152             =head1 SYNOPSIS
153              
154             use Types::Standard qw( HashRef );
155             use MooseX::ShortCut::BuildInstance qw( build_instance );
156             use Types::Standard qw( HashRef );
157             use Data::Dumper;
158             my $workbook_instance = build_instance(
159             package => 'Spreadsheet::Reader::ExcelXML::Workbook',
160             add_attributes =>{
161             _rel_lookup =>{
162             isa => HashRef,
163             traits => ['Hash'],
164             handles =>{ get_rel_info => 'get', },
165             default => sub{ {
166             'rId2' => 'Sheet5',
167             'rId3' => 'Sheet1',
168             'rId1' => 'Sheet2'
169             } },
170             },
171             _sheet_lookup =>{
172             isa => HashRef,
173             traits => ['Hash'],
174             handles =>{ get_sheet_info => 'get', },
175             default => sub{ {
176             'Sheet1' => {
177             'sheet_id' => '1',
178             'sheet_position' => 2,
179             'sheet_name' => 'Sheet1',
180             'is_hidden' => 0,
181             'sheet_rel_id' => 'rId3'
182             },
183             'Sheet2' => {
184             'sheet_position' => 0,
185             'sheet_name' => 'Sheet2',
186             'sheet_id' => '2',
187             'sheet_rel_id' => 'rId1',
188             'is_hidden' => 0
189             },
190             'Sheet5' => {
191             'sheet_position' => 1,
192             'sheet_name' => 'Sheet5',
193             'sheet_id' => '3',
194             'sheet_rel_id' => 'rId2',
195             'is_hidden' => 1
196             }
197             } },
198             },
199             },
200             add_methods =>{
201             get_sheet_names => sub{ [
202             'Sheet2',
203             'Sheet5',
204             'Sheet1'
205             ] },
206             }
207             );
208             my $rels_instance = build_instance(
209             superclasses => ['Spreadsheet::Reader::ExcelXML::XMLReader'],
210             package => 'WorkbookRelsInterface',
211             add_roles_in_sequence =>[
212             'Spreadsheet::Reader::ExcelXML::ZipReader::WorkbookRels',
213             'Spreadsheet::Reader::ExcelXML::WorkbookRelsInterface',
214             ],
215             file => 'xl/_rels/workbook.xml.rels',
216             workbook_inst => $workbook_instance,
217             );
218             print Dumper( $rels_instance->get_worksheet_list );
219              
220             ###########################
221             # SYNOPSIS Screen Output
222             # 01: $VAR1 = [
223             # 02: 'Sheet2',
224             # 03: 'Sheet5',
225             # 04: 'Sheet1'
226             # 05: ]
227             ###########################
228              
229             =head1 DESCRIPTION
230              
231             This documentation is written to explain ways to use this module when writing your own
232             excel parser. To use the general package for excel parsing out of the box please review
233             the documentation for L<Workbooks|Spreadsheet::Reader::ExcelXML>,
234             L<Worksheets|Spreadsheet::Reader::ExcelXML::Worksheet>, and
235             L<Cells|Spreadsheet::Reader::ExcelXML::Cell>
236              
237             This is the Zip based file adaptor for reading the workbook rels data and then updating
238             the general workbook metadata. The transformed data is then accesable through L<Methods
239             |/Methods>. The goal is to standardize the outputs of this transformation metadata from
240             non standard inputs.
241              
242             =head2 Required Methods
243              
244             These are the methods required by the role. A link to the default implementation of
245             these methods is provided.
246              
247             L<Spreadsheet::Reader::ExcelXML/get_sheet_info( $name )>
248              
249             L<Spreadsheet::Reader::ExcelXML/get_rel_info( $rel_id )>
250              
251             L<Spreadsheet::Reader::ExcelXML/get_sheet_names>
252              
253             L<Spreadsheet::Reader::ExcelXML::XMLReader/advance_element_position( $element, [$iterations] )>
254              
255             L<Spreadsheet::Reader::ExcelXML::XMLReader/current_node_parsed>
256              
257             L<Spreadsheet::Reader::ExcelXML::XMLReader/parse_element( [$depth] )>
258              
259             L<Spreadsheet::Reader::ExcelXML::XMLReader/squash_node( $node )>
260              
261             L<Spreadsheet::Reader::ExcelXML::XMLReader/good_load( $state )>
262              
263             =head2 Methods
264              
265             These are the methods provided by this role (only).
266              
267             =head3 load_unique_bits
268              
269             =over
270              
271             B<Definition:> This role is meant to run on top of L<Spreadsheet::Reader::ExcelXML::XMLReader>.
272             When it does the reader will call this function as available when it first starts the file.
273             Therefore this is where the unique Metadata for this file is found and stored. (in the
274             attributes)
275              
276             B<Accepts:> nothing
277              
278             B<Returns:> nothing
279              
280             =back
281              
282             =head3 get_sheet_lookup
283              
284             =over
285              
286             B<Definition:> The sheet lookup is a hashref with keys as sheet names and the values are a sub
287             hashref with $key => $value pairs of sheet meta data containing information like hiddeness and
288             location. This method returns the full set.
289              
290             B<Accepts:> nothing
291              
292             B<Returns:> a full hashref of hashrefs
293              
294             =back
295              
296             =head3 get_worksheet_list
297              
298             =over
299              
300             B<Definition:> returns an ordered arrayref conataining only worksheet names in their visible order
301             from the Excel workbook.
302              
303             B<Accepts:> nothing
304              
305             B<Returns:> an arrayref of names
306              
307             =back
308              
309             =head3 get_chartsheet_list
310              
311             =over
312              
313             B<Definition:> returns an ordered arrayref conataining only chartsheet names in their visible order
314             from the Excel workbook.
315              
316             B<Accepts:> nothing
317              
318             B<Returns:> an arrayref of names
319              
320             =back
321              
322             =head1 SUPPORT
323              
324             =over
325              
326             L<github Spreadsheet::Reader::ExcelXML/issues
327             |https://github.com/jandrew/p5-spreadsheet-reader-excelxml/issues>
328              
329             =back
330              
331             =head1 TODO
332              
333             =over
334              
335             B<1.> Nothing currently
336              
337             =back
338              
339             =head1 AUTHOR
340              
341             =over
342              
343             =item Jed Lund
344              
345             =item jandrew@cpan.org
346              
347             =back
348              
349             =head1 COPYRIGHT
350              
351             This program is free software; you can redistribute
352             it and/or modify it under the same terms as Perl itself.
353              
354             The full text of the license can be found in the
355             LICENSE file included with this module.
356              
357             This software is copyrighted (c) 2016 by Jed Lund
358              
359             =head1 DEPENDENCIES
360              
361             =over
362              
363             L<Spreadsheet::Reader::ExcelXML> - the package
364              
365             =back
366              
367             =head1 SEE ALSO
368              
369             =over
370              
371             L<Spreadsheet::Read> - generic Spreadsheet reader
372              
373             L<Spreadsheet::ParseExcel> - Excel binary version 2003 and earlier (.xls files)
374              
375             L<Spreadsheet::XLSX> - Excel version 2007 and later
376              
377             L<Spreadsheet::ParseXLSX> - Excel version 2007 and later
378              
379             L<Log::Shiras|https://github.com/jandrew/Log-Shiras>
380              
381             =over
382              
383             All lines in this package that use Log::Shiras are commented out
384              
385             =back
386              
387             =back
388              
389             =cut
390              
391             #########1#########2 main pod documentation end 5#########6#########7#########8#########9