File Coverage

lib/Google/RestApi/SheetsApi4/Range/Row.pm
Criterion Covered Total %
statement 60 71 84.5
branch 17 20 85.0
condition 9 14 64.2
subroutine 13 21 61.9
pod 4 12 33.3
total 103 138 74.6


line stmt bran cond sub pod time code
1             package Google::RestApi::SheetsApi4::Range::Row;
2              
3             our $VERSION = '1.0.4';
4              
5 1     1   664 use Google::RestApi::Setup;
  1         5  
  1         7  
6              
7 1     1   13988 use Try::Tiny qw( try catch );
  1         2  
  1         50  
8              
9 1     1   6 use aliased 'Google::RestApi::SheetsApi4::Range';
  1         3  
  1         6  
10 1     1   184 use aliased 'Google::RestApi::SheetsApi4::Range::Cell';
  1         3  
  1         4  
11              
12 1     1   176 use parent Range;
  1         3  
  1         6  
13              
14             sub new {
15 40     40 1 94 my $class = shift;
16 40         135 my %self = @_;
17              
18 40         105 $self{dim} = 'row';
19              
20             # this is fucked up, but want to support creating this object directly and also
21             # via the range::factory method, so have to handle both cases here. try to create
22             # the row directly first (which could also come via the factory method, which will
23             # have already translated the address into a row), and failing that, see if the
24             # range factory can create a row (which will resolve any named or header references).
25             # this has the potential of looping between this and factory method.
26              
27             # if factory has already been used, then this should resolve here.
28 40         79 my $err;
29             try {
30 40     40   1850 state $check = compile(RangeRow);
31 40         108394 ($self{range}) = $check->($self{range});
32             } catch {
33 6     6   733 $err = $_;
34 40         317 };
35 40 100       2220 return $class->SUPER::new(%self) if !$err;
36              
37             # see if the range passed can be translated to what we want via the factory.
38 6         49 my $factory_range;
39             try {
40 6     6   298 $factory_range = Google::RestApi::SheetsApi4::Range::factory(%self);
41 6     0   44 } catch {};
42 6 100 66     197 return $factory_range if $factory_range && $factory_range->isa(__PACKAGE__);
43              
44 2         14 LOGDIE sprintf("Unable to translate '%s' into a worksheet row: $err", flatten_range($self{range}));
45             }
46              
47             sub values {
48 18     18 1 40 my $self = shift;
49 18         37 state $check = compile_named(
50             values => ArrayRef[Str], { optional => 1 },
51             _extra_ => slurpy Any,
52             );
53 18         5373 my $p = named_extra($check->(@_));
54 18 100       80 $p->{values} = [ $p->{values} ] if defined $p->{values};
55 18         78 my $values = $self->SUPER::values(%$p);
56 18 100       108 return defined $values ? $values->[0] : undef;
57             }
58              
59             sub batch_values {
60 12     12 1 25 my $self = shift;
61 12         40 state $check = compile_named(
62             values => ArrayRef[Str], { optional => 1 },
63             );
64 12         2964 my $p = $check->(@_);
65 12 100       249 $p->{values} = [ $p->{values} ] if $p->{values};
66 12         50 return $self->SUPER::batch_values(%$p);
67             }
68              
69             sub cell_at_offset {
70 6     6 0 778 my $self = shift;
71              
72 6         14 state $check = compile(Int, DimColRow, { optional => 1 });
73 6         3982 my ($offset) = $check->(@_); # we're a column, no dim required.
74              
75 6         203 my $row_range = $self->range_to_array($Google::RestApi::SheetsApi4::Range::RANGE_EXPANDED); # can't use aliased
76 6         28 my $cell_range = $row_range->[0];
77 6   50     27 $cell_range->[0] = ($cell_range->[0] || 1) + $offset;
78 6 50 33     39 return if $row_range->[1]->[0] && $cell_range->[0] > $row_range->[1]->[0];
79              
80 6         20 my $cell = Cell->new(worksheet => $self->worksheet(), range => $cell_range);
81 6         34 $cell->share_values($self);
82 6         47 return $cell;
83             }
84              
85             sub is_other_inside {
86 15     15 1 82 my $self = shift;
87              
88 15         32 state $check = compile(HasRange);
89 15         2519 my ($inside_range) = $check->(@_);
90              
91 15         241 my $range = $self->range_to_hash($Google::RestApi::SheetsApi4::Range::RANGE_EXPANDED);
92 15         59 $inside_range = $inside_range->range_to_hash($Google::RestApi::SheetsApi4::Range::RANGE_EXPANDED); # can't use aliased.
93              
94 15 100       148 return if $range->[0]->{row} ne $inside_range->[0]->{row};
95 12 50       68 return if $range->[1]->{row} ne $inside_range->[1]->{row};
96 12 100 100     95 return if $range->[0]->{col} && $range->[0]->{col} > $inside_range->[0]->{col};
97 10 50 66     53 return if $range->[1]->{col} && $range->[1]->{col} < $inside_range->[1]->{col};
98            
99 10         58 return 1;
100             }
101              
102             sub freeze {
103 0     0 0   my $self = shift;
104 0           my $range = $self->range_to_dimension('row');
105 0           return $self->freeze_rows($range->{endIndex});
106             }
107              
108             sub thaw {
109 0     0 0   my $self = shift;
110 0           my $range = $self->range_to_dimension('row');
111 0           return $self->freeze_rows($range->{startIndex});
112             }
113              
114 0     0 0   sub heading { shift->SUPER::heading(@_)->freeze(); }
115 0     0 0   sub insert_d { shift->insert_dimension(inherit => shift); }
116 0     0 0   sub insert_dimension { shift->SUPER::insert_dimension(dimension => 'row', @_); }
117 0     0 0   sub move_dimension { shift->SUPER::move_dimension(dimension => 'row', @_); }
118 0     0 0   sub delete_dimension { shift->SUPER::delete_dimension(dimension => 'row', @_); }
119              
120             1;
121              
122             __END__
123              
124             =head1 NAME
125              
126             Google::RestApi::SheetsApi4::Range::Row - Represents a row within a Worksheet.
127              
128             =head1 DESCRIPTION
129              
130             A Range::Row object modifies the behaviour of the parent Range object
131             to treat the values used within the range as a row in the spreadsheet,
132             in other words, a single flat array instead of arrays of arrays. This
133             object will encapsulate the passed flat array value into a [$value]
134             array of arrays when interacting with Google API.
135              
136             It also adjusts calls defined in Request::Spreadsheet::Worksheet::Range
137             to reflect using a row instead of a general range.
138              
139             See the description and synopsis at Google::RestApi::SheetsApi4.
140              
141             =head1 AUTHORS
142              
143             =over
144              
145             =item
146              
147             Robin Murray mvsjes@cpan.org
148              
149             =back
150              
151             =head1 COPYRIGHT
152              
153             Copyright (c) 2021, Robin Murray. All rights reserved.
154              
155             This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.