File Coverage

lib/Spreadsheet/Reader/ExcelXML/Styles.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1             package Spreadsheet::Reader::ExcelXML::Styles;
2             our $AUTHORITY = 'cpan:JANDREW';
3 21     21   40938 use version; our $VERSION = version->declare('v0.16.8');
  21         35  
  21         539  
4             ###LogSD warn "You uncovered internal logging statements for Spreadsheet::Reader::ExcelXML::Styles-$VERSION";
5              
6 21     21   2304 use 5.010;
  21         53  
7 21     21   76 use Moose::Role;
  21         29  
  21         222  
8             requires qw(
9             should_cache_positions get_default_format get_format
10             loaded_correctly
11             );
12              
13             #########1 Dispatch Tables & Package Variables 5#########6#########7#########8#########9
14              
15              
16              
17             #########1 Public Attributes 3#########4#########5#########6#########7#########8#########9
18              
19              
20              
21             #########1 Public Methods 3#########4#########5#########6#########7#########8#########9
22              
23             ###LogSD sub get_class_space{ 'StylesInterface' }
24              
25             #########1 Private Attributes 3#########4#########5#########6#########7#########8#########9
26              
27              
28              
29             #########1 Private Methods 3#########4#########5#########6#########7#########8#########9
30              
31              
32              
33             #########1 Phinish 3#########4#########5#########6#########7#########8#########9
34              
35 21     21   80113 no Moose::Role;
  21         38  
  21         570  
