File Coverage

lib/Google/RestApi/SheetsApi4/Range/Col.pm
Criterion Covered Total %
statement 66 77 85.7
branch 20 24 83.3
condition 9 14 64.2
subroutine 14 22 63.6
pod 4 12 33.3
total 113 149 75.8


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