RestApi/SheetsApi4/Range.pm

filter: Statementstmt 69.5% 217 / 312 Branchbran 65.9% 58 / 88 Conditioncond 70.0% 28 / 40 MC/DCmcdc 50.0% 11 / 22 Subroutinesub 75.0% 39 / 52 Podpod 73.5% 25 / 34 totaltotal 68.9% 378 / 548 CC 65 SCAR 20.6
CC
65
Cov
69%
range_at_offset
30.0
factory
21.5
range_to_dimension
20.0
CRAP 7.8
« RestApi/SheetsApi4/Request/Spreadsheet/Worksheet.pm ↑ Summary RestApi/Auth/OAuth2Client.pm »
1 package Google::RestApi::SheetsApi4::Range;
2  
3 # some private subroutines here are called by RangeGroup, so think of RangeGroup as
4 # a friend of Range. the routines RangeGroup calls are commented thusly:
5 # "private range routine called here!"
6  
7 # there are different ways to specify ranges. this came about by merging different
8 # ideas from different spreadsheet implementations.
9  
10 # column:
11 # A ====> A:A
12 # A:A ====> A:A
13 # [A] ====> A:A
14 # [[A]] ====> A:A
15 # [1] ====> A:A
16 # [[1]] ====> A:A
17 # {col => A} ====> A:A
18 # [{col => A}] ====> A:A
19 # {col => 1} ====> A:A
20 # [{col => 1}] ====> A:A
21 # a partial column is still considered a column
22 # A5:A10 ====> A5:A10
23  
24 # row:
25 # 1 ====> 1:1
26 # 1:1 ====> 1:1
27 # [undef, 1] ====> 1:1
28 # [[undef, 1]] ====> 1:1
29 # 0's are 'valid' in that they are ignored.
30 # [0, 1] ====> 1:1
31 # [[0, 1]] ====> 1:1
32 # {row => 1} ====> 1:1
33 # [{row => 1}] ====> 1:1
34 # a partial row is still considered a row
35 # D1:1 ====> D1:1
36  
37 # partial rows/columns:
38 # [[A, 5], [A]] ====> A5:A
39 # [{col => A, row => 5}, {col => A}] ====> A5:A
40 # [[A], [A, 5]] ====> A:A5
41 # [{col => A}, {col => A, row => 5}] ====> A:A5
42 # [[5, 1], [undef, 1]] ====> E1:1
43 # [[5, 1], [0, 1]] ====> E1:1
44 # [{col => 5, row => 1}, {row => 1}] ====> E1:1
45  
46 # single cell:
47 # A1 ====> A1
48 # [A, 1] ====> A1
49 # [[A, 1]] ====> A1
50 # [1, 1] ====> A1
51 # [[1, 1]] ====> A1
52 # {col => A, row => 1} ====> A1
53 # [{col => A, row => 1}] ====> A1
54 # {col => 1, row => 1} ====> A1
55 # [{col => 1, row => 1}] ====> A1
56  
57 # same cell twice gets reduced to single cell:
58 # A1:A1 ====> A1
59 # these only get reduced using the factory method.
60 # [[A, 1], [A, 1]] ====> A1
61 # [[1, 1], [1, 1]] ====> A1
62 # [{col => A, row => 1}, {col => A, row => 1}] ====> A1
63 # [{col => 1, row => 1}, {col => 1, row => 1}] ====> A1
64  
65 # general ranges:
66 # A1:B2 ====> A1:B2
67 # [[1,1],[2,2]] ====> A1:B2
68 # [[A,1],[B,2]] ====> A1:B2
69 # [{col => 1, row => 1}, {col => 2, row => 2}] ====> A1:B2
70 # [{col => A, row => 1}, {col => B, row => 2}] ====> A1:B2
71  
72 # mixing is ok:
73 # [A1, [2, 2]] ====> A1:B2
74 # [{col => 1, row => 1}, [2, 2]] ====> A1:B2
75 # [{col => 1. row => 1}, B2] ====> A1:B2
76  
77 # bad ranges:
78 # should be able to support this but makes range routine
79 # too complex, and the simple workaround is to just A1:B2.
80 # [A1, B2]
81  
82 2 our $VERSION = '2.2.5';
83  
84 2 ▶use Google::RestApi::Setup;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 2 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 2 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 ▶use Try::Tiny qw( try catch );
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 2 ▶use experimental qw( switch );
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 2 ▶use aliased 'Google::RestApi::SheetsApi4::Range::Col';
Subroutine BEGIN: called CC=1 SCAR=0.0
95 2 ▶use aliased 'Google::RestApi::SheetsApi4::Range::Row';
Subroutine BEGIN: called CC=1 SCAR=0.0
96 2 ▶use aliased 'Google::RestApi::SheetsApi4::Range::Cell';
Subroutine BEGIN: called CC=1 SCAR=0.0
97 2 ▶use aliased 'Google::RestApi::SheetsApi4::Range::Iterator';
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 2 ▶use parent 'Google::RestApi::SheetsApi4::Request::Spreadsheet::Worksheet::Range';
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 2 Readonly::Scalar our $RANGE_EXPANDED => 1;
102  
103 # this routine returns the best fitting object for the range specified.
104 # A5:A10 will return a Col object. A5:J5 will return a Row object. Etc.
105 # this grew organically into something more complicated that it was worth,
106 # but i got it working reliably so, whatever...
107 124 ▶sub factory {
Subroutine factory: called CC=11 SCAR=30.7
Pod undocumented
108 124 my %original_range_args = @_;
109 124 my $original_range = $original_range_args{range};
110  
111 124 state $check = signature(
112 bless => !!0,
113 multiple => [
114 { named => [
115 worksheet => HasApi,
116 range => RangeCol,
117 dim => DimColRow, { optional => 1 }, # is switched to 'col'.
118 ] },
119 { named => [
120 worksheet => HasApi,
121 range => RangeRow,
122 dim => DimColRow, { optional => 1 }, # is switched to 'row'
123 ] },
124 { named => [
125 worksheet => HasApi,
126 range => RangeCell,
127 dim => DimColRow, { optional => 1 }, # doesn't matter for cells.
128 ] },
129 { named => [
130 worksheet => HasApi,
131 range => RangeAny,
132 dim => DimColRow, { default => 'row' },
133 ] },
134 { named => [
135 worksheet => HasApi,
136 range => RangeNamed,
137 dim => DimColRow, { default => 'row' },
138 ] },
139 ],
140 );
141 124 my @range_args = $check->(@_);
142 124 my $range_args = $range_args[0]; # no idea why it returns an arrayref pointer to a hashref.
143 124 my $range = $range_args->{range};
144  
145 # be careful here, recursive.
146 124 my $result = ${^_TYPE_PARAMS_MULTISIG};
147 124 ▶ if ($result == 0) { return Col->new(%$range_args); }
Branch: 5 branches
BranchTrueFalseTotal
if ($result == 0) { } 12 112 124
elsif ($result == 1) { } 12 100 112
elsif ($result == 2) { } 45 55 100
elsif ($result == 3) { } 55 0 55
elsif ($result == 4) { } 0 0 0
148 12 elsif ($result == 1) { return Row->new(%$range_args); }
149 45 elsif ($result == 2) { return Cell->new(%$range_args); }
150  
151 # if we've translated a range, it now may be a better fit for one of the above.
152 elsif ($result == 3) {
153 # convert the range to A1:A1 format and redrive the factory routine to see
154 # if it ends up being a col, row, or cell range.
155 55 ▶ if ($range ne $original_range) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range ne $original_range) 8 47 55
156 8 DEBUG(sprintf("Range '%s' converted to '$range'", flatten_range($original_range)));
157 # resolve cells by collapsing A1:A1 to just A1. also A:A and 1:1 will be
158 # properly resolved to cols/rows.
159 8 my ($start, $end) = split(':', $range);
160 8 ▶ $range = $start if $end && $start eq $end;
Branch: 1 branch
BranchTrueFalseTotal
if $end and $start eq $end 0 8 8
Condition66%2/3
$end and $start eq $end
!ll&&!rl&&r
260
MC/DC0%0/2
$end and $start eq $end
$end $start eq $end
Truth table66%2/3
A and B
A= $endB= $start eq $end
ABresult
0X0
100
111
161 8 return factory(%$range_args); ##### recursion
162 }
163 # we're already in A1:A1 format so just create a new range object.
164 47 return __PACKAGE__->new(%$range_args);
165 }
166  
167 # range could be a named range or a column/row header. we have to resolve the
168 # range first, then see what the best fit will be above.
169 elsif ($result == 4) {
170 0 my $worksheet = $range_args->{worksheet};
171  
172 0 my $named = $range;
173 0 $range = $worksheet->resolve_header_range($named);
174 0 ▶ if ($range) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range) 0 0 0
175 # remove original range so it doesn't screw up Type::Params.
176 0 delete $range_args->{range};
177 0 $range = factory(%$range_args, range => $range); ##### recursion
178 0 $range->{header_name} = $named;
179 0 return $range;
180 }
181  
182 0 ▶ $range = $worksheet->normalize_named($named)
Branch: 1 branch
BranchTrueFalseTotal
$range = $worksheet->normalize_named($named) or LOGDIE("Unable to resolve named range '${named}'") 0 0 0
183 or LOGDIE("Unable to resolve named range '$named'");
184 # we've resolved the name to A1 format, so redrive factory routine to
185 # generate a range object with the resolved range.
186 # remove original range so it doesn't screw up Type::Params.
187 0 delete $range_args->{range};
188 0 $range = factory(%$range_args, range => $range); ##### recursion
189 0 $range->{named} = $named;
190 0 return $range;
191 }
192  
193 0 LOGDIE("Unable to resolve '$range_args->{range}' to a range object");
194 }
195  
196 # this should not normally be called directly, everything should be created via
197 # the factory method above or more commonly via the 'worksheet::range*' methods.
198 266 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
199 266 my $class = shift;
200  
201 # TODO: sort start range and end range before storing.
202  
203 266 state $check = signature(
204 bless => !!0,
205 named => [
206 worksheet => HasMethods[qw(api worksheet_name)],
207 range => RangeAny,
208 dim => DimColRow, { default => 'row' },
209 ],
210 );
211 266 my $self = $check->(@_);
212 266 $self->{dim} = dims_any($self->{dim}); # convert internally to COLUMN | ROW
213 266 DEBUG("New range " . flatten_range($self->{range}) . " has been created");
214  
215 266 return bless $self, $class;
216 }
217  
218 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
219 0 my $self = shift;
220 0 my $range = $self->range();
221 0 DEBUG("Clearing range '$range'");
222 0 my %p = (
223 uri => "/values/$range:clear",
224 method => 'post',
225 );
226 0 my $response = $self->api(%p);
227 0 $self->clear_cached_values();
228 0 return $response;
229 }
230  
231 0 ▶sub clear_cached_values { delete shift->{cache_range_values}; }
Subroutine clear_cached_values: not called CC=1 SCAR=6.9
Pod undocumented
232  
233 0 ▶sub refresh_values {
Subroutine refresh_values: not called CC=1 SCAR=6.9
Pod documented
234 0 my $self = shift;
235 0 $self->clear_cached_values();
236 0 return $self->values();
237 }
238  
239 # this gets or sets the values of a range immediately, no batch.
240 # if no values passed, gets them. if values passed, sets them.
241 149 ▶sub values {
Subroutine values: called CC=2 SCAR=6.9
Pod documented
242 149 my $self = shift;
243 149 my %p = @_;
244 149 ▶ $self->_send_values(%p) if defined $p{values};
Branch: 1 branch
BranchTrueFalseTotal
if defined $p{"values"} 5 144 149
245 149 return $self->_cache_range_values(%p)->{values};
246 }
247  
248 # immediately update the values for a range (no batch).
249 5 ▶sub _send_values {
Subroutine _send_values: called CC=2 SCAR=6.9
250 5 my $self = shift;
251  
252 5 state $check = signature(
253 bless => !!0,
254 named => [
255 values => ArrayRef[ArrayRef[Str]], { optional => 1 },
256 params => HashRef, { default => {} },
257 content => HashRef, { default => {} },
258 _extra_ => slurpy HashRef,
259 ],
260 );
261 5 my $p = named_extra($check->(@_));
262
263 5 my $range = $self->range();
264  
265 # since we're sending these values, fake a response from the
266 # api to store them in our cache.
267 # if includeValuesInResponse is sent in the params, then the
268 # response will replace the cache again.
269 $self->_cache_range_values(
270 range => $range,
271 majorDimension => $self->dimension(),
272 values => $p->{values},
273 5 );
274  
275 5 $p->{content}->{range} = $range;
276 5 $p->{content}->{values} = delete $p->{values};
277 5 $p->{content}->{majorDimension} = $self->dimension();
278 5 ▶ $p->{params}->{valueInputOption} //= 'USER_ENTERED';
Condition50%1/2
$$p{"params"}{"valueInputOption"} //= "USER_ENTERED"
l!l
05
MC/DC0%0/1
$$p{"params"}{"valueInputOption"} //= "USER_ENTERED"
$$p{"params"}{"valueInputOption"}
Truth table50%1/2
A
A= $$p{"params"}{"valueInputOption"}
Aresult
11
00
279 # $p->{params}->{includeValuesInResponse} = 1;
280 5 $p->{uri} = "/values/$range";
281 5 $p->{method} = 'put';
282  
283 5 my $update = $self->api(%$p);
284  
285 # this fakes out the batch response with this immediate response.
286 # batch response would be in an arrayref, so wrap it thusly.
287 5 return $self->values_response_from_api([ $update ]);
288 }
289  
290 # this stores the response to an update request, either batch or immediate:
291 # ---
292 # spreadsheetId: 1ky2czjhPArP71a6woeo_dxRr8gBOZZxGAPjOCXJvCwA
293 # updatedCells: 6
294 # updatedColumns: 3
295 # updatedRange: Sheet1!B3:D4
296 # updatedRows: 2
297 # if includeValuesInResponse was requested, the values are stashed
298 # from the updatedData response key.
299 # spreadsheet object will call this on a batch update response.
300 36 ▶sub values_response_from_api {
Subroutine values_response_from_api: called CC=3 SCAR=11.3
Pod undocumented
301 36 my $self = shift;
302  
303 36 state $check = signature(positional => [ArrayRef, { optional => 1 }]);
304 36 my ($updates) = $check->(@_);
305 36 ▶ return if !$updates;
Branch: 1 branch
BranchTrueFalseTotal
unless $updates 0 36 36
306  
307 # shift off the next update from the batch api response. if this is
308 # getting called from the spreadsheet object, it means we have an
309 # update response to process.
310 36 my $update = shift @$updates;
311  
312 # updatedData is included if includeValuesInResponse query param was sent.
313 # updatedData:
314 # majorDimension: ROWS
315 # range: Sheet1!A1
316 # values:
317 # - - Fred
318 # if the data is included, then replace any cached values with this latest
319 # updated set of values.
320 0 $self->_cache_range_values(%{ delete $update->{updatedData} })
321 36 ▶ if $update->{updatedData};
Branch: 1 branch
BranchTrueFalseTotal
if $$update{"updatedData"} 0 36 36
322 36 $self->{values_response_from_api} = $update;
323  
324 36 return $self->values();
325 }
326  
327 # this returns the values and major dimension for a given range. it will store
328 # the values returned from an api fetch. it will store the values from a staged
329 # 'batch_values' call for later update from the api. it will store the values
330 # from an update if includeValuesInResponse was included.
331 # cache is in the format:
332 # majorDimension: ROWS
333 # range: Sheet1!A1 # only on an api return value.
334 # values:
335 # - - Fred
336 # if a range is included it's a flag that the dim and values are present from
337 # the returned api call.
338 208 ▶sub _cache_range_values {
Subroutine _cache_range_values: called CC=11 SCAR=28.0
339 208 my $self = shift;
340  
341 208 my %p = @_;
342 # if a range is included, assume this cache is coming from the api as a reply.
343 # this is to store the values for this range when includeValuesInResponse is
344 # added to the url or content on the original values call. you can replace the
345 # original values using the valueRenderOption to replace, say, formulas with their
346 # calculated value.
347 208 ▶ if ($p{range}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"range"}) 59 149 208
348 59 state $check = signature(
349 bless => !!0,
350 named => [
351 majorDimension => DimColRow,
352 range => StrMatch[qr/.+!/],
353 values => ArrayRef, { optional => 1 } # will not exist if values aren't set in the ss.
354 ],
355 );
356 59 my $p = $check->(@_);
357  
358 # remove all quotes for comparison.
359 59 my $self_range = $self->range();
360 59 $self_range =~ s/'//g;
361 59 my $range = $p->{range};
362 59 $range =~ s/'//g;
363 59 my ($worksheet_name) = $range =~ /^(.+)!/;
364  
365 59 ▶ LOGDIE "Setting range data to worksheet name '$worksheet_name' that doesn't belong to this range: " . $self->worksheet_name()
Branch: 1 branch
BranchTrueFalseTotal
if $worksheet_name ne $self->worksheet_name 0 59 59
366 if $worksheet_name ne $self->worksheet_name();
367 # TODO: sometimes the api returns a range that is different from the one we sent to the api.
368 # a column A:A can be returned as A1:A1000 by the api, so have to come up with a way
369 # of identifying when a returned range is a close enough match to the one we have.
370 #LOGDIE "Setting range data to '$range' which is not this range: " . $self_range
371 # if $range ne $self_range;
372 LOGDIE "Setting major dimention to '$p->{majorDimension}' that doesn't belong to this range: " . $self->dimention()
373 59 ▶ if $p->{majorDimension} ne $self->dimension();
Branch: 1 branch
BranchTrueFalseTotal
if $$p{"majorDimension"} ne $self->dimension 0 59 59
374  
375 59 delete $p->{range}; # we only cache the values and dimensions.
376 59 $self->{cache_range_values} = $p;
377 59 DEBUG("Saved cached values for range " . $self->range());
378 }
379  
380 # if the value range was just stored (above) or was previously stored, return it.
381 208 ▶ return $self->{cache_range_values} if $self->{cache_range_values};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"cache_range_values"} 154 54 208
382 54 DEBUG("No local cache values exist for range " . $self->range() . ", checking for shared values");
383  
384 # used by iterators to use a 'parent' range to query the values for this 'child'
385 # range. this is to reduce network calls when iterating through a range.
386 54 my $shared = $self->{shared};
387 54 ▶ if ($shared && $shared->has_values()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shared and $shared->has_values) 0 54 54
Condition33%1/3
$shared and $shared->has_values
!ll&&!rl&&r
5400
MC/DC0%0/2
$shared and $shared->has_values
$shared $shared->has_values
Truth table33%1/3
A and B
A= $sharedB= $shared->has_values
ABresult
0X0
100
111
388 0 my $dim = $shared->{cache_range_values}->{majorDimension};
389 0 my $values = $shared->{cache_range_values}->{values};
390  
391 0 my ($top, $left, $bottom, $right) = $self->offsets($shared);
392 0 my $data;
393 0 ▶ if ($dim =~ /^col/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dim =~ /^col/i) { } 0 0 0
394 0 my @cols = @$values[$left..$right];
395 0 $_ = [ @$_[$top..$bottom] ] foreach (@cols);
396 0 $data = \@cols;
397 } else {
398 0 my @rows = @$values[$top..$bottom];
399 0 $_ = [ @$_[$left..$right] ] foreach (@rows);
400 0 $data = \@rows;
401 }
402  
403 # return a subsection of the cached value for the iterator.
404 0 DEBUG("Returning shared values for range " . $self->range());
405 return {
406 0 majorDimension => $dim,
407 values => $data,
408 };
409 }
410 54 DEBUG("No shared values exist for range " . $self->range());
411  
412 # no values are found for this range, go get them from the api immediately.
413 54 $p{uri} = sprintf("/values/%s", $self->range());
414 54 $p{params}->{majorDimension} = $self->dimension();
415 54 $self->{cache_range_values} = $self->api(%p);
416 54 DEBUG("Cached values loaded from api for range " . $self->range());
417  
418 54 ▶ $self->{cache_range_values}->{values} //= []; # in case there's nothing set in the ss.
Condition100%2/2
$$self{"cache_range_values"}{"values"} //= []
l!l
3915
MC/DC100%1/1
$$self{"cache_range_values"}{"values"} //= []
$$self{"cache_range_values"}{"values"}
Truth table100%2/2
A
A= $$self{"cache_range_values"}{"values"}
Aresult
11
00
419  
420 54 return $self->{cache_range_values};
421 }
422  
423 134 ▶sub has_values {
Subroutine has_values: called CC=3 SCAR=11.4
Pod documented
424 134 my $self = shift;
425 134 ▶ return $self->{cache_range_values} || ($self->{shared} && $self->{shared}->has_values());
Condition66%4/6
$$self{'cache_range_values'} || $$self{'shared'} && $$self{'shared'}->has_values
l!l&&r!l&&!r
103031
$$self{"shared"} and $$self{"shared"}->has_values
!ll&&!rl&&r
2380
MC/DC33%1/3
$$self{'cache_range_values'} || $$self{'shared'} && $$self{'shared'}->has_values
$$self{'cache_range_values'} $$self{"shared"} $$self{"shared"}->has_values
Truth table75%3/4
A || B and C
A= $$self{'cache_range_values'}B= $$self{"shared"}C= $$self{"shared"}->has_values
ABCresult
1XX1
0111
00X0
0100
426 }
427  
428 # for a given range, calculate the offsets from this range.
429 0 ▶sub offsets {
Subroutine offsets: not called CC=1 SCAR=6.9
Pod documented
430 0 my $self = shift;
431  
432 0 state $check = signature(positional => [HasRange]);
433 0 my ($other_range) = $check->(@_);
434  
435 0 my $range = $self->range_to_hash($RANGE_EXPANDED);
436 0 $other_range = $other_range->range_to_hash($RANGE_EXPANDED);
437  
438 0 my $top = $range->[0]->{row} - $other_range->[0]->{row};
439 0 my $left = $range->[0]->{col} - $other_range->[0]->{col};
440 0 my $bottom = $range->[1]->{row} - $other_range->[0]->{row};
441 0 my $right = $range->[1]->{col} - $other_range->[0]->{col};
442  
443 0 return ( $top, $left, $bottom, $right );
444 }
445  
446 22 ▶sub share_values {
Subroutine share_values: called CC=2 SCAR=7.0
Pod documented
447 22 my $self = shift;
448  
449 22 state $check = signature(positional => [HasRange]);
450 22 my ($shared_range) = $check->(@_);
451 22 ▶ return if !$shared_range->is_other_inside($self);
Branch: 1 branch
BranchTrueFalseTotal
unless $shared_range->is_other_inside($self) 0 22 22
452  
453 22 DEBUG(flatten_range($self) . " is sharing values with " . flatten_range($shared_range));
454 22 $self->{shared} = $shared_range;
455 22 return $shared_range;
456 }
457  
458 10 ▶sub is_other_inside {
Subroutine is_other_inside: called CC=5 SCAR=16.3
Pod documented
459 10 my $self = shift;
460  
461 10 state $check = signature(positional => [HasRange]);
462 10 my ($inside_range) = $check->(@_);
463  
464 10 my $range = $self->range_to_hash($RANGE_EXPANDED);
465 10 $inside_range = $inside_range->range_to_hash($RANGE_EXPANDED);
466  
467 return 1 if
468 $range->[0]->{col} <= $inside_range->[0]->{col} &&
469 $range->[0]->{row} <= $inside_range->[0]->{row} &&
470 $range->[1]->{col} >= $inside_range->[1]->{col} &&
471 $range->[1]->{row} >= $inside_range->[1]->{row}
472 10 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
if $$range[0]{"col"} <= $$inside_range[0]{"col"} and $$range[0]{"row"} <= $$inside_range[0]{"row"} and $$range[1]{"col"} >= $$inside_range[1]{"col"} and $$range[1]{"row"} >= $$inside_range[1]{"row"} 8 2 10
Condition66%6/9
$$range[0]{"col"} <= $$inside_range[0]{"col"} and $$range[0]{"row"} <= $$inside_range[0]{"row"} and $$range[1]{"col"} >= $$inside_range[1]{"col"} and $$range[1]{"row"} >= $$inside_range[1]{"row"}
!ll&&!rl&&r
118
$$range[0]{"col"} <= $$inside_range[0]{"col"} and $$range[0]{"row"} <= $$inside_range[0]{"row"} and $$range[1]{"col"} >= $$inside_range[1]{"col"}
!ll&&!rl&&r
019
$$range[0]{"col"} <= $$inside_range[0]{"col"} and $$range[0]{"row"} <= $$inside_range[0]{"row"}
!ll&&!rl&&r
0010
MC/DC50%2/4
$$range[0]{"col"} <= $$inside_range[0]{"col"} and $$range[0]{"row"} <= $$inside_range[0]{"row"} and $$range[1]{"col"} >= $$inside_range[1]{"col"} and $$range[1]{"row"} >= $$inside_range[1]{"row"}
$$range[0]{"col"} <= $$inside_range[0]{"col"} $$range[0]{"row"} <= $$inside_range[0]{"row"} $$range[1]{"col"} >= $$inside_range[1]{"col"} $$range[1]{"row"} >= $$inside_range[1]{"row"}
Truth table60%3/5
A and B and C and D
A= $$range[0]{"col"} <= $$inside_range[0]{"col"}B= $$range[0]{"row"} <= $$inside_range[0]{"row"}C= $$range[1]{"col"} >= $$inside_range[1]{"col"}D= $$range[1]{"row"} >= $$inside_range[1]{"row"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
473 2 return;
474 }
475  
476 # stage batch values get/set for later use by submit_values
477 62 ▶sub batch_values {
Subroutine batch_values: called CC=3 SCAR=11.0
Pod documented
478 62 my $self = shift;
479  
480 62 state $check = signature(
481 bless => !!0,
482 named => [
483 values => ArrayRef, { optional => 1 },
484 ],
485 );
486 62 my $p = $check->(@_);
487  
488 62 ▶ if (defined $p->{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$p{"values"}) 31 31 62
489 # since we're sending these values, fake a response from the
490 # api to store them in our cache.
491 # if includeValuesInResponse is sent in the params, then the
492 # response will replace the cache again.
493 $self->_cache_range_values(
494 range => $self->range(),
495 majorDimension => $self->dimension(),
496 values => $p->{values},
497 31 );
498 }
499  
500 62 ▶ return if !$self->{cache_range_values};
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{"cache_range_values"} 0 62 62
501  
502 return {
503 range => $self->range(),
504 62 %{ $self->{cache_range_values} },
505 };
506 }
507  
508 # tell the api to submit the batch values. the api will call back
509 # values_response_from_api with the results of the update.
510 0 ▶sub submit_values {
Subroutine submit_values: not called CC=1 SCAR=6.9
Pod documented
511 0 my $self = shift;
512 0 $self->spreadsheet()->submit_values(ranges => [ $self ], @_);
513 0 return $self->values();
514 }
515  
516 50 ▶sub submit_requests {
Subroutine submit_requests: called CC=1 SCAR=0.0
Pod documented
517 50 my $self = shift;
518 50 return $self->spreadsheet()->submit_requests(ranges => [ $self ], @_);
519 }
520  
521 0 ▶sub append {
Subroutine append: not called CC=2 SCAR=17.9
Pod undocumented
522 0 my $self = shift;
523  
524 state $check = signature(
525 bless => !!0,
526 named => [
527 0 ▶ values => ArrayRef->plus_coercions(Str, sub { [ $_ ] } ),
Subroutine __ANON__: not called CC=1 SCAR=6.9
528 params => HashRef, { default => {} },
529 content => HashRef, { default => {} },
530 _extra_ => slurpy HashRef,
531 ],
532 );
533 0 my $p = named_extra($check->(@_));
534  
535 0 my $range = $self->range();
536 0 $p->{content}->{range} = $range;
537 0 $p->{content}->{values} = delete $p->{values};
538 0 $p->{content}->{majorDimension} = $self->dimension();
539 0 ▶ $p->{params}->{valueInputOption} //= 'USER_ENTERED';
Condition0%0/2
$$p{"params"}{"valueInputOption"} //= "USER_ENTERED"
l!l
00
MC/DC0%0/1
$$p{"params"}{"valueInputOption"} //= "USER_ENTERED"
$$p{"params"}{"valueInputOption"}
Truth table0%0/2
A
A= $$p{"params"}{"valueInputOption"}
Aresult
11
00
540 0 $p->{uri} = "/values/$range:append";
541 0 $p->{method} = 'post';
542  
543 0 return $self->api(%$p);
544 }
545  
546 943 ▶sub range {
Subroutine range: called CC=1 SCAR=0.0
Pod documented
547 943 my $self = shift;
548 943 TRACE("Range external caller: " . $self->_caller_external());
549 943 TRACE("Range internal caller: " . $self->_caller_internal());
550 943 my $name = $self->worksheet_name();
551 943 return "'$name'!$self->{range}";
552 }
553  
554 # some staic calls...
555  
556 943 ▶sub _caller_internal {
Subroutine _caller_internal: called CC=3 SCAR=11.0
557 943 my ($package, $subroutine, $line, $i) = ('', '', 0);
558 943 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$subroutine =~ /range$/ and do { do { ($package, undef, $line, $subroutine) = caller ++$i } } 986 943 1929
559 1929 ($package, undef, $line, $subroutine) = caller(++$i);
560 } while($subroutine =~ m|range$|);
561 # not usually going to happen, but during testing we call
562 # range directly, so have to backtrack.
563 943 ▶ ($package, undef, $line, $subroutine) = caller(--$i)
Branch: 1 branch
BranchTrueFalseTotal
unless $package 0 943 943
564 if !$package;
565 943 return "$package:$line => $subroutine";
566 }
567  
568 943 ▶sub _caller_external {
Subroutine _caller_external: called CC=3 SCAR=11.0
569 943 my ($package, $subroutine, $line, $i) = ('', '', 0);
570 943 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$package and $package =~ /^(Google::RestApi)/ and do { do { ($package, undef, $line, $subroutine) = caller ++$i } } 3721 943 4664
Condition66%2/3
$package and $package =~ /^(Google::RestApi)/
!ll&&!rl&&r
09433721
MC/DC50%1/2
$package and $package =~ /^(Google::RestApi)/
$package $package =~ /^(Google::RestApi)/
Truth table66%2/3
A and B
A= $packageB= $package =~ /^(Google::RestApi)/
ABresult
0X0
100
111
571 4664 ($package, undef, $line, $subroutine) = caller(++$i);
572 } while($package && $package =~ m[^(Google::RestApi)]);
573 943 return "$package:$line => $subroutine";
574 }
575  
576 # taken from https://metacpan.org/source/DOUGW/Spreadsheet-ParseExcel-0.65/lib/Spreadsheet/ParseExcel/Utility.pm
577 496 ▶sub _col_a2i {
Subroutine _col_a2i: called CC=3 SCAR=11.0
578 496 state $check = signature(positional => [StrMatch[qr/^[A-Z]+$/]]);
579 496 my ($a) = $check->(@_);
580  
581 496 my $result = 0;
582 496 my $incr = 0;
583 496 ▶ for (my $i = length($a); $i > 0 ; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 496 496 992
584 496 my $char = substr($a, $i - 1);
585 496 my $curr = ord(lc($char)) - ord('a') + 1;
586 496 ▶ $curr *= $incr if $incr;
Branch: 1 branch
BranchTrueFalseTotal
if $incr 0 496 496
587 496 $result += $curr;
588 496 $incr += 26;
589 }
590  
591 496 return $result;
592 }
593  
594 524 ▶sub cell_to_array {
Subroutine cell_to_array: called CC=4 SCAR=13.9
Pod undocumented
595 524 my $cell = shift;
596  
597 524 my ($col, $row) = $cell =~ /^([A-Z]*)(\d*)$/;
598 524 ▶ $col = _col_a2i($col) if $col;
Branch: 1 branch
BranchTrueFalseTotal
if $col 496 28 524
599  
600 524 ▶ $col ||= 0;
Condition100%2/2
$col ||= 0
l!l
49628
MC/DC100%1/1
$col ||= 0
$col
Truth table100%2/2
A
A= $col
Aresult
11
00
601 524 ▶ $row ||= 0;
Condition100%2/2
$row ||= 0
l!l
47549
MC/DC100%1/1
$row ||= 0
$row
Truth table100%2/2
A
A= $row
Aresult
11
00
602  
603 524 return [$col, $row];
604 }
605  
606 # back to object calls...
607  
608 # returns [[col, row], [col, row]] for a full range.
609 # returns [col, row] for a col or col(a2:a) or row(a2:2).
610 # passing $RANGE_EXPANDED will always return the former.
611 387 ▶sub range_to_array {
Subroutine range_to_array: called CC=8 SCAR=20.8
Pod documented
612 387 my $self = shift;
613  
614 387 state $check = signature(positional => [Bool, { optional => 1 }]);
615 387 my ($expand_range) = $check->(@_);
616  
617 387 my $range = $self->range();
618 387 ($range) = $range =~ /!(.+)/;
619 387 ▶ $range or LOGDIE "Unable to convert range to array: ", $self->range();
Branch: 1 branch
BranchTrueFalseTotal
$range or LOGDIE("Unable to convert range to array: ", $self->range) 387 0 387
620  
621 387 my ($start_cell, $end_cell) = split(':', $range);
622 387 ▶ $end_cell = undef if defined $end_cell && $start_cell eq $end_cell;
Branch: 1 branch
BranchTrueFalseTotal
if defined $end_cell and $start_cell eq $end_cell 65 322 387
Condition100%3/3
defined $end_cell and $start_cell eq $end_cell
!ll&&!rl&&r
18513765
MC/DC100%2/2
defined $end_cell and $start_cell eq $end_cell
defined $end_cell $start_cell eq $end_cell
Truth table100%3/3
A and B
A= defined $end_cellB= $start_cell eq $end_cell
ABresult
0X0
100
111
623 387 $start_cell = cell_to_array($start_cell);
624 387 ▶ $end_cell = cell_to_array($end_cell) if $end_cell;
Branch: 1 branch
BranchTrueFalseTotal
if $end_cell 137 250 387
625
626 387 ▶ $end_cell = $start_cell if !$end_cell && $expand_range;
Branch: 1 branch
BranchTrueFalseTotal
if not $end_cell and $expand_range 86 301 387
Condition100%3/3
not $end_cell and $expand_range
!ll&&!rl&&r
13716486
MC/DC100%2/2
not $end_cell and $expand_range
not $end_cell $expand_range
Truth table100%3/3
not(A) and B
A= not $end_cellB= $expand_range
ABresult
0X0
100
111
627  
628 387 ▶ return $end_cell ? [$start_cell, $end_cell] : $start_cell;
Branch: 1 branch
BranchTrueFalseTotal
$end_cell ? : 223 164 387
629 }
630  
631 # returns [{col =>, row =>}, {col =>, row => }] for an expanded range.
632 # returns {col =>, row =>} for a cell or col(a2:a) or row(a2:2).
633 # passing $RANGE_EXPANDED will always return the former.
634 190 ▶sub range_to_hash {
Subroutine range_to_hash: called CC=3 SCAR=11.0
Pod documented
635 190 my $self = shift;
636  
637 190 my $range = $self->range_to_array(@_);
638 190 ▶ $range = [ $range ] if !ref($range->[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$range[1] 68 122 190
639 190 my @ranges = map { { col => $_->[0], row => $_->[1] } } @$range;
640  
641 190 ▶ return $ranges[1] ? \@ranges : $ranges[0];
Branch: 1 branch
BranchTrueFalseTotal
$ranges[1] ? : 122 68 190
642 }
643  
644 179 ▶sub range_to_index {
Subroutine range_to_index: called CC=4 SCAR=14.0
Pod documented
645 179 my $self = shift;
646  
647 179 my $array = $self->range_to_array();
648 179 ▶ $array = [$array, $array] if !ref($array->[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$array[1] 96 83 179
649  
650 179 my %range = (
651 sheetId => $self->worksheet()->worksheet_id(),
652 startColumnIndex => max($array->[0]->[0] - 1, 0),
653 startRowIndex => max($array->[0]->[1] - 1, 0),
654 endColumnIndex => $array->[1]->[0],
655 endRowIndex => $array->[1]->[1],
656 );
657 delete $range{endColumnIndex}
658 179 ▶ if $range{endColumnIndex} < $range{startColumnIndex};
Branch: 1 branch
BranchTrueFalseTotal
if $range{"endColumnIndex"} < $range{"startColumnIndex"} 0 179 179
659 delete $range{endRowIndex}
660 179 ▶ if $range{endRowIndex} < $range{startRowIndex};
Branch: 1 branch
BranchTrueFalseTotal
if $range{"endRowIndex"} < $range{"startRowIndex"} 0 179 179
661
662 179 return \%range;
663 }
664  
665 0 ▶sub range_to_dimension {
Subroutine range_to_dimension: not called CC=4 SCAR=30.0
Pod documented
666 0 my $self = shift;
667  
668 0 state $check = signature(positional => [DimColRow]);
669 0 my ($dims) = $check->(@_);
670 0 $dims = dims_any($dims);
671  
672 0 my $array = $self->range_to_array();
673 0 ▶ $array = [$array, $array] if !ref($array->[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$array[1] 0 0 0
674 0 ▶ my $start = $dims =~ /^col/i ? $array->[0]->[0] : $array->[0]->[1];
Branch: 1 branch
BranchTrueFalseTotal
$dims =~ /^col/i ? : 0 0 0
675 0 ▶ my $end = $dims =~ /^col/i ? $array->[1]->[0] : $array->[1]->[1];
Branch: 1 branch
BranchTrueFalseTotal
$dims =~ /^col/i ? : 0 0 0
676  
677 0 my %range = (
678 sheetId => $self->worksheet()->worksheet_id(),
679 startIndex => max($start - 1, 0),
680 endIndex => $end,
681 dimension => $dims,
682 );
683  
684 0 return \%range;
685 }
686  
687 6 ▶sub cell_at_offset {
Subroutine cell_at_offset: called CC=3 SCAR=11.0
Pod undocumented
688 6 my $self = shift;
689  
690 6 state $check = signature(positional => [Int, DimColRow]);
691 6 my ($offset, $dim) = $check->(@_);
692  
693 # it's an a1:b2 range. col/row/cell handle this in the subclass.
694 6 my $range = $self->range_to_hash($RANGE_EXPANDED);
695 6 ▶ my $other_dim = $dim =~ /col/i ? 'row' : 'col';
Branch: 1 branch
BranchTrueFalseTotal
$dim =~ /col/i ? : 3 3 6
696 # TODO: this is really brain dead, figure this out with a bit of arithmetic...
697 my @ranges = map {
698 12 my $outside = $_;
699 map {
700 24 my $inside = $_;
701 24 my $r = { $dim => $inside, $other_dim => $outside }; $r; # for some reason have to stage the ref to a scalar.
702 12 } ($range->[0]->{$other_dim} .. $range->[1]->{$other_dim});
703 6 } ($range->[0]->{$dim} .. $range->[1]->{$dim});
704  
705 6 ▶ my $offset_range = $ranges[$offset] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $offset_range = $ranges[$offset] or return 4 2 6
706 4 my $new_cell = $self->worksheet()->range_cell($offset_range);
707 4 $new_cell->share_values($self);
708  
709 4 return $new_cell;
710 }
711  
712 # doesn't seem to be used anywhere internally.
713 0 ▶sub range_at_offset {
Subroutine range_at_offset: not called CC=5 SCAR=34.0
Pod undocumented
714 0 my $self = shift;
715  
716 0 state $check = signature(
717 bless => !!0,
718 named => [
719 col => Int, { default => 0 },
720 row => Int, { default => 0 },
721 top => Int, { default => 0 },
722 left => Int, { default => 0 },
723 bottom => Int, { default => 0 },
724 right => Int, { default => 0 },
725 ],
726 );
727 0 my $p = $check->(@_);
728  
729 0 my $range = $self->range_to_hash();
730 0 ▶ if (ref($range) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $range eq 'ARRAY') { } 0 0 0
731 0 foreach my $dim (@$range) {
732 0 $dim->{$_} += $p->{$_} foreach (qw(col row));
733 }
734 0 $range->[0]->{row} += $p->{top};
735 0 $range->[0]->{col} += $p->{left};
736 0 $range->[1]->{row} += $p->{bottom};
737 0 $range->[1]->{col} += $p->{right};
738 } else {
739 0 $range->{$_} += $p->{$_} foreach (qw(col row));
740 0 $range->{row} += $p->{top};
741 0 $range->{col} += $p->{left};
742 0 $range->{row} += $p->{bottom};
743 0 $range->{col} += $p->{right};
744 }
745  
746 0 return (ref($self))->new(worksheet => $self->worksheet(), range => $range);
747 }
748  
749 0 ▶sub header_name { shift->{header_name}; }
Subroutine header_name: not called CC=1 SCAR=6.9
Pod undocumented
750 1 ▶sub named { shift->{named}; }
Subroutine named: called CC=1 SCAR=0.0
Pod documented
751 59 ▶sub api { shift->worksheet()->api(@_); }
Subroutine api: called CC=1 SCAR=0.0
Pod documented
752 177 ▶sub dimension { shift->{dim}; }
Subroutine dimension: called CC=1 SCAR=0.0
Pod documented
753 1374 ▶sub worksheet { shift->{worksheet}; }
Subroutine worksheet: called CC=1 SCAR=0.0
Pod documented
754 1002 ▶sub worksheet_name { shift->worksheet()->worksheet_name(@_); }
Subroutine worksheet_name: called CC=1 SCAR=0.0
Pod documented
755 62 ▶sub worksheet_id { shift->worksheet()->worksheet_id(@_); }
Subroutine worksheet_id: called CC=1 SCAR=0.0
Pod documented
756 50 ▶sub spreadsheet { shift->worksheet()->spreadsheet(@_); }
Subroutine spreadsheet: called CC=1 SCAR=0.0
Pod documented
757 0 ▶sub spreadsheet_id { shift->spreadsheet()->spreadsheet_id(@_); }
Subroutine spreadsheet_id: not called CC=1 SCAR=6.9
Pod documented
758 0 ▶sub transaction { shift->spreadsheet()->transaction(); }
Subroutine transaction: not called CC=1 SCAR=6.9
Pod undocumented
759 0 ▶sub iterator { Iterator->new(range => shift, @_); }
Subroutine iterator: not called CC=1 SCAR=6.9
Pod documented
760  
761 2 1;
762  
763 __END__
« RestApi/SheetsApi4/Request/Spreadsheet/Worksheet.pm ↑ Summary RestApi/Auth/OAuth2Client.pm »