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