ReadSXC.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« ParseODS.pm ↑ Summary
1 package Spreadsheet::ReadSXC;
2  
3 11 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 our @EXPORT_OK = qw(read_sxc read_sxc_fh read_xml_file read_xml_string);
10 our $VERSION = '0.39';
11  
12 11 ▶use Archive::Zip ':ERROR_CODES';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶use Spreadsheet::ParseODS;
Subroutine BEGIN: called CC=1 SCAR=2.6
15 use PerlX::Maybe;
16  
17 our @CARP_NOT = qw(Spreadsheet::ParseODS);
18  
19 sub zip_error_handler {}
20  
21 sub read_sxc ($;$) {
22 my ($sxc_file, $options_ref) = @_;
23 #if( !$options_ref->{StrictErrors}) {
24 # -f $sxc_file && -s _ or return undef;
25 #};
26 #open my $fh, '<', $sxc_file
27 # or croak "Couldn't open '$sxc_file': $!";
28 #read_sxc_fh( $fh, $options_ref );
29 _parse_xml( {}, $options_ref, $sxc_file );
30 }
31  
32 sub read_sxc_fh {
33 my( $stream, $options_ref ) = @_;
34 _parse_xml( {}, $options_ref, $stream );
35 }
36  
37 sub read_xml_file ($;$) {
38 my ($xml_file, $options_ref) = @_;
39 if( !$options_ref->{StrictErrors}) {
40 -f $xml_file && -s _ or return undef;
41 };
42 _parse_xml({ method => 'parsefile', type => 'xml' }, $options_ref, $xml_file);
43 }
44  
45 sub read_xml_string ($;$) {
46 my ($xml_string, $options_ref) = @_;
47 _parse_xml( { type => 'xml' }, $options_ref, \$xml_string );
48 }
49  
50 sub _parse_xml {
51 my ($internal_options, $options_ref, $xml_thing) = @_;
52  
53 $options_ref ||= {};
54  
55 my $type;
56 if( $internal_options->{ type } ) {
57 $type = $internal_options->{ type };
58 };
59  
60 my $line_sep = $options_ref->{ReplaceNewlineWith} || "";
61  
62 my $workbook;
63 my $ok = eval {
64 $workbook = Spreadsheet::ParseODS->new(
65 line_separator => $line_sep,
66 #%$options_ref,
67 )->parse( $xml_thing,
68 maybe inputtype => $type );
69 1;
70 };
71 if( my $err = $@ and $options_ref->{ StrictErrors } ) {
72 die "$@\n"
73 };
74 return unless $workbook;
75  
76 # Cut off trailing columns if any
77 # Down-convert from ::Cell to raw values, depending on the options
78  
79 # This conversion is likely best done in an immediate callback to
80 # speed up things
81 my $res = {};
82 for my $s ($workbook->worksheets) {
83 my $rs = $res->{ $s->label } = [];
84 for my $r ($s->row_min..$s->row_max) {
85 if( $options_ref->{DropHiddenRows} and $s->is_row_hidden( $r ) ) {
86 next;
87 };
88  
89 my $rowref;
90 for my $c ($s->col_min..$s->col_max) {
91 # Depending on what type we want, use ->value or ->unformatted
92 # depending on $options_ref->{ ... }
93 if( ! $rowref ) {
94 push @$rs, ($rowref = []);
95 };
96  
97 my $cell = $s->get_cell( $r,$c );
98 if( $options_ref->{DropHiddenColumns} and $s->is_col_hidden($c)) {
99 next;
100 };
101  
102 my ($method,$type) = ('value',$cell->type);
103 $type ||= '';
104  
105 if( $options_ref->{StandardCurrency} and $type =~ qr/^(float|currency|percentage)/) {
106 $method = 'unformatted';
107 };
108 if( $options_ref->{StandardDate} and $type =~ qr/^(date)/) {
109 $method = 'unformatted';
110 };
111 if( $options_ref->{StandardTime} and $type =~ qr/^(time)/) {
112 $method = 'unformatted';
113 };
114  
115 push @$rowref, $s->get_cell( $r,$c )->$method;
116 }
117 }
118 };
119  
120 if ( $options_ref->{OrderBySheet} ) {
121 return [ map { $res->{ $_->{label} } } $workbook->worksheets ]
122 } else {
123 return $res
124 }
125 }
126  
127 1;
128  
129 __END__
« ParseODS.pm ↑ Summary