File Coverage

lib/Google/RestApi/SheetsApi4/RangeGroup.pm
Criterion Covered Total %
statement 94 119 78.9
branch 12 18 66.6
condition 2 3 66.6
subroutine 21 28 75.0
pod 13 20 65.0
total 142 188 75.5


line stmt bran cond sub pod time code
1              
2             # some private subroutines of Range are called from here,
3             # so think of RangeGroup as a friend of Range. the routines
4             # called are commented thusly:
5             # "private range routine called here!"
6              
7             our $VERSION = '1.0.2';
8              
9             use Google::RestApi::Setup;
10 1     1   492  
  1         3  
  1         6  
11             use List::Util qw( first );
12 1     1   20096 use Scalar::Util ();
  1         3  
  1         60  
13 1     1   6 use Storable qw( dclone );
  1         2  
  1         17  
14 1     1   63  
  1         2  
  1         46  
15             use aliased 'Google::RestApi::SheetsApi4::RangeGroup::Iterator';
16 1     1   5  
  1         2  
  1         7  
17             my $class = shift;
18              
19 26     26 1 71 state $check = compile_named(
20             spreadsheet => HasApi,
21 26         72 ranges => ArrayRef[HasRange], { default => [] },
22             );
23              
24             return bless $check->(@_), $class;
25             }
26 26         1197  
27             my $self = shift;
28             state $check = compile(slurpy ArrayRef[HasRange]);
29             my ($ranges) = $check->(@_);
30 0     0 1 0 push(@{ $self->{ranges} }, @$ranges);
31 0         0 return;
32 0         0 }
33 0         0  
  0         0  
34 0         0 my $self = shift;
35              
36             $self->clear_cached_values();
37              
38 0     0 1 0 my @ranges = map { $_->range(); } $self->ranges();
39             my %p = (
40 0         0 content => { ranges => \@ranges },
41             uri => "/values:batchClear",
42 0         0 method => "post",
  0         0  
43 0         0 );
44             return $self->api(%p);
45             }
46              
47             my $self = shift;
48 0         0 $_->clear_cached_values() foreach $self->ranges();
49             return;
50             }
51              
52 0     0 0 0 my $self = shift;
53 0         0 $self->clear_cached_values();
54 0         0 return $self->values();
55             }
56              
57             my $self = shift;
58 0     0 0 0  
59 0         0 state $check = compile_named(
60 0         0 params => HashRef, { default => {} },
61             _extra_ => slurpy Any,
62             );
63             my $p = named_extra($check->(@_));
64 19     19 1 418  
65             my @ranges = $self->ranges();
66 19         53 my @needs_values = grep { !$_->has_values(); } @ranges;
67             # for some reason google designed this so that you can set
68             # various ranges in the params, but only one majorDimension
69             # for the entire url. it's a bit strange since the return
70 19         853 # values have a majorDimension in each range. all other parts
71             # of their api treat range/majorDimension as a unit except for
72 19         80 # batchGet. so we have to potentially do two calls for a
73 19         52 # batch get, one for cols, and one for rows.
  54         140  
74             my (@cols, @rows);
75             foreach (@needs_values) {
76             if ($_->dimension() =~ /^col/i) { push(@cols, $_); }
77             else { push(@rows, $_); }
78             }
79             $self->_batch_get('col', dclone($p), \@cols);
80             $self->_batch_get('row', dclone($p), \@rows);
81 19         35  
82 19         48 my @values = map { $_->values(); } @ranges;
83 23 100       59 return \@values;
  3         10  
84 20         53 }
85              
86 19         1013 my $self = shift;
87 19         266  
88             my ($dim, $p, $ranges) = @_;
89 19         68 return if !$ranges || !@$ranges;
  54         170  
90 19         251  
91             my @ranges = map { $_->range(); } @$ranges;
92             $p->{params}->{ranges} = \@ranges;
93             $p->{params}->{majorDimension} = 'COLUMNS' if $dim =~ /^col/i;
94 38     38   69 $p->{uri} = "/values:batchGet";
95              
96 38         98 my $response = $self->api(%$p);
97 38 100 66     197  
98             my $value_ranges = $response->{valueRanges};
99 9         23 # private range routine called here!
  23         62  
