File Coverage

lib/Google/RestApi/SheetsApi4/Request/Spreadsheet/Worksheet.pm
Criterion Covered Total %
statement 21 52 40.3
branch 1 4 25.0
condition 1 7 14.2
subroutine 9 29 31.0
pod 0 23 0.0
total 32 115 27.8


line stmt bran cond sub pod time code
1             package Google::RestApi::SheetsApi4::Request::Spreadsheet::Worksheet;
2              
3             our $VERSION = '1.0.3';
4              
5 1     1   505 use Google::RestApi::Setup;
  1         3  
  1         6  
6              
7 1     1   13712 use aliased "Google::RestApi::SheetsApi4::Request";
  1         3  
  1         7  
8              
9 1     1   118 use parent "Google::RestApi::SheetsApi4::Request::Spreadsheet";
  1         3  
  1         11  
10              
11 0     0 0 0 sub worksheet_id { LOGDIE "Pure virtual function 'worksheet_id' must be overridden"; }
12              
13 1     1 0 29 sub ws_rename { shift->update_worksheet_properties(properties => { title => shift }); }
14 1     1 0 994 sub ws_index { shift->update_worksheet_properties(properties => { index => shift }); }
15 1     1 0 980 sub ws_hide { shift->update_worksheet_properties(properties => { hidden => bool(shift) }); }
16 0     0 0 0 sub ws_hidden { ws_hide(@_); }
17 4     4 0 1877 sub ws_right_to_left { shift->update_worksheet_properties(properties => { rightToLeft => bool(shift) }); }
18 2 50   2 0 1984 sub ws_left_to_right { shift->ws_right_to_left(bool(shift) eq 'true' ? 0 : 1); }
19              
20 0   0 0   0 sub _ws_tab_rgba { shift->ws_tab_color({ (shift) => (shift // 1) }); }
21 0     0 0 0 sub ws_tab_red { shift->_ws_tab_rgba('red' => shift); }
22 0     0 0 0 sub ws_tab_blue { shift->_ws_tab_rgba('blue' => shift); }
23 0     0 0 0 sub ws_tab_green { shift->_ws_tab_rgba('green' => shift); }
24 0     0 0 0 sub ws_tab_alpha { shift->_ws_tab_rgba('alpha' => shift); }
25 0     0 0 0 sub ws_tab_black { shift->ws_tab_color(cl_black()); }
26 0     0 0 0 sub ws_tab_white { shift->ws_tab_color(cl_white()); }
27 0     0 0 0 sub ws_tab_color { shift->update_worksheet_properties(properties => { tabColor => shift }); }
28              
29 0     0 0 0 sub freeze_cols { shift->_freeze('col', @_); }
30 0     0 0 0 sub freeze_rows { shift->_freeze('row', @_); }
31 0     0 0 0 sub unfreeze_cols { shift->freeze_cols(); }
32 0     0 0 0 sub unfreeze_rows { shift->freeze_rows(); }
33             sub _freeze {
34 0     0   0 my $self = shift;
35              
36 0         0 my $dim = shift;
37 0         0 state $check = compile(PositiveOrZeroInt, { default => 0 });
38 0         0 my ($count) = $check->(@_);
39              
40             # "frozenColumnCount" or "frozenRowCount".
41 0 0       0 my $frozen = "frozen" . ($dim eq 'col' ? "Column" : "Row") . "Count";
42              
43 0         0 return $self->update_worksheet_properties(
44             properties => { gridProperties => { $frozen => $count }, },
45             fields => "gridProperties.$frozen",
46             );
47             }
48              
49             sub update_worksheet_properties {
50 7     7 0 16 my $self = shift;
51              
52 7         16 state $check = compile_named(
53             properties => HashRef,
54             fields => Str, { optional => 1 },
55             );
56 7         3304 my $p = $check->(@_);
57              
58 7         202 my $properties = $p->{properties};
59 7   33     57 my $fields = $p->{fields} || join(',', sort keys %$properties);
60              
61 7         30 $self->batch_requests(
62             updateSheetProperties => {
63             properties => {
64             sheetId => $self->worksheet_id(),
65             %$properties,
66             },
67             fields => $fields,
68             },
69             );
70              
71 7         48 return $self;
72             }
73              
74 0     0 0   sub clear_values { shift->_clear("userEnteredValue"); }
75 0     0 0   sub clear_formatting { shift->_clear("userEnteredFormat"); }
76             sub _clear {
77 0     0     my $self = shift;
78              
79 0           my $fields = shift;
80 0   0       my $range = shift || { sheetId => $self->worksheet_id() };
81 0           $self->batch_requests(
82             updateCells => {
83             range => $range,
84             fields => $fields,
85             },
86             );
87              
88 0           return $self;
89             }
90              
91 0     0 0   sub reset { shift->clear_formatting()->clear_values()->unfreeze_cols()->unfreeze_rows(); }
92              
93             sub delete_worksheet {
94 0     0 0   my $self = shift;
95 0           $self->batch_requests(
96             deleteSheet => { sheetId => $self->worksheet_id() }
97             );
98 0           return $self;
99             }
100              
101             1;
102              
103             __END__
104              
105             =head1 NAME
106              
107             Google::RestApi::SheetsApi4::Request::Spreadsheet::Worksheet - Build Google API's batchRequests for a Worksheet.
108              
109             =head1 DESCRIPTION
110              
111             Deriving from the Request::Spreadsheet object, this adds the ability to create
112             requests that have to do with worksheet properties.
113              
114             See the description and synopsis at Google::RestApi::SheetsApi4::Request.
115             and Google::RestApi::SheetsApi4.
116              
117             =head1 AUTHORS
118              
119             =over
120              
121             =item
122              
123             Robin Murray mvsjes@cpan.org
124              
125             =back
126              
127             =head1 COPYRIGHT
128              
129             Copyright (c) 2021, Robin Murray. All rights reserved.
130              
131             This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.