ReadGnumeric.pm

filter: Statementstmt 93.7% 241 / 257 Branchbran 83.0% 93 / 112 Conditioncond 86.8% 33 / 38 MC/DCmcdc 75.0% 15 / 20 Subroutinesub 97.4% 38 / 39 Podpod 100% 5 / 5 totaltotal 90.2% 425 / 471 CC 80 SCAR 19.6
CC
80
Cov
90%
_process_Sheet_elt
20.5
_process_Cell_elt
10.2
find_style_for_cell
7.0
CRAP 7.1
↑ Summary Gnumeric/Base.pm »
1 # -*- mode: perl; -*-
2 #
3 # Spreadsheet reader for Gnumeric format that returns a
4 # Spreadsheet::Read-compatible data structure.
5 #
6 # Documentation below "__END__".
7 #
8 # [created. -- rgr, 26-Dec-22.]
9 #
10  
11 package Spreadsheet::ReadGnumeric;
12  
13 4 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶use XML::Parser::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Spreadsheet::Gnumeric::StyleRegion;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 our $VERSION = '0.4';
22  
23 4 ▶use parent qw(Spreadsheet::Gnumeric::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 Spreadsheet::ReadGnumeric->define_instance_accessors
27 (# User options to control parsing.
28 qw(gzipped_p cells rc attr merge),
29 qw(process_formulas minimal_attributes convert_colors),
30 # Intermediate data structures for sheet parsing.
31 qw(sheet style_regions style_attributes formulas),
32 # XML parsing internals.
33 qw(current_elt current_attrs chars namespaces element_stack));
34 }
35  
36 13 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
37 13 my ($class, @options) = @_;
38  
39 13 my $self = $class->SUPER::new(@options);
40 13 ▶ $self->{_cells} //= 1;
Condition100%2/2
$self->{"_cells"} //= 1
l!l
67
MC/DC100%1/1
$self->{"_cells"} //= 1
$self->{"_cells"}
Truth table100%2/2
A
A= $self->{"_cells"}
Aresult
11
00
41 13 ▶ $self->{_rc} //= 1;
Condition100%2/2
$self->{"_rc"} //= 1
l!l
67
MC/DC100%1/1
$self->{"_rc"} //= 1
$self->{"_rc"}
Truth table100%2/2
A
A= $self->{"_rc"}
Aresult
11
00
42 13 ▶ $self->{_convert_colors} //= 1;
Condition100%2/2
$self->{"_convert_colors"} //= 1
l!l
112
MC/DC100%1/1
$self->{"_convert_colors"} //= 1
$self->{"_convert_colors"}
Truth table100%2/2
A
A= $self->{"_convert_colors"}
Aresult
11
00
43 13 return $self;
44 }
45  
46 83 ▶sub sheets {
Subroutine sheets: called CC=2 SCAR=6.9
Pod documented
47 # Read-only slot. Make sure the sheets slot is initialized.
48 83 my ($self) = @_;
49  
50 83 my $sheets = $self->{_sheets};
51 83 ▶ unless ($sheets) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sheets) 13 70 83
52 # [I'm afraid this is just cargo-culting here. -- rgr, 27-Dec-22.]
53 13 my $parser_data = { parser => __PACKAGE__,
54 type => 'gnumeric',
55 version => $VERSION };
56 13 my $attrs = { parsers => [ $parser_data ],
57 %$parser_data,
58 error => undef };
59 13 $sheets = [ $attrs ];
60 13 $self->{_sheets} = $sheets;
61 }
62 83 return $sheets;
63 }
64  
65 ### XML parsing support.
66  
67 # This is because XML::Parser::Lite callbacks are called with the Expat object
68 # and not us, so we must bind $Self dynamically around the parsing operation.
69 4 ▶use vars qw($Self);
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 0 ▶sub _context_string {
Subroutine _context_string: not called CC=2 SCAR=17.9
72 0 my ($self) = @_;
73  
74 0 my $stack = $self->element_stack;
75 0 ▶ return $self->current_elt
Branch: 1 branch
BranchTrueFalseTotal
unless $stack 0 0 0
76 unless $stack;
77 0 return join(' ', (map { $_->[0]; } @$stack), $self->current_elt);
78 }
79  
80 13154 ▶sub _decode_xlmns_name {
Subroutine _decode_xlmns_name: called CC=4 SCAR=14.2
81 # Figure out whether we have a Gnumeric element name.
82 13154 my ($self, $elt_name) = @_;
83  
84 13154 my ($ns_prefix, $base_elt_name) = $elt_name =~ /^([^:]+):([^:]+)$/;
85 13154 ▶ if ($ns_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns_prefix) { } 13154 0 13154
86 # See if the prefix is the one we want.
87 13154 my $url = $self->namespaces->{$ns_prefix};
88 13154 ▶ if ($url && $url eq "http://www.gnumeric.org/v10.dtd") {
Branch: 1 branch
BranchTrueFalseTotal
if ($url and $url eq 'http://www.gnumeric.org/v10.dtd') { } 12930 224 13154
Condition66%2/3
$url and $url eq "http://www.gnumeric.org/v10.dtd"
!ll&&!rl&&r
022412930
MC/DC50%1/2
$url and $url eq "http://www.gnumeric.org/v10.dtd"
$url $url eq "http://www.gnumeric.org/v10.dtd"
Truth table66%2/3
A and B
A= $urlB= $url eq "http://www.gnumeric.org/v10.dtd"
ABresult
0X0
100
111
89 # Belongs to the Gnumeric schema.
90 12930 return ($base_elt_name, 1);
91 }
92 else {
93 # It's something else.
94 224 return ($elt_name, 0);
95 }
96 }
97 else {
98 # Assume unqualified names belong to Gnumeric (even though we've never
99 # seen any).
100 0 return ($elt_name, 1);
101 }
102 }
103  
104 6577 ▶sub _handle_start {
Subroutine _handle_start: called CC=7 SCAR=19.5
105 6577 my ($expat, $elt, @attrs) = @_;
106 6577 my $attrs = { @attrs };
107  
108 # Establish the new namespace scope. We do this first, because it may
109 # define the prefix used on this element.
110 6577 ▶ my $old_ns_scope = $Self->namespaces || { };
Condition100%2/2
$Spreadsheet::ReadGnumeric::Self->namespaces || {}
l!l
656413
MC/DC100%1/1
$Spreadsheet::ReadGnumeric::Self->namespaces || {}
$Spreadsheet::ReadGnumeric::Self->namespaces
Truth table100%2/2
A
A= $Spreadsheet::ReadGnumeric::Self->namespaces
Aresult
11
00
111 6577 my $new_ns_scope = { %$old_ns_scope };
112 6577 ▶ if (grep { /^xmlns:(.*)$/ } keys(%$attrs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^xmlns:(.*)$/;} keys %$attrs) 26 6551 6577
113 # Copy so as not to clobber the outer scope.
114 26 $new_ns_scope = { %$old_ns_scope };
115 26 for my $attr (keys(%$attrs)) {
116 117 ▶ if ($attr =~ /^xmlns:(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /^xmlns:(.*)$/) 91 26 117
117 91 my $ns_prefix = $1;
118 91 $new_ns_scope->{$ns_prefix} = $attrs->{$attr};
119 }
120 }
121 }
122 6577 $Self->namespaces($new_ns_scope);
123  
124 # Stack the outer context.
125 6577 ▶ my $stack = $Self->element_stack || [ ];
Condition100%2/2
$Spreadsheet::ReadGnumeric::Self->element_stack || []
l!l
656413
MC/DC100%1/1
$Spreadsheet::ReadGnumeric::Self->element_stack || []
$Spreadsheet::ReadGnumeric::Self->element_stack
Truth table100%2/2
A
A= $Spreadsheet::ReadGnumeric::Self->element_stack
Aresult
11
00
126 6577 ▶ push(@$stack, [ $Self->current_elt, $Self->current_attrs,
Branch: 1 branch
BranchTrueFalseTotal
if $Spreadsheet::ReadGnumeric::Self->current_elt 6564 13 6577
127 $Self->chars, $old_ns_scope ])
128 if $Self->current_elt;
129 6577 $Self->element_stack($stack);
130  
131 # Install the new element context.
132 6577 my ($decoded_name) = $Self->_decode_xlmns_name($elt);
133 6577 $Self->current_elt($decoded_name);
134 6577 $Self->current_attrs($attrs);
135 6577 $Self->chars('');
136 }
137  
138 11339 ▶sub _handle_char {
Subroutine _handle_char: called CC=1 SCAR=0.0
139 # Just collect them in our "chars" slot.
140 11339 my ($expat, $chars) = @_;
141  
142 11339 if (0) {
143 warn("handle_char: '$chars' in ", $Self->_context_string, "\n")
144 unless $chars =~ /^\s*$/;
145 }
146 11339 $Self->{_chars} .= $chars;
147 }
148  
149 6577 ▶sub _handle_end {
Subroutine _handle_end: called CC=4 SCAR=13.9
150 6577 my ($expat, $raw_elt_name) = @_;
151  
152 # Process the completed element.
153 6577 my ($elt_name, $gnumeric_p) = $Self->_decode_xlmns_name($raw_elt_name);
154 6577 ▶ if ($gnumeric_p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gnumeric_p) 6465 112 6577
155 6465 my $method = "_process_${elt_name}_elt";
156 6465 ▶ $Self->$method($Self->chars, %{$Self->current_attrs})
Branch: 1 branch
BranchTrueFalseTotal
if $Spreadsheet::ReadGnumeric::Self->can($method) 4275 2190 6465
157 if $Self->can($method);
158 }
159  
160 # Restore the outer element context.
161 6577 my $stack = $Self->element_stack;
162 return
163 6577 ▶ unless @$stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @$stack 13 6564 6577
164 6564 my ($elt, $attrs, $chars, $old_ns_scope) = @{pop(@$stack)};
165 6564 $Self->current_elt($elt);
166 6564 $Self->current_attrs($attrs);
167 6564 $Self->chars($chars);
168 6564 $Self->namespaces($old_ns_scope);
169 }
170  
171 ### Utility routines.
172  
173 144 ▶sub _decode_alpha {
Subroutine _decode_alpha: called CC=2 SCAR=6.9
174 # Note that this is one-based, so "A" is 1, "Z" is 26, and "AA" is 27. But
175 # there is no consistent zero digit, so this is not really base 26; if it
176 # were, "A" should be zero and "Z" should overflow to "BA".
177 144 my ($alpha) = @_;
178  
179 144 ▶ if (length($alpha) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $alpha == 0) { } 54 90 144
180 54 return 0;
181 }
182 else {
183 90 return (26 * _decode_alpha(substr($alpha, 0, -1))
184 + ord(lc(substr($alpha, -1))) - ord('a') + 1)
185 }
186 }
187  
188 22 ▶sub _decode_cell_name {
Subroutine _decode_cell_name: called CC=2 SCAR=7.0
189 22 my ($cell_name) = @_;
190  
191 22 my ($alpha, $digits) = $cell_name =~ /^([a-zA-Z]+)(\d+)$/;
192 return
193 22 ▶ unless $alpha;
Branch: 1 branch
BranchTrueFalseTotal
unless $alpha 0 22 22
194 22 return (_decode_alpha($alpha), 0 + $digits);
195 }
196  
197 1322 ▶sub _encode_cell_name {
Subroutine _encode_cell_name: called CC=2 SCAR=6.9
198 # Note that $value is one-based, so 1 corresponds to "A". See the note
199 # under _decode_alpha.
200 1322 my ($value) = @_;
201  
202 1322 ▶ return ($value <= 26
Branch: 1 branch
BranchTrueFalseTotal
$value <= 26 ? : 1286 36 1322
203 ? chr(ord('A') + $value - 1)
204 : (_encode_cell_name(int(($value - 1) / 26))
205 . _encode_cell_name(1 + ($value - 1) % 26)));
206 }
207  
208 3 ▶sub _tokenize_formula {
Subroutine _tokenize_formula: called CC=4 SCAR=14.2
209 # This only cares about cell names; everything else is left as strings.
210 # But we have to tokenize well enough that we don't mistake the "E9" in the
211 # number "9E9" for a cell name, which is why we must distinguish numbers.
212 # The result is an arrayref of two kinds of elements: Cell names are
213 # turned into two-element arrayrefs of _decode_cell_name indices, and
214 # everything else (including whitespace) remains a string.
215 3 my ($formula) = @_;
216  
217 3 my $parsed = [ ];
218 3 ▶ while (length($formula)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $formula) 29 3 32
219 29 ▶ if ($formula =~ /^([-+]?[\d.]+([-+]?[eE]\d+)?)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($formula =~ /^([-+]?[\d.]+([-+]?[eE]\d+)?)/) { } 0 29 29
elsif ($formula =~ /^(\w+\d+)/) { } 14 15 29
220 # This includes some malformed numeric constants, but we don't have
221 # to care; there shouldn't be any such in saved spreadsheets.
222 0 my $number = $1;
223 0 push(@$parsed, $number);
224 0 $formula = substr($formula, length($number));
225 }
226 elsif ($formula =~/^(\w+\d+)/) {
227 14 my $name = $1;
228 14 push(@$parsed, [ _decode_cell_name($name) ]);
229 14 $formula = substr($formula, length($name));
230 }
231 else {
232 15 my ($stuff) = $formula =~ /^([^\w\d]+)/;
233 15 push(@$parsed, $stuff);
234 15 $formula = substr($formula, length($stuff));
235 }
236 }
237 3 return $parsed;
238 }
239  
240 7 ▶sub _untokenize_formula {
Subroutine _untokenize_formula: called CC=2 SCAR=6.9
241 # Given a parsed formula returned by _tokenize_formula above plus
242 # column and row offsets, construct a new formula string and return it.
243 7 my ($parsed, $delta_col, $delta_row) = @_;
244  
245 join('', map {
246 7 ▶ if (ref($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 32 35 67
247 32 my ($col, $row) = @$_;
248 32 _encode_cell_name($col + $delta_col) . ($row + $delta_row);
249 }
250 else {
251 35 $_;
252 }
253 } @$parsed);
254 }
255  
256 ### Spreadsheet parsing
257  
258 13 ▶sub _parse_stream {
Subroutine _parse_stream: called CC=1 SCAR=0.0
259 # Create a new XML::Parser::Lite instance, use it to drive the parsing of
260 # $xml_stream (which we assume is uncompressed), and return the resulting
261 # spreadsheet object.
262 13 my ($self, $xml_stream) = @_;
263  
264 13 my $parser = XML::Parser::Lite->new
265 (Style => 'Stream',
266 Handlers => { Start => \&_handle_start,
267 End => \&_handle_end,
268 Char => \&_handle_char });
269 13 local $Self = $self;
270 13 $parser->parse(join('', <$xml_stream>));
271 13 return $self->sheets;
272 }
273  
274 13 ▶sub stream_gzipped_p {
Subroutine stream_gzipped_p: called CC=3 SCAR=12.3
Pod documented
275 13 my ($self, $stream, $file) = @_;
276  
277 # The point of the gzipped_p slot is to allow callers to suppress the gzip
278 # test if the stream is not seekable.
279 13 my $gzipped_p = $self->gzipped_p;
280 13 ▶ if (! defined($gzipped_p)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gzipped_p) 13 0 13
281 13 ▶ read($stream, my $block, 2) or do {
Branch: 1 branch
BranchTrueFalseTotal
read $stream, my $block, 2 or do { my $file_msg = "from stream"; $file_msg = " from '${file}'"; die "${self}: Failed to read opening bytes${file_msg}: $!" } 13 0 13
282 0 my $file_msg = 'from stream';
283 0 $file_msg = " from '$file'";
284 0 die "$self: Failed to read opening bytes$file_msg: $!";
285 };
286 # Test if gzipped (/usr/share/misc/magic).
287 13 $gzipped_p = $block eq "\037\213";
288 13 seek($stream, 0, 0);
289 }
290 13 return $gzipped_p;
291 }
292  
293 13 ▶sub parse {
Subroutine parse: called CC=6 SCAR=17.9
Pod documented
294 13 my ($self, $input) = @_;
295  
296 13 my $stream;
297 13 ▶ if (ref($input)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $input) { } 2 11 13
elsif ($input =~ /\A(\037\213|<\?xml)/) { } 2 9 11
298 # Assume it's a stream.
299 2 $stream = $input;
300 }
301 elsif ($input =~ m/\A(\037\213|<\?xml)/) {
302 # $input is literal content, compressed and/or XML.
303 2 open($stream, '<', \$input);
304 }
305 else {
306 9 ▶ open($stream, '<', $input)
Branch: 1 branch
BranchTrueFalseTotal
open $stream, "<", $input or die "${self}: Failed to open '${input}': $!" 9 0 9
307 or die "$self: Failed to open '$input': $!";
308 }
309 13 ▶ binmode($stream, ':gzip')
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
if $self->stream_gzipped_p($stream, ref $input ? () : $input) 6 7 13
ref $input ? : 2 11 13
310 if $self->stream_gzipped_p($stream, ref($input) ? () : $input);
311 13 binmode($stream, ':encoding(UTF-8)');
312 13 return $self->_parse_stream($stream);
313 }
314  
315 171 ▶sub _process_Name_elt {
Subroutine _process_Name_elt: called CC=2 SCAR=6.9
316 # Record the sheet name.
317 171 my ($self, $name) = @_;
318  
319 # Find the enclosing element, which needs to be "Sheet".
320 171 my $stack = $self->element_stack;
321 return
322 171 ▶ unless $stack->[@$stack-1][0] eq 'Sheet';
Branch: 1 branch
BranchTrueFalseTotal
unless $$stack[@$stack - 1][0] eq "Sheet" 136 35 171
323 35 my $sheets = $self->sheets;
324 35 $sheets->[0]{sheet}{$name} = @$sheets;
325 35 $self->{_sheet}{label} = $name;
326 }
327  
328 35 ▶sub _process_MaxCol_elt {
Subroutine _process_MaxCol_elt: called CC=1 SCAR=0.0
329 35 my ($self, $maxcol) = @_;
330  
331 35 $self->{_sheet}{mincol} = 1;
332 35 $self->{_sheet}{maxcol} = 1 + $maxcol;
333 }
334  
335 35 ▶sub _process_MaxRow_elt {
Subroutine _process_MaxRow_elt: called CC=2 SCAR=7.0
336 35 my ($self, $maxrow) = @_;
337  
338 return
339 35 ▶ unless defined($maxrow);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxrow 0 35 35
340 35 $self->{_sheet}{minrow} = 1;
341 35 $self->{_sheet}{maxrow} = 1 + $maxrow;
342 }
343  
344 4 ▶sub _process_Merge_elt {
Subroutine _process_Merge_elt: called CC=2 SCAR=7.0
345 # We don't care about MergedRegions, as that's just a container.
346 4 my ($self, $text) = @_;
347  
348 4 push(@{$self->{_sheet}{merged}},
349 4 ▶ [ map { _decode_cell_name($_) } split(':', $text) ])
Branch: 1 branch
BranchTrueFalseTotal
if $self->attr 4 0 4
350 if $self->attr;
351 }
352  
353 2348 ▶sub _process_Cell_elt {
Subroutine _process_Cell_elt: called CC=10 SCAR=23.2
354 2348 my ($self, $text, %keys) = @_;
355  
356 # Both $row and $col are zero-based; the cell matrix is one-based.
357 2348 my ($row, $col) = ($keys{Row}, $keys{Col});
358  
359 # Deal with ExprID references.
360 2348 ▶ if (my $id = $keys{ExprID}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $keys{"ExprID"}) 720 1628 2348
361 720 ▶ if (! $self->process_formulas) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $self->process_formulas) { } 710 10 720
elsif ($text) { } 4 6 10
elsif (my $expr = $self->{'_formulas'}[$id]) { } 6 0 6
362 # The caller doesn't care.
363 }
364 elsif ($text) {
365 # Defining this formula.
366 4 $self->{_formulas}[$id] = [ $col, $row, $text, undef ];
367 }
368 elsif (my $expr = $self->{_formulas}[$id]) {
369 # Reference to a previously defined formula.
370 6 my ($def_col, $def_row, $def_text, $parsed) = @$expr;
371 6 ▶ $parsed = $expr->[3] = _tokenize_formula($def_text)
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 2 4 6
372 unless $parsed;
373 6 $text = _untokenize_formula($parsed,
374 $col - $def_col, $row - $def_row);
375 }
376 else {
377 # The spreadsheet save code should ensure this never happens.
378 0 my $cell = _encode_cell_name($col + 1) . ($row + 1);
379 0 my $label = $self->{_sheet}{label};
380 0 warn("Undefined ExprID '$id' in $cell of ",
381 "sheet '$label'; ignoring.\n");
382 }
383 }
384  
385 # Ignore empty cells.
386 return
387 2348 ▶ unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 692 1656 2348
388 1656 ▶ $self->{_sheet}{cell}[$col + 1][$row + 1] = $text
Branch: 1 branch
BranchTrueFalseTotal
if $self->rc 1176 480 1656
389 if $self->rc;
390 1656 ▶ $self->{_sheet}{_encode_cell_name($col + 1) . ($row + 1)} = $text
Branch: 1 branch
BranchTrueFalseTotal
if $self->cells 1176 480 1656
391 if $self->cells;
392 1656 ▶ $self->{_sheet}{attr}[$col + 1][$row + 1]
Branch: 1 branch
BranchTrueFalseTotal
if $self->attr 342 1314 1656
393 = $self->find_style_for_cell($col, $row)
394 if $self->attr;
395 }
396  
397 35 ▶sub _process_Sheet_elt {
Subroutine _process_Sheet_elt: called CC=20 SCAR=30.2
398 # Add $self->sheet to $self->sheets.
399 35 my ($self, $text, %keys) = @_;
400  
401 35 my $sheets = $self->sheets;
402 35 my $attrs = $sheets->[0];
403 35 my $indx = $attrs->{sheets} = @$sheets;
404 35 my $sheet = $self->sheet;
405 $self->{_sheet}{cell}[0] = [ ] # for consistency.
406 35 ▶ if $self->{_sheet}{cell};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_sheet"}{"cell"} 11 24 35
407 35 push(@$sheets, $sheet);
408 35 $sheet->{indx} = $indx;
409 35 my $attr = $self->attr;
410 35 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 8 27 35
411 8 ▶ $sheet->{style_regions} = $self->style_regions
Branch: 1 branch
BranchTrueFalseTotal
if $attr eq "keep" 3 5 8
412 if $attr eq 'keep';
413 8 $self->style_regions([ ]);
414 # Distribute non-minimal attributes.
415 8 ▶ unless ($self->minimal_attributes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->minimal_attributes) 8 0 8
416 8 for my $col (1 .. $sheet->{maxcol}) {
417 1544 ▶ for my $row (1 .. $sheet->{maxrow} || 0) {
Condition50%1/2
$sheet->{'maxrow'} || 0
l!l
15440
MC/DC0%0/1
$sheet->{'maxrow'} || 0
$sheet->{'maxrow'}
Truth table50%1/2
A
A= $sheet->{'maxrow'}
Aresult
11
00
418 28704 ▶ $self->{_sheet}{attr}[$col][$row]
Condition66%2/3
$self->{"_sheet"}{"attr"}[$col][$row] ||= $self->find_style_for_cell($col - 1, $row - 1)
l!l&&r!l&&!r
342028362
MC/DC50%1/2
$self->{"_sheet"}{"attr"}[$col][$row] ||= $self->find_style_for_cell($col - 1, $row - 1)
$self->{"_sheet"}{"attr"}[$col][$row] $self->find_style_for_cell($col - 1, $row - 1)
Truth table66%2/3
A ||= B
A= $self->{"_sheet"}{"attr"}[$col][$row]B= $self->find_style_for_cell($col - 1, $row - 1)
ABresult
1X1
011
000
419 ||= $self->find_style_for_cell($col - 1, $row - 1);
420 }
421 }
422 }
423 # Distribute merging information.
424 8 my $merged = $sheet->{merged};
425 8 ▶ if ($merged) {
Branch: 1 branch
BranchTrueFalseTotal
if ($merged) 2 6 8
426 2 my $attrs = $sheet->{attr};
427 2 my $merge_p = $self->merge;
428 2 for my $merge (@$merged) {
429 4 my ($col1, $row1, $col2, $row2) = @$merge;
430 4 my $base_cell_name = _encode_cell_name($col1) . $row1;
431 4 ▶ my $merge_flag = $merge_p ? $base_cell_name : 1;
Branch: 1 branch
BranchTrueFalseTotal
$merge_p ? : 2 2 4
432 my $base_cell_contents = ($self->rc
433 ? $sheet->{cell}[$col1][$row1]
434 4 ▶ : $sheet->{$base_cell_name});
Branch: 1 branch
BranchTrueFalseTotal
$self->rc ? : 4 0 4
435 4 for my $col ($col1 .. $col2) {
436 6 for my $row ($row1 .. $row2) {
437 12 ▶ my $cell_attrs
Condition80%4/5
$attrs->[$col][$row] || $self->find_style_for_cell($col - 1, $row - 1) || {}
l!l
48
$attrs->[$col][$row] or $self->find_style_for_cell($col - 1, $row - 1)
l!l&&r!l&&!r
408
MC/DC50%1/2
$attrs->[$col][$row] || $self->find_style_for_cell($col - 1, $row - 1) || {}
$attrs->[$col][$row] $self->find_style_for_cell($col - 1, $row - 1)
Truth table66%2/3
A or B
A= $attrs->[$col][$row]B= $self->find_style_for_cell($col - 1, $row - 1)
ABresult
1X1
011
000
438 = ($attrs->[$col][$row]
439 || $self->find_style_for_cell($col - 1,
440 $row - 1)
441 || { });
442 # Copy the hash so we don't accidentally mark other
443 # cells as merged due to shared structure.
444 12 $cell_attrs = { %$cell_attrs };
445 12 $cell_attrs->{merged} = $merge_flag;
446 12 $attrs->[$col][$row] = $cell_attrs;
447 # When $merge_p, also copy cell contents.
448 12 ▶ if ($merge_p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($merge_p) 6 6 12
449 6 ▶ $sheet->{cell}[$col][$row] = $base_cell_contents
Branch: 1 branch
BranchTrueFalseTotal
if $self->rc 6 0 6
450 if $self->rc;
451 6 my $cell_name = _encode_cell_name($col) . $row;
452 6 ▶ $sheet->{$cell_name} = $base_cell_contents
Branch: 1 branch
BranchTrueFalseTotal
if $self->cells 6 0 6
453 if $self->cells;
454 }
455 }
456 }
457 }
458 }
459 }
460 35 $self->sheet({ });
461 }
462  
463 ## Parsing cell style attributes
464  
465 1665 ▶sub _convert_identity {
Subroutine _convert_identity: called CC=1 SCAR=0.0
466 # We assume this is 0 or 1.
467 1665 my ($value) = @_;
468  
469 1665 return $value;
470 }
471  
472 216 ▶sub _convert_align {
Subroutine _convert_align: called CC=2 SCAR=7.2
473 # This is a string which we truncate and downcase -- and hope the caller
474 # can figure it out, since Gnumeric seems to use non-standard values.
475 216 my ($value) = @_;
476  
477 216 ▶ return $value =~ /^GNM_.ALIGN_(.*)$/ ? lc($1) : lc($value);
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /^GNM_.ALIGN_(.*)$/ ? : 216 0 216
478 }
479  
480 171 ▶sub _convert_color {
Subroutine _convert_color: called CC=1 SCAR=0.0
481 171 my ($color) = @_;
482  
483 513 ▶ my $convert_primary = sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
484 # Pad and/or truncate the value.
485 513 my ($value) = @_;
486  
487 513 my $len = length($value);
488 513 ▶ if ($len <= 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len <= 2) { } 342 171 513
elsif ($len == 3) { } 0 171 171
489 # Less than #xFF we round to zero.
490 342 return '00';
491 }
492 elsif ($len == 3) {
493 0 return '0' . substr($value, 0, 1);
494 }
495 else {
496 171 return substr($value, 0, 2);
497 }
498 171 };
499  
500 171 return join('', '#', map { $convert_primary->($_); } split(':', $color));
501 }
502  
503 4 my %style_conversion_map
504 = (Back => [ bgcolor => \&_convert_color, 1 ],
505 Bold => [ bold => \&_convert_identity ],
506 # Font is not an attribute.
507 Fore => [ fgcolor => \&_convert_color, 1 ],
508 Format => [ format => \&_convert_identity ],
509 HAlign => [ halign => \&_convert_align ],
510 Hidden => [ hidden => \&_convert_identity ],
511 Indent => [ indent => \&_convert_identity ],
512 Italic => [ italic => \&_convert_identity ],
513 Locked => [ locked => \&_convert_identity ],
514 PatternColor => [ pattern_color => \&_convert_color, 1 ],
515 Rotation => [ rotation => \&_convert_identity ],
516 Script => [ script => \&_convert_identity ],
517 Shade => [ shade => \&_convert_identity ],
518 ShrinkToFit => [ shrink_to_fit => \&_convert_identity ],
519 Unit => [ size => \&_convert_identity ],
520 StrikeThrough => [ strike_through => \&_convert_identity ],
521 Underline => [ uline => \&_convert_identity ],
522 VAlign => [ valign => \&_convert_align ],
523 WrapText => [ wrap => \&_convert_identity ]);
524  
525 216 ▶sub _convert_attributes {
Subroutine _convert_attributes: called CC=5 SCAR=16.1
526 # It is a useful coincidence that the cell metadata Spreadsheet::Read
527 # calls attributes are stored in XML attributes by Gnumeric. So we convert
528 # Gnumeric cell attributes to Spreadsheet::Read cell attributes.
529 216 my ($self, $cell_attributes, $gnumeric_attributes) = @_;
530  
531 216 my $convert_colors = $self->convert_colors;
532 216 for my $name (keys(%$gnumeric_attributes)) {
533 2052 my $value = $gnumeric_attributes->{$name};
534 2052 my $conversion = $style_conversion_map{$name};
535 2052 ▶ my ($converted_name, $converter, $color_p)
Branch: 1 branch
BranchTrueFalseTotal
$conversion ? : 2052 0 2052
536 = ($conversion ? @$conversion : (lc($name), \&_convert_identity));
537 2052 ▶ $converter = \&_convert_identity
Branch: 1 branch
BranchTrueFalseTotal
if $color_p and not $convert_colors 153 1899 2052
Condition100%3/3
$color_p and not $convert_colors
!ll&&!rl&&r
1728171153
MC/DC100%2/2
$color_p and not $convert_colors
$color_p not $convert_colors
Truth table100%3/3
A and not(B)
A= $color_pB= not $convert_colors
ABresult
0X0
100
111
538 if $color_p && ! $convert_colors;
539 2052 my $converted_value = $converter->($value);
540 2052 $cell_attributes->{$converted_name} = $converted_value;
541 }
542 }
543  
544 549 ▶sub _process_Font_elt {
Subroutine _process_Font_elt: called CC=2 SCAR=6.9
545 # Process a font definition. This is contained inside <Style>, so it ends
546 # before it, so we initialize the style_attributes slot here, then </Style>
547 # fills it out.
548 549 my ($self, $font_name, %keys) = @_;
549  
550 return
551 549 ▶ unless $self->attr;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->attr 441 108 549
552 108 my $style_attributes = { font => $font_name };
553 108 $self->_convert_attributes($style_attributes, \%keys);
554 108 $self->style_attributes($style_attributes);
555 }
556  
557 549 ▶sub _process_Style_elt {
Subroutine _process_Style_elt: called CC=3 SCAR=11.0
558 # Process style attributes.
559 549 my ($self, $text, %keys) = @_;
560  
561 return
562 549 ▶ unless $self->attr;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->attr 441 108 549
563 108 ▶ my $style_attributes = $self->style_attributes or die;
Branch: 1 branch
BranchTrueFalseTotal
my $style_attributes = $self->style_attributes or die 108 0 108
564 108 $self->_convert_attributes($style_attributes, \%keys);
565 }
566  
567 28712 ▶sub find_style_for_cell {
Subroutine find_style_for_cell: called CC=7 SCAR=19.5
Pod documented
568 # Assuming $self->attr is true, find the style region for ($col, $row) and
569 # return its attribute hash.
570 28712 my ($self, $col, $row) = @_;
571  
572 28712 my $found;
573 28712 for my $style_region (@{$self->{_style_regions}}) {
574 15746 ▶ if ($style_region->start_col <= $col
Branch: 1 branch
BranchTrueFalseTotal
if ($style_region->start_col <= $col and $col <= $style_region->end_col and $style_region->start_row <= $row and $row <= $style_region->end_row) 342 15404 15746
Condition100%9/9
$style_region->start_col <= $col and $col <= $style_region->end_col and $style_region->start_row <= $row and $row <= $style_region->end_row
!ll&&!rl&&r
135561848342
$style_region->start_col <= $col and $col <= $style_region->end_col and $style_region->start_row <= $row
!ll&&!rl&&r
130045522190
$style_region->start_col <= $col and $col <= $style_region->end_col
!ll&&!rl&&r
955234522742
MC/DC100%4/4
$style_region->start_col <= $col and $col <= $style_region->end_col and $style_region->start_row <= $row and $row <= $style_region->end_row
$style_region->start_col <= $col $col <= $style_region->end_col $style_region->start_row <= $row $row <= $style_region->end_row
Truth table100%5/5
A and B and C and D
A= $style_region->start_col <= $colB= $col <= $style_region->end_colC= $style_region->start_row <= $rowD= $row <= $style_region->end_row
ABCDresult
0XXX0
10XX0
110X0
11100
11111
575 && $col <= $style_region->end_col
576 && $style_region->start_row <= $row
577 && $row <= $style_region->end_row) {
578 342 die "duplicate at ($col, $row)"
579 # style regions are supposed to be disjoint; enable this for
580 # extra paranoia.
581 if 0 && $found;
582 342 $found = $style_region;
583 }
584 }
585 28712 ▶ return $found && $found->style_attributes;
Condition66%2/3
$found && $found->style_attributes
!ll&&!rl&&r
283700342
MC/DC50%1/2
$found && $found->style_attributes
$found $found->style_attributes
Truth table66%2/3
A && B
A= $foundB= $found->style_attributes
ABresult
0X0
100
111
586 }
587  
588 549 ▶sub _process_StyleRegion_elt {
Subroutine _process_StyleRegion_elt: called CC=2 SCAR=6.9
589 # Collect a style region for the style attributes we have just defined.
590 549 my ($self, $text, %keys) = @_;
591  
592 return
593 549 ▶ unless $self->attr;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->attr 441 108 549
594 my $style_region = Spreadsheet::Gnumeric::StyleRegion->new
595 (start_col => $keys{startCol}, end_col => $keys{endCol},
596 start_row => $keys{startRow}, end_row => $keys{endRow},
597 108 style_attributes => $self->style_attributes);
598 108 push(@{$self->{_style_regions}}, $style_region);
599 }
600  
601 4 1;
602  
603 __END__
↑ Summary Gnumeric/Base.pm »