36              
37             1;
38              
39             #########1 Documentation 3#########4#########5#########6#########7#########8#########9
40             __END__
41              
42             =head1 NAME
43              
44             Spreadsheet::Reader::ExcelXML::Styles - The styles interface
45              
46             =head1 SYNOPSIS
47              
48             #!/usr/bin/env perl
49             use Data::Dumper;
50             use MooseX::ShortCut::BuildInstance qw( build_instance );
51             use Types::Standard qw( ConsumerOf HasMethods Int );
52             use Spreadsheet::Reader::ExcelXML::Error;
53             use Spreadsheet::Reader::ExcelXML::Styles;
54             use Spreadsheet::Reader::ExcelXML::XMLReader::PositionStyles;
55             use Spreadsheet::Reader::ExcelXML::XMLReader;
56             use Spreadsheet::Reader::Format::FmtDefault;
57             use Spreadsheet::Reader::Format::ParseExcelFormatStrings;
58             use Spreadsheet::Reader::Format;
59             my $workbook_instance = build_instance(
60             package => 'Spreadsheet::Reader::ExcelXML::Workbook',
61             add_attributes =>{
62             formatter_inst =>{
63             isa => ConsumerOf[ 'Spreadsheet::Reader::Format' ],# Interface
64             writer => 'set_formatter_inst',
65             reader => 'get_formatter_inst',
66             predicate => '_has_formatter_inst',
67             handles => { qw(
68             get_formatter_region get_excel_region
69             has_target_encoding has_target_encoding
70             get_target_encoding get_target_encoding
71             set_target_encoding set_target_encoding
72             change_output_encoding change_output_encoding
73             set_defined_excel_formats set_defined_excel_formats
74             get_defined_conversion get_defined_conversion
75             parse_excel_format_string parse_excel_format_string
76             set_date_behavior set_date_behavior
77             set_european_first set_european_first
78             set_formatter_cache_behavior set_cache_behavior
79             get_excel_region get_excel_region
80             ),
81             },
82             },
83             epoch_year =>{
84             isa => Int,
85             reader => 'get_epoch_year',
86             default => 1904,
87             },
88             error_inst =>{
89             isa => HasMethods[qw(
90             error set_error clear_error set_warnings if_warn
91             ) ],
92             clearer => '_clear_error_inst',
93             reader => 'get_error_inst',
94             required => 1,
95             handles =>[ qw(
96             error set_error clear_error set_warnings if_warn
97             ) ],
98             default => sub{ Spreadsheet::Reader::ExcelXML::Error->new() },
99             },
100             },
101             add_methods =>{
102             get_empty_return_type => sub{ 1 },
103             },
104             );
105             my $format_instance = build_instance(
106             package => 'FormatInstance',
107             superclasses => [ 'Spreadsheet::Reader::Format::FmtDefault' ],
108             add_roles_in_sequence =>[qw(
109             Spreadsheet::Reader::Format::ParseExcelFormatStrings
110             Spreadsheet::Reader::Format
111             )],
112             target_encoding => 'latin1',# Adjust the string output encoding here
113             workbook_inst => $workbook_instance,
114             );
115             $workbook_instance->set_formatter_inst( $format_instance );
116             my $test_instance = build_instance(
117             package => 'StylesInterface',
118             superclasses => ['Spreadsheet::Reader::ExcelXML::XMLReader'],
119             add_roles_in_sequence => [
120             'Spreadsheet::Reader::ExcelXML::XMLReader::PositionStyles',
121             'Spreadsheet::Reader::ExcelXML::Styles',
122             ],
123             file => '../../../../t/test_files/xl/styles.xml',,
124             workbook_inst => $workbook_instance,
125             );
126             print Dumper( $test_instance->get_format( 2 ) );
127              
128             #######################################
129             # SYNOPSIS Screen Output
130             # 01: $VAR1 = {
131             # 02: 'cell_style' => {
132             # 03: 'builtinId' => '0',
133             # 04: 'xfId' => '0',
134             # 05: 'name' => 'Normal'
135             # 06: },
136             # 07: 'cell_font' => {
137             # 08: 'name' => 'Calibri',
138             # 09: 'family' => '2',
139             # 10: 'scheme' => 'minor',
140             # 11: 'sz' => '11',
141             # 12: 'color' => {
142             # 13: 'theme' => '1'
143             # 14: }
144             # 15: },
145             # 16: 'cell_fill' => {
146             # 17: 'patternFill' => {
147             # 18: 'patternType' => 'none'
148             # 19: }
149             # 20: },
150             # 21: 'cell_border' => {
151             # 22: 'diagonal' => undef,
152             # 23: 'bottom' => undef,
153             # 24: 'right' => undef,
154             # 25: 'top' => undef,
155             # 26: 'left' => undef
156             # 27: },
157             # 28: 'cell_coercion' => bless( {
158             ~~ Skipped 142 lines ~~
159             #170: 'display_name' => 'Excel_date_164',
160             #171: 'name' => 'DATESTRING',
161             #172: }, 'Type::Tiny' ),
162             #173: 'applyNumberFormat' => '1',
163             #174: };
164             #######################################
165              
166             =head1 DESCRIPTION
167              
168             This documentation is written to explain ways to use this module. To use the general
169             package for excel parsing out of the box please review the documentation for L<Workbooks
170             |Spreadsheet::Reader::ExcelXML>, L<Worksheets
171             |Spreadsheet::Reader::ExcelXML::Worksheet>, and
172             L<Cells|Spreadsheet::Reader::ExcelXML::Cell>.
173              
174             This role is written as the interface for getting useful data from the sub file 'styles.xml'
175             that is a member of a zipped (.xlsx) archive or a stand alone XML text file containing an
176             equivalent subset of information in the 'Styles' node. The styles.xml file contains the
177             format and display options used by Excel for showing the stored data. The SYNOPSIS shows
178             the (very convoluted) way to get this interface wired up and working. Unless you are
179             trying to rewrite this package don't pay attention to that. The package will build it
180             for you. This interface doesn't hold any of the functionality it just mandates certain
181             behaviors below it. The documentation is the explanation of how the final class should
182             perform when the layers below are correctly implemented.
183              
184             =head2 Method(s)
185              
186             These are the methods mandated by this interface.
187              
188             =head3 get_format( ($position|$name), [$header], [$exclude_header] )
189              
190             =over
191              
192             B<Definition:> This will return the styles information from the identified $position
193             (counting from zero) or $name. The target position is usually drawn from the cell
194             data stored in the worksheet. The information is returned as a perl hash ref. Since
195             the styles data is in two tiers it finds all the subtier information for each indicated
196             piece and appends them to the hash ref as values for each type key.
197              
198             B<Accepts position 0:> dependant on the role implementation; $position = an integer for
199             the styles $position. (from L<Spreadsheet::Reader::ExcelXML::XMLReader::PositionStyles>),
200             $name = a (sub) node name indicating which styles node should be returned (from
201             L<Spreadsheet::Reader::ExcelXML::XMLReader::NamedStyles>)
202              
203             B<Accepts position 1:> $header = the target header key (use the
204             L<Spreadsheet::Reader::ExcelXML::Cell/Attributes> that are cell formats as the definition
205             of range for this.) It will cause only this header subset to be returned
206              
207             B<Accepts position 2:> $exclude_header = the target header key (use the
208             L<Spreadsheet::Reader::ExcelXML::Cell/Attributes> that are cell formats as the definition
209             of range for this.) It will exclude the header from the returned data set.
210              
211             B<Returns:> a hash ref of data
212              
213             =back
214              
215             =head3 get_default_format( [$header], [$exclude_header] )
216              
217             =over
218              
219             B<Definition:> For any cell that does not have a unquely identified format excel generally
220             stores a default format for the remainder of the sheet. This will return the two
221             tiered default styles information. The information is returned in the same format as the
222             get_format method.
223              
224             B<Accepts position 0:> $header = the target header key (use the
225             L<Spreadsheet::Reader::ExcelXML::Cell/Attributes> that are cell formats as the definition
226             of range for this.) It will cause only this header subset to be returned
227              
228             B<Accepts position 1:> $exclude_header = the target header key (optional at position 2) (use the
229             L<Spreadsheet::Reader::ExcelXML::Cell/Attributes> that are cell formats as the definition
230             of range for this.) It will exclude the header from the returned data set.
231              
232             B<Returns:> a hash ref of data
233              
234             =back
235              
236             =head3 loaded_correctly
237              
238             =over
239              
240             B<Definition:> When building a styles reader it may be that the file is deformed. This is
241             the way to know if the reader thought the file was good.
242              
243             B<Accepts:> Nothing
244              
245             B<Returns:> (1|0)
246              
247             =back
248              
249             =head2 Attributes
250              
251             Data passed to new when creating an instance with this interface. For
252             modification of this(ese) attribute(s) see the listed 'attribute
253             methods'. For more information on attributes see
254             L<Moose::Manual::Attributes>. The easiest way to modify this(ese)
255             attribute(s) is during instance creation before it is passed to the
256             workbook or parser.
257              
258             =head3 file
259              
260             =over
261              
262             B<Definition:> This attribute holds the file handle for the file being read. If
263             the full file name and path is passed to the attribute the class will coerce that
264             into an L<IO::File> file handle.
265              
266             B<Default:> no default - this must be provided to read a file
267              
268             B<Required:> yes
269              
270             B<Range:> any unencrypted styles.xml file name and path or IO::File file
271             handle with that content.
272              
273             B<attribute methods> Methods provided to adjust this attribute
274              
275             =over
276              
277             B<set_file>
278              
279             =over
280              
281             B<Definition:> change the file value in the attribute (this will reboot
282             the file instance and lock the file)
283              
284             =back
285              
286             B<get_file>
287              
288             =over
289              
290             B<Definition:> Returns the file handle of the file even if a file name
291             was passed
292              
293             =back
294              
295             B<has_file>
296              
297             =over
298              
299             B<Definition:> this is used to see if the file loaded correctly.
300              
301             =back
302              
303             B<clear_file>
304              
305             =over
306              
307             B<Definition:> this clears (and unlocks) the file handle
308              
309             =back
310              
311             =back
312              
313             =back
314              
315             =head3 cache_positions
316              
317             =over
318              
319             B<Definition:> Especially for sheets with lots of stored formats the
320             parser can slow way down when accessing each postion. This is
321             because the are not stored sequentially and the reader is a JIT linear
322             parser. To go back it must restart and index through each position till
323             it gets to the right place. This is especially true for excel sheets
324             that have experienced any significant level of manual intervention prior
325             to being read. This attribute sets caching (default on) for styles
326             so the parser builds and stores all the styles settings at the beginning.
327             If the file is cached it will close and release the file handle in order
328             to free up some space. (a small win in exchange for the space taken by
329             the cache).
330              
331             B<Default:> 1 = caching is on
332              
333             B<Range:> 1|0
334              
335             B<Attribute required:> yes
336              
337             B<attribute methods> Methods provided to adjust this attribute
338              
339             =over
340              
341             none - (will be autoset by L<Spreadsheet::Reader::ExcelXML/cache_positions>)
342              
343             =back
344              
345             =back
346              
347             =head1 SUPPORT
348              
349             =over
350              
351             L<github Spreadsheet::Reader::ExcelXML/issues
352             |https://github.com/jandrew/p5-spreadsheet-reader-excelxml/issues>
353              
354             =back
355              
356             =head1 TODO
357              
358             =over
359              
360             B<1.> Nothing yet
361              
362             =back
363              
364             =head1 AUTHOR
365              
366             =over
367              
368             =item Jed Lund
369              
370             =item jandrew@cpan.org
371              
372             =back
373              
374             =head1 COPYRIGHT
375              
376             This program is free software; you can redistribute
377             it and/or modify it under the same terms as Perl itself.
378              
379             The full text of the license can be found in the
380             LICENSE file included with this module.
381              
382             This software is copyrighted (c) 2016 by Jed Lund
383              
384             =head1 DEPENDENCIES
385              
386             =over
387              
388             L<Spreadsheet::Reader::ExcelXML> - the package
389              
390             =back
391              
392             =head1 SEE ALSO
393              
394             =over
395              
396             L<Spreadsheet::Read> - generic Spreadsheet reader
397              
398             L<Spreadsheet::ParseExcel> - Excel binary version 2003 and earlier (.xls files)
399              
400             L<Spreadsheet::XLSX> - Excel version 2007 and later
401              
402             L<Spreadsheet::ParseXLSX> - Excel version 2007 and later
403              
404             L<Log::Shiras|https://github.com/jandrew/Log-Shiras>
405              
406             =over
407              
408             All lines in this package that use Log::Shiras are commented out
409              
410             =back
411              
412             =back
413              
414             =cut
415              
416             #########1#########2 main pod documentation end 5#########6#########7#########8#########9