1 | <?php |
||
11 | class Api |
||
12 | { |
||
13 | /** @var Collection */ |
||
14 | protected $requests; |
||
15 | |||
16 | /** @var Client */ |
||
17 | protected $client; |
||
18 | |||
19 | /** @var string */ |
||
20 | protected $spreadsheetId; |
||
21 | |||
22 | /** |
||
23 | * SheetService constructor. |
||
24 | * |
||
25 | * @param Client $client |
||
26 | */ |
||
27 | 17 | public function __construct(Client $client) |
|
32 | |||
33 | /** |
||
34 | * Set the service speadsheet ID. |
||
35 | * |
||
36 | * @param string $spreadsheetId |
||
37 | * @return $this |
||
38 | */ |
||
39 | public function setSpreadsheetId($spreadsheetId) |
||
45 | |||
46 | public function addBatchRequests($requests) |
||
47 | { |
||
48 | $requests = is_array($requests) ? $requests : [$requests]; |
||
49 | foreach ($requests as $request) { |
||
50 | $this->requests->push($request); |
||
51 | } |
||
52 | |||
53 | return $this; |
||
54 | } |
||
55 | |||
56 | public function sendBatchRequests() |
||
57 | { |
||
58 | $sheets = new Google_Service_Sheets($this->client); |
||
59 | |||
60 | $sheets->spreadsheets->batchUpdate( |
||
61 | $this->spreadsheetId, |
||
62 | new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ |
||
63 | 'requests' => $this->requests->toArray(), |
||
64 | ]) |
||
65 | ); |
||
66 | |||
67 | $this->requests = new Collection([]); |
||
68 | |||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | public function frozenRowRequest($sheetId, $frozonRowCount = 1) |
||
73 | { |
||
74 | return new Google_Service_Sheets_Request([ |
||
75 | 'updateSheetProperties' => [ |
||
76 | 'properties' => [ |
||
77 | 'sheetId' => $sheetId, |
||
78 | 'gridProperties' => [ |
||
79 | 'frozenRowCount' => $frozonRowCount, |
||
80 | ], |
||
81 | ], |
||
82 | 'fields' => 'gridProperties.frozenRowCount', |
||
83 | ], |
||
84 | ]); |
||
85 | } |
||
86 | |||
87 | public function frozenColumnRequest($sheetId, $frozonColumnCount = 1) |
||
88 | { |
||
89 | return new Google_Service_Sheets_Request([ |
||
90 | 'updateSheetProperties' => [ |
||
91 | 'properties' => [ |
||
92 | 'sheetId' => $sheetId, |
||
93 | 'gridProperties' => [ |
||
94 | 'frozenColumnCount' => $frozonColumnCount, |
||
95 | ], |
||
96 | ], |
||
97 | 'fields' => 'gridProperties.frozenColumnCount', |
||
98 | ], |
||
99 | ]); |
||
100 | } |
||
101 | |||
102 | public function styleArea($range, $styles) |
||
103 | { |
||
104 | return new Google_Service_Sheets_Request([ |
||
105 | 'repeatCell' => [ |
||
106 | 'range' => $range, |
||
107 | 'cell' => [ |
||
108 | 'userEnteredFormat' => [ |
||
109 | 'backgroundColor' => $this->fractalColors($styles['backgroundColor']), |
||
110 | 'horizontalAlignment' => $styles['horizontalAlignment'], |
||
111 | 'verticalAlignment' => $styles['verticalAlignment'], |
||
112 | 'textFormat' => [ |
||
113 | 'foregroundColor' => $this->fractalColors($styles['foregroundColor']), |
||
114 | 'fontSize' => $styles['fontSize'], |
||
115 | 'fontFamily' => $styles['fontFamily'], |
||
116 | 'bold' => $styles['bold'], |
||
117 | ], |
||
118 | ], |
||
119 | ], |
||
120 | 'fields' => 'userEnteredFormat(backgroundColor,textFormat,horizontalAlignment)', |
||
121 | ], |
||
122 | ]); |
||
123 | } |
||
124 | |||
125 | public function fixedColumnWidthRequest($sheetId, $startIndex, $endIndex, $width) |
||
126 | { |
||
127 | return new Google_Service_Sheets_Request([ |
||
128 | 'updateDimensionProperties' => [ |
||
129 | 'range' => [ |
||
130 | 'sheetId' => $sheetId, |
||
131 | 'dimension' => 'COLUMNS', |
||
132 | 'startIndex' => $startIndex, |
||
133 | 'endIndex' => $endIndex, |
||
134 | ], |
||
135 | 'properties' => [ |
||
136 | 'pixelSize' => $width, |
||
137 | ], |
||
138 | 'fields' => 'pixelSize', |
||
139 | ], |
||
140 | ]); |
||
141 | } |
||
142 | |||
143 | public function setSheetPropertiesRequest($sheetId, $title, $tabColor) |
||
144 | { |
||
145 | return new Google_Service_Sheets_Request([ |
||
146 | 'updateSheetProperties' => [ |
||
147 | 'properties' => [ |
||
148 | 'sheetId' => $sheetId, |
||
149 | 'title' => $title, |
||
150 | 'tabColor' => $this->fractalColors($tabColor), |
||
151 | ], |
||
152 | 'fields' => 'title,tabColor', |
||
153 | ], |
||
154 | ]); |
||
155 | } |
||
156 | |||
157 | public function addSheetRequest($title, $rowCount, $columnCount, $tabColor = null) |
||
158 | { |
||
159 | $properties = [ |
||
160 | 'title' => $title, |
||
161 | 'gridProperties' => [ |
||
162 | 'rowCount' => $rowCount, |
||
163 | 'columnCount' => $columnCount, |
||
164 | ], |
||
165 | ]; |
||
166 | |||
167 | if (! is_null($tabColor)) { |
||
168 | $properties['tabColor'] = $this->fractalColors($tabColor); |
||
169 | } |
||
170 | |||
171 | return new Google_Service_Sheets_Request(['addSheet' => ['properties' => $properties]]); |
||
172 | } |
||
173 | |||
174 | public function clearSheetRequest($sheetId) |
||
175 | { |
||
176 | return new Google_Service_Sheets_Request([ |
||
177 | 'updateCells' => [ |
||
178 | 'range' => [ |
||
179 | 'sheetId' => $sheetId, |
||
180 | ], |
||
181 | 'fields' => 'userEnteredValue', |
||
182 | ], |
||
183 | ]); |
||
184 | } |
||
185 | |||
186 | public function protectRangeRequest($range, $description) |
||
187 | { |
||
188 | return new Google_Service_Sheets_Request([ |
||
189 | 'addProtectedRange' => [ |
||
190 | 'protectedRange' => [ |
||
191 | 'range' => $range, |
||
192 | 'description' => $description, |
||
193 | 'warningOnly' => false, |
||
194 | 'editors' => [ |
||
195 | 'users' => [config('translation_sheet.serviceAccountEmail')], |
||
196 | ], |
||
197 | ], |
||
198 | ], |
||
199 | |||
200 | ]); |
||
201 | } |
||
202 | |||
203 | public function writeCells($shortRange, $values) |
||
204 | { |
||
205 | $sheets = new \Google_Service_Sheets($this->client); |
||
206 | |||
207 | $request = new Google_Service_Sheets_BatchUpdateValuesRequest([ |
||
208 | 'valueInputOption' => 'RAW', |
||
209 | 'data' => [ |
||
210 | 'range' => $shortRange, |
||
211 | 'majorDimension' => 'ROWS', |
||
212 | 'values' => $values, |
||
213 | ], |
||
214 | ]); |
||
215 | |||
216 | $sheets->spreadsheets_values->batchUpdate($this->spreadsheetId, $request); |
||
217 | } |
||
218 | |||
219 | public function readCells($sheetId, $range) |
||
227 | |||
228 | public function getSheetRowCount($sheetId) |
||
234 | |||
235 | public function getSheet($sheetId) |
||
236 | { |
||
237 | $sheets = $this->getSheets(); |
||
238 | |||
239 | foreach ($sheets as $sheet) { |
||
240 | if ($sheet['modelData']['properties']['sheetId'] === $sheetId) { |
||
241 | return $sheet; |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | |||
246 | public function getSheets() |
||
252 | |||
253 | public function getSheetProtectedRanges($sheetId, $description = null) |
||
254 | { |
||
255 | $sheet = $this->getSheet($sheetId); |
||
256 | |||
257 | if (is_null($description)) { |
||
258 | return $sheet['protectedRanges']; |
||
259 | } |
||
260 | |||
261 | $ranges = []; |
||
262 | foreach ($sheet['protectedRanges'] as $range) { |
||
263 | if ($range->description === $description) { |
||
264 | $ranges[] = $range; |
||
270 | |||
271 | public function deleteColumnsFrom($sheetId, $fromColumnIndex) |
||
283 | |||
284 | public function deleteRowsFrom($sheetId, $fromRowIndex) |
||
296 | |||
297 | public function deleteProtectedRange($protectedRangeId) |
||
305 | |||
306 | /** |
||
307 | * Return Fractal RGB color array with r,g,b between 0 and 1. |
||
308 | * |
||
309 | * @param mixed $color array of RGB color ([255, 255, 255]) or hex string (#FFFFFF) |
||
310 | * |
||
311 | * @return array |
||
312 | */ |
||
313 | protected function fractalColors($color) |
||
327 | } |
||
328 |