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.4';
4              
5 1     1   697 use Google::RestApi::Setup;
  1         4  
  1         7  
6              
7 1     1   13830 use Try::Tiny qw( try catch );
  1         3  
  1         51  
8              
9 1     1   6 use aliased 'Google::RestApi::SheetsApi4::Range';
  1         2  
  1         7  
10 1     1   193 use aliased 'Google::RestApi::SheetsApi4::Range::Cell';
  1         2  
  1         5  
11              
12 1     1   111 use parent Range;
  1         4  
  1         4  
13              
14             sub new {
15 43     43 1 102 my $class = shift;
16 43         169 my %self = @_;
17              
18 43         127 $self{dim} = 'col';
19 43         143 $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         161 my $err;
30             try {
31 43     43   2137 state $check = compile(RangeCol);
32 43         119528 ($self{range}) = $check->($self{range});
33             } catch {
34 4     4   553 $err = $_;
35 43         386 };
36 43 100       2453 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         36 my $factory_range;
40             try {
41 4     4   191 $factory_range = Google::RestApi::SheetsApi4::Range::factory(%self);
42 4     0   37 } catch {};
43 4 100 66     170 return $factory_range if $factory_range && $factory_range->isa(__PACKAGE__);
44              
45 2         15 LOGDIE sprintf("Unable to translate '%s' into a worksheet column: $err", flatten_range($self{range}));
46             }
47              
48             sub _col_i2a {
49 43     43   98 my $col = shift;
50 43 100       424 return $col if $col =~ qr/\D/; # if any non-digits found, we're good.
51 1         7 my $l = int($col / 27);
52 1         4 my $r = $col - $l * 26;
53 1 50       11 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         36 state $check = compile_named(
59             values => ArrayRef[Str], { optional => 1 },
60             _extra_ => slurpy Any,
61             );
62 18         4549 my $p = named_extra($check->(@_));
63 18 100       61 $p->{values} = [ $p->{values} ] if defined $p->{values};
64 18         71 my $values = $self->SUPER::values(%$p);
65 18 100       101 return defined $values ? $values->[0] : undef;
66             }
67              
68             sub batch_values {
69 12     12 1 20 my $self = shift;
70 12         25 state $check = compile_named(
71             values => ArrayRef[Str], { optional => 1 },
72             );
73 12         2645 my $p = $check->(@_);
74 12 100       281 $p->{values} = [ $p->{values} ] if $p->{values};
75 12         52 return $self->SUPER::batch_values(%$p);
76             }
77              
78             sub cell_at_offset {
79 12     12 0 787 my $self = shift;
80              
81 12         26 state $check = compile(Int, DimColRow, { optional => 1 });
82 12         4039 my ($offset) = $check->(@_); # we're a column, no dim required.
83              
84 12         412 my $col_range = $self->range_to_array($Google::RestApi::SheetsApi4::Range::RANGE_EXPANDED); # can't use aliased.
85 12         35 my $cell_range = $col_range->[0];
86 12   50     61 $cell_range->[1] = ($cell_range->[1] || 1) + $offset;
87 12 50 33     74 return if $col_range->[1]->[1] && $cell_range->[1] > $col_range->[1]->[1];
88              
89 12         41 my $cell = Cell->new(worksheet => $self->worksheet(), range => $cell_range);
90 12         65 $cell->share_values($self);
91 12         58 return $cell;
92             }
93              
94             sub is_other_inside {
95 21     21 1 101 my $self = shift;
96              
97 21         37 state $check = compile(HasRange);
98 21         2475 my ($inside_range) = $check->(@_);
99              
100 21         262 my $range = $self->range_to_hash($Google::RestApi::SheetsApi4::Range::RANGE_EXPANDED);
101 21         74 $inside_range = $inside_range->range_to_hash($Google::RestApi::SheetsApi4::Range::RANGE_EXPANDED); # can't use aliased.
102              
103 21 100       147 return if $range->[0]->{col} < $inside_range->[0]->{col};
104 18 50       71 return if $range->[1]->{col} > $inside_range->[1]->{col};
105 18 100 100     87 return if $range->[0]->{row} && $range->[0]->{row} > $inside_range->[0]->{row};
106 16 50 66     74 return if $range->[1]->{row} && $range->[1]->{row} < $inside_range->[1]->{row};
107            
108 16         82 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.