100 9         31 my @ranges2 = map { $_->range(); } @$ranges;
101 9 100       42 $ranges->[$_]->_cache_range_values(%{ $value_ranges->[$_] })
102 9         38 foreach (0..$#$ranges);
103              
104 9         46 return;
105             }
106 9         33  
107             my $self = shift;
108 9         27  
  23         80  
109 23         102 state $check = compile_named(
110 9         51 values => ArrayRef, { optional => 1 },
111             );
112 9         25 my $p = $check->(@_);
113              
114             my $values = $p->{values};
115             if (defined $values) {
116 10     10 1 21 my @ranges = $self->ranges();
117             LOGDIE "Too many values provided for range group" if scalar @$values > scalar @ranges;
118 10         28 $ranges[$_]->batch_values(values => $values->[$_]) foreach (0..$#$values);
119             return $self;
120             }
121 10         703  
122             my @batch_values = map {
123 10         164 $_->has_values() ? ($_->batch_values()) : ();
124 10 50       115 } $self->ranges();
125 0         0  
126 0 0       0 return \@batch_values;
127 0         0 }
128 0         0  
129             my $self = shift;
130             state $check = compile(ArrayRef);
131             my ($updates) = $check->(@_);
132 10 50       43 my @updates = map {
  31         88  
133             $_->has_values() ? ($_->values_response_from_api($updates)) : ();
134             } $self->ranges();
135 10         59 return \@updates;
136             }
137              
138             my $self = shift;
139 10     10 0 22 $self->spreadsheet()->submit_values(ranges => [ $self ], @_);
140 10         30 return $self->values();
141 10         632 }
142              
143 10 50       138 my $self = shift;
  31         110  
144             my @batch_requests = map {
145 10         45 $_->batch_requests(@_);
146             } $self->ranges();
147             return @batch_requests;
148             }
149 10     10 1 293  
150 10         152 my $self = shift;
151 10         59 return $self->spreadsheet()->submit_requests(ranges => [ $self ], @_);
152             # return $self;
153             }
154              
155 2     2 1 3 my $self = shift;
156             state $check = compile(ArrayRef);
157 2         6 my ($requests) = $check->(@_);
  6         14  
158             my @requests = map {
159 2         9 $_->requests_response_from_api($requests);
160             } $self->ranges();
161             return \@requests;
162             }
163 1     1 1 36  
164 1         5 my $self = shift;
165             return Iterator->new(@_, range_group => $self);
166             }
167              
168             my $self = shift;
169 1     1 0 3 return first { $_->has_values(); } $self->ranges();
170 1         5 }
171 1         595  
172             # enables pass-throughs to the underlying ranges so you can go:
173 1         11 # $range_group->red()->bold() etc.
  3         25  
174             our $AUTOLOAD;
175 1         4 return if $AUTOLOAD =~ /DESTROY$/;
176             return if $AUTOLOAD =~ /\0$/; # wtf is this???
177              
178             my $self = shift;
179 1     1 1 35 my $method = (split('::', $AUTOLOAD))[-1];
180 1         10 $_->$method(@_) for $self->ranges();
181             return $self;
182             }
183              
184 10     10 1 19  
185 10     10   88 1;
  10         52  
186              
187              
188             =head1 NAME
189              
190             Google::RestApi::SheetsApi4::RangeGroup - Represents a group of ranges in a Worksheet.
191 28     28   1341  
192 28 100       607 =head1 DESCRIPTION
193 2 50       8  
194             A RangeGroup is a lightweight object that represents a collection of ranges
195 2         5 on which you can operate as one unit (e.g. RangeGroup::submit_values
196 2         27 will submit all batch values for the underlying ranges).
197 2         9  
198 2         34 See the description and synopsis at Google::RestApi::SheetsApi4.
199              
200             =head1 NAVIGATION
201 64     64 1 109  
  64         275  
