gws-cli 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gws/__init__.py +3 -0
- gws/__main__.py +9 -0
- gws/auth/__init__.py +14 -0
- gws/auth/oauth.py +211 -0
- gws/auth/provider.py +97 -0
- gws/auth/scopes.py +47 -0
- gws/auth/server.py +700 -0
- gws/cli.py +1123 -0
- gws/commands/__init__.py +1 -0
- gws/commands/_account.py +55 -0
- gws/commands/calendar.py +688 -0
- gws/commands/contacts.py +291 -0
- gws/commands/convert.py +122 -0
- gws/commands/docs.py +1432 -0
- gws/commands/drive.py +580 -0
- gws/commands/gmail.py +885 -0
- gws/commands/sheets.py +1550 -0
- gws/commands/slides.py +1107 -0
- gws/config.py +451 -0
- gws/context.py +14 -0
- gws/crypto.py +217 -0
- gws/exceptions.py +58 -0
- gws/output.py +111 -0
- gws/services/__init__.py +1 -0
- gws/services/base.py +47 -0
- gws/services/calendar.py +1377 -0
- gws/services/contacts.py +812 -0
- gws/services/convert.py +879 -0
- gws/services/docs.py +3723 -0
- gws/services/drive.py +1807 -0
- gws/services/gmail.py +2001 -0
- gws/services/sheets.py +3689 -0
- gws/services/slides.py +2845 -0
- gws/utils/__init__.py +1 -0
- gws/utils/colors.py +25 -0
- gws/utils/diagrams.py +220 -0
- gws/utils/retry.py +111 -0
- gws/utils/security.py +23 -0
- gws_cli-1.0.0.dist-info/METADATA +283 -0
- gws_cli-1.0.0.dist-info/RECORD +43 -0
- gws_cli-1.0.0.dist-info/WHEEL +4 -0
- gws_cli-1.0.0.dist-info/entry_points.txt +2 -0
- gws_cli-1.0.0.dist-info/licenses/LICENSE +21 -0
gws/services/sheets.py
ADDED
|
@@ -0,0 +1,3689 @@
|
|
|
1
|
+
"""Google Sheets service operations."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from googleapiclient.errors import HttpError
|
|
6
|
+
|
|
7
|
+
from gws.services.base import BaseService
|
|
8
|
+
import json
|
|
9
|
+
from gws.output import output_success, output_error, output_external_content
|
|
10
|
+
from gws.exceptions import ExitCode
|
|
11
|
+
from gws.utils.colors import parse_hex_color
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class SheetsService(BaseService):
|
|
15
|
+
"""Google Sheets operations."""
|
|
16
|
+
|
|
17
|
+
SERVICE_NAME = "sheets"
|
|
18
|
+
VERSION = "v4"
|
|
19
|
+
|
|
20
|
+
def _unescape_text(self, text: str) -> str:
|
|
21
|
+
"""Remove unnecessary escape sequences from text.
|
|
22
|
+
|
|
23
|
+
This handles cases where shell environments escape special characters
|
|
24
|
+
like exclamation marks (\\! -> !).
|
|
25
|
+
"""
|
|
26
|
+
return text.replace("\\!", "!")
|
|
27
|
+
|
|
28
|
+
def metadata(self, spreadsheet_id: str) -> dict[str, Any]:
|
|
29
|
+
"""Get spreadsheet metadata."""
|
|
30
|
+
try:
|
|
31
|
+
spreadsheet = self.execute(
|
|
32
|
+
self.service.spreadsheets()
|
|
33
|
+
.get(
|
|
34
|
+
spreadsheetId=spreadsheet_id,
|
|
35
|
+
fields="spreadsheetId,properties,sheets(properties)",
|
|
36
|
+
)
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
sheets = [
|
|
40
|
+
{
|
|
41
|
+
"title": sheet["properties"]["title"],
|
|
42
|
+
"sheet_id": sheet["properties"]["sheetId"],
|
|
43
|
+
"index": sheet["properties"]["index"],
|
|
44
|
+
"row_count": sheet["properties"]["gridProperties"]["rowCount"],
|
|
45
|
+
"column_count": sheet["properties"]["gridProperties"]["columnCount"],
|
|
46
|
+
}
|
|
47
|
+
for sheet in spreadsheet.get("sheets", [])
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
output_success(
|
|
51
|
+
operation="sheets.metadata",
|
|
52
|
+
spreadsheet_id=spreadsheet_id,
|
|
53
|
+
title=spreadsheet.get("properties", {}).get("title", ""),
|
|
54
|
+
sheets=sheets,
|
|
55
|
+
sheet_count=len(sheets),
|
|
56
|
+
)
|
|
57
|
+
return spreadsheet
|
|
58
|
+
except HttpError as e:
|
|
59
|
+
output_error(
|
|
60
|
+
error_code="API_ERROR",
|
|
61
|
+
operation="sheets.metadata",
|
|
62
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
63
|
+
)
|
|
64
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
65
|
+
|
|
66
|
+
def read(
|
|
67
|
+
self,
|
|
68
|
+
spreadsheet_id: str,
|
|
69
|
+
range_notation: str | None = None,
|
|
70
|
+
value_render_option: str = "FORMATTED_VALUE",
|
|
71
|
+
) -> dict[str, Any]:
|
|
72
|
+
"""Read values from a spreadsheet.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
spreadsheet_id: The spreadsheet ID.
|
|
76
|
+
range_notation: A1 notation range (e.g., 'Sheet1!A1:C10').
|
|
77
|
+
value_render_option: How values should be rendered:
|
|
78
|
+
- FORMATTED_VALUE (default): Values as displayed in the UI
|
|
79
|
+
- UNFORMATTED_VALUE: Raw values without formatting
|
|
80
|
+
- FORMULA: The formulas in cells (not computed values)
|
|
81
|
+
"""
|
|
82
|
+
try:
|
|
83
|
+
# Unescape shell-escaped characters in range
|
|
84
|
+
if range_notation:
|
|
85
|
+
range_notation = self._unescape_text(range_notation)
|
|
86
|
+
if range_notation:
|
|
87
|
+
result = self.execute(
|
|
88
|
+
self.service.spreadsheets()
|
|
89
|
+
.values()
|
|
90
|
+
.get(
|
|
91
|
+
spreadsheetId=spreadsheet_id,
|
|
92
|
+
range=range_notation,
|
|
93
|
+
valueRenderOption=value_render_option,
|
|
94
|
+
)
|
|
95
|
+
)
|
|
96
|
+
else:
|
|
97
|
+
# Get all data from first sheet
|
|
98
|
+
spreadsheet = self.execute(
|
|
99
|
+
self.service.spreadsheets()
|
|
100
|
+
.get(
|
|
101
|
+
spreadsheetId=spreadsheet_id,
|
|
102
|
+
fields="sheets(properties(title))",
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
first_sheet = spreadsheet["sheets"][0]["properties"]["title"]
|
|
106
|
+
result = self.execute(
|
|
107
|
+
self.service.spreadsheets()
|
|
108
|
+
.values()
|
|
109
|
+
.get(
|
|
110
|
+
spreadsheetId=spreadsheet_id,
|
|
111
|
+
range=first_sheet,
|
|
112
|
+
valueRenderOption=value_render_option,
|
|
113
|
+
)
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
values = result.get("values", [])
|
|
117
|
+
# Wrap cell values as JSON string for security scanning
|
|
118
|
+
output_external_content(
|
|
119
|
+
operation="sheets.read",
|
|
120
|
+
source_type="spreadsheet",
|
|
121
|
+
source_id=spreadsheet_id,
|
|
122
|
+
content_fields={
|
|
123
|
+
"values": json.dumps(values),
|
|
124
|
+
},
|
|
125
|
+
spreadsheet_id=spreadsheet_id,
|
|
126
|
+
range=result.get("range", range_notation),
|
|
127
|
+
row_count=len(values),
|
|
128
|
+
)
|
|
129
|
+
return result
|
|
130
|
+
except HttpError as e:
|
|
131
|
+
output_error(
|
|
132
|
+
error_code="API_ERROR",
|
|
133
|
+
operation="sheets.read",
|
|
134
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
135
|
+
)
|
|
136
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
137
|
+
|
|
138
|
+
def create(
|
|
139
|
+
self,
|
|
140
|
+
title: str,
|
|
141
|
+
sheet_titles: list[str] | None = None,
|
|
142
|
+
folder_id: str | None = None,
|
|
143
|
+
) -> dict[str, Any]:
|
|
144
|
+
"""Create a new spreadsheet."""
|
|
145
|
+
try:
|
|
146
|
+
body: dict[str, Any] = {"properties": {"title": title}}
|
|
147
|
+
|
|
148
|
+
if sheet_titles:
|
|
149
|
+
body["sheets"] = [
|
|
150
|
+
{"properties": {"title": sheet_title}}
|
|
151
|
+
for sheet_title in sheet_titles
|
|
152
|
+
]
|
|
153
|
+
|
|
154
|
+
spreadsheet = self.execute(
|
|
155
|
+
self.service.spreadsheets().create(body=body)
|
|
156
|
+
)
|
|
157
|
+
spreadsheet_id = spreadsheet["spreadsheetId"]
|
|
158
|
+
|
|
159
|
+
# Move to folder if specified
|
|
160
|
+
if folder_id:
|
|
161
|
+
file = self.execute(
|
|
162
|
+
self.drive_service.files().get(
|
|
163
|
+
fileId=spreadsheet_id, fields="parents"
|
|
164
|
+
)
|
|
165
|
+
)
|
|
166
|
+
previous_parents = ",".join(file.get("parents", []))
|
|
167
|
+
|
|
168
|
+
self.execute(
|
|
169
|
+
self.drive_service.files().update(
|
|
170
|
+
fileId=spreadsheet_id,
|
|
171
|
+
addParents=folder_id,
|
|
172
|
+
removeParents=previous_parents,
|
|
173
|
+
fields="id, parents",
|
|
174
|
+
)
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
output_success(
|
|
178
|
+
operation="sheets.create",
|
|
179
|
+
spreadsheet_id=spreadsheet_id,
|
|
180
|
+
title=title,
|
|
181
|
+
web_view_link=f"https://docs.google.com/spreadsheets/d/{spreadsheet_id}/edit",
|
|
182
|
+
)
|
|
183
|
+
return spreadsheet
|
|
184
|
+
except HttpError as e:
|
|
185
|
+
output_error(
|
|
186
|
+
error_code="API_ERROR",
|
|
187
|
+
operation="sheets.create",
|
|
188
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
189
|
+
)
|
|
190
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
191
|
+
|
|
192
|
+
def write(
|
|
193
|
+
self,
|
|
194
|
+
spreadsheet_id: str,
|
|
195
|
+
range_notation: str,
|
|
196
|
+
values: list[list[Any]],
|
|
197
|
+
input_option: str = "USER_ENTERED",
|
|
198
|
+
) -> dict[str, Any]:
|
|
199
|
+
"""Write values to a spreadsheet."""
|
|
200
|
+
try:
|
|
201
|
+
# Unescape shell-escaped characters in range
|
|
202
|
+
range_notation = self._unescape_text(range_notation)
|
|
203
|
+
body = {"values": values}
|
|
204
|
+
result = self.execute(
|
|
205
|
+
self.service.spreadsheets()
|
|
206
|
+
.values()
|
|
207
|
+
.update(
|
|
208
|
+
spreadsheetId=spreadsheet_id,
|
|
209
|
+
range=range_notation,
|
|
210
|
+
valueInputOption=input_option,
|
|
211
|
+
body=body,
|
|
212
|
+
)
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
output_success(
|
|
216
|
+
operation="sheets.write",
|
|
217
|
+
spreadsheet_id=spreadsheet_id,
|
|
218
|
+
range=result.get("updatedRange"),
|
|
219
|
+
updated_rows=result.get("updatedRows", 0),
|
|
220
|
+
updated_columns=result.get("updatedColumns", 0),
|
|
221
|
+
updated_cells=result.get("updatedCells", 0),
|
|
222
|
+
)
|
|
223
|
+
return result
|
|
224
|
+
except HttpError as e:
|
|
225
|
+
output_error(
|
|
226
|
+
error_code="API_ERROR",
|
|
227
|
+
operation="sheets.write",
|
|
228
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
229
|
+
)
|
|
230
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
231
|
+
|
|
232
|
+
def append(
|
|
233
|
+
self,
|
|
234
|
+
spreadsheet_id: str,
|
|
235
|
+
range_notation: str,
|
|
236
|
+
values: list[list[Any]],
|
|
237
|
+
input_option: str = "USER_ENTERED",
|
|
238
|
+
) -> dict[str, Any]:
|
|
239
|
+
"""Append values to a spreadsheet."""
|
|
240
|
+
try:
|
|
241
|
+
# Unescape shell-escaped characters in range
|
|
242
|
+
range_notation = self._unescape_text(range_notation)
|
|
243
|
+
body = {"values": values}
|
|
244
|
+
result = self.execute(
|
|
245
|
+
self.service.spreadsheets()
|
|
246
|
+
.values()
|
|
247
|
+
.append(
|
|
248
|
+
spreadsheetId=spreadsheet_id,
|
|
249
|
+
range=range_notation,
|
|
250
|
+
valueInputOption=input_option,
|
|
251
|
+
insertDataOption="INSERT_ROWS",
|
|
252
|
+
body=body,
|
|
253
|
+
)
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
updates = result.get("updates", {})
|
|
257
|
+
output_success(
|
|
258
|
+
operation="sheets.append",
|
|
259
|
+
spreadsheet_id=spreadsheet_id,
|
|
260
|
+
range=updates.get("updatedRange"),
|
|
261
|
+
updated_rows=updates.get("updatedRows", 0),
|
|
262
|
+
updated_cells=updates.get("updatedCells", 0),
|
|
263
|
+
)
|
|
264
|
+
return result
|
|
265
|
+
except HttpError as e:
|
|
266
|
+
output_error(
|
|
267
|
+
error_code="API_ERROR",
|
|
268
|
+
operation="sheets.append",
|
|
269
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
270
|
+
)
|
|
271
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
272
|
+
|
|
273
|
+
def clear(
|
|
274
|
+
self,
|
|
275
|
+
spreadsheet_id: str,
|
|
276
|
+
range_notation: str,
|
|
277
|
+
) -> dict[str, Any]:
|
|
278
|
+
"""Clear values from a range."""
|
|
279
|
+
try:
|
|
280
|
+
# Unescape shell-escaped characters in range
|
|
281
|
+
range_notation = self._unescape_text(range_notation)
|
|
282
|
+
result = self.execute(
|
|
283
|
+
self.service.spreadsheets()
|
|
284
|
+
.values()
|
|
285
|
+
.clear(spreadsheetId=spreadsheet_id, range=range_notation, body={})
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
output_success(
|
|
289
|
+
operation="sheets.clear",
|
|
290
|
+
spreadsheet_id=spreadsheet_id,
|
|
291
|
+
cleared_range=result.get("clearedRange"),
|
|
292
|
+
)
|
|
293
|
+
return result
|
|
294
|
+
except HttpError as e:
|
|
295
|
+
output_error(
|
|
296
|
+
error_code="API_ERROR",
|
|
297
|
+
operation="sheets.clear",
|
|
298
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
299
|
+
)
|
|
300
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
301
|
+
|
|
302
|
+
def add_sheet(
|
|
303
|
+
self,
|
|
304
|
+
spreadsheet_id: str,
|
|
305
|
+
title: str,
|
|
306
|
+
) -> dict[str, Any]:
|
|
307
|
+
"""Add a new sheet to the spreadsheet."""
|
|
308
|
+
try:
|
|
309
|
+
requests = [
|
|
310
|
+
{
|
|
311
|
+
"addSheet": {
|
|
312
|
+
"properties": {
|
|
313
|
+
"title": title,
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
]
|
|
318
|
+
|
|
319
|
+
result = self.execute(
|
|
320
|
+
self.service.spreadsheets()
|
|
321
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
reply = result.get("replies", [{}])[0]
|
|
325
|
+
sheet_id = reply.get("addSheet", {}).get("properties", {}).get("sheetId")
|
|
326
|
+
|
|
327
|
+
output_success(
|
|
328
|
+
operation="sheets.add_sheet",
|
|
329
|
+
spreadsheet_id=spreadsheet_id,
|
|
330
|
+
sheet_title=title,
|
|
331
|
+
sheet_id=sheet_id,
|
|
332
|
+
)
|
|
333
|
+
return result
|
|
334
|
+
except HttpError as e:
|
|
335
|
+
output_error(
|
|
336
|
+
error_code="API_ERROR",
|
|
337
|
+
operation="sheets.add_sheet",
|
|
338
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
339
|
+
)
|
|
340
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
341
|
+
|
|
342
|
+
def delete_sheet(
|
|
343
|
+
self,
|
|
344
|
+
spreadsheet_id: str,
|
|
345
|
+
sheet_id: int,
|
|
346
|
+
) -> dict[str, Any]:
|
|
347
|
+
"""Delete a sheet from the spreadsheet."""
|
|
348
|
+
try:
|
|
349
|
+
requests = [{"deleteSheet": {"sheetId": sheet_id}}]
|
|
350
|
+
|
|
351
|
+
result = self.execute(
|
|
352
|
+
self.service.spreadsheets()
|
|
353
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
354
|
+
)
|
|
355
|
+
|
|
356
|
+
output_success(
|
|
357
|
+
operation="sheets.delete_sheet",
|
|
358
|
+
spreadsheet_id=spreadsheet_id,
|
|
359
|
+
deleted_sheet_id=sheet_id,
|
|
360
|
+
)
|
|
361
|
+
return result
|
|
362
|
+
except HttpError as e:
|
|
363
|
+
output_error(
|
|
364
|
+
error_code="API_ERROR",
|
|
365
|
+
operation="sheets.delete_sheet",
|
|
366
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
367
|
+
)
|
|
368
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
369
|
+
|
|
370
|
+
def rename_sheet(
|
|
371
|
+
self,
|
|
372
|
+
spreadsheet_id: str,
|
|
373
|
+
sheet_id: int,
|
|
374
|
+
new_title: str,
|
|
375
|
+
) -> dict[str, Any]:
|
|
376
|
+
"""Rename a sheet."""
|
|
377
|
+
try:
|
|
378
|
+
requests = [
|
|
379
|
+
{
|
|
380
|
+
"updateSheetProperties": {
|
|
381
|
+
"properties": {
|
|
382
|
+
"sheetId": sheet_id,
|
|
383
|
+
"title": new_title,
|
|
384
|
+
},
|
|
385
|
+
"fields": "title",
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
]
|
|
389
|
+
|
|
390
|
+
result = self.execute(
|
|
391
|
+
self.service.spreadsheets()
|
|
392
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
393
|
+
)
|
|
394
|
+
|
|
395
|
+
output_success(
|
|
396
|
+
operation="sheets.rename_sheet",
|
|
397
|
+
spreadsheet_id=spreadsheet_id,
|
|
398
|
+
sheet_id=sheet_id,
|
|
399
|
+
new_title=new_title,
|
|
400
|
+
)
|
|
401
|
+
return result
|
|
402
|
+
except HttpError as e:
|
|
403
|
+
output_error(
|
|
404
|
+
error_code="API_ERROR",
|
|
405
|
+
operation="sheets.rename_sheet",
|
|
406
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
407
|
+
)
|
|
408
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
409
|
+
|
|
410
|
+
def format_cells(
|
|
411
|
+
self,
|
|
412
|
+
spreadsheet_id: str,
|
|
413
|
+
sheet_id: int,
|
|
414
|
+
start_row: int,
|
|
415
|
+
end_row: int,
|
|
416
|
+
start_col: int,
|
|
417
|
+
end_col: int,
|
|
418
|
+
bold: bool | None = None,
|
|
419
|
+
italic: bool | None = None,
|
|
420
|
+
font_size: int | None = None,
|
|
421
|
+
background_color: str | None = None,
|
|
422
|
+
foreground_color: str | None = None,
|
|
423
|
+
) -> dict[str, Any]:
|
|
424
|
+
"""Apply formatting to a cell range."""
|
|
425
|
+
try:
|
|
426
|
+
cell_format: dict[str, Any] = {}
|
|
427
|
+
fields = []
|
|
428
|
+
|
|
429
|
+
if bold is not None or italic is not None or font_size is not None:
|
|
430
|
+
text_format: dict[str, Any] = {}
|
|
431
|
+
if bold is not None:
|
|
432
|
+
text_format["bold"] = bold
|
|
433
|
+
fields.append("userEnteredFormat.textFormat.bold")
|
|
434
|
+
if italic is not None:
|
|
435
|
+
text_format["italic"] = italic
|
|
436
|
+
fields.append("userEnteredFormat.textFormat.italic")
|
|
437
|
+
if font_size is not None:
|
|
438
|
+
text_format["fontSize"] = font_size
|
|
439
|
+
fields.append("userEnteredFormat.textFormat.fontSize")
|
|
440
|
+
cell_format["textFormat"] = text_format
|
|
441
|
+
|
|
442
|
+
if background_color is not None:
|
|
443
|
+
rgb = parse_hex_color(background_color)
|
|
444
|
+
cell_format["backgroundColor"] = rgb
|
|
445
|
+
fields.append("userEnteredFormat.backgroundColor")
|
|
446
|
+
|
|
447
|
+
if foreground_color is not None:
|
|
448
|
+
rgb = parse_hex_color(foreground_color)
|
|
449
|
+
if "textFormat" not in cell_format:
|
|
450
|
+
cell_format["textFormat"] = {}
|
|
451
|
+
cell_format["textFormat"]["foregroundColor"] = rgb
|
|
452
|
+
fields.append("userEnteredFormat.textFormat.foregroundColor")
|
|
453
|
+
|
|
454
|
+
if not fields:
|
|
455
|
+
output_error(
|
|
456
|
+
error_code="INVALID_ARGS",
|
|
457
|
+
operation="sheets.format",
|
|
458
|
+
message="At least one formatting option required",
|
|
459
|
+
)
|
|
460
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
461
|
+
|
|
462
|
+
requests = [
|
|
463
|
+
{
|
|
464
|
+
"repeatCell": {
|
|
465
|
+
"range": {
|
|
466
|
+
"sheetId": sheet_id,
|
|
467
|
+
"startRowIndex": start_row,
|
|
468
|
+
"endRowIndex": end_row,
|
|
469
|
+
"startColumnIndex": start_col,
|
|
470
|
+
"endColumnIndex": end_col,
|
|
471
|
+
},
|
|
472
|
+
"cell": {"userEnteredFormat": cell_format},
|
|
473
|
+
"fields": ",".join(fields),
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
]
|
|
477
|
+
|
|
478
|
+
result = self.execute(
|
|
479
|
+
self.service.spreadsheets()
|
|
480
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
481
|
+
)
|
|
482
|
+
|
|
483
|
+
output_success(
|
|
484
|
+
operation="sheets.format",
|
|
485
|
+
spreadsheet_id=spreadsheet_id,
|
|
486
|
+
sheet_id=sheet_id,
|
|
487
|
+
range=f"R{start_row}C{start_col}:R{end_row}C{end_col}",
|
|
488
|
+
formatting=fields,
|
|
489
|
+
)
|
|
490
|
+
return result
|
|
491
|
+
except HttpError as e:
|
|
492
|
+
output_error(
|
|
493
|
+
error_code="API_ERROR",
|
|
494
|
+
operation="sheets.format",
|
|
495
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
496
|
+
)
|
|
497
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
498
|
+
|
|
499
|
+
def batch_get(
|
|
500
|
+
self,
|
|
501
|
+
spreadsheet_id: str,
|
|
502
|
+
ranges: list[str],
|
|
503
|
+
) -> dict[str, Any]:
|
|
504
|
+
"""Read multiple ranges at once."""
|
|
505
|
+
try:
|
|
506
|
+
# Unescape shell-escaped characters in all ranges
|
|
507
|
+
ranges = [self._unescape_text(r) for r in ranges]
|
|
508
|
+
result = self.execute(
|
|
509
|
+
self.service.spreadsheets()
|
|
510
|
+
.values()
|
|
511
|
+
.batchGet(spreadsheetId=spreadsheet_id, ranges=ranges)
|
|
512
|
+
)
|
|
513
|
+
|
|
514
|
+
value_ranges = result.get("valueRanges", [])
|
|
515
|
+
output_data = [
|
|
516
|
+
{
|
|
517
|
+
"range": vr.get("range"),
|
|
518
|
+
"values": vr.get("values", []),
|
|
519
|
+
}
|
|
520
|
+
for vr in value_ranges
|
|
521
|
+
]
|
|
522
|
+
|
|
523
|
+
output_success(
|
|
524
|
+
operation="sheets.batch_get",
|
|
525
|
+
spreadsheet_id=spreadsheet_id,
|
|
526
|
+
ranges_requested=len(ranges),
|
|
527
|
+
ranges_returned=len(value_ranges),
|
|
528
|
+
data=output_data,
|
|
529
|
+
)
|
|
530
|
+
return result
|
|
531
|
+
except HttpError as e:
|
|
532
|
+
output_error(
|
|
533
|
+
error_code="API_ERROR",
|
|
534
|
+
operation="sheets.batch_get",
|
|
535
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
536
|
+
)
|
|
537
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
538
|
+
|
|
539
|
+
def format_cells_extended(
|
|
540
|
+
self,
|
|
541
|
+
spreadsheet_id: str,
|
|
542
|
+
sheet_id: int,
|
|
543
|
+
start_row: int,
|
|
544
|
+
end_row: int,
|
|
545
|
+
start_col: int,
|
|
546
|
+
end_col: int,
|
|
547
|
+
bold: bool | None = None,
|
|
548
|
+
italic: bool | None = None,
|
|
549
|
+
underline: bool | None = None,
|
|
550
|
+
strikethrough: bool | None = None,
|
|
551
|
+
font_family: str | None = None,
|
|
552
|
+
font_size: int | None = None,
|
|
553
|
+
foreground_color: str | None = None,
|
|
554
|
+
background_color: str | None = None,
|
|
555
|
+
horizontal_alignment: str | None = None,
|
|
556
|
+
vertical_alignment: str | None = None,
|
|
557
|
+
text_wrap: str | None = None,
|
|
558
|
+
number_format: str | None = None,
|
|
559
|
+
) -> dict[str, Any]:
|
|
560
|
+
"""Apply extended formatting to a cell range.
|
|
561
|
+
|
|
562
|
+
Args:
|
|
563
|
+
spreadsheet_id: The spreadsheet ID.
|
|
564
|
+
sheet_id: The sheet ID (numeric).
|
|
565
|
+
start_row: Start row index (0-based).
|
|
566
|
+
end_row: End row index (exclusive).
|
|
567
|
+
start_col: Start column index (0-based).
|
|
568
|
+
end_col: End column index (exclusive).
|
|
569
|
+
bold: Bold text.
|
|
570
|
+
italic: Italic text.
|
|
571
|
+
underline: Underline text.
|
|
572
|
+
strikethrough: Strikethrough text.
|
|
573
|
+
font_family: Font family name.
|
|
574
|
+
font_size: Font size in points.
|
|
575
|
+
foreground_color: Text color (hex).
|
|
576
|
+
background_color: Cell background color (hex).
|
|
577
|
+
horizontal_alignment: Alignment (LEFT, CENTER, RIGHT).
|
|
578
|
+
vertical_alignment: Vertical alignment (TOP, MIDDLE, BOTTOM).
|
|
579
|
+
text_wrap: Wrap strategy (OVERFLOW_CELL, CLIP, WRAP).
|
|
580
|
+
number_format: Number format pattern (e.g., "#,##0.00", "0%").
|
|
581
|
+
"""
|
|
582
|
+
try:
|
|
583
|
+
cell_format: dict[str, Any] = {}
|
|
584
|
+
fields = []
|
|
585
|
+
|
|
586
|
+
# Text formatting
|
|
587
|
+
text_format: dict[str, Any] = {}
|
|
588
|
+
if bold is not None:
|
|
589
|
+
text_format["bold"] = bold
|
|
590
|
+
fields.append("userEnteredFormat.textFormat.bold")
|
|
591
|
+
if italic is not None:
|
|
592
|
+
text_format["italic"] = italic
|
|
593
|
+
fields.append("userEnteredFormat.textFormat.italic")
|
|
594
|
+
if underline is not None:
|
|
595
|
+
text_format["underline"] = underline
|
|
596
|
+
fields.append("userEnteredFormat.textFormat.underline")
|
|
597
|
+
if strikethrough is not None:
|
|
598
|
+
text_format["strikethrough"] = strikethrough
|
|
599
|
+
fields.append("userEnteredFormat.textFormat.strikethrough")
|
|
600
|
+
if font_family is not None:
|
|
601
|
+
text_format["fontFamily"] = font_family
|
|
602
|
+
fields.append("userEnteredFormat.textFormat.fontFamily")
|
|
603
|
+
if font_size is not None:
|
|
604
|
+
text_format["fontSize"] = font_size
|
|
605
|
+
fields.append("userEnteredFormat.textFormat.fontSize")
|
|
606
|
+
if foreground_color is not None:
|
|
607
|
+
rgb = parse_hex_color(foreground_color)
|
|
608
|
+
text_format["foregroundColor"] = rgb
|
|
609
|
+
fields.append("userEnteredFormat.textFormat.foregroundColor")
|
|
610
|
+
if text_format:
|
|
611
|
+
cell_format["textFormat"] = text_format
|
|
612
|
+
|
|
613
|
+
# Background color
|
|
614
|
+
if background_color is not None:
|
|
615
|
+
rgb = parse_hex_color(background_color)
|
|
616
|
+
cell_format["backgroundColor"] = rgb
|
|
617
|
+
fields.append("userEnteredFormat.backgroundColor")
|
|
618
|
+
|
|
619
|
+
# Alignment
|
|
620
|
+
if horizontal_alignment is not None:
|
|
621
|
+
valid = {"LEFT", "CENTER", "RIGHT"}
|
|
622
|
+
if horizontal_alignment.upper() not in valid:
|
|
623
|
+
output_error(
|
|
624
|
+
error_code="INVALID_ARGS",
|
|
625
|
+
operation="sheets.format_extended",
|
|
626
|
+
message=f"horizontal_alignment must be one of: {valid}",
|
|
627
|
+
)
|
|
628
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
629
|
+
cell_format["horizontalAlignment"] = horizontal_alignment.upper()
|
|
630
|
+
fields.append("userEnteredFormat.horizontalAlignment")
|
|
631
|
+
|
|
632
|
+
if vertical_alignment is not None:
|
|
633
|
+
valid = {"TOP", "MIDDLE", "BOTTOM"}
|
|
634
|
+
if vertical_alignment.upper() not in valid:
|
|
635
|
+
output_error(
|
|
636
|
+
error_code="INVALID_ARGS",
|
|
637
|
+
operation="sheets.format_extended",
|
|
638
|
+
message=f"vertical_alignment must be one of: {valid}",
|
|
639
|
+
)
|
|
640
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
641
|
+
cell_format["verticalAlignment"] = vertical_alignment.upper()
|
|
642
|
+
fields.append("userEnteredFormat.verticalAlignment")
|
|
643
|
+
|
|
644
|
+
# Text wrap
|
|
645
|
+
if text_wrap is not None:
|
|
646
|
+
valid = {"OVERFLOW_CELL", "CLIP", "WRAP"}
|
|
647
|
+
if text_wrap.upper() not in valid:
|
|
648
|
+
output_error(
|
|
649
|
+
error_code="INVALID_ARGS",
|
|
650
|
+
operation="sheets.format_extended",
|
|
651
|
+
message=f"text_wrap must be one of: {valid}",
|
|
652
|
+
)
|
|
653
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
654
|
+
cell_format["wrapStrategy"] = text_wrap.upper()
|
|
655
|
+
fields.append("userEnteredFormat.wrapStrategy")
|
|
656
|
+
|
|
657
|
+
# Number format
|
|
658
|
+
if number_format is not None:
|
|
659
|
+
cell_format["numberFormat"] = {
|
|
660
|
+
"type": "NUMBER",
|
|
661
|
+
"pattern": number_format,
|
|
662
|
+
}
|
|
663
|
+
fields.append("userEnteredFormat.numberFormat")
|
|
664
|
+
|
|
665
|
+
if not fields:
|
|
666
|
+
output_error(
|
|
667
|
+
error_code="INVALID_ARGS",
|
|
668
|
+
operation="sheets.format_extended",
|
|
669
|
+
message="At least one formatting option required",
|
|
670
|
+
)
|
|
671
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
672
|
+
|
|
673
|
+
requests = [
|
|
674
|
+
{
|
|
675
|
+
"repeatCell": {
|
|
676
|
+
"range": {
|
|
677
|
+
"sheetId": sheet_id,
|
|
678
|
+
"startRowIndex": start_row,
|
|
679
|
+
"endRowIndex": end_row,
|
|
680
|
+
"startColumnIndex": start_col,
|
|
681
|
+
"endColumnIndex": end_col,
|
|
682
|
+
},
|
|
683
|
+
"cell": {"userEnteredFormat": cell_format},
|
|
684
|
+
"fields": ",".join(fields),
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
]
|
|
688
|
+
|
|
689
|
+
result = self.execute(
|
|
690
|
+
self.service.spreadsheets()
|
|
691
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
692
|
+
)
|
|
693
|
+
|
|
694
|
+
output_success(
|
|
695
|
+
operation="sheets.format_extended",
|
|
696
|
+
spreadsheet_id=spreadsheet_id,
|
|
697
|
+
sheet_id=sheet_id,
|
|
698
|
+
range=f"R{start_row}C{start_col}:R{end_row}C{end_col}",
|
|
699
|
+
formatting=fields,
|
|
700
|
+
)
|
|
701
|
+
return result
|
|
702
|
+
except HttpError as e:
|
|
703
|
+
output_error(
|
|
704
|
+
error_code="API_ERROR",
|
|
705
|
+
operation="sheets.format_extended",
|
|
706
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
707
|
+
)
|
|
708
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
709
|
+
|
|
710
|
+
def set_borders(
|
|
711
|
+
self,
|
|
712
|
+
spreadsheet_id: str,
|
|
713
|
+
sheet_id: int,
|
|
714
|
+
start_row: int,
|
|
715
|
+
end_row: int,
|
|
716
|
+
start_col: int,
|
|
717
|
+
end_col: int,
|
|
718
|
+
top: bool = False,
|
|
719
|
+
bottom: bool = False,
|
|
720
|
+
left: bool = False,
|
|
721
|
+
right: bool = False,
|
|
722
|
+
inner_horizontal: bool = False,
|
|
723
|
+
inner_vertical: bool = False,
|
|
724
|
+
color: str = "#000000",
|
|
725
|
+
style: str = "SOLID",
|
|
726
|
+
width: int = 1,
|
|
727
|
+
) -> dict[str, Any]:
|
|
728
|
+
"""Set borders on a cell range.
|
|
729
|
+
|
|
730
|
+
Args:
|
|
731
|
+
spreadsheet_id: The spreadsheet ID.
|
|
732
|
+
sheet_id: The sheet ID (numeric).
|
|
733
|
+
start_row: Start row index (0-based).
|
|
734
|
+
end_row: End row index (exclusive).
|
|
735
|
+
start_col: Start column index (0-based).
|
|
736
|
+
end_col: End column index (exclusive).
|
|
737
|
+
top: Apply top border.
|
|
738
|
+
bottom: Apply bottom border.
|
|
739
|
+
left: Apply left border.
|
|
740
|
+
right: Apply right border.
|
|
741
|
+
inner_horizontal: Apply horizontal borders between rows.
|
|
742
|
+
inner_vertical: Apply vertical borders between columns.
|
|
743
|
+
color: Border color (hex).
|
|
744
|
+
style: Border style (SOLID, DOTTED, DASHED, SOLID_MEDIUM, SOLID_THICK, DOUBLE).
|
|
745
|
+
width: Border width (1, 2, or 3).
|
|
746
|
+
"""
|
|
747
|
+
try:
|
|
748
|
+
valid_styles = {
|
|
749
|
+
"SOLID",
|
|
750
|
+
"DOTTED",
|
|
751
|
+
"DASHED",
|
|
752
|
+
"SOLID_MEDIUM",
|
|
753
|
+
"SOLID_THICK",
|
|
754
|
+
"DOUBLE",
|
|
755
|
+
}
|
|
756
|
+
if style.upper() not in valid_styles:
|
|
757
|
+
output_error(
|
|
758
|
+
error_code="INVALID_ARGS",
|
|
759
|
+
operation="sheets.set_borders",
|
|
760
|
+
message=f"style must be one of: {valid_styles}",
|
|
761
|
+
)
|
|
762
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
763
|
+
|
|
764
|
+
rgb = parse_hex_color(color)
|
|
765
|
+
border_style = {
|
|
766
|
+
"style": style.upper(),
|
|
767
|
+
"width": width,
|
|
768
|
+
"color": rgb,
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
update_borders: dict[str, Any] = {
|
|
772
|
+
"range": {
|
|
773
|
+
"sheetId": sheet_id,
|
|
774
|
+
"startRowIndex": start_row,
|
|
775
|
+
"endRowIndex": end_row,
|
|
776
|
+
"startColumnIndex": start_col,
|
|
777
|
+
"endColumnIndex": end_col,
|
|
778
|
+
},
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
if top:
|
|
782
|
+
update_borders["top"] = border_style
|
|
783
|
+
if bottom:
|
|
784
|
+
update_borders["bottom"] = border_style
|
|
785
|
+
if left:
|
|
786
|
+
update_borders["left"] = border_style
|
|
787
|
+
if right:
|
|
788
|
+
update_borders["right"] = border_style
|
|
789
|
+
if inner_horizontal:
|
|
790
|
+
update_borders["innerHorizontal"] = border_style
|
|
791
|
+
if inner_vertical:
|
|
792
|
+
update_borders["innerVertical"] = border_style
|
|
793
|
+
|
|
794
|
+
if not any([top, bottom, left, right, inner_horizontal, inner_vertical]):
|
|
795
|
+
output_error(
|
|
796
|
+
error_code="INVALID_ARGS",
|
|
797
|
+
operation="sheets.set_borders",
|
|
798
|
+
message="At least one border side required",
|
|
799
|
+
)
|
|
800
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
801
|
+
|
|
802
|
+
requests = [{"updateBorders": update_borders}]
|
|
803
|
+
|
|
804
|
+
result = self.execute(
|
|
805
|
+
self.service.spreadsheets()
|
|
806
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
807
|
+
)
|
|
808
|
+
|
|
809
|
+
output_success(
|
|
810
|
+
operation="sheets.set_borders",
|
|
811
|
+
spreadsheet_id=spreadsheet_id,
|
|
812
|
+
sheet_id=sheet_id,
|
|
813
|
+
range=f"R{start_row}C{start_col}:R{end_row}C{end_col}",
|
|
814
|
+
)
|
|
815
|
+
return result
|
|
816
|
+
except HttpError as e:
|
|
817
|
+
output_error(
|
|
818
|
+
error_code="API_ERROR",
|
|
819
|
+
operation="sheets.set_borders",
|
|
820
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
821
|
+
)
|
|
822
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
823
|
+
|
|
824
|
+
def merge_cells(
|
|
825
|
+
self,
|
|
826
|
+
spreadsheet_id: str,
|
|
827
|
+
sheet_id: int,
|
|
828
|
+
start_row: int,
|
|
829
|
+
end_row: int,
|
|
830
|
+
start_col: int,
|
|
831
|
+
end_col: int,
|
|
832
|
+
merge_type: str = "MERGE_ALL",
|
|
833
|
+
) -> dict[str, Any]:
|
|
834
|
+
"""Merge cells in a range.
|
|
835
|
+
|
|
836
|
+
Args:
|
|
837
|
+
spreadsheet_id: The spreadsheet ID.
|
|
838
|
+
sheet_id: The sheet ID (numeric).
|
|
839
|
+
start_row: Start row index (0-based).
|
|
840
|
+
end_row: End row index (exclusive).
|
|
841
|
+
start_col: Start column index (0-based).
|
|
842
|
+
end_col: End column index (exclusive).
|
|
843
|
+
merge_type: Type of merge (MERGE_ALL, MERGE_COLUMNS, MERGE_ROWS).
|
|
844
|
+
"""
|
|
845
|
+
try:
|
|
846
|
+
valid_types = {"MERGE_ALL", "MERGE_COLUMNS", "MERGE_ROWS"}
|
|
847
|
+
if merge_type.upper() not in valid_types:
|
|
848
|
+
output_error(
|
|
849
|
+
error_code="INVALID_ARGS",
|
|
850
|
+
operation="sheets.merge_cells",
|
|
851
|
+
message=f"merge_type must be one of: {valid_types}",
|
|
852
|
+
)
|
|
853
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
854
|
+
|
|
855
|
+
requests = [
|
|
856
|
+
{
|
|
857
|
+
"mergeCells": {
|
|
858
|
+
"range": {
|
|
859
|
+
"sheetId": sheet_id,
|
|
860
|
+
"startRowIndex": start_row,
|
|
861
|
+
"endRowIndex": end_row,
|
|
862
|
+
"startColumnIndex": start_col,
|
|
863
|
+
"endColumnIndex": end_col,
|
|
864
|
+
},
|
|
865
|
+
"mergeType": merge_type.upper(),
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
]
|
|
869
|
+
|
|
870
|
+
result = self.execute(
|
|
871
|
+
self.service.spreadsheets()
|
|
872
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
873
|
+
)
|
|
874
|
+
|
|
875
|
+
output_success(
|
|
876
|
+
operation="sheets.merge_cells",
|
|
877
|
+
spreadsheet_id=spreadsheet_id,
|
|
878
|
+
sheet_id=sheet_id,
|
|
879
|
+
range=f"R{start_row}C{start_col}:R{end_row}C{end_col}",
|
|
880
|
+
merge_type=merge_type.upper(),
|
|
881
|
+
)
|
|
882
|
+
return result
|
|
883
|
+
except HttpError as e:
|
|
884
|
+
output_error(
|
|
885
|
+
error_code="API_ERROR",
|
|
886
|
+
operation="sheets.merge_cells",
|
|
887
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
888
|
+
)
|
|
889
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
890
|
+
|
|
891
|
+
def unmerge_cells(
|
|
892
|
+
self,
|
|
893
|
+
spreadsheet_id: str,
|
|
894
|
+
sheet_id: int,
|
|
895
|
+
start_row: int,
|
|
896
|
+
end_row: int,
|
|
897
|
+
start_col: int,
|
|
898
|
+
end_col: int,
|
|
899
|
+
) -> dict[str, Any]:
|
|
900
|
+
"""Unmerge cells in a range.
|
|
901
|
+
|
|
902
|
+
Args:
|
|
903
|
+
spreadsheet_id: The spreadsheet ID.
|
|
904
|
+
sheet_id: The sheet ID (numeric).
|
|
905
|
+
start_row: Start row index (0-based).
|
|
906
|
+
end_row: End row index (exclusive).
|
|
907
|
+
start_col: Start column index (0-based).
|
|
908
|
+
end_col: End column index (exclusive).
|
|
909
|
+
"""
|
|
910
|
+
try:
|
|
911
|
+
requests = [
|
|
912
|
+
{
|
|
913
|
+
"unmergeCells": {
|
|
914
|
+
"range": {
|
|
915
|
+
"sheetId": sheet_id,
|
|
916
|
+
"startRowIndex": start_row,
|
|
917
|
+
"endRowIndex": end_row,
|
|
918
|
+
"startColumnIndex": start_col,
|
|
919
|
+
"endColumnIndex": end_col,
|
|
920
|
+
},
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
]
|
|
924
|
+
|
|
925
|
+
result = self.execute(
|
|
926
|
+
self.service.spreadsheets()
|
|
927
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
928
|
+
)
|
|
929
|
+
|
|
930
|
+
output_success(
|
|
931
|
+
operation="sheets.unmerge_cells",
|
|
932
|
+
spreadsheet_id=spreadsheet_id,
|
|
933
|
+
sheet_id=sheet_id,
|
|
934
|
+
range=f"R{start_row}C{start_col}:R{end_row}C{end_col}",
|
|
935
|
+
)
|
|
936
|
+
return result
|
|
937
|
+
except HttpError as e:
|
|
938
|
+
output_error(
|
|
939
|
+
error_code="API_ERROR",
|
|
940
|
+
operation="sheets.unmerge_cells",
|
|
941
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
942
|
+
)
|
|
943
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
944
|
+
|
|
945
|
+
def set_column_width(
|
|
946
|
+
self,
|
|
947
|
+
spreadsheet_id: str,
|
|
948
|
+
sheet_id: int,
|
|
949
|
+
start_column: int,
|
|
950
|
+
end_column: int,
|
|
951
|
+
width: int,
|
|
952
|
+
) -> dict[str, Any]:
|
|
953
|
+
"""Set column width.
|
|
954
|
+
|
|
955
|
+
Args:
|
|
956
|
+
spreadsheet_id: The spreadsheet ID.
|
|
957
|
+
sheet_id: The sheet ID (numeric).
|
|
958
|
+
start_column: Start column index (0-based).
|
|
959
|
+
end_column: End column index (exclusive).
|
|
960
|
+
width: Width in pixels.
|
|
961
|
+
"""
|
|
962
|
+
try:
|
|
963
|
+
requests = [
|
|
964
|
+
{
|
|
965
|
+
"updateDimensionProperties": {
|
|
966
|
+
"range": {
|
|
967
|
+
"sheetId": sheet_id,
|
|
968
|
+
"dimension": "COLUMNS",
|
|
969
|
+
"startIndex": start_column,
|
|
970
|
+
"endIndex": end_column,
|
|
971
|
+
},
|
|
972
|
+
"properties": {"pixelSize": width},
|
|
973
|
+
"fields": "pixelSize",
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
]
|
|
977
|
+
|
|
978
|
+
result = self.execute(
|
|
979
|
+
self.service.spreadsheets()
|
|
980
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
981
|
+
)
|
|
982
|
+
|
|
983
|
+
output_success(
|
|
984
|
+
operation="sheets.set_column_width",
|
|
985
|
+
spreadsheet_id=spreadsheet_id,
|
|
986
|
+
sheet_id=sheet_id,
|
|
987
|
+
columns=f"{start_column}-{end_column}",
|
|
988
|
+
width=width,
|
|
989
|
+
)
|
|
990
|
+
return result
|
|
991
|
+
except HttpError as e:
|
|
992
|
+
output_error(
|
|
993
|
+
error_code="API_ERROR",
|
|
994
|
+
operation="sheets.set_column_width",
|
|
995
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
996
|
+
)
|
|
997
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
998
|
+
|
|
999
|
+
def set_row_height(
|
|
1000
|
+
self,
|
|
1001
|
+
spreadsheet_id: str,
|
|
1002
|
+
sheet_id: int,
|
|
1003
|
+
start_row: int,
|
|
1004
|
+
end_row: int,
|
|
1005
|
+
height: int,
|
|
1006
|
+
) -> dict[str, Any]:
|
|
1007
|
+
"""Set row height.
|
|
1008
|
+
|
|
1009
|
+
Args:
|
|
1010
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1011
|
+
sheet_id: The sheet ID (numeric).
|
|
1012
|
+
start_row: Start row index (0-based).
|
|
1013
|
+
end_row: End row index (exclusive).
|
|
1014
|
+
height: Height in pixels.
|
|
1015
|
+
"""
|
|
1016
|
+
try:
|
|
1017
|
+
requests = [
|
|
1018
|
+
{
|
|
1019
|
+
"updateDimensionProperties": {
|
|
1020
|
+
"range": {
|
|
1021
|
+
"sheetId": sheet_id,
|
|
1022
|
+
"dimension": "ROWS",
|
|
1023
|
+
"startIndex": start_row,
|
|
1024
|
+
"endIndex": end_row,
|
|
1025
|
+
},
|
|
1026
|
+
"properties": {"pixelSize": height},
|
|
1027
|
+
"fields": "pixelSize",
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
]
|
|
1031
|
+
|
|
1032
|
+
result = self.execute(
|
|
1033
|
+
self.service.spreadsheets()
|
|
1034
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1035
|
+
)
|
|
1036
|
+
|
|
1037
|
+
output_success(
|
|
1038
|
+
operation="sheets.set_row_height",
|
|
1039
|
+
spreadsheet_id=spreadsheet_id,
|
|
1040
|
+
sheet_id=sheet_id,
|
|
1041
|
+
rows=f"{start_row}-{end_row}",
|
|
1042
|
+
height=height,
|
|
1043
|
+
)
|
|
1044
|
+
return result
|
|
1045
|
+
except HttpError as e:
|
|
1046
|
+
output_error(
|
|
1047
|
+
error_code="API_ERROR",
|
|
1048
|
+
operation="sheets.set_row_height",
|
|
1049
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1050
|
+
)
|
|
1051
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1052
|
+
|
|
1053
|
+
def auto_resize_columns(
|
|
1054
|
+
self,
|
|
1055
|
+
spreadsheet_id: str,
|
|
1056
|
+
sheet_id: int,
|
|
1057
|
+
start_column: int,
|
|
1058
|
+
end_column: int,
|
|
1059
|
+
) -> dict[str, Any]:
|
|
1060
|
+
"""Auto-resize columns to fit content.
|
|
1061
|
+
|
|
1062
|
+
Args:
|
|
1063
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1064
|
+
sheet_id: The sheet ID (numeric).
|
|
1065
|
+
start_column: Start column index (0-based).
|
|
1066
|
+
end_column: End column index (exclusive).
|
|
1067
|
+
"""
|
|
1068
|
+
try:
|
|
1069
|
+
requests = [
|
|
1070
|
+
{
|
|
1071
|
+
"autoResizeDimensions": {
|
|
1072
|
+
"dimensions": {
|
|
1073
|
+
"sheetId": sheet_id,
|
|
1074
|
+
"dimension": "COLUMNS",
|
|
1075
|
+
"startIndex": start_column,
|
|
1076
|
+
"endIndex": end_column,
|
|
1077
|
+
},
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
]
|
|
1081
|
+
|
|
1082
|
+
result = self.execute(
|
|
1083
|
+
self.service.spreadsheets()
|
|
1084
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1085
|
+
)
|
|
1086
|
+
|
|
1087
|
+
output_success(
|
|
1088
|
+
operation="sheets.auto_resize_columns",
|
|
1089
|
+
spreadsheet_id=spreadsheet_id,
|
|
1090
|
+
sheet_id=sheet_id,
|
|
1091
|
+
columns=f"{start_column}-{end_column}",
|
|
1092
|
+
)
|
|
1093
|
+
return result
|
|
1094
|
+
except HttpError as e:
|
|
1095
|
+
output_error(
|
|
1096
|
+
error_code="API_ERROR",
|
|
1097
|
+
operation="sheets.auto_resize_columns",
|
|
1098
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1099
|
+
)
|
|
1100
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1101
|
+
|
|
1102
|
+
def freeze_rows(
|
|
1103
|
+
self,
|
|
1104
|
+
spreadsheet_id: str,
|
|
1105
|
+
sheet_id: int,
|
|
1106
|
+
num_rows: int,
|
|
1107
|
+
) -> dict[str, Any]:
|
|
1108
|
+
"""Freeze rows at the top of the sheet.
|
|
1109
|
+
|
|
1110
|
+
Args:
|
|
1111
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1112
|
+
sheet_id: The sheet ID (numeric).
|
|
1113
|
+
num_rows: Number of rows to freeze (0 to unfreeze).
|
|
1114
|
+
"""
|
|
1115
|
+
try:
|
|
1116
|
+
requests = [
|
|
1117
|
+
{
|
|
1118
|
+
"updateSheetProperties": {
|
|
1119
|
+
"properties": {
|
|
1120
|
+
"sheetId": sheet_id,
|
|
1121
|
+
"gridProperties": {"frozenRowCount": num_rows},
|
|
1122
|
+
},
|
|
1123
|
+
"fields": "gridProperties.frozenRowCount",
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
]
|
|
1127
|
+
|
|
1128
|
+
result = self.execute(
|
|
1129
|
+
self.service.spreadsheets()
|
|
1130
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1131
|
+
)
|
|
1132
|
+
|
|
1133
|
+
output_success(
|
|
1134
|
+
operation="sheets.freeze_rows",
|
|
1135
|
+
spreadsheet_id=spreadsheet_id,
|
|
1136
|
+
sheet_id=sheet_id,
|
|
1137
|
+
frozen_rows=num_rows,
|
|
1138
|
+
)
|
|
1139
|
+
return result
|
|
1140
|
+
except HttpError as e:
|
|
1141
|
+
output_error(
|
|
1142
|
+
error_code="API_ERROR",
|
|
1143
|
+
operation="sheets.freeze_rows",
|
|
1144
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1145
|
+
)
|
|
1146
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1147
|
+
|
|
1148
|
+
def freeze_columns(
|
|
1149
|
+
self,
|
|
1150
|
+
spreadsheet_id: str,
|
|
1151
|
+
sheet_id: int,
|
|
1152
|
+
num_columns: int,
|
|
1153
|
+
) -> dict[str, Any]:
|
|
1154
|
+
"""Freeze columns at the left of the sheet.
|
|
1155
|
+
|
|
1156
|
+
Args:
|
|
1157
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1158
|
+
sheet_id: The sheet ID (numeric).
|
|
1159
|
+
num_columns: Number of columns to freeze (0 to unfreeze).
|
|
1160
|
+
"""
|
|
1161
|
+
try:
|
|
1162
|
+
requests = [
|
|
1163
|
+
{
|
|
1164
|
+
"updateSheetProperties": {
|
|
1165
|
+
"properties": {
|
|
1166
|
+
"sheetId": sheet_id,
|
|
1167
|
+
"gridProperties": {"frozenColumnCount": num_columns},
|
|
1168
|
+
},
|
|
1169
|
+
"fields": "gridProperties.frozenColumnCount",
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
]
|
|
1173
|
+
|
|
1174
|
+
result = self.execute(
|
|
1175
|
+
self.service.spreadsheets()
|
|
1176
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1177
|
+
)
|
|
1178
|
+
|
|
1179
|
+
output_success(
|
|
1180
|
+
operation="sheets.freeze_columns",
|
|
1181
|
+
spreadsheet_id=spreadsheet_id,
|
|
1182
|
+
sheet_id=sheet_id,
|
|
1183
|
+
frozen_columns=num_columns,
|
|
1184
|
+
)
|
|
1185
|
+
return result
|
|
1186
|
+
except HttpError as e:
|
|
1187
|
+
output_error(
|
|
1188
|
+
error_code="API_ERROR",
|
|
1189
|
+
operation="sheets.freeze_columns",
|
|
1190
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1191
|
+
)
|
|
1192
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1193
|
+
|
|
1194
|
+
def add_conditional_format(
|
|
1195
|
+
self,
|
|
1196
|
+
spreadsheet_id: str,
|
|
1197
|
+
sheet_id: int,
|
|
1198
|
+
start_row: int,
|
|
1199
|
+
end_row: int,
|
|
1200
|
+
start_col: int,
|
|
1201
|
+
end_col: int,
|
|
1202
|
+
condition_type: str,
|
|
1203
|
+
condition_values: list[str],
|
|
1204
|
+
background_color: str | None = None,
|
|
1205
|
+
foreground_color: str | None = None,
|
|
1206
|
+
bold: bool | None = None,
|
|
1207
|
+
) -> dict[str, Any]:
|
|
1208
|
+
"""Add a conditional formatting rule.
|
|
1209
|
+
|
|
1210
|
+
Args:
|
|
1211
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1212
|
+
sheet_id: The sheet ID (numeric).
|
|
1213
|
+
start_row: Start row index (0-based).
|
|
1214
|
+
end_row: End row index (exclusive).
|
|
1215
|
+
start_col: Start column index (0-based).
|
|
1216
|
+
end_col: End column index (exclusive).
|
|
1217
|
+
condition_type: Condition type (NUMBER_GREATER, NUMBER_LESS, NUMBER_EQ,
|
|
1218
|
+
TEXT_CONTAINS, TEXT_STARTS_WITH, BLANK, NOT_BLANK, etc.).
|
|
1219
|
+
condition_values: List of values for the condition.
|
|
1220
|
+
background_color: Cell background color when condition is met (hex).
|
|
1221
|
+
foreground_color: Text color when condition is met (hex).
|
|
1222
|
+
bold: Bold text when condition is met.
|
|
1223
|
+
"""
|
|
1224
|
+
try:
|
|
1225
|
+
from gws.utils.colors import parse_hex_color
|
|
1226
|
+
|
|
1227
|
+
valid_types = {
|
|
1228
|
+
"NUMBER_GREATER",
|
|
1229
|
+
"NUMBER_GREATER_THAN_EQ",
|
|
1230
|
+
"NUMBER_LESS",
|
|
1231
|
+
"NUMBER_LESS_THAN_EQ",
|
|
1232
|
+
"NUMBER_EQ",
|
|
1233
|
+
"NUMBER_NOT_EQ",
|
|
1234
|
+
"NUMBER_BETWEEN",
|
|
1235
|
+
"NUMBER_NOT_BETWEEN",
|
|
1236
|
+
"TEXT_CONTAINS",
|
|
1237
|
+
"TEXT_NOT_CONTAINS",
|
|
1238
|
+
"TEXT_STARTS_WITH",
|
|
1239
|
+
"TEXT_ENDS_WITH",
|
|
1240
|
+
"TEXT_EQ",
|
|
1241
|
+
"BLANK",
|
|
1242
|
+
"NOT_BLANK",
|
|
1243
|
+
"CUSTOM_FORMULA",
|
|
1244
|
+
}
|
|
1245
|
+
if condition_type.upper() not in valid_types:
|
|
1246
|
+
output_error(
|
|
1247
|
+
error_code="INVALID_ARGS",
|
|
1248
|
+
operation="sheets.add_conditional_format",
|
|
1249
|
+
message=f"condition_type must be one of: {valid_types}",
|
|
1250
|
+
)
|
|
1251
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
1252
|
+
|
|
1253
|
+
# Build format
|
|
1254
|
+
cell_format: dict[str, Any] = {}
|
|
1255
|
+
if background_color is not None:
|
|
1256
|
+
rgb = parse_hex_color(background_color)
|
|
1257
|
+
cell_format["backgroundColor"] = rgb
|
|
1258
|
+
if foreground_color is not None or bold is not None:
|
|
1259
|
+
text_format: dict[str, Any] = {}
|
|
1260
|
+
if foreground_color is not None:
|
|
1261
|
+
rgb = parse_hex_color(foreground_color)
|
|
1262
|
+
text_format["foregroundColor"] = rgb
|
|
1263
|
+
if bold is not None:
|
|
1264
|
+
text_format["bold"] = bold
|
|
1265
|
+
cell_format["textFormat"] = text_format
|
|
1266
|
+
|
|
1267
|
+
if not cell_format:
|
|
1268
|
+
output_error(
|
|
1269
|
+
error_code="INVALID_ARGS",
|
|
1270
|
+
operation="sheets.add_conditional_format",
|
|
1271
|
+
message="At least one format option required",
|
|
1272
|
+
)
|
|
1273
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
1274
|
+
|
|
1275
|
+
# Build condition values
|
|
1276
|
+
condition_value_objs = [
|
|
1277
|
+
{"userEnteredValue": v} for v in condition_values
|
|
1278
|
+
]
|
|
1279
|
+
|
|
1280
|
+
requests = [
|
|
1281
|
+
{
|
|
1282
|
+
"addConditionalFormatRule": {
|
|
1283
|
+
"rule": {
|
|
1284
|
+
"ranges": [
|
|
1285
|
+
{
|
|
1286
|
+
"sheetId": sheet_id,
|
|
1287
|
+
"startRowIndex": start_row,
|
|
1288
|
+
"endRowIndex": end_row,
|
|
1289
|
+
"startColumnIndex": start_col,
|
|
1290
|
+
"endColumnIndex": end_col,
|
|
1291
|
+
}
|
|
1292
|
+
],
|
|
1293
|
+
"booleanRule": {
|
|
1294
|
+
"condition": {
|
|
1295
|
+
"type": condition_type.upper(),
|
|
1296
|
+
"values": condition_value_objs,
|
|
1297
|
+
},
|
|
1298
|
+
"format": cell_format,
|
|
1299
|
+
},
|
|
1300
|
+
},
|
|
1301
|
+
"index": 0,
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
]
|
|
1305
|
+
|
|
1306
|
+
result = self.execute(
|
|
1307
|
+
self.service.spreadsheets()
|
|
1308
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1309
|
+
)
|
|
1310
|
+
|
|
1311
|
+
output_success(
|
|
1312
|
+
operation="sheets.add_conditional_format",
|
|
1313
|
+
spreadsheet_id=spreadsheet_id,
|
|
1314
|
+
sheet_id=sheet_id,
|
|
1315
|
+
condition_type=condition_type.upper(),
|
|
1316
|
+
)
|
|
1317
|
+
return result
|
|
1318
|
+
except HttpError as e:
|
|
1319
|
+
output_error(
|
|
1320
|
+
error_code="API_ERROR",
|
|
1321
|
+
operation="sheets.add_conditional_format",
|
|
1322
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1323
|
+
)
|
|
1324
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1325
|
+
|
|
1326
|
+
def add_color_scale(
|
|
1327
|
+
self,
|
|
1328
|
+
spreadsheet_id: str,
|
|
1329
|
+
sheet_id: int,
|
|
1330
|
+
start_row: int,
|
|
1331
|
+
end_row: int,
|
|
1332
|
+
start_col: int,
|
|
1333
|
+
end_col: int,
|
|
1334
|
+
min_color: str,
|
|
1335
|
+
max_color: str,
|
|
1336
|
+
mid_color: str | None = None,
|
|
1337
|
+
) -> dict[str, Any]:
|
|
1338
|
+
"""Add a color scale conditional formatting rule.
|
|
1339
|
+
|
|
1340
|
+
Args:
|
|
1341
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1342
|
+
sheet_id: The sheet ID (numeric).
|
|
1343
|
+
start_row: Start row index (0-based).
|
|
1344
|
+
end_row: End row index (exclusive).
|
|
1345
|
+
start_col: Start column index (0-based).
|
|
1346
|
+
end_col: End column index (exclusive).
|
|
1347
|
+
min_color: Color for minimum values (hex).
|
|
1348
|
+
max_color: Color for maximum values (hex).
|
|
1349
|
+
mid_color: Color for midpoint values (hex, optional).
|
|
1350
|
+
"""
|
|
1351
|
+
try:
|
|
1352
|
+
from gws.utils.colors import parse_hex_color
|
|
1353
|
+
|
|
1354
|
+
gradient_rule: dict[str, Any] = {
|
|
1355
|
+
"minpoint": {
|
|
1356
|
+
"type": "MIN",
|
|
1357
|
+
"color": parse_hex_color(min_color),
|
|
1358
|
+
},
|
|
1359
|
+
"maxpoint": {
|
|
1360
|
+
"type": "MAX",
|
|
1361
|
+
"color": parse_hex_color(max_color),
|
|
1362
|
+
},
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
if mid_color is not None:
|
|
1366
|
+
gradient_rule["midpoint"] = {
|
|
1367
|
+
"type": "PERCENTILE",
|
|
1368
|
+
"value": "50",
|
|
1369
|
+
"color": parse_hex_color(mid_color),
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
requests = [
|
|
1373
|
+
{
|
|
1374
|
+
"addConditionalFormatRule": {
|
|
1375
|
+
"rule": {
|
|
1376
|
+
"ranges": [
|
|
1377
|
+
{
|
|
1378
|
+
"sheetId": sheet_id,
|
|
1379
|
+
"startRowIndex": start_row,
|
|
1380
|
+
"endRowIndex": end_row,
|
|
1381
|
+
"startColumnIndex": start_col,
|
|
1382
|
+
"endColumnIndex": end_col,
|
|
1383
|
+
}
|
|
1384
|
+
],
|
|
1385
|
+
"gradientRule": gradient_rule,
|
|
1386
|
+
},
|
|
1387
|
+
"index": 0,
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
]
|
|
1391
|
+
|
|
1392
|
+
result = self.execute(
|
|
1393
|
+
self.service.spreadsheets()
|
|
1394
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1395
|
+
)
|
|
1396
|
+
|
|
1397
|
+
output_success(
|
|
1398
|
+
operation="sheets.add_color_scale",
|
|
1399
|
+
spreadsheet_id=spreadsheet_id,
|
|
1400
|
+
sheet_id=sheet_id,
|
|
1401
|
+
min_color=min_color,
|
|
1402
|
+
max_color=max_color,
|
|
1403
|
+
)
|
|
1404
|
+
return result
|
|
1405
|
+
except HttpError as e:
|
|
1406
|
+
output_error(
|
|
1407
|
+
error_code="API_ERROR",
|
|
1408
|
+
operation="sheets.add_color_scale",
|
|
1409
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1410
|
+
)
|
|
1411
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1412
|
+
|
|
1413
|
+
def clear_conditional_formats(
|
|
1414
|
+
self,
|
|
1415
|
+
spreadsheet_id: str,
|
|
1416
|
+
sheet_id: int,
|
|
1417
|
+
) -> dict[str, Any]:
|
|
1418
|
+
"""Clear all conditional formatting rules from a sheet.
|
|
1419
|
+
|
|
1420
|
+
Args:
|
|
1421
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1422
|
+
sheet_id: The sheet ID (numeric).
|
|
1423
|
+
"""
|
|
1424
|
+
try:
|
|
1425
|
+
# First, get the spreadsheet to find all conditional format rules
|
|
1426
|
+
spreadsheet = self.execute(
|
|
1427
|
+
self.service.spreadsheets()
|
|
1428
|
+
.get(spreadsheetId=spreadsheet_id)
|
|
1429
|
+
)
|
|
1430
|
+
|
|
1431
|
+
# Find the sheet
|
|
1432
|
+
sheet = None
|
|
1433
|
+
for s in spreadsheet.get("sheets", []):
|
|
1434
|
+
if s["properties"]["sheetId"] == sheet_id:
|
|
1435
|
+
sheet = s
|
|
1436
|
+
break
|
|
1437
|
+
|
|
1438
|
+
if sheet is None:
|
|
1439
|
+
output_error(
|
|
1440
|
+
error_code="NOT_FOUND",
|
|
1441
|
+
operation="sheets.clear_conditional_formats",
|
|
1442
|
+
message=f"Sheet with ID {sheet_id} not found",
|
|
1443
|
+
)
|
|
1444
|
+
raise SystemExit(ExitCode.NOT_FOUND)
|
|
1445
|
+
|
|
1446
|
+
conditional_formats = sheet.get("conditionalFormats", [])
|
|
1447
|
+
if not conditional_formats:
|
|
1448
|
+
output_success(
|
|
1449
|
+
operation="sheets.clear_conditional_formats",
|
|
1450
|
+
spreadsheet_id=spreadsheet_id,
|
|
1451
|
+
sheet_id=sheet_id,
|
|
1452
|
+
rules_cleared=0,
|
|
1453
|
+
)
|
|
1454
|
+
return {"cleared": 0}
|
|
1455
|
+
|
|
1456
|
+
# Delete rules from highest index to lowest
|
|
1457
|
+
requests = []
|
|
1458
|
+
for i in range(len(conditional_formats) - 1, -1, -1):
|
|
1459
|
+
requests.append(
|
|
1460
|
+
{
|
|
1461
|
+
"deleteConditionalFormatRule": {
|
|
1462
|
+
"sheetId": sheet_id,
|
|
1463
|
+
"index": i,
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
)
|
|
1467
|
+
|
|
1468
|
+
result = self.execute(
|
|
1469
|
+
self.service.spreadsheets()
|
|
1470
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1471
|
+
)
|
|
1472
|
+
|
|
1473
|
+
output_success(
|
|
1474
|
+
operation="sheets.clear_conditional_formats",
|
|
1475
|
+
spreadsheet_id=spreadsheet_id,
|
|
1476
|
+
sheet_id=sheet_id,
|
|
1477
|
+
rules_cleared=len(conditional_formats),
|
|
1478
|
+
)
|
|
1479
|
+
return result
|
|
1480
|
+
except HttpError as e:
|
|
1481
|
+
output_error(
|
|
1482
|
+
error_code="API_ERROR",
|
|
1483
|
+
operation="sheets.clear_conditional_formats",
|
|
1484
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1485
|
+
)
|
|
1486
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1487
|
+
|
|
1488
|
+
def insert_rows(
|
|
1489
|
+
self,
|
|
1490
|
+
spreadsheet_id: str,
|
|
1491
|
+
sheet_id: int,
|
|
1492
|
+
start_index: int,
|
|
1493
|
+
count: int,
|
|
1494
|
+
inherit_from_before: bool = True,
|
|
1495
|
+
) -> dict[str, Any]:
|
|
1496
|
+
"""Insert rows at a specific index.
|
|
1497
|
+
|
|
1498
|
+
Args:
|
|
1499
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1500
|
+
sheet_id: The sheet ID (numeric).
|
|
1501
|
+
start_index: Row index to insert at (0-based).
|
|
1502
|
+
count: Number of rows to insert.
|
|
1503
|
+
inherit_from_before: If True, new rows inherit formatting from row above.
|
|
1504
|
+
"""
|
|
1505
|
+
try:
|
|
1506
|
+
requests = [
|
|
1507
|
+
{
|
|
1508
|
+
"insertDimension": {
|
|
1509
|
+
"range": {
|
|
1510
|
+
"sheetId": sheet_id,
|
|
1511
|
+
"dimension": "ROWS",
|
|
1512
|
+
"startIndex": start_index,
|
|
1513
|
+
"endIndex": start_index + count,
|
|
1514
|
+
},
|
|
1515
|
+
"inheritFromBefore": inherit_from_before,
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
]
|
|
1519
|
+
|
|
1520
|
+
result = self.execute(
|
|
1521
|
+
self.service.spreadsheets()
|
|
1522
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1523
|
+
)
|
|
1524
|
+
|
|
1525
|
+
output_success(
|
|
1526
|
+
operation="sheets.insert_rows",
|
|
1527
|
+
spreadsheet_id=spreadsheet_id,
|
|
1528
|
+
sheet_id=sheet_id,
|
|
1529
|
+
start_index=start_index,
|
|
1530
|
+
count=count,
|
|
1531
|
+
)
|
|
1532
|
+
return result
|
|
1533
|
+
except HttpError as e:
|
|
1534
|
+
output_error(
|
|
1535
|
+
error_code="API_ERROR",
|
|
1536
|
+
operation="sheets.insert_rows",
|
|
1537
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1538
|
+
)
|
|
1539
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1540
|
+
|
|
1541
|
+
def insert_columns(
|
|
1542
|
+
self,
|
|
1543
|
+
spreadsheet_id: str,
|
|
1544
|
+
sheet_id: int,
|
|
1545
|
+
start_index: int,
|
|
1546
|
+
count: int,
|
|
1547
|
+
inherit_from_before: bool = True,
|
|
1548
|
+
) -> dict[str, Any]:
|
|
1549
|
+
"""Insert columns at a specific index.
|
|
1550
|
+
|
|
1551
|
+
Args:
|
|
1552
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1553
|
+
sheet_id: The sheet ID (numeric).
|
|
1554
|
+
start_index: Column index to insert at (0-based).
|
|
1555
|
+
count: Number of columns to insert.
|
|
1556
|
+
inherit_from_before: If True, new columns inherit formatting from column to the left.
|
|
1557
|
+
"""
|
|
1558
|
+
try:
|
|
1559
|
+
requests = [
|
|
1560
|
+
{
|
|
1561
|
+
"insertDimension": {
|
|
1562
|
+
"range": {
|
|
1563
|
+
"sheetId": sheet_id,
|
|
1564
|
+
"dimension": "COLUMNS",
|
|
1565
|
+
"startIndex": start_index,
|
|
1566
|
+
"endIndex": start_index + count,
|
|
1567
|
+
},
|
|
1568
|
+
"inheritFromBefore": inherit_from_before,
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
]
|
|
1572
|
+
|
|
1573
|
+
result = self.execute(
|
|
1574
|
+
self.service.spreadsheets()
|
|
1575
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1576
|
+
)
|
|
1577
|
+
|
|
1578
|
+
output_success(
|
|
1579
|
+
operation="sheets.insert_columns",
|
|
1580
|
+
spreadsheet_id=spreadsheet_id,
|
|
1581
|
+
sheet_id=sheet_id,
|
|
1582
|
+
start_index=start_index,
|
|
1583
|
+
count=count,
|
|
1584
|
+
)
|
|
1585
|
+
return result
|
|
1586
|
+
except HttpError as e:
|
|
1587
|
+
output_error(
|
|
1588
|
+
error_code="API_ERROR",
|
|
1589
|
+
operation="sheets.insert_columns",
|
|
1590
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1591
|
+
)
|
|
1592
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1593
|
+
|
|
1594
|
+
def delete_rows(
|
|
1595
|
+
self,
|
|
1596
|
+
spreadsheet_id: str,
|
|
1597
|
+
sheet_id: int,
|
|
1598
|
+
start_index: int,
|
|
1599
|
+
end_index: int,
|
|
1600
|
+
) -> dict[str, Any]:
|
|
1601
|
+
"""Delete rows in a range.
|
|
1602
|
+
|
|
1603
|
+
Args:
|
|
1604
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1605
|
+
sheet_id: The sheet ID (numeric).
|
|
1606
|
+
start_index: Start row index (0-based).
|
|
1607
|
+
end_index: End row index (exclusive).
|
|
1608
|
+
"""
|
|
1609
|
+
try:
|
|
1610
|
+
requests = [
|
|
1611
|
+
{
|
|
1612
|
+
"deleteDimension": {
|
|
1613
|
+
"range": {
|
|
1614
|
+
"sheetId": sheet_id,
|
|
1615
|
+
"dimension": "ROWS",
|
|
1616
|
+
"startIndex": start_index,
|
|
1617
|
+
"endIndex": end_index,
|
|
1618
|
+
},
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
]
|
|
1622
|
+
|
|
1623
|
+
result = self.execute(
|
|
1624
|
+
self.service.spreadsheets()
|
|
1625
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1626
|
+
)
|
|
1627
|
+
|
|
1628
|
+
output_success(
|
|
1629
|
+
operation="sheets.delete_rows",
|
|
1630
|
+
spreadsheet_id=spreadsheet_id,
|
|
1631
|
+
sheet_id=sheet_id,
|
|
1632
|
+
start_index=start_index,
|
|
1633
|
+
end_index=end_index,
|
|
1634
|
+
deleted_count=end_index - start_index,
|
|
1635
|
+
)
|
|
1636
|
+
return result
|
|
1637
|
+
except HttpError as e:
|
|
1638
|
+
output_error(
|
|
1639
|
+
error_code="API_ERROR",
|
|
1640
|
+
operation="sheets.delete_rows",
|
|
1641
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1642
|
+
)
|
|
1643
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1644
|
+
|
|
1645
|
+
def delete_columns(
|
|
1646
|
+
self,
|
|
1647
|
+
spreadsheet_id: str,
|
|
1648
|
+
sheet_id: int,
|
|
1649
|
+
start_index: int,
|
|
1650
|
+
end_index: int,
|
|
1651
|
+
) -> dict[str, Any]:
|
|
1652
|
+
"""Delete columns in a range.
|
|
1653
|
+
|
|
1654
|
+
Args:
|
|
1655
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1656
|
+
sheet_id: The sheet ID (numeric).
|
|
1657
|
+
start_index: Start column index (0-based).
|
|
1658
|
+
end_index: End column index (exclusive).
|
|
1659
|
+
"""
|
|
1660
|
+
try:
|
|
1661
|
+
requests = [
|
|
1662
|
+
{
|
|
1663
|
+
"deleteDimension": {
|
|
1664
|
+
"range": {
|
|
1665
|
+
"sheetId": sheet_id,
|
|
1666
|
+
"dimension": "COLUMNS",
|
|
1667
|
+
"startIndex": start_index,
|
|
1668
|
+
"endIndex": end_index,
|
|
1669
|
+
},
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
]
|
|
1673
|
+
|
|
1674
|
+
result = self.execute(
|
|
1675
|
+
self.service.spreadsheets()
|
|
1676
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1677
|
+
)
|
|
1678
|
+
|
|
1679
|
+
output_success(
|
|
1680
|
+
operation="sheets.delete_columns",
|
|
1681
|
+
spreadsheet_id=spreadsheet_id,
|
|
1682
|
+
sheet_id=sheet_id,
|
|
1683
|
+
start_index=start_index,
|
|
1684
|
+
end_index=end_index,
|
|
1685
|
+
deleted_count=end_index - start_index,
|
|
1686
|
+
)
|
|
1687
|
+
return result
|
|
1688
|
+
except HttpError as e:
|
|
1689
|
+
output_error(
|
|
1690
|
+
error_code="API_ERROR",
|
|
1691
|
+
operation="sheets.delete_columns",
|
|
1692
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1693
|
+
)
|
|
1694
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1695
|
+
|
|
1696
|
+
def sort_range(
|
|
1697
|
+
self,
|
|
1698
|
+
spreadsheet_id: str,
|
|
1699
|
+
sheet_id: int,
|
|
1700
|
+
start_row: int,
|
|
1701
|
+
end_row: int,
|
|
1702
|
+
start_col: int,
|
|
1703
|
+
end_col: int,
|
|
1704
|
+
sort_column: int,
|
|
1705
|
+
ascending: bool = True,
|
|
1706
|
+
) -> dict[str, Any]:
|
|
1707
|
+
"""Sort a range by a column.
|
|
1708
|
+
|
|
1709
|
+
Args:
|
|
1710
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1711
|
+
sheet_id: The sheet ID (numeric).
|
|
1712
|
+
start_row: Start row index (0-based).
|
|
1713
|
+
end_row: End row index (exclusive).
|
|
1714
|
+
start_col: Start column index (0-based).
|
|
1715
|
+
end_col: End column index (exclusive).
|
|
1716
|
+
sort_column: Column index to sort by (0-based).
|
|
1717
|
+
ascending: Sort ascending if True, descending if False.
|
|
1718
|
+
"""
|
|
1719
|
+
try:
|
|
1720
|
+
requests = [
|
|
1721
|
+
{
|
|
1722
|
+
"sortRange": {
|
|
1723
|
+
"range": {
|
|
1724
|
+
"sheetId": sheet_id,
|
|
1725
|
+
"startRowIndex": start_row,
|
|
1726
|
+
"endRowIndex": end_row,
|
|
1727
|
+
"startColumnIndex": start_col,
|
|
1728
|
+
"endColumnIndex": end_col,
|
|
1729
|
+
},
|
|
1730
|
+
"sortSpecs": [
|
|
1731
|
+
{
|
|
1732
|
+
"dimensionIndex": sort_column,
|
|
1733
|
+
"sortOrder": "ASCENDING" if ascending else "DESCENDING",
|
|
1734
|
+
}
|
|
1735
|
+
],
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
]
|
|
1739
|
+
|
|
1740
|
+
result = self.execute(
|
|
1741
|
+
self.service.spreadsheets()
|
|
1742
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1743
|
+
)
|
|
1744
|
+
|
|
1745
|
+
output_success(
|
|
1746
|
+
operation="sheets.sort_range",
|
|
1747
|
+
spreadsheet_id=spreadsheet_id,
|
|
1748
|
+
sheet_id=sheet_id,
|
|
1749
|
+
range=f"R{start_row}C{start_col}:R{end_row}C{end_col}",
|
|
1750
|
+
sort_column=sort_column,
|
|
1751
|
+
ascending=ascending,
|
|
1752
|
+
)
|
|
1753
|
+
return result
|
|
1754
|
+
except HttpError as e:
|
|
1755
|
+
output_error(
|
|
1756
|
+
error_code="API_ERROR",
|
|
1757
|
+
operation="sheets.sort_range",
|
|
1758
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1759
|
+
)
|
|
1760
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1761
|
+
|
|
1762
|
+
def find_replace(
|
|
1763
|
+
self,
|
|
1764
|
+
spreadsheet_id: str,
|
|
1765
|
+
find: str,
|
|
1766
|
+
replace: str,
|
|
1767
|
+
sheet_id: int | None = None,
|
|
1768
|
+
all_sheets: bool = False,
|
|
1769
|
+
match_case: bool = False,
|
|
1770
|
+
match_entire_cell: bool = False,
|
|
1771
|
+
use_regex: bool = False,
|
|
1772
|
+
) -> dict[str, Any]:
|
|
1773
|
+
"""Find and replace text in a spreadsheet.
|
|
1774
|
+
|
|
1775
|
+
Args:
|
|
1776
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1777
|
+
find: Text to find.
|
|
1778
|
+
replace: Replacement text.
|
|
1779
|
+
sheet_id: Sheet ID to search in (optional, omit for all_sheets).
|
|
1780
|
+
all_sheets: If True, search all sheets.
|
|
1781
|
+
match_case: Case-sensitive search.
|
|
1782
|
+
match_entire_cell: Match entire cell content only.
|
|
1783
|
+
use_regex: Treat find as a regular expression.
|
|
1784
|
+
"""
|
|
1785
|
+
try:
|
|
1786
|
+
find_replace_request: dict[str, Any] = {
|
|
1787
|
+
"find": find,
|
|
1788
|
+
"replacement": replace,
|
|
1789
|
+
"matchCase": match_case,
|
|
1790
|
+
"matchEntireCell": match_entire_cell,
|
|
1791
|
+
"searchByRegex": use_regex,
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
if all_sheets:
|
|
1795
|
+
find_replace_request["allSheets"] = True
|
|
1796
|
+
elif sheet_id is not None:
|
|
1797
|
+
find_replace_request["sheetId"] = sheet_id
|
|
1798
|
+
else:
|
|
1799
|
+
find_replace_request["allSheets"] = True
|
|
1800
|
+
|
|
1801
|
+
requests = [{"findReplace": find_replace_request}]
|
|
1802
|
+
|
|
1803
|
+
result = self.execute(
|
|
1804
|
+
self.service.spreadsheets()
|
|
1805
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1806
|
+
)
|
|
1807
|
+
|
|
1808
|
+
# Extract replacement count from response
|
|
1809
|
+
replies = result.get("replies", [{}])
|
|
1810
|
+
find_replace_response = replies[0].get("findReplace", {})
|
|
1811
|
+
occurrences_changed = find_replace_response.get("occurrencesChanged", 0)
|
|
1812
|
+
values_changed = find_replace_response.get("valuesChanged", 0)
|
|
1813
|
+
sheets_changed = find_replace_response.get("sheetsChanged", 0)
|
|
1814
|
+
|
|
1815
|
+
output_success(
|
|
1816
|
+
operation="sheets.find_replace",
|
|
1817
|
+
spreadsheet_id=spreadsheet_id,
|
|
1818
|
+
find=find,
|
|
1819
|
+
replace=replace,
|
|
1820
|
+
occurrences_changed=occurrences_changed,
|
|
1821
|
+
values_changed=values_changed,
|
|
1822
|
+
sheets_changed=sheets_changed,
|
|
1823
|
+
)
|
|
1824
|
+
return result
|
|
1825
|
+
except HttpError as e:
|
|
1826
|
+
output_error(
|
|
1827
|
+
error_code="API_ERROR",
|
|
1828
|
+
operation="sheets.find_replace",
|
|
1829
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1830
|
+
)
|
|
1831
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1832
|
+
|
|
1833
|
+
def duplicate_sheet(
|
|
1834
|
+
self,
|
|
1835
|
+
spreadsheet_id: str,
|
|
1836
|
+
sheet_id: int,
|
|
1837
|
+
new_name: str | None = None,
|
|
1838
|
+
insert_index: int | None = None,
|
|
1839
|
+
) -> dict[str, Any]:
|
|
1840
|
+
"""Duplicate a sheet within the spreadsheet.
|
|
1841
|
+
|
|
1842
|
+
Args:
|
|
1843
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1844
|
+
sheet_id: The sheet ID to duplicate.
|
|
1845
|
+
new_name: Name for the new sheet (optional).
|
|
1846
|
+
insert_index: Position to insert the new sheet (optional).
|
|
1847
|
+
"""
|
|
1848
|
+
try:
|
|
1849
|
+
duplicate_request: dict[str, Any] = {
|
|
1850
|
+
"sourceSheetId": sheet_id,
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
if new_name is not None:
|
|
1854
|
+
duplicate_request["newSheetName"] = new_name
|
|
1855
|
+
if insert_index is not None:
|
|
1856
|
+
duplicate_request["insertSheetIndex"] = insert_index
|
|
1857
|
+
|
|
1858
|
+
requests = [{"duplicateSheet": duplicate_request}]
|
|
1859
|
+
|
|
1860
|
+
result = self.execute(
|
|
1861
|
+
self.service.spreadsheets()
|
|
1862
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1863
|
+
)
|
|
1864
|
+
|
|
1865
|
+
# Extract new sheet info from response
|
|
1866
|
+
replies = result.get("replies", [{}])
|
|
1867
|
+
duplicate_response = replies[0].get("duplicateSheet", {}).get("properties", {})
|
|
1868
|
+
new_sheet_id = duplicate_response.get("sheetId")
|
|
1869
|
+
new_sheet_title = duplicate_response.get("title")
|
|
1870
|
+
|
|
1871
|
+
output_success(
|
|
1872
|
+
operation="sheets.duplicate_sheet",
|
|
1873
|
+
spreadsheet_id=spreadsheet_id,
|
|
1874
|
+
source_sheet_id=sheet_id,
|
|
1875
|
+
new_sheet_id=new_sheet_id,
|
|
1876
|
+
new_sheet_title=new_sheet_title,
|
|
1877
|
+
)
|
|
1878
|
+
return result
|
|
1879
|
+
except HttpError as e:
|
|
1880
|
+
output_error(
|
|
1881
|
+
error_code="API_ERROR",
|
|
1882
|
+
operation="sheets.duplicate_sheet",
|
|
1883
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
1884
|
+
)
|
|
1885
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1886
|
+
|
|
1887
|
+
def set_data_validation(
|
|
1888
|
+
self,
|
|
1889
|
+
spreadsheet_id: str,
|
|
1890
|
+
sheet_id: int,
|
|
1891
|
+
start_row: int,
|
|
1892
|
+
end_row: int,
|
|
1893
|
+
start_col: int,
|
|
1894
|
+
end_col: int,
|
|
1895
|
+
validation_type: str,
|
|
1896
|
+
values: list[str] | None = None,
|
|
1897
|
+
formula: str | None = None,
|
|
1898
|
+
strict: bool = True,
|
|
1899
|
+
show_dropdown: bool = True,
|
|
1900
|
+
) -> dict[str, Any]:
|
|
1901
|
+
"""Set data validation on a range.
|
|
1902
|
+
|
|
1903
|
+
Args:
|
|
1904
|
+
spreadsheet_id: The spreadsheet ID.
|
|
1905
|
+
sheet_id: The sheet ID (numeric).
|
|
1906
|
+
start_row: Start row index (0-based).
|
|
1907
|
+
end_row: End row index (exclusive).
|
|
1908
|
+
start_col: Start column index (0-based).
|
|
1909
|
+
end_col: End column index (exclusive).
|
|
1910
|
+
validation_type: Type of validation (ONE_OF_LIST, NUMBER_GREATER,
|
|
1911
|
+
NUMBER_LESS, NUMBER_BETWEEN, TEXT_CONTAINS, CUSTOM_FORMULA, etc.).
|
|
1912
|
+
values: List of values for ONE_OF_LIST or condition values.
|
|
1913
|
+
formula: Custom formula for CUSTOM_FORMULA type.
|
|
1914
|
+
strict: If True, reject invalid input; if False, show warning.
|
|
1915
|
+
show_dropdown: Show dropdown for list validation.
|
|
1916
|
+
"""
|
|
1917
|
+
try:
|
|
1918
|
+
valid_types = {
|
|
1919
|
+
"ONE_OF_LIST",
|
|
1920
|
+
"NUMBER_GREATER",
|
|
1921
|
+
"NUMBER_LESS",
|
|
1922
|
+
"NUMBER_EQUAL",
|
|
1923
|
+
"NUMBER_NOT_EQUAL",
|
|
1924
|
+
"NUMBER_GREATER_THAN_EQ",
|
|
1925
|
+
"NUMBER_LESS_THAN_EQ",
|
|
1926
|
+
"NUMBER_BETWEEN",
|
|
1927
|
+
"NUMBER_NOT_BETWEEN",
|
|
1928
|
+
"TEXT_CONTAINS",
|
|
1929
|
+
"TEXT_NOT_CONTAINS",
|
|
1930
|
+
"TEXT_STARTS_WITH",
|
|
1931
|
+
"TEXT_ENDS_WITH",
|
|
1932
|
+
"TEXT_EQ",
|
|
1933
|
+
"TEXT_IS_EMAIL",
|
|
1934
|
+
"TEXT_IS_URL",
|
|
1935
|
+
"DATE_EQ",
|
|
1936
|
+
"DATE_BEFORE",
|
|
1937
|
+
"DATE_AFTER",
|
|
1938
|
+
"DATE_ON_OR_BEFORE",
|
|
1939
|
+
"DATE_ON_OR_AFTER",
|
|
1940
|
+
"DATE_BETWEEN",
|
|
1941
|
+
"DATE_NOT_BETWEEN",
|
|
1942
|
+
"DATE_IS_VALID",
|
|
1943
|
+
"CUSTOM_FORMULA",
|
|
1944
|
+
"BOOLEAN",
|
|
1945
|
+
}
|
|
1946
|
+
if validation_type.upper() not in valid_types:
|
|
1947
|
+
output_error(
|
|
1948
|
+
error_code="INVALID_ARGS",
|
|
1949
|
+
operation="sheets.set_data_validation",
|
|
1950
|
+
message=f"validation_type must be one of: {sorted(valid_types)}",
|
|
1951
|
+
)
|
|
1952
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
1953
|
+
|
|
1954
|
+
# Build condition based on type
|
|
1955
|
+
condition: dict[str, Any] = {"type": validation_type.upper()}
|
|
1956
|
+
|
|
1957
|
+
if validation_type.upper() == "ONE_OF_LIST" and values:
|
|
1958
|
+
condition["values"] = [{"userEnteredValue": v} for v in values]
|
|
1959
|
+
elif validation_type.upper() == "CUSTOM_FORMULA" and formula:
|
|
1960
|
+
condition["values"] = [{"userEnteredValue": formula}]
|
|
1961
|
+
elif values:
|
|
1962
|
+
condition["values"] = [{"userEnteredValue": v} for v in values]
|
|
1963
|
+
|
|
1964
|
+
validation_rule: dict[str, Any] = {
|
|
1965
|
+
"condition": condition,
|
|
1966
|
+
"strict": strict,
|
|
1967
|
+
}
|
|
1968
|
+
|
|
1969
|
+
if validation_type.upper() == "ONE_OF_LIST":
|
|
1970
|
+
validation_rule["showCustomUi"] = show_dropdown
|
|
1971
|
+
|
|
1972
|
+
requests = [
|
|
1973
|
+
{
|
|
1974
|
+
"setDataValidation": {
|
|
1975
|
+
"range": {
|
|
1976
|
+
"sheetId": sheet_id,
|
|
1977
|
+
"startRowIndex": start_row,
|
|
1978
|
+
"endRowIndex": end_row,
|
|
1979
|
+
"startColumnIndex": start_col,
|
|
1980
|
+
"endColumnIndex": end_col,
|
|
1981
|
+
},
|
|
1982
|
+
"rule": validation_rule,
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
]
|
|
1986
|
+
|
|
1987
|
+
result = self.execute(
|
|
1988
|
+
self.service.spreadsheets()
|
|
1989
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
1990
|
+
)
|
|
1991
|
+
|
|
1992
|
+
output_success(
|
|
1993
|
+
operation="sheets.set_data_validation",
|
|
1994
|
+
spreadsheet_id=spreadsheet_id,
|
|
1995
|
+
sheet_id=sheet_id,
|
|
1996
|
+
range=f"R{start_row}C{start_col}:R{end_row}C{end_col}",
|
|
1997
|
+
validation_type=validation_type.upper(),
|
|
1998
|
+
)
|
|
1999
|
+
return result
|
|
2000
|
+
except HttpError as e:
|
|
2001
|
+
output_error(
|
|
2002
|
+
error_code="API_ERROR",
|
|
2003
|
+
operation="sheets.set_data_validation",
|
|
2004
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2005
|
+
)
|
|
2006
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2007
|
+
|
|
2008
|
+
def clear_data_validation(
|
|
2009
|
+
self,
|
|
2010
|
+
spreadsheet_id: str,
|
|
2011
|
+
sheet_id: int,
|
|
2012
|
+
start_row: int,
|
|
2013
|
+
end_row: int,
|
|
2014
|
+
start_col: int,
|
|
2015
|
+
end_col: int,
|
|
2016
|
+
) -> dict[str, Any]:
|
|
2017
|
+
"""Clear data validation from a range.
|
|
2018
|
+
|
|
2019
|
+
Args:
|
|
2020
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2021
|
+
sheet_id: The sheet ID (numeric).
|
|
2022
|
+
start_row: Start row index (0-based).
|
|
2023
|
+
end_row: End row index (exclusive).
|
|
2024
|
+
start_col: Start column index (0-based).
|
|
2025
|
+
end_col: End column index (exclusive).
|
|
2026
|
+
"""
|
|
2027
|
+
try:
|
|
2028
|
+
requests = [
|
|
2029
|
+
{
|
|
2030
|
+
"setDataValidation": {
|
|
2031
|
+
"range": {
|
|
2032
|
+
"sheetId": sheet_id,
|
|
2033
|
+
"startRowIndex": start_row,
|
|
2034
|
+
"endRowIndex": end_row,
|
|
2035
|
+
"startColumnIndex": start_col,
|
|
2036
|
+
"endColumnIndex": end_col,
|
|
2037
|
+
},
|
|
2038
|
+
# Omitting 'rule' clears validation
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
]
|
|
2042
|
+
|
|
2043
|
+
result = self.execute(
|
|
2044
|
+
self.service.spreadsheets()
|
|
2045
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2046
|
+
)
|
|
2047
|
+
|
|
2048
|
+
output_success(
|
|
2049
|
+
operation="sheets.clear_data_validation",
|
|
2050
|
+
spreadsheet_id=spreadsheet_id,
|
|
2051
|
+
sheet_id=sheet_id,
|
|
2052
|
+
range=f"R{start_row}C{start_col}:R{end_row}C{end_col}",
|
|
2053
|
+
)
|
|
2054
|
+
return result
|
|
2055
|
+
except HttpError as e:
|
|
2056
|
+
output_error(
|
|
2057
|
+
error_code="API_ERROR",
|
|
2058
|
+
operation="sheets.clear_data_validation",
|
|
2059
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2060
|
+
)
|
|
2061
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2062
|
+
|
|
2063
|
+
def add_chart(
|
|
2064
|
+
self,
|
|
2065
|
+
spreadsheet_id: str,
|
|
2066
|
+
sheet_id: int,
|
|
2067
|
+
chart_type: str,
|
|
2068
|
+
data_range_start_row: int,
|
|
2069
|
+
data_range_end_row: int,
|
|
2070
|
+
data_range_start_col: int,
|
|
2071
|
+
data_range_end_col: int,
|
|
2072
|
+
anchor_row: int,
|
|
2073
|
+
anchor_col: int,
|
|
2074
|
+
title: str | None = None,
|
|
2075
|
+
legend_position: str = "BOTTOM_LEGEND",
|
|
2076
|
+
) -> dict[str, Any]:
|
|
2077
|
+
"""Add a chart to a sheet.
|
|
2078
|
+
|
|
2079
|
+
Args:
|
|
2080
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2081
|
+
sheet_id: The sheet ID (numeric).
|
|
2082
|
+
chart_type: Type of chart (LINE, COLUMN, BAR, PIE, SCATTER, AREA).
|
|
2083
|
+
data_range_start_row: Start row of data range (0-based).
|
|
2084
|
+
data_range_end_row: End row of data range (exclusive).
|
|
2085
|
+
data_range_start_col: Start column of data range (0-based).
|
|
2086
|
+
data_range_end_col: End column of data range (exclusive).
|
|
2087
|
+
anchor_row: Row to anchor chart (0-based).
|
|
2088
|
+
anchor_col: Column to anchor chart (0-based).
|
|
2089
|
+
title: Chart title (optional).
|
|
2090
|
+
legend_position: Legend position (BOTTOM_LEGEND, LEFT_LEGEND,
|
|
2091
|
+
RIGHT_LEGEND, TOP_LEGEND, NO_LEGEND).
|
|
2092
|
+
"""
|
|
2093
|
+
try:
|
|
2094
|
+
valid_types = {"LINE", "COLUMN", "BAR", "PIE", "SCATTER", "AREA"}
|
|
2095
|
+
if chart_type.upper() not in valid_types:
|
|
2096
|
+
output_error(
|
|
2097
|
+
error_code="INVALID_ARGS",
|
|
2098
|
+
operation="sheets.add_chart",
|
|
2099
|
+
message=f"chart_type must be one of: {valid_types}",
|
|
2100
|
+
)
|
|
2101
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
2102
|
+
|
|
2103
|
+
# Build data range
|
|
2104
|
+
data_range = {
|
|
2105
|
+
"sheetId": sheet_id,
|
|
2106
|
+
"startRowIndex": data_range_start_row,
|
|
2107
|
+
"endRowIndex": data_range_end_row,
|
|
2108
|
+
"startColumnIndex": data_range_start_col,
|
|
2109
|
+
"endColumnIndex": data_range_end_col,
|
|
2110
|
+
}
|
|
2111
|
+
|
|
2112
|
+
# Build chart spec based on type
|
|
2113
|
+
chart_spec: dict[str, Any] = {
|
|
2114
|
+
"title": title or "",
|
|
2115
|
+
"basicChart": {
|
|
2116
|
+
"chartType": chart_type.upper(),
|
|
2117
|
+
"legendPosition": legend_position.upper(),
|
|
2118
|
+
"domains": [{"domain": {"sourceRange": {"sources": [data_range]}}}],
|
|
2119
|
+
"series": [{"series": {"sourceRange": {"sources": [data_range]}}}],
|
|
2120
|
+
},
|
|
2121
|
+
}
|
|
2122
|
+
|
|
2123
|
+
requests = [
|
|
2124
|
+
{
|
|
2125
|
+
"addChart": {
|
|
2126
|
+
"chart": {
|
|
2127
|
+
"spec": chart_spec,
|
|
2128
|
+
"position": {
|
|
2129
|
+
"overlayPosition": {
|
|
2130
|
+
"anchorCell": {
|
|
2131
|
+
"sheetId": sheet_id,
|
|
2132
|
+
"rowIndex": anchor_row,
|
|
2133
|
+
"columnIndex": anchor_col,
|
|
2134
|
+
},
|
|
2135
|
+
"widthPixels": 600,
|
|
2136
|
+
"heightPixels": 400,
|
|
2137
|
+
}
|
|
2138
|
+
},
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
]
|
|
2143
|
+
|
|
2144
|
+
result = self.execute(
|
|
2145
|
+
self.service.spreadsheets()
|
|
2146
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2147
|
+
)
|
|
2148
|
+
|
|
2149
|
+
# Extract chart ID from response
|
|
2150
|
+
replies = result.get("replies", [{}])
|
|
2151
|
+
chart_id = replies[0].get("addChart", {}).get("chart", {}).get("chartId")
|
|
2152
|
+
|
|
2153
|
+
output_success(
|
|
2154
|
+
operation="sheets.add_chart",
|
|
2155
|
+
spreadsheet_id=spreadsheet_id,
|
|
2156
|
+
sheet_id=sheet_id,
|
|
2157
|
+
chart_id=chart_id,
|
|
2158
|
+
chart_type=chart_type.upper(),
|
|
2159
|
+
)
|
|
2160
|
+
return result
|
|
2161
|
+
except HttpError as e:
|
|
2162
|
+
output_error(
|
|
2163
|
+
error_code="API_ERROR",
|
|
2164
|
+
operation="sheets.add_chart",
|
|
2165
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2166
|
+
)
|
|
2167
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2168
|
+
|
|
2169
|
+
def delete_chart(
|
|
2170
|
+
self,
|
|
2171
|
+
spreadsheet_id: str,
|
|
2172
|
+
chart_id: int,
|
|
2173
|
+
) -> dict[str, Any]:
|
|
2174
|
+
"""Delete a chart from a spreadsheet.
|
|
2175
|
+
|
|
2176
|
+
Args:
|
|
2177
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2178
|
+
chart_id: The chart ID to delete.
|
|
2179
|
+
"""
|
|
2180
|
+
try:
|
|
2181
|
+
requests = [{"deleteEmbeddedObject": {"objectId": chart_id}}]
|
|
2182
|
+
|
|
2183
|
+
result = self.execute(
|
|
2184
|
+
self.service.spreadsheets()
|
|
2185
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2186
|
+
)
|
|
2187
|
+
|
|
2188
|
+
output_success(
|
|
2189
|
+
operation="sheets.delete_chart",
|
|
2190
|
+
spreadsheet_id=spreadsheet_id,
|
|
2191
|
+
chart_id=chart_id,
|
|
2192
|
+
)
|
|
2193
|
+
return result
|
|
2194
|
+
except HttpError as e:
|
|
2195
|
+
output_error(
|
|
2196
|
+
error_code="API_ERROR",
|
|
2197
|
+
operation="sheets.delete_chart",
|
|
2198
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2199
|
+
)
|
|
2200
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2201
|
+
|
|
2202
|
+
def add_banding(
|
|
2203
|
+
self,
|
|
2204
|
+
spreadsheet_id: str,
|
|
2205
|
+
sheet_id: int,
|
|
2206
|
+
start_row: int,
|
|
2207
|
+
end_row: int,
|
|
2208
|
+
start_col: int,
|
|
2209
|
+
end_col: int,
|
|
2210
|
+
header_color: str | None = None,
|
|
2211
|
+
first_color: str | None = None,
|
|
2212
|
+
second_color: str | None = None,
|
|
2213
|
+
) -> dict[str, Any]:
|
|
2214
|
+
"""Add alternating row colors (banding) to a range.
|
|
2215
|
+
|
|
2216
|
+
Args:
|
|
2217
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2218
|
+
sheet_id: The sheet ID (numeric).
|
|
2219
|
+
start_row: Start row index (0-based).
|
|
2220
|
+
end_row: End row index (exclusive).
|
|
2221
|
+
start_col: Start column index (0-based).
|
|
2222
|
+
end_col: End column index (exclusive).
|
|
2223
|
+
header_color: Header row color (hex, e.g., '#4285F4').
|
|
2224
|
+
first_color: First alternating color (hex, e.g., '#FFFFFF').
|
|
2225
|
+
second_color: Second alternating color (hex, e.g., '#F3F3F3').
|
|
2226
|
+
"""
|
|
2227
|
+
try:
|
|
2228
|
+
row_properties: dict[str, Any] = {}
|
|
2229
|
+
|
|
2230
|
+
if header_color:
|
|
2231
|
+
row_properties["headerColor"] = parse_hex_color(header_color)
|
|
2232
|
+
if first_color:
|
|
2233
|
+
row_properties["firstBandColor"] = parse_hex_color(first_color)
|
|
2234
|
+
if second_color:
|
|
2235
|
+
row_properties["secondBandColor"] = parse_hex_color(second_color)
|
|
2236
|
+
|
|
2237
|
+
# Use defaults if no colors provided
|
|
2238
|
+
if not row_properties:
|
|
2239
|
+
row_properties = {
|
|
2240
|
+
"headerColor": {"red": 0.26, "green": 0.52, "blue": 0.96},
|
|
2241
|
+
"firstBandColor": {"red": 1.0, "green": 1.0, "blue": 1.0},
|
|
2242
|
+
"secondBandColor": {"red": 0.95, "green": 0.95, "blue": 0.95},
|
|
2243
|
+
}
|
|
2244
|
+
|
|
2245
|
+
requests = [
|
|
2246
|
+
{
|
|
2247
|
+
"addBanding": {
|
|
2248
|
+
"bandedRange": {
|
|
2249
|
+
"range": {
|
|
2250
|
+
"sheetId": sheet_id,
|
|
2251
|
+
"startRowIndex": start_row,
|
|
2252
|
+
"endRowIndex": end_row,
|
|
2253
|
+
"startColumnIndex": start_col,
|
|
2254
|
+
"endColumnIndex": end_col,
|
|
2255
|
+
},
|
|
2256
|
+
"rowProperties": row_properties,
|
|
2257
|
+
}
|
|
2258
|
+
}
|
|
2259
|
+
}
|
|
2260
|
+
]
|
|
2261
|
+
|
|
2262
|
+
result = self.execute(
|
|
2263
|
+
self.service.spreadsheets()
|
|
2264
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2265
|
+
)
|
|
2266
|
+
|
|
2267
|
+
# Extract banded range ID from response
|
|
2268
|
+
replies = result.get("replies", [{}])
|
|
2269
|
+
banded_range_id = (
|
|
2270
|
+
replies[0].get("addBanding", {}).get("bandedRange", {}).get("bandedRangeId")
|
|
2271
|
+
)
|
|
2272
|
+
|
|
2273
|
+
output_success(
|
|
2274
|
+
operation="sheets.add_banding",
|
|
2275
|
+
spreadsheet_id=spreadsheet_id,
|
|
2276
|
+
sheet_id=sheet_id,
|
|
2277
|
+
range=f"R{start_row}C{start_col}:R{end_row}C{end_col}",
|
|
2278
|
+
banded_range_id=banded_range_id,
|
|
2279
|
+
)
|
|
2280
|
+
return result
|
|
2281
|
+
except HttpError as e:
|
|
2282
|
+
output_error(
|
|
2283
|
+
error_code="API_ERROR",
|
|
2284
|
+
operation="sheets.add_banding",
|
|
2285
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2286
|
+
)
|
|
2287
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2288
|
+
|
|
2289
|
+
def delete_banding(
|
|
2290
|
+
self,
|
|
2291
|
+
spreadsheet_id: str,
|
|
2292
|
+
banded_range_id: int,
|
|
2293
|
+
) -> dict[str, Any]:
|
|
2294
|
+
"""Delete a banded range (alternating colors) from a spreadsheet.
|
|
2295
|
+
|
|
2296
|
+
Args:
|
|
2297
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2298
|
+
banded_range_id: The banded range ID to delete.
|
|
2299
|
+
"""
|
|
2300
|
+
try:
|
|
2301
|
+
requests = [{"deleteBanding": {"bandedRangeId": banded_range_id}}]
|
|
2302
|
+
|
|
2303
|
+
result = self.execute(
|
|
2304
|
+
self.service.spreadsheets()
|
|
2305
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2306
|
+
)
|
|
2307
|
+
|
|
2308
|
+
output_success(
|
|
2309
|
+
operation="sheets.delete_banding",
|
|
2310
|
+
spreadsheet_id=spreadsheet_id,
|
|
2311
|
+
banded_range_id=banded_range_id,
|
|
2312
|
+
)
|
|
2313
|
+
return result
|
|
2314
|
+
except HttpError as e:
|
|
2315
|
+
output_error(
|
|
2316
|
+
error_code="API_ERROR",
|
|
2317
|
+
operation="sheets.delete_banding",
|
|
2318
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2319
|
+
)
|
|
2320
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2321
|
+
|
|
2322
|
+
# =========================================================================
|
|
2323
|
+
# FILTER OPERATIONS
|
|
2324
|
+
# =========================================================================
|
|
2325
|
+
|
|
2326
|
+
def set_basic_filter(
|
|
2327
|
+
self,
|
|
2328
|
+
spreadsheet_id: str,
|
|
2329
|
+
sheet_id: int,
|
|
2330
|
+
start_row: int,
|
|
2331
|
+
end_row: int,
|
|
2332
|
+
start_col: int,
|
|
2333
|
+
end_col: int,
|
|
2334
|
+
) -> dict[str, Any]:
|
|
2335
|
+
"""Set a basic filter on a range.
|
|
2336
|
+
|
|
2337
|
+
Args:
|
|
2338
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2339
|
+
sheet_id: The sheet ID.
|
|
2340
|
+
start_row: Starting row index (0-indexed).
|
|
2341
|
+
end_row: Ending row index (exclusive).
|
|
2342
|
+
start_col: Starting column index (0-indexed).
|
|
2343
|
+
end_col: Ending column index (exclusive).
|
|
2344
|
+
"""
|
|
2345
|
+
try:
|
|
2346
|
+
requests = [{
|
|
2347
|
+
"setBasicFilter": {
|
|
2348
|
+
"filter": {
|
|
2349
|
+
"range": {
|
|
2350
|
+
"sheetId": sheet_id,
|
|
2351
|
+
"startRowIndex": start_row,
|
|
2352
|
+
"endRowIndex": end_row,
|
|
2353
|
+
"startColumnIndex": start_col,
|
|
2354
|
+
"endColumnIndex": end_col,
|
|
2355
|
+
}
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
}]
|
|
2359
|
+
|
|
2360
|
+
result = self.execute(
|
|
2361
|
+
self.service.spreadsheets()
|
|
2362
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2363
|
+
)
|
|
2364
|
+
|
|
2365
|
+
output_success(
|
|
2366
|
+
operation="sheets.set_basic_filter",
|
|
2367
|
+
spreadsheet_id=spreadsheet_id,
|
|
2368
|
+
sheet_id=sheet_id,
|
|
2369
|
+
range=f"R{start_row}C{start_col}:R{end_row}C{end_col}",
|
|
2370
|
+
)
|
|
2371
|
+
return result
|
|
2372
|
+
except HttpError as e:
|
|
2373
|
+
output_error(
|
|
2374
|
+
error_code="API_ERROR",
|
|
2375
|
+
operation="sheets.set_basic_filter",
|
|
2376
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2377
|
+
)
|
|
2378
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2379
|
+
|
|
2380
|
+
def clear_basic_filter(
|
|
2381
|
+
self,
|
|
2382
|
+
spreadsheet_id: str,
|
|
2383
|
+
sheet_id: int,
|
|
2384
|
+
) -> dict[str, Any]:
|
|
2385
|
+
"""Clear the basic filter from a sheet.
|
|
2386
|
+
|
|
2387
|
+
Args:
|
|
2388
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2389
|
+
sheet_id: The sheet ID.
|
|
2390
|
+
"""
|
|
2391
|
+
try:
|
|
2392
|
+
requests = [{"clearBasicFilter": {"sheetId": sheet_id}}]
|
|
2393
|
+
|
|
2394
|
+
result = self.execute(
|
|
2395
|
+
self.service.spreadsheets()
|
|
2396
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2397
|
+
)
|
|
2398
|
+
|
|
2399
|
+
output_success(
|
|
2400
|
+
operation="sheets.clear_basic_filter",
|
|
2401
|
+
spreadsheet_id=spreadsheet_id,
|
|
2402
|
+
sheet_id=sheet_id,
|
|
2403
|
+
)
|
|
2404
|
+
return result
|
|
2405
|
+
except HttpError as e:
|
|
2406
|
+
output_error(
|
|
2407
|
+
error_code="API_ERROR",
|
|
2408
|
+
operation="sheets.clear_basic_filter",
|
|
2409
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2410
|
+
)
|
|
2411
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2412
|
+
|
|
2413
|
+
def create_filter_view(
|
|
2414
|
+
self,
|
|
2415
|
+
spreadsheet_id: str,
|
|
2416
|
+
sheet_id: int,
|
|
2417
|
+
title: str,
|
|
2418
|
+
start_row: int,
|
|
2419
|
+
end_row: int,
|
|
2420
|
+
start_col: int,
|
|
2421
|
+
end_col: int,
|
|
2422
|
+
) -> dict[str, Any]:
|
|
2423
|
+
"""Create a named filter view.
|
|
2424
|
+
|
|
2425
|
+
Args:
|
|
2426
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2427
|
+
sheet_id: The sheet ID.
|
|
2428
|
+
title: Name for the filter view.
|
|
2429
|
+
start_row: Starting row index (0-indexed).
|
|
2430
|
+
end_row: Ending row index (exclusive).
|
|
2431
|
+
start_col: Starting column index (0-indexed).
|
|
2432
|
+
end_col: Ending column index (exclusive).
|
|
2433
|
+
"""
|
|
2434
|
+
try:
|
|
2435
|
+
requests = [{
|
|
2436
|
+
"addFilterView": {
|
|
2437
|
+
"filter": {
|
|
2438
|
+
"title": title,
|
|
2439
|
+
"range": {
|
|
2440
|
+
"sheetId": sheet_id,
|
|
2441
|
+
"startRowIndex": start_row,
|
|
2442
|
+
"endRowIndex": end_row,
|
|
2443
|
+
"startColumnIndex": start_col,
|
|
2444
|
+
"endColumnIndex": end_col,
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
2448
|
+
}]
|
|
2449
|
+
|
|
2450
|
+
result = self.execute(
|
|
2451
|
+
self.service.spreadsheets()
|
|
2452
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2453
|
+
)
|
|
2454
|
+
|
|
2455
|
+
filter_view_id = (
|
|
2456
|
+
result.get("replies", [{}])[0]
|
|
2457
|
+
.get("addFilterView", {})
|
|
2458
|
+
.get("filter", {})
|
|
2459
|
+
.get("filterViewId")
|
|
2460
|
+
)
|
|
2461
|
+
|
|
2462
|
+
output_success(
|
|
2463
|
+
operation="sheets.create_filter_view",
|
|
2464
|
+
spreadsheet_id=spreadsheet_id,
|
|
2465
|
+
sheet_id=sheet_id,
|
|
2466
|
+
title=title,
|
|
2467
|
+
filter_view_id=filter_view_id,
|
|
2468
|
+
)
|
|
2469
|
+
return result
|
|
2470
|
+
except HttpError as e:
|
|
2471
|
+
output_error(
|
|
2472
|
+
error_code="API_ERROR",
|
|
2473
|
+
operation="sheets.create_filter_view",
|
|
2474
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2475
|
+
)
|
|
2476
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2477
|
+
|
|
2478
|
+
def list_filter_views(
|
|
2479
|
+
self,
|
|
2480
|
+
spreadsheet_id: str,
|
|
2481
|
+
) -> dict[str, Any]:
|
|
2482
|
+
"""List all filter views in a spreadsheet.
|
|
2483
|
+
|
|
2484
|
+
Args:
|
|
2485
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2486
|
+
"""
|
|
2487
|
+
try:
|
|
2488
|
+
spreadsheet = self.execute(
|
|
2489
|
+
self.service.spreadsheets()
|
|
2490
|
+
.get(spreadsheetId=spreadsheet_id)
|
|
2491
|
+
)
|
|
2492
|
+
|
|
2493
|
+
filter_views = []
|
|
2494
|
+
for sheet in spreadsheet.get("sheets", []):
|
|
2495
|
+
sheet_title = sheet["properties"]["title"]
|
|
2496
|
+
sheet_id = sheet["properties"]["sheetId"]
|
|
2497
|
+
for fv in sheet.get("filterViews", []):
|
|
2498
|
+
filter_views.append({
|
|
2499
|
+
"filter_view_id": fv.get("filterViewId"),
|
|
2500
|
+
"title": fv.get("title"),
|
|
2501
|
+
"sheet_id": sheet_id,
|
|
2502
|
+
"sheet_title": sheet_title,
|
|
2503
|
+
"range": fv.get("range"),
|
|
2504
|
+
})
|
|
2505
|
+
|
|
2506
|
+
output_success(
|
|
2507
|
+
operation="sheets.list_filter_views",
|
|
2508
|
+
spreadsheet_id=spreadsheet_id,
|
|
2509
|
+
count=len(filter_views),
|
|
2510
|
+
filter_views=filter_views,
|
|
2511
|
+
)
|
|
2512
|
+
return {"filter_views": filter_views}
|
|
2513
|
+
except HttpError as e:
|
|
2514
|
+
output_error(
|
|
2515
|
+
error_code="API_ERROR",
|
|
2516
|
+
operation="sheets.list_filter_views",
|
|
2517
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2518
|
+
)
|
|
2519
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2520
|
+
|
|
2521
|
+
def delete_filter_view(
|
|
2522
|
+
self,
|
|
2523
|
+
spreadsheet_id: str,
|
|
2524
|
+
filter_view_id: int,
|
|
2525
|
+
) -> dict[str, Any]:
|
|
2526
|
+
"""Delete a filter view.
|
|
2527
|
+
|
|
2528
|
+
Args:
|
|
2529
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2530
|
+
filter_view_id: The filter view ID to delete.
|
|
2531
|
+
"""
|
|
2532
|
+
try:
|
|
2533
|
+
requests = [{"deleteFilterView": {"filterId": filter_view_id}}]
|
|
2534
|
+
|
|
2535
|
+
result = self.execute(
|
|
2536
|
+
self.service.spreadsheets()
|
|
2537
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2538
|
+
)
|
|
2539
|
+
|
|
2540
|
+
output_success(
|
|
2541
|
+
operation="sheets.delete_filter_view",
|
|
2542
|
+
spreadsheet_id=spreadsheet_id,
|
|
2543
|
+
filter_view_id=filter_view_id,
|
|
2544
|
+
)
|
|
2545
|
+
return result
|
|
2546
|
+
except HttpError as e:
|
|
2547
|
+
output_error(
|
|
2548
|
+
error_code="API_ERROR",
|
|
2549
|
+
operation="sheets.delete_filter_view",
|
|
2550
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2551
|
+
)
|
|
2552
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2553
|
+
|
|
2554
|
+
# =========================================================================
|
|
2555
|
+
# PIVOT TABLE OPERATIONS
|
|
2556
|
+
# =========================================================================
|
|
2557
|
+
|
|
2558
|
+
def create_pivot_table(
|
|
2559
|
+
self,
|
|
2560
|
+
spreadsheet_id: str,
|
|
2561
|
+
source_sheet_id: int,
|
|
2562
|
+
source_start_row: int,
|
|
2563
|
+
source_end_row: int,
|
|
2564
|
+
source_start_col: int,
|
|
2565
|
+
source_end_col: int,
|
|
2566
|
+
target_sheet_id: int,
|
|
2567
|
+
target_row: int,
|
|
2568
|
+
target_col: int,
|
|
2569
|
+
row_source_columns: list[int],
|
|
2570
|
+
value_source_columns: list[int],
|
|
2571
|
+
value_functions: list[str] | None = None,
|
|
2572
|
+
column_source_columns: list[int] | None = None,
|
|
2573
|
+
) -> dict[str, Any]:
|
|
2574
|
+
"""Create a pivot table.
|
|
2575
|
+
|
|
2576
|
+
Args:
|
|
2577
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2578
|
+
source_sheet_id: Sheet ID containing source data.
|
|
2579
|
+
source_start_row: Source data start row (0-indexed).
|
|
2580
|
+
source_end_row: Source data end row (exclusive).
|
|
2581
|
+
source_start_col: Source data start column (0-indexed).
|
|
2582
|
+
source_end_col: Source data end column (exclusive).
|
|
2583
|
+
target_sheet_id: Sheet ID for pivot table output.
|
|
2584
|
+
target_row: Target row for pivot table (0-indexed).
|
|
2585
|
+
target_col: Target column for pivot table (0-indexed).
|
|
2586
|
+
row_source_columns: List of column indices to use as row groups.
|
|
2587
|
+
value_source_columns: List of column indices to aggregate.
|
|
2588
|
+
value_functions: Aggregation functions (SUM, AVERAGE, COUNT, MAX, MIN, etc.).
|
|
2589
|
+
column_source_columns: Optional column indices for column groups.
|
|
2590
|
+
"""
|
|
2591
|
+
try:
|
|
2592
|
+
if value_functions is None:
|
|
2593
|
+
value_functions = ["SUM"] * len(value_source_columns)
|
|
2594
|
+
|
|
2595
|
+
# Build row groups
|
|
2596
|
+
rows = []
|
|
2597
|
+
for col_idx in row_source_columns:
|
|
2598
|
+
rows.append({
|
|
2599
|
+
"sourceColumnOffset": col_idx,
|
|
2600
|
+
"showTotals": True,
|
|
2601
|
+
"sortOrder": "ASCENDING",
|
|
2602
|
+
})
|
|
2603
|
+
|
|
2604
|
+
# Build values
|
|
2605
|
+
values = []
|
|
2606
|
+
for i, col_idx in enumerate(value_source_columns):
|
|
2607
|
+
func = value_functions[i] if i < len(value_functions) else "SUM"
|
|
2608
|
+
values.append({
|
|
2609
|
+
"sourceColumnOffset": col_idx,
|
|
2610
|
+
"summarizeFunction": func.upper(),
|
|
2611
|
+
})
|
|
2612
|
+
|
|
2613
|
+
# Build columns (optional)
|
|
2614
|
+
columns = []
|
|
2615
|
+
if column_source_columns:
|
|
2616
|
+
for col_idx in column_source_columns:
|
|
2617
|
+
columns.append({
|
|
2618
|
+
"sourceColumnOffset": col_idx,
|
|
2619
|
+
"showTotals": True,
|
|
2620
|
+
"sortOrder": "ASCENDING",
|
|
2621
|
+
})
|
|
2622
|
+
|
|
2623
|
+
pivot_table: dict[str, Any] = {
|
|
2624
|
+
"source": {
|
|
2625
|
+
"sheetId": source_sheet_id,
|
|
2626
|
+
"startRowIndex": source_start_row,
|
|
2627
|
+
"endRowIndex": source_end_row,
|
|
2628
|
+
"startColumnIndex": source_start_col,
|
|
2629
|
+
"endColumnIndex": source_end_col,
|
|
2630
|
+
},
|
|
2631
|
+
"rows": rows,
|
|
2632
|
+
"values": values,
|
|
2633
|
+
}
|
|
2634
|
+
if columns:
|
|
2635
|
+
pivot_table["columns"] = columns
|
|
2636
|
+
|
|
2637
|
+
requests = [{
|
|
2638
|
+
"updateCells": {
|
|
2639
|
+
"rows": [{
|
|
2640
|
+
"values": [{
|
|
2641
|
+
"pivotTable": pivot_table
|
|
2642
|
+
}]
|
|
2643
|
+
}],
|
|
2644
|
+
"start": {
|
|
2645
|
+
"sheetId": target_sheet_id,
|
|
2646
|
+
"rowIndex": target_row,
|
|
2647
|
+
"columnIndex": target_col,
|
|
2648
|
+
},
|
|
2649
|
+
"fields": "pivotTable",
|
|
2650
|
+
}
|
|
2651
|
+
}]
|
|
2652
|
+
|
|
2653
|
+
result = self.execute(
|
|
2654
|
+
self.service.spreadsheets()
|
|
2655
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2656
|
+
)
|
|
2657
|
+
|
|
2658
|
+
output_success(
|
|
2659
|
+
operation="sheets.create_pivot_table",
|
|
2660
|
+
spreadsheet_id=spreadsheet_id,
|
|
2661
|
+
source_sheet_id=source_sheet_id,
|
|
2662
|
+
target_sheet_id=target_sheet_id,
|
|
2663
|
+
target_location=f"R{target_row}C{target_col}",
|
|
2664
|
+
row_groups=len(row_source_columns),
|
|
2665
|
+
values=len(value_source_columns),
|
|
2666
|
+
)
|
|
2667
|
+
return result
|
|
2668
|
+
except HttpError as e:
|
|
2669
|
+
output_error(
|
|
2670
|
+
error_code="API_ERROR",
|
|
2671
|
+
operation="sheets.create_pivot_table",
|
|
2672
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2673
|
+
)
|
|
2674
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2675
|
+
|
|
2676
|
+
def list_pivot_tables(
|
|
2677
|
+
self,
|
|
2678
|
+
spreadsheet_id: str,
|
|
2679
|
+
) -> dict[str, Any]:
|
|
2680
|
+
"""List all pivot tables in a spreadsheet.
|
|
2681
|
+
|
|
2682
|
+
Args:
|
|
2683
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2684
|
+
"""
|
|
2685
|
+
try:
|
|
2686
|
+
spreadsheet = self.execute(
|
|
2687
|
+
self.service.spreadsheets()
|
|
2688
|
+
.get(spreadsheetId=spreadsheet_id, includeGridData=False)
|
|
2689
|
+
)
|
|
2690
|
+
|
|
2691
|
+
pivot_tables = []
|
|
2692
|
+
for sheet in spreadsheet.get("sheets", []):
|
|
2693
|
+
sheet_title = sheet["properties"]["title"]
|
|
2694
|
+
sheet_id = sheet["properties"]["sheetId"]
|
|
2695
|
+
for pt in sheet.get("data", [{}])[0].get("rowData", []) if sheet.get("data") else []:
|
|
2696
|
+
for cell in pt.get("values", []):
|
|
2697
|
+
if "pivotTable" in cell:
|
|
2698
|
+
pivot_tables.append({
|
|
2699
|
+
"sheet_id": sheet_id,
|
|
2700
|
+
"sheet_title": sheet_title,
|
|
2701
|
+
"source": cell["pivotTable"].get("source"),
|
|
2702
|
+
})
|
|
2703
|
+
|
|
2704
|
+
# Alternative: check the sheets structure for pivot tables
|
|
2705
|
+
for sheet in spreadsheet.get("sheets", []):
|
|
2706
|
+
sheet_title = sheet["properties"]["title"]
|
|
2707
|
+
sheet_id = sheet["properties"]["sheetId"]
|
|
2708
|
+
for pivot in sheet.get("pivotTables", []):
|
|
2709
|
+
pivot_tables.append({
|
|
2710
|
+
"sheet_id": sheet_id,
|
|
2711
|
+
"sheet_title": sheet_title,
|
|
2712
|
+
"source": pivot.get("source"),
|
|
2713
|
+
"rows": len(pivot.get("rows", [])),
|
|
2714
|
+
"columns": len(pivot.get("columns", [])),
|
|
2715
|
+
"values": len(pivot.get("values", [])),
|
|
2716
|
+
})
|
|
2717
|
+
|
|
2718
|
+
output_success(
|
|
2719
|
+
operation="sheets.list_pivot_tables",
|
|
2720
|
+
spreadsheet_id=spreadsheet_id,
|
|
2721
|
+
count=len(pivot_tables),
|
|
2722
|
+
pivot_tables=pivot_tables,
|
|
2723
|
+
)
|
|
2724
|
+
return {"pivot_tables": pivot_tables}
|
|
2725
|
+
except HttpError as e:
|
|
2726
|
+
output_error(
|
|
2727
|
+
error_code="API_ERROR",
|
|
2728
|
+
operation="sheets.list_pivot_tables",
|
|
2729
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2730
|
+
)
|
|
2731
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2732
|
+
|
|
2733
|
+
# =========================================================================
|
|
2734
|
+
# PROTECTED RANGES
|
|
2735
|
+
# =========================================================================
|
|
2736
|
+
|
|
2737
|
+
def protect_range(
|
|
2738
|
+
self,
|
|
2739
|
+
spreadsheet_id: str,
|
|
2740
|
+
sheet_id: int,
|
|
2741
|
+
start_row: int,
|
|
2742
|
+
end_row: int,
|
|
2743
|
+
start_col: int,
|
|
2744
|
+
end_col: int,
|
|
2745
|
+
description: str | None = None,
|
|
2746
|
+
warning_only: bool = False,
|
|
2747
|
+
editors: list[str] | None = None,
|
|
2748
|
+
) -> dict[str, Any]:
|
|
2749
|
+
"""Protect a range from editing.
|
|
2750
|
+
|
|
2751
|
+
Args:
|
|
2752
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2753
|
+
sheet_id: The sheet ID.
|
|
2754
|
+
start_row: Start row index (0-indexed).
|
|
2755
|
+
end_row: End row index (exclusive).
|
|
2756
|
+
start_col: Start column index (0-indexed).
|
|
2757
|
+
end_col: End column index (exclusive).
|
|
2758
|
+
description: Description of the protected range.
|
|
2759
|
+
warning_only: If True, shows warning but allows editing.
|
|
2760
|
+
editors: List of email addresses that can edit.
|
|
2761
|
+
"""
|
|
2762
|
+
try:
|
|
2763
|
+
protected_range: dict[str, Any] = {
|
|
2764
|
+
"range": {
|
|
2765
|
+
"sheetId": sheet_id,
|
|
2766
|
+
"startRowIndex": start_row,
|
|
2767
|
+
"endRowIndex": end_row,
|
|
2768
|
+
"startColumnIndex": start_col,
|
|
2769
|
+
"endColumnIndex": end_col,
|
|
2770
|
+
},
|
|
2771
|
+
"warningOnly": warning_only,
|
|
2772
|
+
}
|
|
2773
|
+
|
|
2774
|
+
if description:
|
|
2775
|
+
protected_range["description"] = description
|
|
2776
|
+
|
|
2777
|
+
if editors:
|
|
2778
|
+
protected_range["editors"] = {"users": editors}
|
|
2779
|
+
|
|
2780
|
+
requests = [{"addProtectedRange": {"protectedRange": protected_range}}]
|
|
2781
|
+
|
|
2782
|
+
result = self.execute(
|
|
2783
|
+
self.service.spreadsheets()
|
|
2784
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2785
|
+
)
|
|
2786
|
+
|
|
2787
|
+
protected_range_id = (
|
|
2788
|
+
result.get("replies", [{}])[0]
|
|
2789
|
+
.get("addProtectedRange", {})
|
|
2790
|
+
.get("protectedRange", {})
|
|
2791
|
+
.get("protectedRangeId")
|
|
2792
|
+
)
|
|
2793
|
+
|
|
2794
|
+
output_success(
|
|
2795
|
+
operation="sheets.protect_range",
|
|
2796
|
+
spreadsheet_id=spreadsheet_id,
|
|
2797
|
+
sheet_id=sheet_id,
|
|
2798
|
+
protected_range_id=protected_range_id,
|
|
2799
|
+
range=f"R{start_row}C{start_col}:R{end_row}C{end_col}",
|
|
2800
|
+
warning_only=warning_only,
|
|
2801
|
+
)
|
|
2802
|
+
return result
|
|
2803
|
+
except HttpError as e:
|
|
2804
|
+
output_error(
|
|
2805
|
+
error_code="API_ERROR",
|
|
2806
|
+
operation="sheets.protect_range",
|
|
2807
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2808
|
+
)
|
|
2809
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2810
|
+
|
|
2811
|
+
def protect_sheet(
|
|
2812
|
+
self,
|
|
2813
|
+
spreadsheet_id: str,
|
|
2814
|
+
sheet_id: int,
|
|
2815
|
+
description: str | None = None,
|
|
2816
|
+
warning_only: bool = False,
|
|
2817
|
+
editors: list[str] | None = None,
|
|
2818
|
+
) -> dict[str, Any]:
|
|
2819
|
+
"""Protect an entire sheet from editing.
|
|
2820
|
+
|
|
2821
|
+
Args:
|
|
2822
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2823
|
+
sheet_id: The sheet ID.
|
|
2824
|
+
description: Description of the protection.
|
|
2825
|
+
warning_only: If True, shows warning but allows editing.
|
|
2826
|
+
editors: List of email addresses that can edit.
|
|
2827
|
+
"""
|
|
2828
|
+
try:
|
|
2829
|
+
protected_range: dict[str, Any] = {
|
|
2830
|
+
"range": {"sheetId": sheet_id},
|
|
2831
|
+
"warningOnly": warning_only,
|
|
2832
|
+
}
|
|
2833
|
+
|
|
2834
|
+
if description:
|
|
2835
|
+
protected_range["description"] = description
|
|
2836
|
+
|
|
2837
|
+
if editors:
|
|
2838
|
+
protected_range["editors"] = {"users": editors}
|
|
2839
|
+
|
|
2840
|
+
requests = [{"addProtectedRange": {"protectedRange": protected_range}}]
|
|
2841
|
+
|
|
2842
|
+
result = self.execute(
|
|
2843
|
+
self.service.spreadsheets()
|
|
2844
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2845
|
+
)
|
|
2846
|
+
|
|
2847
|
+
protected_range_id = (
|
|
2848
|
+
result.get("replies", [{}])[0]
|
|
2849
|
+
.get("addProtectedRange", {})
|
|
2850
|
+
.get("protectedRange", {})
|
|
2851
|
+
.get("protectedRangeId")
|
|
2852
|
+
)
|
|
2853
|
+
|
|
2854
|
+
output_success(
|
|
2855
|
+
operation="sheets.protect_sheet",
|
|
2856
|
+
spreadsheet_id=spreadsheet_id,
|
|
2857
|
+
sheet_id=sheet_id,
|
|
2858
|
+
protected_range_id=protected_range_id,
|
|
2859
|
+
warning_only=warning_only,
|
|
2860
|
+
)
|
|
2861
|
+
return result
|
|
2862
|
+
except HttpError as e:
|
|
2863
|
+
output_error(
|
|
2864
|
+
error_code="API_ERROR",
|
|
2865
|
+
operation="sheets.protect_sheet",
|
|
2866
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2867
|
+
)
|
|
2868
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2869
|
+
|
|
2870
|
+
def list_protected_ranges(
|
|
2871
|
+
self,
|
|
2872
|
+
spreadsheet_id: str,
|
|
2873
|
+
) -> dict[str, Any]:
|
|
2874
|
+
"""List all protected ranges in a spreadsheet.
|
|
2875
|
+
|
|
2876
|
+
Args:
|
|
2877
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2878
|
+
"""
|
|
2879
|
+
try:
|
|
2880
|
+
spreadsheet = self.execute(
|
|
2881
|
+
self.service.spreadsheets()
|
|
2882
|
+
.get(spreadsheetId=spreadsheet_id)
|
|
2883
|
+
)
|
|
2884
|
+
|
|
2885
|
+
protected_ranges = []
|
|
2886
|
+
for sheet in spreadsheet.get("sheets", []):
|
|
2887
|
+
sheet_title = sheet["properties"]["title"]
|
|
2888
|
+
sheet_id = sheet["properties"]["sheetId"]
|
|
2889
|
+
for pr in sheet.get("protectedRanges", []):
|
|
2890
|
+
protected_ranges.append({
|
|
2891
|
+
"protected_range_id": pr.get("protectedRangeId"),
|
|
2892
|
+
"description": pr.get("description"),
|
|
2893
|
+
"sheet_id": sheet_id,
|
|
2894
|
+
"sheet_title": sheet_title,
|
|
2895
|
+
"range": pr.get("range"),
|
|
2896
|
+
"warning_only": pr.get("warningOnly", False),
|
|
2897
|
+
"editors": pr.get("editors", {}).get("users", []),
|
|
2898
|
+
})
|
|
2899
|
+
|
|
2900
|
+
output_success(
|
|
2901
|
+
operation="sheets.list_protected_ranges",
|
|
2902
|
+
spreadsheet_id=spreadsheet_id,
|
|
2903
|
+
count=len(protected_ranges),
|
|
2904
|
+
protected_ranges=protected_ranges,
|
|
2905
|
+
)
|
|
2906
|
+
return {"protected_ranges": protected_ranges}
|
|
2907
|
+
except HttpError as e:
|
|
2908
|
+
output_error(
|
|
2909
|
+
error_code="API_ERROR",
|
|
2910
|
+
operation="sheets.list_protected_ranges",
|
|
2911
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2912
|
+
)
|
|
2913
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2914
|
+
|
|
2915
|
+
def unprotect_range(
|
|
2916
|
+
self,
|
|
2917
|
+
spreadsheet_id: str,
|
|
2918
|
+
protected_range_id: int,
|
|
2919
|
+
) -> dict[str, Any]:
|
|
2920
|
+
"""Remove protection from a range.
|
|
2921
|
+
|
|
2922
|
+
Args:
|
|
2923
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2924
|
+
protected_range_id: The protected range ID to remove.
|
|
2925
|
+
"""
|
|
2926
|
+
try:
|
|
2927
|
+
requests = [{"deleteProtectedRange": {"protectedRangeId": protected_range_id}}]
|
|
2928
|
+
|
|
2929
|
+
result = self.execute(
|
|
2930
|
+
self.service.spreadsheets()
|
|
2931
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2932
|
+
)
|
|
2933
|
+
|
|
2934
|
+
output_success(
|
|
2935
|
+
operation="sheets.unprotect_range",
|
|
2936
|
+
spreadsheet_id=spreadsheet_id,
|
|
2937
|
+
protected_range_id=protected_range_id,
|
|
2938
|
+
)
|
|
2939
|
+
return result
|
|
2940
|
+
except HttpError as e:
|
|
2941
|
+
output_error(
|
|
2942
|
+
error_code="API_ERROR",
|
|
2943
|
+
operation="sheets.unprotect_range",
|
|
2944
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
2945
|
+
)
|
|
2946
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
2947
|
+
|
|
2948
|
+
# =========================================================================
|
|
2949
|
+
# NAMED RANGES
|
|
2950
|
+
# =========================================================================
|
|
2951
|
+
|
|
2952
|
+
def create_named_range(
|
|
2953
|
+
self,
|
|
2954
|
+
spreadsheet_id: str,
|
|
2955
|
+
name: str,
|
|
2956
|
+
sheet_id: int,
|
|
2957
|
+
start_row: int,
|
|
2958
|
+
end_row: int,
|
|
2959
|
+
start_col: int,
|
|
2960
|
+
end_col: int,
|
|
2961
|
+
) -> dict[str, Any]:
|
|
2962
|
+
"""Create a named range.
|
|
2963
|
+
|
|
2964
|
+
Args:
|
|
2965
|
+
spreadsheet_id: The spreadsheet ID.
|
|
2966
|
+
name: Name for the range (must be valid identifier).
|
|
2967
|
+
sheet_id: The sheet ID.
|
|
2968
|
+
start_row: Start row index (0-indexed).
|
|
2969
|
+
end_row: End row index (exclusive).
|
|
2970
|
+
start_col: Start column index (0-indexed).
|
|
2971
|
+
end_col: End column index (exclusive).
|
|
2972
|
+
"""
|
|
2973
|
+
try:
|
|
2974
|
+
requests = [{
|
|
2975
|
+
"addNamedRange": {
|
|
2976
|
+
"namedRange": {
|
|
2977
|
+
"name": name,
|
|
2978
|
+
"range": {
|
|
2979
|
+
"sheetId": sheet_id,
|
|
2980
|
+
"startRowIndex": start_row,
|
|
2981
|
+
"endRowIndex": end_row,
|
|
2982
|
+
"startColumnIndex": start_col,
|
|
2983
|
+
"endColumnIndex": end_col,
|
|
2984
|
+
}
|
|
2985
|
+
}
|
|
2986
|
+
}
|
|
2987
|
+
}]
|
|
2988
|
+
|
|
2989
|
+
result = self.execute(
|
|
2990
|
+
self.service.spreadsheets()
|
|
2991
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
2992
|
+
)
|
|
2993
|
+
|
|
2994
|
+
named_range_id = (
|
|
2995
|
+
result.get("replies", [{}])[0]
|
|
2996
|
+
.get("addNamedRange", {})
|
|
2997
|
+
.get("namedRange", {})
|
|
2998
|
+
.get("namedRangeId")
|
|
2999
|
+
)
|
|
3000
|
+
|
|
3001
|
+
output_success(
|
|
3002
|
+
operation="sheets.create_named_range",
|
|
3003
|
+
spreadsheet_id=spreadsheet_id,
|
|
3004
|
+
name=name,
|
|
3005
|
+
named_range_id=named_range_id,
|
|
3006
|
+
range=f"R{start_row}C{start_col}:R{end_row}C{end_col}",
|
|
3007
|
+
)
|
|
3008
|
+
return result
|
|
3009
|
+
except HttpError as e:
|
|
3010
|
+
output_error(
|
|
3011
|
+
error_code="API_ERROR",
|
|
3012
|
+
operation="sheets.create_named_range",
|
|
3013
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
3014
|
+
)
|
|
3015
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
3016
|
+
|
|
3017
|
+
def list_named_ranges(
|
|
3018
|
+
self,
|
|
3019
|
+
spreadsheet_id: str,
|
|
3020
|
+
) -> dict[str, Any]:
|
|
3021
|
+
"""List all named ranges in a spreadsheet.
|
|
3022
|
+
|
|
3023
|
+
Args:
|
|
3024
|
+
spreadsheet_id: The spreadsheet ID.
|
|
3025
|
+
"""
|
|
3026
|
+
try:
|
|
3027
|
+
spreadsheet = self.execute(
|
|
3028
|
+
self.service.spreadsheets()
|
|
3029
|
+
.get(spreadsheetId=spreadsheet_id)
|
|
3030
|
+
)
|
|
3031
|
+
|
|
3032
|
+
named_ranges = []
|
|
3033
|
+
for nr in spreadsheet.get("namedRanges", []):
|
|
3034
|
+
range_info = nr.get("range", {})
|
|
3035
|
+
named_ranges.append({
|
|
3036
|
+
"named_range_id": nr.get("namedRangeId"),
|
|
3037
|
+
"name": nr.get("name"),
|
|
3038
|
+
"sheet_id": range_info.get("sheetId"),
|
|
3039
|
+
"start_row": range_info.get("startRowIndex"),
|
|
3040
|
+
"end_row": range_info.get("endRowIndex"),
|
|
3041
|
+
"start_col": range_info.get("startColumnIndex"),
|
|
3042
|
+
"end_col": range_info.get("endColumnIndex"),
|
|
3043
|
+
})
|
|
3044
|
+
|
|
3045
|
+
output_success(
|
|
3046
|
+
operation="sheets.list_named_ranges",
|
|
3047
|
+
spreadsheet_id=spreadsheet_id,
|
|
3048
|
+
count=len(named_ranges),
|
|
3049
|
+
named_ranges=named_ranges,
|
|
3050
|
+
)
|
|
3051
|
+
return {"named_ranges": named_ranges}
|
|
3052
|
+
except HttpError as e:
|
|
3053
|
+
output_error(
|
|
3054
|
+
error_code="API_ERROR",
|
|
3055
|
+
operation="sheets.list_named_ranges",
|
|
3056
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
3057
|
+
)
|
|
3058
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
3059
|
+
|
|
3060
|
+
def delete_named_range(
|
|
3061
|
+
self,
|
|
3062
|
+
spreadsheet_id: str,
|
|
3063
|
+
named_range_id: str,
|
|
3064
|
+
) -> dict[str, Any]:
|
|
3065
|
+
"""Delete a named range.
|
|
3066
|
+
|
|
3067
|
+
Args:
|
|
3068
|
+
spreadsheet_id: The spreadsheet ID.
|
|
3069
|
+
named_range_id: The named range ID to delete.
|
|
3070
|
+
"""
|
|
3071
|
+
try:
|
|
3072
|
+
requests = [{"deleteNamedRange": {"namedRangeId": named_range_id}}]
|
|
3073
|
+
|
|
3074
|
+
result = self.execute(
|
|
3075
|
+
self.service.spreadsheets()
|
|
3076
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
3077
|
+
)
|
|
3078
|
+
|
|
3079
|
+
output_success(
|
|
3080
|
+
operation="sheets.delete_named_range",
|
|
3081
|
+
spreadsheet_id=spreadsheet_id,
|
|
3082
|
+
named_range_id=named_range_id,
|
|
3083
|
+
)
|
|
3084
|
+
return result
|
|
3085
|
+
except HttpError as e:
|
|
3086
|
+
output_error(
|
|
3087
|
+
error_code="API_ERROR",
|
|
3088
|
+
operation="sheets.delete_named_range",
|
|
3089
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
3090
|
+
)
|
|
3091
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
3092
|
+
|
|
3093
|
+
# =========================================================================
|
|
3094
|
+
# CHART UPDATES
|
|
3095
|
+
# =========================================================================
|
|
3096
|
+
|
|
3097
|
+
def update_chart(
|
|
3098
|
+
self,
|
|
3099
|
+
spreadsheet_id: str,
|
|
3100
|
+
chart_id: int,
|
|
3101
|
+
title: str | None = None,
|
|
3102
|
+
chart_type: str | None = None,
|
|
3103
|
+
legend_position: str | None = None,
|
|
3104
|
+
) -> dict[str, Any]:
|
|
3105
|
+
"""Update a chart's specification.
|
|
3106
|
+
|
|
3107
|
+
Args:
|
|
3108
|
+
spreadsheet_id: The spreadsheet ID.
|
|
3109
|
+
chart_id: The chart ID to update.
|
|
3110
|
+
title: New chart title.
|
|
3111
|
+
chart_type: Chart type (LINE, BAR, COLUMN, AREA, SCATTER, PIE, etc.).
|
|
3112
|
+
legend_position: Legend position (TOP, BOTTOM, LEFT, RIGHT, NONE).
|
|
3113
|
+
"""
|
|
3114
|
+
try:
|
|
3115
|
+
# Build the chart spec update
|
|
3116
|
+
spec: dict[str, Any] = {}
|
|
3117
|
+
fields = []
|
|
3118
|
+
|
|
3119
|
+
if title is not None:
|
|
3120
|
+
spec["title"] = title
|
|
3121
|
+
fields.append("title")
|
|
3122
|
+
|
|
3123
|
+
if chart_type is not None:
|
|
3124
|
+
# Most chart types use basicChart
|
|
3125
|
+
spec["basicChart"] = {"chartType": chart_type}
|
|
3126
|
+
fields.append("basicChart.chartType")
|
|
3127
|
+
|
|
3128
|
+
if legend_position is not None:
|
|
3129
|
+
if "basicChart" not in spec:
|
|
3130
|
+
spec["basicChart"] = {}
|
|
3131
|
+
spec["basicChart"]["legendPosition"] = legend_position
|
|
3132
|
+
fields.append("basicChart.legendPosition")
|
|
3133
|
+
|
|
3134
|
+
if not fields:
|
|
3135
|
+
output_error(
|
|
3136
|
+
error_code="INVALID_ARGUMENT",
|
|
3137
|
+
operation="sheets.update_chart",
|
|
3138
|
+
message="At least one update field required (title, chart_type, legend_position)",
|
|
3139
|
+
)
|
|
3140
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
3141
|
+
|
|
3142
|
+
requests = [{
|
|
3143
|
+
"updateChartSpec": {
|
|
3144
|
+
"chartId": chart_id,
|
|
3145
|
+
"spec": spec,
|
|
3146
|
+
}
|
|
3147
|
+
}]
|
|
3148
|
+
|
|
3149
|
+
result = self.execute(
|
|
3150
|
+
self.service.spreadsheets()
|
|
3151
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
3152
|
+
)
|
|
3153
|
+
|
|
3154
|
+
output_success(
|
|
3155
|
+
operation="sheets.update_chart",
|
|
3156
|
+
spreadsheet_id=spreadsheet_id,
|
|
3157
|
+
chart_id=chart_id,
|
|
3158
|
+
updated_fields=fields,
|
|
3159
|
+
)
|
|
3160
|
+
return result
|
|
3161
|
+
except HttpError as e:
|
|
3162
|
+
output_error(
|
|
3163
|
+
error_code="API_ERROR",
|
|
3164
|
+
operation="sheets.update_chart",
|
|
3165
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
3166
|
+
)
|
|
3167
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
3168
|
+
|
|
3169
|
+
# =========================================================================
|
|
3170
|
+
# ROW/COLUMN MOVEMENT
|
|
3171
|
+
# =========================================================================
|
|
3172
|
+
|
|
3173
|
+
def move_rows(
|
|
3174
|
+
self,
|
|
3175
|
+
spreadsheet_id: str,
|
|
3176
|
+
sheet_id: int,
|
|
3177
|
+
source_start: int,
|
|
3178
|
+
source_end: int,
|
|
3179
|
+
destination_index: int,
|
|
3180
|
+
) -> dict[str, Any]:
|
|
3181
|
+
"""Move rows to a new position.
|
|
3182
|
+
|
|
3183
|
+
Args:
|
|
3184
|
+
spreadsheet_id: The spreadsheet ID.
|
|
3185
|
+
sheet_id: The sheet ID (numeric).
|
|
3186
|
+
source_start: Starting row index (0-based).
|
|
3187
|
+
source_end: Ending row index (exclusive).
|
|
3188
|
+
destination_index: Where to move the rows (0-based).
|
|
3189
|
+
"""
|
|
3190
|
+
try:
|
|
3191
|
+
requests = [{
|
|
3192
|
+
"moveDimension": {
|
|
3193
|
+
"source": {
|
|
3194
|
+
"sheetId": sheet_id,
|
|
3195
|
+
"dimension": "ROWS",
|
|
3196
|
+
"startIndex": source_start,
|
|
3197
|
+
"endIndex": source_end,
|
|
3198
|
+
},
|
|
3199
|
+
"destinationIndex": destination_index,
|
|
3200
|
+
}
|
|
3201
|
+
}]
|
|
3202
|
+
|
|
3203
|
+
result = self.execute(
|
|
3204
|
+
self.service.spreadsheets()
|
|
3205
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
3206
|
+
)
|
|
3207
|
+
|
|
3208
|
+
output_success(
|
|
3209
|
+
operation="sheets.move_rows",
|
|
3210
|
+
spreadsheet_id=spreadsheet_id,
|
|
3211
|
+
sheet_id=sheet_id,
|
|
3212
|
+
source_range=f"{source_start}:{source_end}",
|
|
3213
|
+
destination_index=destination_index,
|
|
3214
|
+
)
|
|
3215
|
+
return result
|
|
3216
|
+
except HttpError as e:
|
|
3217
|
+
output_error(
|
|
3218
|
+
error_code="API_ERROR",
|
|
3219
|
+
operation="sheets.move_rows",
|
|
3220
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
3221
|
+
)
|
|
3222
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
3223
|
+
|
|
3224
|
+
def move_columns(
|
|
3225
|
+
self,
|
|
3226
|
+
spreadsheet_id: str,
|
|
3227
|
+
sheet_id: int,
|
|
3228
|
+
source_start: int,
|
|
3229
|
+
source_end: int,
|
|
3230
|
+
destination_index: int,
|
|
3231
|
+
) -> dict[str, Any]:
|
|
3232
|
+
"""Move columns to a new position.
|
|
3233
|
+
|
|
3234
|
+
Args:
|
|
3235
|
+
spreadsheet_id: The spreadsheet ID.
|
|
3236
|
+
sheet_id: The sheet ID (numeric).
|
|
3237
|
+
source_start: Starting column index (0-based).
|
|
3238
|
+
source_end: Ending column index (exclusive).
|
|
3239
|
+
destination_index: Where to move the columns (0-based).
|
|
3240
|
+
"""
|
|
3241
|
+
try:
|
|
3242
|
+
requests = [{
|
|
3243
|
+
"moveDimension": {
|
|
3244
|
+
"source": {
|
|
3245
|
+
"sheetId": sheet_id,
|
|
3246
|
+
"dimension": "COLUMNS",
|
|
3247
|
+
"startIndex": source_start,
|
|
3248
|
+
"endIndex": source_end,
|
|
3249
|
+
},
|
|
3250
|
+
"destinationIndex": destination_index,
|
|
3251
|
+
}
|
|
3252
|
+
}]
|
|
3253
|
+
|
|
3254
|
+
result = self.execute(
|
|
3255
|
+
self.service.spreadsheets()
|
|
3256
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
3257
|
+
)
|
|
3258
|
+
|
|
3259
|
+
output_success(
|
|
3260
|
+
operation="sheets.move_columns",
|
|
3261
|
+
spreadsheet_id=spreadsheet_id,
|
|
3262
|
+
sheet_id=sheet_id,
|
|
3263
|
+
source_range=f"{source_start}:{source_end}",
|
|
3264
|
+
destination_index=destination_index,
|
|
3265
|
+
)
|
|
3266
|
+
return result
|
|
3267
|
+
except HttpError as e:
|
|
3268
|
+
output_error(
|
|
3269
|
+
error_code="API_ERROR",
|
|
3270
|
+
operation="sheets.move_columns",
|
|
3271
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
3272
|
+
)
|
|
3273
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
3274
|
+
|
|
3275
|
+
# =========================================================================
|
|
3276
|
+
# COPY/PASTE & AUTO-FILL
|
|
3277
|
+
# =========================================================================
|
|
3278
|
+
|
|
3279
|
+
def copy_paste(
|
|
3280
|
+
self,
|
|
3281
|
+
spreadsheet_id: str,
|
|
3282
|
+
source_sheet_id: int,
|
|
3283
|
+
source_start_row: int,
|
|
3284
|
+
source_end_row: int,
|
|
3285
|
+
source_start_col: int,
|
|
3286
|
+
source_end_col: int,
|
|
3287
|
+
dest_sheet_id: int,
|
|
3288
|
+
dest_start_row: int,
|
|
3289
|
+
dest_start_col: int,
|
|
3290
|
+
paste_type: str = "PASTE_NORMAL",
|
|
3291
|
+
) -> dict[str, Any]:
|
|
3292
|
+
"""Copy a range and paste it to another location.
|
|
3293
|
+
|
|
3294
|
+
Args:
|
|
3295
|
+
spreadsheet_id: The spreadsheet ID.
|
|
3296
|
+
source_sheet_id: Source sheet ID (numeric).
|
|
3297
|
+
source_start_row: Source start row (0-based).
|
|
3298
|
+
source_end_row: Source end row (exclusive).
|
|
3299
|
+
source_start_col: Source start column (0-based).
|
|
3300
|
+
source_end_col: Source end column (exclusive).
|
|
3301
|
+
dest_sheet_id: Destination sheet ID (numeric).
|
|
3302
|
+
dest_start_row: Destination start row (0-based).
|
|
3303
|
+
dest_start_col: Destination start column (0-based).
|
|
3304
|
+
paste_type: What to paste (PASTE_NORMAL, PASTE_VALUES, PASTE_FORMAT,
|
|
3305
|
+
PASTE_NO_BORDERS, PASTE_FORMULA, PASTE_DATA_VALIDATION,
|
|
3306
|
+
PASTE_CONDITIONAL_FORMATTING).
|
|
3307
|
+
"""
|
|
3308
|
+
try:
|
|
3309
|
+
requests = [{
|
|
3310
|
+
"copyPaste": {
|
|
3311
|
+
"source": {
|
|
3312
|
+
"sheetId": source_sheet_id,
|
|
3313
|
+
"startRowIndex": source_start_row,
|
|
3314
|
+
"endRowIndex": source_end_row,
|
|
3315
|
+
"startColumnIndex": source_start_col,
|
|
3316
|
+
"endColumnIndex": source_end_col,
|
|
3317
|
+
},
|
|
3318
|
+
"destination": {
|
|
3319
|
+
"sheetId": dest_sheet_id,
|
|
3320
|
+
"startRowIndex": dest_start_row,
|
|
3321
|
+
"endRowIndex": dest_start_row + (source_end_row - source_start_row),
|
|
3322
|
+
"startColumnIndex": dest_start_col,
|
|
3323
|
+
"endColumnIndex": dest_start_col + (source_end_col - source_start_col),
|
|
3324
|
+
},
|
|
3325
|
+
"pasteType": paste_type,
|
|
3326
|
+
}
|
|
3327
|
+
}]
|
|
3328
|
+
|
|
3329
|
+
result = self.execute(
|
|
3330
|
+
self.service.spreadsheets()
|
|
3331
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
3332
|
+
)
|
|
3333
|
+
|
|
3334
|
+
output_success(
|
|
3335
|
+
operation="sheets.copy_paste",
|
|
3336
|
+
spreadsheet_id=spreadsheet_id,
|
|
3337
|
+
paste_type=paste_type,
|
|
3338
|
+
)
|
|
3339
|
+
return result
|
|
3340
|
+
except HttpError as e:
|
|
3341
|
+
output_error(
|
|
3342
|
+
error_code="API_ERROR",
|
|
3343
|
+
operation="sheets.copy_paste",
|
|
3344
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
3345
|
+
)
|
|
3346
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
3347
|
+
|
|
3348
|
+
def auto_fill(
|
|
3349
|
+
self,
|
|
3350
|
+
spreadsheet_id: str,
|
|
3351
|
+
sheet_id: int,
|
|
3352
|
+
source_start_row: int,
|
|
3353
|
+
source_end_row: int,
|
|
3354
|
+
source_start_col: int,
|
|
3355
|
+
source_end_col: int,
|
|
3356
|
+
fill_start_row: int,
|
|
3357
|
+
fill_end_row: int,
|
|
3358
|
+
fill_start_col: int,
|
|
3359
|
+
fill_end_col: int,
|
|
3360
|
+
use_alternate_series: bool = False,
|
|
3361
|
+
) -> dict[str, Any]:
|
|
3362
|
+
"""Auto-fill a range based on source data.
|
|
3363
|
+
|
|
3364
|
+
Args:
|
|
3365
|
+
spreadsheet_id: The spreadsheet ID.
|
|
3366
|
+
sheet_id: The sheet ID (numeric).
|
|
3367
|
+
source_start_row: Source range start row (0-based).
|
|
3368
|
+
source_end_row: Source range end row (exclusive).
|
|
3369
|
+
source_start_col: Source range start column (0-based).
|
|
3370
|
+
source_end_col: Source range end column (exclusive).
|
|
3371
|
+
fill_start_row: Fill range start row (0-based).
|
|
3372
|
+
fill_end_row: Fill range end row (exclusive).
|
|
3373
|
+
fill_start_col: Fill range start column (0-based).
|
|
3374
|
+
fill_end_col: Fill range end column (exclusive).
|
|
3375
|
+
use_alternate_series: Use alternate series for auto-fill.
|
|
3376
|
+
"""
|
|
3377
|
+
try:
|
|
3378
|
+
requests = [{
|
|
3379
|
+
"autoFill": {
|
|
3380
|
+
"useAlternateSeries": use_alternate_series,
|
|
3381
|
+
"sourceAndDestination": {
|
|
3382
|
+
"source": {
|
|
3383
|
+
"sheetId": sheet_id,
|
|
3384
|
+
"startRowIndex": source_start_row,
|
|
3385
|
+
"endRowIndex": source_end_row,
|
|
3386
|
+
"startColumnIndex": source_start_col,
|
|
3387
|
+
"endColumnIndex": source_end_col,
|
|
3388
|
+
},
|
|
3389
|
+
"dimension": "ROWS" if fill_end_row > source_end_row else "COLUMNS",
|
|
3390
|
+
"fillLength": max(
|
|
3391
|
+
fill_end_row - source_end_row,
|
|
3392
|
+
fill_end_col - source_end_col
|
|
3393
|
+
),
|
|
3394
|
+
},
|
|
3395
|
+
}
|
|
3396
|
+
}]
|
|
3397
|
+
|
|
3398
|
+
result = self.execute(
|
|
3399
|
+
self.service.spreadsheets()
|
|
3400
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
3401
|
+
)
|
|
3402
|
+
|
|
3403
|
+
output_success(
|
|
3404
|
+
operation="sheets.auto_fill",
|
|
3405
|
+
spreadsheet_id=spreadsheet_id,
|
|
3406
|
+
sheet_id=sheet_id,
|
|
3407
|
+
)
|
|
3408
|
+
return result
|
|
3409
|
+
except HttpError as e:
|
|
3410
|
+
output_error(
|
|
3411
|
+
error_code="API_ERROR",
|
|
3412
|
+
operation="sheets.auto_fill",
|
|
3413
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
3414
|
+
)
|
|
3415
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
3416
|
+
|
|
3417
|
+
# =========================================================================
|
|
3418
|
+
# DATA CLEANUP
|
|
3419
|
+
# =========================================================================
|
|
3420
|
+
|
|
3421
|
+
def trim_whitespace(
|
|
3422
|
+
self,
|
|
3423
|
+
spreadsheet_id: str,
|
|
3424
|
+
sheet_id: int,
|
|
3425
|
+
start_row: int = 0,
|
|
3426
|
+
end_row: int | None = None,
|
|
3427
|
+
start_col: int = 0,
|
|
3428
|
+
end_col: int | None = None,
|
|
3429
|
+
) -> dict[str, Any]:
|
|
3430
|
+
"""Trim leading and trailing whitespace from cells.
|
|
3431
|
+
|
|
3432
|
+
Args:
|
|
3433
|
+
spreadsheet_id: The spreadsheet ID.
|
|
3434
|
+
sheet_id: The sheet ID (numeric).
|
|
3435
|
+
start_row: Start row index (0-based).
|
|
3436
|
+
end_row: End row index (exclusive), None for entire sheet.
|
|
3437
|
+
start_col: Start column index (0-based).
|
|
3438
|
+
end_col: End column index (exclusive), None for entire sheet.
|
|
3439
|
+
"""
|
|
3440
|
+
try:
|
|
3441
|
+
range_spec: dict[str, Any] = {"sheetId": sheet_id}
|
|
3442
|
+
if start_row > 0:
|
|
3443
|
+
range_spec["startRowIndex"] = start_row
|
|
3444
|
+
if end_row is not None:
|
|
3445
|
+
range_spec["endRowIndex"] = end_row
|
|
3446
|
+
if start_col > 0:
|
|
3447
|
+
range_spec["startColumnIndex"] = start_col
|
|
3448
|
+
if end_col is not None:
|
|
3449
|
+
range_spec["endColumnIndex"] = end_col
|
|
3450
|
+
|
|
3451
|
+
requests = [{
|
|
3452
|
+
"trimWhitespace": {
|
|
3453
|
+
"range": range_spec,
|
|
3454
|
+
}
|
|
3455
|
+
}]
|
|
3456
|
+
|
|
3457
|
+
result = self.execute(
|
|
3458
|
+
self.service.spreadsheets()
|
|
3459
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
3460
|
+
)
|
|
3461
|
+
|
|
3462
|
+
output_success(
|
|
3463
|
+
operation="sheets.trim_whitespace",
|
|
3464
|
+
spreadsheet_id=spreadsheet_id,
|
|
3465
|
+
sheet_id=sheet_id,
|
|
3466
|
+
)
|
|
3467
|
+
return result
|
|
3468
|
+
except HttpError as e:
|
|
3469
|
+
output_error(
|
|
3470
|
+
error_code="API_ERROR",
|
|
3471
|
+
operation="sheets.trim_whitespace",
|
|
3472
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
3473
|
+
)
|
|
3474
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
3475
|
+
|
|
3476
|
+
def text_to_columns(
|
|
3477
|
+
self,
|
|
3478
|
+
spreadsheet_id: str,
|
|
3479
|
+
sheet_id: int,
|
|
3480
|
+
start_row: int,
|
|
3481
|
+
end_row: int,
|
|
3482
|
+
source_column: int,
|
|
3483
|
+
delimiter_type: str = "COMMA",
|
|
3484
|
+
custom_delimiter: str | None = None,
|
|
3485
|
+
) -> dict[str, Any]:
|
|
3486
|
+
"""Split text in a column into multiple columns.
|
|
3487
|
+
|
|
3488
|
+
Args:
|
|
3489
|
+
spreadsheet_id: The spreadsheet ID.
|
|
3490
|
+
sheet_id: The sheet ID (numeric).
|
|
3491
|
+
start_row: Start row index (0-based).
|
|
3492
|
+
end_row: End row index (exclusive).
|
|
3493
|
+
source_column: Column index to split (0-based).
|
|
3494
|
+
delimiter_type: Type of delimiter (COMMA, SEMICOLON, PERIOD, SPACE, CUSTOM, AUTODETECT).
|
|
3495
|
+
custom_delimiter: Custom delimiter string (required if delimiter_type is CUSTOM).
|
|
3496
|
+
"""
|
|
3497
|
+
try:
|
|
3498
|
+
request: dict[str, Any] = {
|
|
3499
|
+
"textToColumns": {
|
|
3500
|
+
"source": {
|
|
3501
|
+
"sheetId": sheet_id,
|
|
3502
|
+
"startRowIndex": start_row,
|
|
3503
|
+
"endRowIndex": end_row,
|
|
3504
|
+
"startColumnIndex": source_column,
|
|
3505
|
+
"endColumnIndex": source_column + 1,
|
|
3506
|
+
},
|
|
3507
|
+
"delimiterType": delimiter_type,
|
|
3508
|
+
}
|
|
3509
|
+
}
|
|
3510
|
+
|
|
3511
|
+
if delimiter_type == "CUSTOM" and custom_delimiter:
|
|
3512
|
+
request["textToColumns"]["delimiter"] = custom_delimiter
|
|
3513
|
+
|
|
3514
|
+
result = self.execute(
|
|
3515
|
+
self.service.spreadsheets()
|
|
3516
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": [request]})
|
|
3517
|
+
)
|
|
3518
|
+
|
|
3519
|
+
output_success(
|
|
3520
|
+
operation="sheets.text_to_columns",
|
|
3521
|
+
spreadsheet_id=spreadsheet_id,
|
|
3522
|
+
sheet_id=sheet_id,
|
|
3523
|
+
delimiter_type=delimiter_type,
|
|
3524
|
+
)
|
|
3525
|
+
return result
|
|
3526
|
+
except HttpError as e:
|
|
3527
|
+
output_error(
|
|
3528
|
+
error_code="API_ERROR",
|
|
3529
|
+
operation="sheets.text_to_columns",
|
|
3530
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
3531
|
+
)
|
|
3532
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
3533
|
+
|
|
3534
|
+
# =========================================================================
|
|
3535
|
+
# BANDING & FILTER VIEW UPDATES
|
|
3536
|
+
# =========================================================================
|
|
3537
|
+
|
|
3538
|
+
def update_banding(
|
|
3539
|
+
self,
|
|
3540
|
+
spreadsheet_id: str,
|
|
3541
|
+
banded_range_id: int,
|
|
3542
|
+
header_color: str | None = None,
|
|
3543
|
+
first_band_color: str | None = None,
|
|
3544
|
+
second_band_color: str | None = None,
|
|
3545
|
+
footer_color: str | None = None,
|
|
3546
|
+
) -> dict[str, Any]:
|
|
3547
|
+
"""Update banding colors on a range.
|
|
3548
|
+
|
|
3549
|
+
Args:
|
|
3550
|
+
spreadsheet_id: The spreadsheet ID.
|
|
3551
|
+
banded_range_id: The banded range ID to update.
|
|
3552
|
+
header_color: Header row color (hex, e.g., '#4285F4').
|
|
3553
|
+
first_band_color: First alternating color (hex).
|
|
3554
|
+
second_band_color: Second alternating color (hex).
|
|
3555
|
+
footer_color: Footer row color (hex).
|
|
3556
|
+
"""
|
|
3557
|
+
try:
|
|
3558
|
+
properties: dict[str, Any] = {"bandedRangeId": banded_range_id}
|
|
3559
|
+
fields = ["bandedRangeId"]
|
|
3560
|
+
|
|
3561
|
+
if header_color:
|
|
3562
|
+
properties["rowProperties"] = properties.get("rowProperties", {})
|
|
3563
|
+
properties["rowProperties"]["headerColor"] = parse_hex_color(header_color)
|
|
3564
|
+
fields.append("rowProperties.headerColor")
|
|
3565
|
+
|
|
3566
|
+
if first_band_color:
|
|
3567
|
+
properties["rowProperties"] = properties.get("rowProperties", {})
|
|
3568
|
+
properties["rowProperties"]["firstBandColor"] = parse_hex_color(first_band_color)
|
|
3569
|
+
fields.append("rowProperties.firstBandColor")
|
|
3570
|
+
|
|
3571
|
+
if second_band_color:
|
|
3572
|
+
properties["rowProperties"] = properties.get("rowProperties", {})
|
|
3573
|
+
properties["rowProperties"]["secondBandColor"] = parse_hex_color(second_band_color)
|
|
3574
|
+
fields.append("rowProperties.secondBandColor")
|
|
3575
|
+
|
|
3576
|
+
if footer_color:
|
|
3577
|
+
properties["rowProperties"] = properties.get("rowProperties", {})
|
|
3578
|
+
properties["rowProperties"]["footerColor"] = parse_hex_color(footer_color)
|
|
3579
|
+
fields.append("rowProperties.footerColor")
|
|
3580
|
+
|
|
3581
|
+
if len(fields) == 1:
|
|
3582
|
+
output_error(
|
|
3583
|
+
error_code="INVALID_ARGUMENT",
|
|
3584
|
+
operation="sheets.update_banding",
|
|
3585
|
+
message="At least one color parameter required",
|
|
3586
|
+
)
|
|
3587
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
3588
|
+
|
|
3589
|
+
requests = [{
|
|
3590
|
+
"updateBanding": {
|
|
3591
|
+
"bandedRange": properties,
|
|
3592
|
+
"fields": ",".join(fields),
|
|
3593
|
+
}
|
|
3594
|
+
}]
|
|
3595
|
+
|
|
3596
|
+
result = self.execute(
|
|
3597
|
+
self.service.spreadsheets()
|
|
3598
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
3599
|
+
)
|
|
3600
|
+
|
|
3601
|
+
output_success(
|
|
3602
|
+
operation="sheets.update_banding",
|
|
3603
|
+
spreadsheet_id=spreadsheet_id,
|
|
3604
|
+
banded_range_id=banded_range_id,
|
|
3605
|
+
)
|
|
3606
|
+
return result
|
|
3607
|
+
except HttpError as e:
|
|
3608
|
+
output_error(
|
|
3609
|
+
error_code="API_ERROR",
|
|
3610
|
+
operation="sheets.update_banding",
|
|
3611
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
3612
|
+
)
|
|
3613
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
3614
|
+
|
|
3615
|
+
def update_filter_view(
|
|
3616
|
+
self,
|
|
3617
|
+
spreadsheet_id: str,
|
|
3618
|
+
filter_view_id: int,
|
|
3619
|
+
title: str | None = None,
|
|
3620
|
+
start_row: int | None = None,
|
|
3621
|
+
end_row: int | None = None,
|
|
3622
|
+
start_col: int | None = None,
|
|
3623
|
+
end_col: int | None = None,
|
|
3624
|
+
) -> dict[str, Any]:
|
|
3625
|
+
"""Update a filter view's properties.
|
|
3626
|
+
|
|
3627
|
+
Args:
|
|
3628
|
+
spreadsheet_id: The spreadsheet ID.
|
|
3629
|
+
filter_view_id: The filter view ID to update.
|
|
3630
|
+
title: New title for the filter view.
|
|
3631
|
+
start_row: New start row index (0-based).
|
|
3632
|
+
end_row: New end row index (exclusive).
|
|
3633
|
+
start_col: New start column index (0-based).
|
|
3634
|
+
end_col: New end column index (exclusive).
|
|
3635
|
+
"""
|
|
3636
|
+
try:
|
|
3637
|
+
filter_view: dict[str, Any] = {"filterViewId": filter_view_id}
|
|
3638
|
+
fields = []
|
|
3639
|
+
|
|
3640
|
+
if title is not None:
|
|
3641
|
+
filter_view["title"] = title
|
|
3642
|
+
fields.append("title")
|
|
3643
|
+
|
|
3644
|
+
if any(x is not None for x in [start_row, end_row, start_col, end_col]):
|
|
3645
|
+
filter_view["range"] = {}
|
|
3646
|
+
if start_row is not None:
|
|
3647
|
+
filter_view["range"]["startRowIndex"] = start_row
|
|
3648
|
+
if end_row is not None:
|
|
3649
|
+
filter_view["range"]["endRowIndex"] = end_row
|
|
3650
|
+
if start_col is not None:
|
|
3651
|
+
filter_view["range"]["startColumnIndex"] = start_col
|
|
3652
|
+
if end_col is not None:
|
|
3653
|
+
filter_view["range"]["endColumnIndex"] = end_col
|
|
3654
|
+
fields.append("range")
|
|
3655
|
+
|
|
3656
|
+
if not fields:
|
|
3657
|
+
output_error(
|
|
3658
|
+
error_code="INVALID_ARGUMENT",
|
|
3659
|
+
operation="sheets.update_filter_view",
|
|
3660
|
+
message="At least one update field required (title or range bounds)",
|
|
3661
|
+
)
|
|
3662
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
3663
|
+
|
|
3664
|
+
requests = [{
|
|
3665
|
+
"updateFilterView": {
|
|
3666
|
+
"filter": filter_view,
|
|
3667
|
+
"fields": ",".join(fields),
|
|
3668
|
+
}
|
|
3669
|
+
}]
|
|
3670
|
+
|
|
3671
|
+
result = self.execute(
|
|
3672
|
+
self.service.spreadsheets()
|
|
3673
|
+
.batchUpdate(spreadsheetId=spreadsheet_id, body={"requests": requests})
|
|
3674
|
+
)
|
|
3675
|
+
|
|
3676
|
+
output_success(
|
|
3677
|
+
operation="sheets.update_filter_view",
|
|
3678
|
+
spreadsheet_id=spreadsheet_id,
|
|
3679
|
+
filter_view_id=filter_view_id,
|
|
3680
|
+
updated_fields=fields,
|
|
3681
|
+
)
|
|
3682
|
+
return result
|
|
3683
|
+
except HttpError as e:
|
|
3684
|
+
output_error(
|
|
3685
|
+
error_code="API_ERROR",
|
|
3686
|
+
operation="sheets.update_filter_view",
|
|
3687
|
+
message=f"Google Sheets API error: {e.reason}",
|
|
3688
|
+
)
|
|
3689
|
+
raise SystemExit(ExitCode.API_ERROR)
|