202 26     26 1 148 =over
203 9     9 1 28  
204 0     0 0   =item * L<Google::RestApi::SheetsApi4>
205 0     0 0    
206 0     0 0   =item * L<Google::RestApi::SheetsApi4::Spreadsheet>
207              
208             =item * L<Google::RestApi::SheetsApi4::Worksheet>
209              
210             =item * L<Google::RestApi::SheetsApi4::Range>
211              
212             =item * L<Google::RestApi::SheetsApi4::Range::All>
213              
214             =item * L<Google::RestApi::SheetsApi4::Range::Col>
215              
216             =item * L<Google::RestApi::SheetsApi4::Range::Row>
217              
218             =item * L<Google::RestApi::SheetsApi4::Range::Cell>
219              
220             =item * L<Google::RestApi::SheetsApi4::RangeGroup>
221              
222             =item * L<Google::RestApi::SheetsApi4::RangeGroup::Iterator>
223              
224             =item * L<Google::RestApi::SheetsApi4::RangeGroup::Tie>
225              
226             =item * L<Google::RestApi::SheetsApi4::RangeGroup::Tie::Iterator>
227              
228             =item * L<Google::RestApi::SheetsApi4::Request::Spreadsheet>
229              
230             =item * L<Google::RestApi::SheetsApi4::Request::Spreadsheet::Worksheet>
231              
232             =item * L<Google::RestApi::SheetsApi4::Request::Spreadsheet::Worksheet::Range>
233              
234             =back
235              
236             =head1 SUBROUTINES
237              
238             =over
239              
240             =item new(spreadsheet => <Spreadsheet>, ranges => <arrayref<Range>>);
241              
242             Creates a new range group object for the given spreadsheet.
243              
244             spreadsheet: The parent Spreadsheet object for this range group.
245             ranges: The array of ranges to be grouped into this range group.
246              
247             You would not normally call this directly, you'd use Spreadsheet::range_group
248             method to create the range group object for you.
249              
250             =item api(%args);
251              
252             Calls the parent spreadsheet's 'api' routine with the ranges added into
253             the URI or content appropriately.
254              
255             You would not normally call this directly unless you were
256             making a Google API call not currently supported by this API
257             framework.
258              
259             =item push_ranges(<arrayref<Range>>);
260              
261             Adds the extra ranges to this range group. No attempt is made to
262             check for duplicate range objects.
263              
264             =item clear();
265              
266             Clears each range in the range group in one call using batchClear
267             Google API call.
268              
269             =item values(%args);
270              
271             Fetches the values of the spreadsheet for each range in the group. Note
272             that there is no way to set values with this method as it is assumed
273             that setting will be done via routine batch_values.
274              
275             'args' are passed to the SheetsApi4's 'api' routine so you may add
276             extra arguments to the 'params' as necessary.
277              
278             =item batch_values(values => <arrayref>);
279              
280             Gets or sets the queued batch values for each range in the range group.
281             Batch values can be set on particular ranges individually, or can be
282             set with this routine all in one shot.
283              
284             =item submit_values(%args);
285              
286             Sends the previously queued batch values to Google API, if any.
287              
288             'args' are passed to the SheetsApi4's 'api' routine so you may add
289             extra arguments to the 'params' or 'content' as necessary.
290              
291             =item batch_requests();
292              
293             Gets the queued batch requests for each range in the group.
294              
295             =item submit_requests(%args);
296              
297             Sends the previously queued requests (formatting, sheet properties etc)
298             to Google API, if any.
299              
300             'args' are passed to the SheetsApi4's 'api' routine so you may add
301             extra arguments to the 'params' or 'content' as necessary.
302              
303             =item iterator(%args);
304              
305             Returns an iterator for this range group. Any 'args' are passed to the
306             'new' routine for the iterator.
307              
308             =item has_values();
309              
310             Returns a true value if any of the underlying ranges has values
311             associated with it.
312              
313             =item ranges();
314              
315             Returns the array of Range objects in this range group.
316              
317             =item spreadsheet();
318              
319             Returns the parent Spreadsheet object.
320              
321             =back
322              
323             =head1 AUTHORS
324              
325             =over
326              
327             =item
328              
329             Robin Murray mvsjes@cpan.org
330              
331             =back
332              
333             =head1 COPYRIGHT
334              
335             Copyright (c) 2021, Robin Murray. All rights reserved.
336              
337             This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.