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/commands/sheets.py ADDED
@@ -0,0 +1,1550 @@
1
+ """Sheets CLI commands."""
2
+
3
+ import json
4
+ import sys
5
+ import typer
6
+ from typing import Annotated, Optional
7
+
8
+ from gws.commands._account import account_callback
9
+ from gws.services.sheets import SheetsService
10
+ from gws.output import output_error
11
+ from gws.exceptions import ExitCode
12
+
13
+ app = typer.Typer(
14
+ name="sheets",
15
+ help="Google Sheets spreadsheet operations.",
16
+ no_args_is_help=True,
17
+ callback=account_callback,
18
+ )
19
+
20
+
21
+ @app.command("metadata")
22
+ def get_metadata(
23
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
24
+ ) -> None:
25
+ """Get spreadsheet metadata and sheet list."""
26
+ service = SheetsService()
27
+ service.metadata(spreadsheet_id=spreadsheet_id)
28
+
29
+
30
+ @app.command("read")
31
+ def read_values(
32
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
33
+ range_notation: Annotated[
34
+ Optional[str],
35
+ typer.Argument(help="A1 notation range (e.g., 'Sheet1!A1:C10')."),
36
+ ] = None,
37
+ formulas: Annotated[
38
+ bool,
39
+ typer.Option("--formulas", help="Return formulas instead of computed values."),
40
+ ] = False,
41
+ ) -> None:
42
+ """Read values from a spreadsheet range."""
43
+ service = SheetsService()
44
+ value_render_option = "FORMULA" if formulas else "FORMATTED_VALUE"
45
+ service.read(
46
+ spreadsheet_id=spreadsheet_id,
47
+ range_notation=range_notation,
48
+ value_render_option=value_render_option,
49
+ )
50
+
51
+
52
+ @app.command("create")
53
+ def create_spreadsheet(
54
+ title: Annotated[str, typer.Argument(help="Spreadsheet title.")],
55
+ sheets: Annotated[
56
+ Optional[str],
57
+ typer.Option("--sheets", "-s", help="Comma-separated sheet names."),
58
+ ] = None,
59
+ folder_id: Annotated[
60
+ Optional[str],
61
+ typer.Option("--folder", "-f", help="Folder ID to create spreadsheet in."),
62
+ ] = None,
63
+ ) -> None:
64
+ """Create a new spreadsheet."""
65
+ sheet_titles = sheets.split(",") if sheets else None
66
+
67
+ service = SheetsService()
68
+ service.create(title=title, sheet_titles=sheet_titles, folder_id=folder_id)
69
+
70
+
71
+ @app.command("write")
72
+ def write_values(
73
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
74
+ range_notation: Annotated[str, typer.Argument(help="A1 notation range.")],
75
+ stdin: Annotated[
76
+ bool,
77
+ typer.Option("--stdin", help="Read values as JSON from stdin."),
78
+ ] = False,
79
+ values: Annotated[
80
+ Optional[str],
81
+ typer.Option("--values", "-v", help="JSON array of rows."),
82
+ ] = None,
83
+ ) -> None:
84
+ """Write values to a spreadsheet range.
85
+
86
+ Values should be a JSON array of arrays, e.g., '[["A1", "B1"], ["A2", "B2"]]'
87
+ """
88
+ if stdin:
89
+ try:
90
+ data = json.loads(sys.stdin.read())
91
+ except json.JSONDecodeError as e:
92
+ output_error(
93
+ error_code="INVALID_ARGS",
94
+ operation="sheets.write",
95
+ message=f"Invalid JSON input: {e}",
96
+ )
97
+ raise typer.Exit(ExitCode.INVALID_ARGS)
98
+ if isinstance(data, list):
99
+ values_list = data
100
+ else:
101
+ values_list = data.get("values", [])
102
+ elif values:
103
+ try:
104
+ values_list = json.loads(values)
105
+ except json.JSONDecodeError as e:
106
+ output_error(
107
+ error_code="INVALID_ARGS",
108
+ operation="sheets.write",
109
+ message=f"Invalid JSON input: {e}",
110
+ )
111
+ raise typer.Exit(ExitCode.INVALID_ARGS)
112
+ else:
113
+ output_error(
114
+ error_code="INVALID_ARGS",
115
+ operation="sheets.write",
116
+ message="Either --stdin or --values required",
117
+ )
118
+ raise typer.Exit(ExitCode.INVALID_ARGS)
119
+
120
+ service = SheetsService()
121
+ service.write(
122
+ spreadsheet_id=spreadsheet_id,
123
+ range_notation=range_notation,
124
+ values=values_list,
125
+ )
126
+
127
+
128
+ @app.command("append")
129
+ def append_values(
130
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
131
+ range_notation: Annotated[str, typer.Argument(help="A1 notation range to append to.")],
132
+ stdin: Annotated[
133
+ bool,
134
+ typer.Option("--stdin", help="Read values as JSON from stdin."),
135
+ ] = False,
136
+ values: Annotated[
137
+ Optional[str],
138
+ typer.Option("--values", "-v", help="JSON array of rows."),
139
+ ] = None,
140
+ ) -> None:
141
+ """Append values to a spreadsheet."""
142
+ if stdin:
143
+ try:
144
+ data = json.loads(sys.stdin.read())
145
+ except json.JSONDecodeError as e:
146
+ output_error(
147
+ error_code="INVALID_ARGS",
148
+ operation="sheets.append",
149
+ message=f"Invalid JSON input: {e}",
150
+ )
151
+ raise typer.Exit(ExitCode.INVALID_ARGS)
152
+ if isinstance(data, list):
153
+ values_list = data
154
+ else:
155
+ values_list = data.get("values", [])
156
+ elif values:
157
+ try:
158
+ values_list = json.loads(values)
159
+ except json.JSONDecodeError as e:
160
+ output_error(
161
+ error_code="INVALID_ARGS",
162
+ operation="sheets.append",
163
+ message=f"Invalid JSON input: {e}",
164
+ )
165
+ raise typer.Exit(ExitCode.INVALID_ARGS)
166
+ else:
167
+ output_error(
168
+ error_code="INVALID_ARGS",
169
+ operation="sheets.append",
170
+ message="Either --stdin or --values required",
171
+ )
172
+ raise typer.Exit(ExitCode.INVALID_ARGS)
173
+
174
+ service = SheetsService()
175
+ service.append(
176
+ spreadsheet_id=spreadsheet_id,
177
+ range_notation=range_notation,
178
+ values=values_list,
179
+ )
180
+
181
+
182
+ @app.command("clear")
183
+ def clear_values(
184
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
185
+ range_notation: Annotated[str, typer.Argument(help="A1 notation range to clear.")],
186
+ ) -> None:
187
+ """Clear values from a range."""
188
+ service = SheetsService()
189
+ service.clear(spreadsheet_id=spreadsheet_id, range_notation=range_notation)
190
+
191
+
192
+ @app.command("add-sheet")
193
+ def add_sheet(
194
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
195
+ title: Annotated[str, typer.Argument(help="New sheet title.")],
196
+ ) -> None:
197
+ """Add a new sheet to the spreadsheet."""
198
+ service = SheetsService()
199
+ service.add_sheet(spreadsheet_id=spreadsheet_id, title=title)
200
+
201
+
202
+ @app.command("delete-sheet")
203
+ def delete_sheet(
204
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
205
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric) to delete.")],
206
+ ) -> None:
207
+ """Delete a sheet from the spreadsheet."""
208
+ service = SheetsService()
209
+ service.delete_sheet(spreadsheet_id=spreadsheet_id, sheet_id=sheet_id)
210
+
211
+
212
+ @app.command("rename-sheet")
213
+ def rename_sheet(
214
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
215
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
216
+ new_title: Annotated[str, typer.Argument(help="New title for the sheet.")],
217
+ ) -> None:
218
+ """Rename a sheet."""
219
+ service = SheetsService()
220
+ service.rename_sheet(
221
+ spreadsheet_id=spreadsheet_id,
222
+ sheet_id=sheet_id,
223
+ new_title=new_title,
224
+ )
225
+
226
+
227
+ @app.command("format")
228
+ def format_cells(
229
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
230
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
231
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
232
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive, 0-indexed).")],
233
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
234
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive, 0-indexed).")],
235
+ bold: Annotated[
236
+ Optional[bool],
237
+ typer.Option("--bold/--no-bold", "-b", help="Bold formatting."),
238
+ ] = None,
239
+ italic: Annotated[
240
+ Optional[bool],
241
+ typer.Option("--italic/--no-italic", "-i", help="Italic formatting."),
242
+ ] = None,
243
+ font_size: Annotated[
244
+ Optional[int],
245
+ typer.Option("--font-size", "-s", help="Font size in points."),
246
+ ] = None,
247
+ background_color: Annotated[
248
+ Optional[str],
249
+ typer.Option("--bg-color", help="Background color (hex, e.g., #FFFF00)."),
250
+ ] = None,
251
+ foreground_color: Annotated[
252
+ Optional[str],
253
+ typer.Option("--fg-color", help="Text color (hex, e.g., #FF0000)."),
254
+ ] = None,
255
+ ) -> None:
256
+ """Apply formatting to a cell range."""
257
+ service = SheetsService()
258
+ service.format_cells(
259
+ spreadsheet_id=spreadsheet_id,
260
+ sheet_id=sheet_id,
261
+ start_row=start_row,
262
+ end_row=end_row,
263
+ start_col=start_col,
264
+ end_col=end_col,
265
+ bold=bold,
266
+ italic=italic,
267
+ font_size=font_size,
268
+ background_color=background_color,
269
+ foreground_color=foreground_color,
270
+ )
271
+
272
+
273
+ @app.command("batch-get")
274
+ def batch_get(
275
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
276
+ ranges: Annotated[str, typer.Argument(help="Comma-separated A1 notation ranges.")],
277
+ ) -> None:
278
+ """Read multiple ranges at once."""
279
+ range_list = [r.strip() for r in ranges.split(",")]
280
+
281
+ service = SheetsService()
282
+ service.batch_get(spreadsheet_id=spreadsheet_id, ranges=range_list)
283
+
284
+
285
+ @app.command("format-extended")
286
+ def format_cells_extended(
287
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
288
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
289
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
290
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
291
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
292
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
293
+ bold: Annotated[
294
+ Optional[bool],
295
+ typer.Option("--bold/--no-bold", "-b", help="Bold formatting."),
296
+ ] = None,
297
+ italic: Annotated[
298
+ Optional[bool],
299
+ typer.Option("--italic/--no-italic", "-i", help="Italic formatting."),
300
+ ] = None,
301
+ underline: Annotated[
302
+ Optional[bool],
303
+ typer.Option("--underline/--no-underline", "-u", help="Underline formatting."),
304
+ ] = None,
305
+ strikethrough: Annotated[
306
+ Optional[bool],
307
+ typer.Option("--strikethrough/--no-strikethrough", help="Strikethrough."),
308
+ ] = None,
309
+ font_family: Annotated[
310
+ Optional[str],
311
+ typer.Option("--font", "-f", help="Font family name."),
312
+ ] = None,
313
+ font_size: Annotated[
314
+ Optional[int],
315
+ typer.Option("--size", "-s", help="Font size in points."),
316
+ ] = None,
317
+ foreground_color: Annotated[
318
+ Optional[str],
319
+ typer.Option("--fg-color", help="Text color (hex)."),
320
+ ] = None,
321
+ background_color: Annotated[
322
+ Optional[str],
323
+ typer.Option("--bg-color", help="Background color (hex)."),
324
+ ] = None,
325
+ horizontal_alignment: Annotated[
326
+ Optional[str],
327
+ typer.Option("--h-align", help="Horizontal alignment (LEFT, CENTER, RIGHT)."),
328
+ ] = None,
329
+ vertical_alignment: Annotated[
330
+ Optional[str],
331
+ typer.Option("--v-align", help="Vertical alignment (TOP, MIDDLE, BOTTOM)."),
332
+ ] = None,
333
+ text_wrap: Annotated[
334
+ Optional[str],
335
+ typer.Option("--wrap", help="Text wrap (OVERFLOW_CELL, CLIP, WRAP)."),
336
+ ] = None,
337
+ number_format: Annotated[
338
+ Optional[str],
339
+ typer.Option("--number-format", help="Number format pattern (e.g., '#,##0.00')."),
340
+ ] = None,
341
+ ) -> None:
342
+ """Apply extended formatting to a cell range."""
343
+ service = SheetsService()
344
+ service.format_cells_extended(
345
+ spreadsheet_id=spreadsheet_id,
346
+ sheet_id=sheet_id,
347
+ start_row=start_row,
348
+ end_row=end_row,
349
+ start_col=start_col,
350
+ end_col=end_col,
351
+ bold=bold,
352
+ italic=italic,
353
+ underline=underline,
354
+ strikethrough=strikethrough,
355
+ font_family=font_family,
356
+ font_size=font_size,
357
+ foreground_color=foreground_color,
358
+ background_color=background_color,
359
+ horizontal_alignment=horizontal_alignment,
360
+ vertical_alignment=vertical_alignment,
361
+ text_wrap=text_wrap,
362
+ number_format=number_format,
363
+ )
364
+
365
+
366
+ @app.command("set-borders")
367
+ def set_borders(
368
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
369
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
370
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
371
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
372
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
373
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
374
+ top: Annotated[bool, typer.Option("--top", help="Apply top border.")] = False,
375
+ bottom: Annotated[bool, typer.Option("--bottom", help="Apply bottom border.")] = False,
376
+ left: Annotated[bool, typer.Option("--left", help="Apply left border.")] = False,
377
+ right: Annotated[bool, typer.Option("--right", help="Apply right border.")] = False,
378
+ inner_horizontal: Annotated[
379
+ bool, typer.Option("--inner-h", help="Apply inner horizontal borders.")
380
+ ] = False,
381
+ inner_vertical: Annotated[
382
+ bool, typer.Option("--inner-v", help="Apply inner vertical borders.")
383
+ ] = False,
384
+ all_borders: Annotated[
385
+ bool, typer.Option("--all", "-a", help="Apply all borders (outer + inner).")
386
+ ] = False,
387
+ color: Annotated[
388
+ str, typer.Option("--color", "-c", help="Border color (hex).")
389
+ ] = "#000000",
390
+ style: Annotated[
391
+ str, typer.Option("--style", help="Border style (SOLID, DOTTED, DASHED, etc.).")
392
+ ] = "SOLID",
393
+ width: Annotated[
394
+ int, typer.Option("--width", "-w", help="Border width (1, 2, or 3).")
395
+ ] = 1,
396
+ ) -> None:
397
+ """Set borders on a cell range."""
398
+ if all_borders:
399
+ top = bottom = left = right = inner_horizontal = inner_vertical = True
400
+
401
+ service = SheetsService()
402
+ service.set_borders(
403
+ spreadsheet_id=spreadsheet_id,
404
+ sheet_id=sheet_id,
405
+ start_row=start_row,
406
+ end_row=end_row,
407
+ start_col=start_col,
408
+ end_col=end_col,
409
+ top=top,
410
+ bottom=bottom,
411
+ left=left,
412
+ right=right,
413
+ inner_horizontal=inner_horizontal,
414
+ inner_vertical=inner_vertical,
415
+ color=color,
416
+ style=style,
417
+ width=width,
418
+ )
419
+
420
+
421
+ @app.command("merge-cells")
422
+ def merge_cells(
423
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
424
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
425
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
426
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
427
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
428
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
429
+ merge_type: Annotated[
430
+ str,
431
+ typer.Option("--type", "-t", help="Merge type (MERGE_ALL, MERGE_COLUMNS, MERGE_ROWS)."),
432
+ ] = "MERGE_ALL",
433
+ ) -> None:
434
+ """Merge cells in a range."""
435
+ service = SheetsService()
436
+ service.merge_cells(
437
+ spreadsheet_id=spreadsheet_id,
438
+ sheet_id=sheet_id,
439
+ start_row=start_row,
440
+ end_row=end_row,
441
+ start_col=start_col,
442
+ end_col=end_col,
443
+ merge_type=merge_type,
444
+ )
445
+
446
+
447
+ @app.command("unmerge-cells")
448
+ def unmerge_cells(
449
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
450
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
451
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
452
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
453
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
454
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
455
+ ) -> None:
456
+ """Unmerge cells in a range."""
457
+ service = SheetsService()
458
+ service.unmerge_cells(
459
+ spreadsheet_id=spreadsheet_id,
460
+ sheet_id=sheet_id,
461
+ start_row=start_row,
462
+ end_row=end_row,
463
+ start_col=start_col,
464
+ end_col=end_col,
465
+ )
466
+
467
+
468
+ @app.command("set-column-width")
469
+ def set_column_width(
470
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
471
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
472
+ start_column: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
473
+ end_column: Annotated[int, typer.Argument(help="End column (exclusive).")],
474
+ width: Annotated[int, typer.Argument(help="Width in pixels.")],
475
+ ) -> None:
476
+ """Set column width."""
477
+ service = SheetsService()
478
+ service.set_column_width(
479
+ spreadsheet_id=spreadsheet_id,
480
+ sheet_id=sheet_id,
481
+ start_column=start_column,
482
+ end_column=end_column,
483
+ width=width,
484
+ )
485
+
486
+
487
+ @app.command("set-row-height")
488
+ def set_row_height(
489
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
490
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
491
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
492
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
493
+ height: Annotated[int, typer.Argument(help="Height in pixels.")],
494
+ ) -> None:
495
+ """Set row height."""
496
+ service = SheetsService()
497
+ service.set_row_height(
498
+ spreadsheet_id=spreadsheet_id,
499
+ sheet_id=sheet_id,
500
+ start_row=start_row,
501
+ end_row=end_row,
502
+ height=height,
503
+ )
504
+
505
+
506
+ @app.command("auto-resize-columns")
507
+ def auto_resize_columns(
508
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
509
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
510
+ start_column: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
511
+ end_column: Annotated[int, typer.Argument(help="End column (exclusive).")],
512
+ ) -> None:
513
+ """Auto-resize columns to fit content."""
514
+ service = SheetsService()
515
+ service.auto_resize_columns(
516
+ spreadsheet_id=spreadsheet_id,
517
+ sheet_id=sheet_id,
518
+ start_column=start_column,
519
+ end_column=end_column,
520
+ )
521
+
522
+
523
+ @app.command("freeze-rows")
524
+ def freeze_rows(
525
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
526
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
527
+ num_rows: Annotated[int, typer.Argument(help="Number of rows to freeze (0 to unfreeze).")],
528
+ ) -> None:
529
+ """Freeze rows at the top of the sheet."""
530
+ service = SheetsService()
531
+ service.freeze_rows(
532
+ spreadsheet_id=spreadsheet_id,
533
+ sheet_id=sheet_id,
534
+ num_rows=num_rows,
535
+ )
536
+
537
+
538
+ @app.command("freeze-columns")
539
+ def freeze_columns(
540
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
541
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
542
+ num_columns: Annotated[int, typer.Argument(help="Number of columns to freeze (0 to unfreeze).")],
543
+ ) -> None:
544
+ """Freeze columns at the left of the sheet."""
545
+ service = SheetsService()
546
+ service.freeze_columns(
547
+ spreadsheet_id=spreadsheet_id,
548
+ sheet_id=sheet_id,
549
+ num_columns=num_columns,
550
+ )
551
+
552
+
553
+ @app.command("add-conditional-format")
554
+ def add_conditional_format(
555
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
556
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
557
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
558
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
559
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
560
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
561
+ condition_type: Annotated[
562
+ str,
563
+ typer.Option("--condition", "-c", help="Condition type (NUMBER_GREATER, TEXT_CONTAINS, etc.)."),
564
+ ],
565
+ condition_values: Annotated[
566
+ str,
567
+ typer.Option("--values", "-v", help="Comma-separated condition values."),
568
+ ] = "",
569
+ background_color: Annotated[
570
+ Optional[str],
571
+ typer.Option("--bg-color", help="Background color when condition met (hex)."),
572
+ ] = None,
573
+ foreground_color: Annotated[
574
+ Optional[str],
575
+ typer.Option("--fg-color", help="Text color when condition met (hex)."),
576
+ ] = None,
577
+ bold: Annotated[
578
+ Optional[bool],
579
+ typer.Option("--bold/--no-bold", "-b", help="Bold when condition met."),
580
+ ] = None,
581
+ ) -> None:
582
+ """Add a conditional formatting rule."""
583
+ values_list = [v.strip() for v in condition_values.split(",") if v.strip()]
584
+
585
+ service = SheetsService()
586
+ service.add_conditional_format(
587
+ spreadsheet_id=spreadsheet_id,
588
+ sheet_id=sheet_id,
589
+ start_row=start_row,
590
+ end_row=end_row,
591
+ start_col=start_col,
592
+ end_col=end_col,
593
+ condition_type=condition_type,
594
+ condition_values=values_list,
595
+ background_color=background_color,
596
+ foreground_color=foreground_color,
597
+ bold=bold,
598
+ )
599
+
600
+
601
+ @app.command("add-color-scale")
602
+ def add_color_scale(
603
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
604
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
605
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
606
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
607
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
608
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
609
+ min_color: Annotated[
610
+ str, typer.Option("--min-color", help="Color for minimum values (hex).")
611
+ ],
612
+ max_color: Annotated[
613
+ str, typer.Option("--max-color", help="Color for maximum values (hex).")
614
+ ],
615
+ mid_color: Annotated[
616
+ Optional[str],
617
+ typer.Option("--mid-color", help="Color for midpoint values (hex, optional)."),
618
+ ] = None,
619
+ ) -> None:
620
+ """Add a color scale conditional formatting rule."""
621
+ service = SheetsService()
622
+ service.add_color_scale(
623
+ spreadsheet_id=spreadsheet_id,
624
+ sheet_id=sheet_id,
625
+ start_row=start_row,
626
+ end_row=end_row,
627
+ start_col=start_col,
628
+ end_col=end_col,
629
+ min_color=min_color,
630
+ max_color=max_color,
631
+ mid_color=mid_color,
632
+ )
633
+
634
+
635
+ @app.command("clear-conditional-formats")
636
+ def clear_conditional_formats(
637
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
638
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
639
+ ) -> None:
640
+ """Clear all conditional formatting rules from a sheet."""
641
+ service = SheetsService()
642
+ service.clear_conditional_formats(
643
+ spreadsheet_id=spreadsheet_id,
644
+ sheet_id=sheet_id,
645
+ )
646
+
647
+
648
+ @app.command("insert-rows")
649
+ def insert_rows(
650
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
651
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
652
+ start_index: Annotated[int, typer.Argument(help="Row index to insert at (0-indexed).")],
653
+ count: Annotated[int, typer.Argument(help="Number of rows to insert.")],
654
+ inherit_from_before: Annotated[
655
+ bool,
656
+ typer.Option("--inherit-before/--inherit-after", help="Inherit formatting from row before (default) or after."),
657
+ ] = True,
658
+ ) -> None:
659
+ """Insert rows at a specific index."""
660
+ service = SheetsService()
661
+ service.insert_rows(
662
+ spreadsheet_id=spreadsheet_id,
663
+ sheet_id=sheet_id,
664
+ start_index=start_index,
665
+ count=count,
666
+ inherit_from_before=inherit_from_before,
667
+ )
668
+
669
+
670
+ @app.command("insert-columns")
671
+ def insert_columns(
672
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
673
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
674
+ start_index: Annotated[int, typer.Argument(help="Column index to insert at (0-indexed).")],
675
+ count: Annotated[int, typer.Argument(help="Number of columns to insert.")],
676
+ inherit_from_before: Annotated[
677
+ bool,
678
+ typer.Option("--inherit-before/--inherit-after", help="Inherit formatting from column before (default) or after."),
679
+ ] = True,
680
+ ) -> None:
681
+ """Insert columns at a specific index."""
682
+ service = SheetsService()
683
+ service.insert_columns(
684
+ spreadsheet_id=spreadsheet_id,
685
+ sheet_id=sheet_id,
686
+ start_index=start_index,
687
+ count=count,
688
+ inherit_from_before=inherit_from_before,
689
+ )
690
+
691
+
692
+ @app.command("delete-rows")
693
+ def delete_rows(
694
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
695
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
696
+ start_index: Annotated[int, typer.Argument(help="Start row index (0-indexed, inclusive).")],
697
+ end_index: Annotated[int, typer.Argument(help="End row index (0-indexed, exclusive).")],
698
+ ) -> None:
699
+ """Delete rows in a range."""
700
+ service = SheetsService()
701
+ service.delete_rows(
702
+ spreadsheet_id=spreadsheet_id,
703
+ sheet_id=sheet_id,
704
+ start_index=start_index,
705
+ end_index=end_index,
706
+ )
707
+
708
+
709
+ @app.command("delete-columns")
710
+ def delete_columns(
711
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
712
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
713
+ start_index: Annotated[int, typer.Argument(help="Start column index (0-indexed, inclusive).")],
714
+ end_index: Annotated[int, typer.Argument(help="End column index (0-indexed, exclusive).")],
715
+ ) -> None:
716
+ """Delete columns in a range."""
717
+ service = SheetsService()
718
+ service.delete_columns(
719
+ spreadsheet_id=spreadsheet_id,
720
+ sheet_id=sheet_id,
721
+ start_index=start_index,
722
+ end_index=end_index,
723
+ )
724
+
725
+
726
+ @app.command("sort")
727
+ def sort_range(
728
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
729
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
730
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
731
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
732
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
733
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
734
+ sort_column: Annotated[int, typer.Argument(help="Column index to sort by (0-indexed).")],
735
+ descending: Annotated[
736
+ bool,
737
+ typer.Option("--desc", "-d", help="Sort in descending order."),
738
+ ] = False,
739
+ ) -> None:
740
+ """Sort a range by a column."""
741
+ service = SheetsService()
742
+ service.sort_range(
743
+ spreadsheet_id=spreadsheet_id,
744
+ sheet_id=sheet_id,
745
+ start_row=start_row,
746
+ end_row=end_row,
747
+ start_col=start_col,
748
+ end_col=end_col,
749
+ sort_column=sort_column,
750
+ ascending=not descending,
751
+ )
752
+
753
+
754
+ @app.command("find-replace")
755
+ def find_replace(
756
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
757
+ find: Annotated[str, typer.Argument(help="Text to find.")],
758
+ replace: Annotated[str, typer.Argument(help="Replacement text.")],
759
+ sheet_id: Annotated[
760
+ Optional[int],
761
+ typer.Option("--sheet-id", "-s", help="Limit to specific sheet ID."),
762
+ ] = None,
763
+ all_sheets: Annotated[
764
+ bool,
765
+ typer.Option("--all-sheets", "-a", help="Search all sheets."),
766
+ ] = False,
767
+ match_case: Annotated[
768
+ bool,
769
+ typer.Option("--match-case", "-c", help="Case-sensitive matching."),
770
+ ] = False,
771
+ match_entire_cell: Annotated[
772
+ bool,
773
+ typer.Option("--entire-cell", "-e", help="Match entire cell contents."),
774
+ ] = False,
775
+ use_regex: Annotated[
776
+ bool,
777
+ typer.Option("--regex", "-r", help="Use regular expression."),
778
+ ] = False,
779
+ ) -> None:
780
+ """Find and replace text in a spreadsheet."""
781
+ service = SheetsService()
782
+ service.find_replace(
783
+ spreadsheet_id=spreadsheet_id,
784
+ find=find,
785
+ replace=replace,
786
+ sheet_id=sheet_id,
787
+ all_sheets=all_sheets,
788
+ match_case=match_case,
789
+ match_entire_cell=match_entire_cell,
790
+ use_regex=use_regex,
791
+ )
792
+
793
+
794
+ @app.command("duplicate-sheet")
795
+ def duplicate_sheet(
796
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
797
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID to duplicate.")],
798
+ new_name: Annotated[
799
+ Optional[str],
800
+ typer.Option("--name", "-n", help="Name for the new sheet."),
801
+ ] = None,
802
+ insert_index: Annotated[
803
+ Optional[int],
804
+ typer.Option("--index", "-i", help="Position to insert the new sheet."),
805
+ ] = None,
806
+ ) -> None:
807
+ """Duplicate a sheet within the spreadsheet."""
808
+ service = SheetsService()
809
+ service.duplicate_sheet(
810
+ spreadsheet_id=spreadsheet_id,
811
+ sheet_id=sheet_id,
812
+ new_name=new_name,
813
+ insert_index=insert_index,
814
+ )
815
+
816
+
817
+ @app.command("set-validation")
818
+ def set_validation(
819
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
820
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
821
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
822
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
823
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
824
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
825
+ validation_type: Annotated[
826
+ str,
827
+ typer.Option(
828
+ "--type", "-t",
829
+ help="Validation type (ONE_OF_LIST, NUMBER_GREATER, TEXT_CONTAINS, etc.).",
830
+ ),
831
+ ],
832
+ values: Annotated[
833
+ Optional[str],
834
+ typer.Option("--values", "-v", help="Comma-separated values for list or conditions."),
835
+ ] = None,
836
+ formula: Annotated[
837
+ Optional[str],
838
+ typer.Option("--formula", "-f", help="Custom formula for CUSTOM_FORMULA type."),
839
+ ] = None,
840
+ allow_invalid: Annotated[
841
+ bool,
842
+ typer.Option("--allow-invalid", help="Allow invalid input with warning."),
843
+ ] = False,
844
+ no_dropdown: Annotated[
845
+ bool,
846
+ typer.Option("--no-dropdown", help="Hide dropdown for list validation."),
847
+ ] = False,
848
+ ) -> None:
849
+ """Set data validation on a cell range.
850
+
851
+ Validation types: ONE_OF_LIST, NUMBER_GREATER, NUMBER_LESS, NUMBER_BETWEEN,
852
+ TEXT_CONTAINS, TEXT_IS_EMAIL, DATE_BEFORE, CUSTOM_FORMULA, etc.
853
+ """
854
+ values_list = [v.strip() for v in values.split(",")] if values else None
855
+
856
+ service = SheetsService()
857
+ service.set_data_validation(
858
+ spreadsheet_id=spreadsheet_id,
859
+ sheet_id=sheet_id,
860
+ start_row=start_row,
861
+ end_row=end_row,
862
+ start_col=start_col,
863
+ end_col=end_col,
864
+ validation_type=validation_type,
865
+ values=values_list,
866
+ formula=formula,
867
+ strict=not allow_invalid,
868
+ show_dropdown=not no_dropdown,
869
+ )
870
+
871
+
872
+ @app.command("clear-validation")
873
+ def clear_validation(
874
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
875
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
876
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
877
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
878
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
879
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
880
+ ) -> None:
881
+ """Clear data validation from a cell range."""
882
+ service = SheetsService()
883
+ service.clear_data_validation(
884
+ spreadsheet_id=spreadsheet_id,
885
+ sheet_id=sheet_id,
886
+ start_row=start_row,
887
+ end_row=end_row,
888
+ start_col=start_col,
889
+ end_col=end_col,
890
+ )
891
+
892
+
893
+ @app.command("add-chart")
894
+ def add_chart(
895
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
896
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
897
+ chart_type: Annotated[
898
+ str, typer.Argument(help="Chart type (LINE, COLUMN, BAR, PIE, SCATTER, AREA).")
899
+ ],
900
+ data_start_row: Annotated[int, typer.Argument(help="Data range start row (0-indexed).")],
901
+ data_end_row: Annotated[int, typer.Argument(help="Data range end row (exclusive).")],
902
+ data_start_col: Annotated[int, typer.Argument(help="Data range start column (0-indexed).")],
903
+ data_end_col: Annotated[int, typer.Argument(help="Data range end column (exclusive).")],
904
+ anchor_row: Annotated[int, typer.Option("--anchor-row", "-r", help="Anchor row (0-indexed).")] = 0,
905
+ anchor_col: Annotated[int, typer.Option("--anchor-col", "-c", help="Anchor column (0-indexed).")] = 5,
906
+ title: Annotated[
907
+ Optional[str],
908
+ typer.Option("--title", "-t", help="Chart title."),
909
+ ] = None,
910
+ legend: Annotated[
911
+ str,
912
+ typer.Option("--legend", "-l", help="Legend position (BOTTOM_LEGEND, NO_LEGEND, etc.)."),
913
+ ] = "BOTTOM_LEGEND",
914
+ ) -> None:
915
+ """Add a chart to a sheet."""
916
+ service = SheetsService()
917
+ service.add_chart(
918
+ spreadsheet_id=spreadsheet_id,
919
+ sheet_id=sheet_id,
920
+ chart_type=chart_type,
921
+ data_range_start_row=data_start_row,
922
+ data_range_end_row=data_end_row,
923
+ data_range_start_col=data_start_col,
924
+ data_range_end_col=data_end_col,
925
+ anchor_row=anchor_row,
926
+ anchor_col=anchor_col,
927
+ title=title,
928
+ legend_position=legend,
929
+ )
930
+
931
+
932
+ @app.command("delete-chart")
933
+ def delete_chart(
934
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
935
+ chart_id: Annotated[int, typer.Argument(help="Chart ID to delete.")],
936
+ ) -> None:
937
+ """Delete a chart from a spreadsheet."""
938
+ service = SheetsService()
939
+ service.delete_chart(
940
+ spreadsheet_id=spreadsheet_id,
941
+ chart_id=chart_id,
942
+ )
943
+
944
+
945
+ @app.command("add-banding")
946
+ def add_banding(
947
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
948
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
949
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
950
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
951
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
952
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
953
+ header_color: Annotated[
954
+ Optional[str],
955
+ typer.Option("--header-color", help="Header row color (hex, e.g., #4285F4)."),
956
+ ] = None,
957
+ first_color: Annotated[
958
+ Optional[str],
959
+ typer.Option("--first-color", help="First alternating color (hex)."),
960
+ ] = None,
961
+ second_color: Annotated[
962
+ Optional[str],
963
+ typer.Option("--second-color", help="Second alternating color (hex)."),
964
+ ] = None,
965
+ ) -> None:
966
+ """Add alternating row colors (banding) to a range."""
967
+ service = SheetsService()
968
+ service.add_banding(
969
+ spreadsheet_id=spreadsheet_id,
970
+ sheet_id=sheet_id,
971
+ start_row=start_row,
972
+ end_row=end_row,
973
+ start_col=start_col,
974
+ end_col=end_col,
975
+ header_color=header_color,
976
+ first_color=first_color,
977
+ second_color=second_color,
978
+ )
979
+
980
+
981
+ @app.command("delete-banding")
982
+ def delete_banding(
983
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
984
+ banded_range_id: Annotated[int, typer.Argument(help="Banded range ID to delete.")],
985
+ ) -> None:
986
+ """Delete a banded range (alternating colors) from a spreadsheet."""
987
+ service = SheetsService()
988
+ service.delete_banding(
989
+ spreadsheet_id=spreadsheet_id,
990
+ banded_range_id=banded_range_id,
991
+ )
992
+
993
+
994
+ # ===== Filters =====
995
+
996
+
997
+ @app.command("set-basic-filter")
998
+ def set_basic_filter(
999
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1000
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
1001
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
1002
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
1003
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
1004
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
1005
+ ) -> None:
1006
+ """Set a basic filter on a range (enables filter dropdowns in column headers)."""
1007
+ service = SheetsService()
1008
+ service.set_basic_filter(
1009
+ spreadsheet_id=spreadsheet_id,
1010
+ sheet_id=sheet_id,
1011
+ start_row=start_row,
1012
+ end_row=end_row,
1013
+ start_col=start_col,
1014
+ end_col=end_col,
1015
+ )
1016
+
1017
+
1018
+ @app.command("clear-basic-filter")
1019
+ def clear_basic_filter(
1020
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1021
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
1022
+ ) -> None:
1023
+ """Clear the basic filter from a sheet."""
1024
+ service = SheetsService()
1025
+ service.clear_basic_filter(
1026
+ spreadsheet_id=spreadsheet_id,
1027
+ sheet_id=sheet_id,
1028
+ )
1029
+
1030
+
1031
+ @app.command("create-filter-view")
1032
+ def create_filter_view(
1033
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1034
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
1035
+ title: Annotated[str, typer.Argument(help="Filter view name.")],
1036
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
1037
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
1038
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
1039
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
1040
+ ) -> None:
1041
+ """Create a named filter view (saveable filter configuration)."""
1042
+ service = SheetsService()
1043
+ service.create_filter_view(
1044
+ spreadsheet_id=spreadsheet_id,
1045
+ sheet_id=sheet_id,
1046
+ title=title,
1047
+ start_row=start_row,
1048
+ end_row=end_row,
1049
+ start_col=start_col,
1050
+ end_col=end_col,
1051
+ )
1052
+
1053
+
1054
+ @app.command("list-filter-views")
1055
+ def list_filter_views(
1056
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1057
+ ) -> None:
1058
+ """List all filter views in a spreadsheet."""
1059
+ service = SheetsService()
1060
+ service.list_filter_views(spreadsheet_id=spreadsheet_id)
1061
+
1062
+
1063
+ @app.command("delete-filter-view")
1064
+ def delete_filter_view(
1065
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1066
+ filter_view_id: Annotated[int, typer.Argument(help="Filter view ID to delete.")],
1067
+ ) -> None:
1068
+ """Delete a filter view."""
1069
+ service = SheetsService()
1070
+ service.delete_filter_view(
1071
+ spreadsheet_id=spreadsheet_id,
1072
+ filter_view_id=filter_view_id,
1073
+ )
1074
+
1075
+
1076
+ # ===== Pivot Tables =====
1077
+
1078
+
1079
+ @app.command("create-pivot-table")
1080
+ def create_pivot_table(
1081
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1082
+ source_sheet_id: Annotated[int, typer.Argument(help="Source data sheet ID.")],
1083
+ source_start_row: Annotated[int, typer.Argument(help="Source start row (0-indexed).")],
1084
+ source_end_row: Annotated[int, typer.Argument(help="Source end row (exclusive).")],
1085
+ source_start_col: Annotated[int, typer.Argument(help="Source start column (0-indexed).")],
1086
+ source_end_col: Annotated[int, typer.Argument(help="Source end column (exclusive).")],
1087
+ target_sheet_id: Annotated[int, typer.Argument(help="Target sheet ID for pivot table.")],
1088
+ target_row: Annotated[int, typer.Argument(help="Target start row (0-indexed).")],
1089
+ target_col: Annotated[int, typer.Argument(help="Target start column (0-indexed).")],
1090
+ row_columns: Annotated[str, typer.Argument(help="Comma-separated column indices for row grouping.")],
1091
+ value_columns: Annotated[str, typer.Argument(help="Comma-separated column indices for values.")],
1092
+ value_functions: Annotated[
1093
+ Optional[str],
1094
+ typer.Option("--functions", "-f", help="Comma-separated aggregation functions (SUM, AVERAGE, COUNT, MAX, MIN). Defaults to SUM."),
1095
+ ] = None,
1096
+ column_columns: Annotated[
1097
+ Optional[str],
1098
+ typer.Option("--column-groups", "-c", help="Comma-separated column indices for column grouping."),
1099
+ ] = None,
1100
+ ) -> None:
1101
+ """Create a pivot table from source data.
1102
+
1103
+ Example:
1104
+ gws-cli sheets create-pivot-table SPREADSHEET_ID 0 0 100 0 5 0 0 0 "0" "2,3" -f "SUM,AVERAGE"
1105
+ """
1106
+ row_cols = [int(c.strip()) for c in row_columns.split(",")]
1107
+ val_cols = [int(c.strip()) for c in value_columns.split(",")]
1108
+ val_funcs = [f.strip().upper() for f in value_functions.split(",")] if value_functions else None
1109
+ col_cols = [int(c.strip()) for c in column_columns.split(",")] if column_columns else None
1110
+
1111
+ service = SheetsService()
1112
+ service.create_pivot_table(
1113
+ spreadsheet_id=spreadsheet_id,
1114
+ source_sheet_id=source_sheet_id,
1115
+ source_start_row=source_start_row,
1116
+ source_end_row=source_end_row,
1117
+ source_start_col=source_start_col,
1118
+ source_end_col=source_end_col,
1119
+ target_sheet_id=target_sheet_id,
1120
+ target_row=target_row,
1121
+ target_col=target_col,
1122
+ row_source_columns=row_cols,
1123
+ value_source_columns=val_cols,
1124
+ value_functions=val_funcs,
1125
+ column_source_columns=col_cols,
1126
+ )
1127
+
1128
+
1129
+ @app.command("list-pivot-tables")
1130
+ def list_pivot_tables(
1131
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1132
+ ) -> None:
1133
+ """List all pivot tables in a spreadsheet."""
1134
+ service = SheetsService()
1135
+ service.list_pivot_tables(spreadsheet_id=spreadsheet_id)
1136
+
1137
+
1138
+ # ===== Protected Ranges =====
1139
+
1140
+
1141
+ @app.command("protect-range")
1142
+ def protect_range(
1143
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1144
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
1145
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
1146
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
1147
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
1148
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
1149
+ description: Annotated[
1150
+ Optional[str],
1151
+ typer.Option("--description", "-d", help="Description of the protected range."),
1152
+ ] = None,
1153
+ warning_only: Annotated[
1154
+ bool,
1155
+ typer.Option("--warning-only", "-w", help="Show warning but allow editing."),
1156
+ ] = False,
1157
+ editors: Annotated[
1158
+ Optional[str],
1159
+ typer.Option("--editors", "-e", help="Comma-separated email addresses that can edit."),
1160
+ ] = None,
1161
+ ) -> None:
1162
+ """Protect a range from editing."""
1163
+ editor_list = [e.strip() for e in editors.split(",")] if editors else None
1164
+
1165
+ service = SheetsService()
1166
+ service.protect_range(
1167
+ spreadsheet_id=spreadsheet_id,
1168
+ sheet_id=sheet_id,
1169
+ start_row=start_row,
1170
+ end_row=end_row,
1171
+ start_col=start_col,
1172
+ end_col=end_col,
1173
+ description=description,
1174
+ warning_only=warning_only,
1175
+ editors=editor_list,
1176
+ )
1177
+
1178
+
1179
+ @app.command("protect-sheet")
1180
+ def protect_sheet(
1181
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1182
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
1183
+ description: Annotated[
1184
+ Optional[str],
1185
+ typer.Option("--description", "-d", help="Description of the protection."),
1186
+ ] = None,
1187
+ warning_only: Annotated[
1188
+ bool,
1189
+ typer.Option("--warning-only", "-w", help="Show warning but allow editing."),
1190
+ ] = False,
1191
+ editors: Annotated[
1192
+ Optional[str],
1193
+ typer.Option("--editors", "-e", help="Comma-separated email addresses that can edit."),
1194
+ ] = None,
1195
+ ) -> None:
1196
+ """Protect an entire sheet from editing."""
1197
+ editor_list = [e.strip() for e in editors.split(",")] if editors else None
1198
+
1199
+ service = SheetsService()
1200
+ service.protect_sheet(
1201
+ spreadsheet_id=spreadsheet_id,
1202
+ sheet_id=sheet_id,
1203
+ description=description,
1204
+ warning_only=warning_only,
1205
+ editors=editor_list,
1206
+ )
1207
+
1208
+
1209
+ @app.command("list-protected-ranges")
1210
+ def list_protected_ranges(
1211
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1212
+ ) -> None:
1213
+ """List all protected ranges in a spreadsheet."""
1214
+ service = SheetsService()
1215
+ service.list_protected_ranges(spreadsheet_id=spreadsheet_id)
1216
+
1217
+
1218
+ @app.command("unprotect-range")
1219
+ def unprotect_range(
1220
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1221
+ protected_range_id: Annotated[int, typer.Argument(help="Protected range ID to remove.")],
1222
+ ) -> None:
1223
+ """Remove protection from a range."""
1224
+ service = SheetsService()
1225
+ service.unprotect_range(
1226
+ spreadsheet_id=spreadsheet_id,
1227
+ protected_range_id=protected_range_id,
1228
+ )
1229
+
1230
+
1231
+ # ===== Named Ranges =====
1232
+
1233
+
1234
+ @app.command("create-named-range")
1235
+ def create_named_range(
1236
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1237
+ name: Annotated[str, typer.Argument(help="Name for the range (must be valid identifier).")],
1238
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
1239
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
1240
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
1241
+ start_col: Annotated[int, typer.Argument(help="Start column (0-indexed).")],
1242
+ end_col: Annotated[int, typer.Argument(help="End column (exclusive).")],
1243
+ ) -> None:
1244
+ """Create a named range for use in formulas."""
1245
+ service = SheetsService()
1246
+ service.create_named_range(
1247
+ spreadsheet_id=spreadsheet_id,
1248
+ name=name,
1249
+ sheet_id=sheet_id,
1250
+ start_row=start_row,
1251
+ end_row=end_row,
1252
+ start_col=start_col,
1253
+ end_col=end_col,
1254
+ )
1255
+
1256
+
1257
+ @app.command("list-named-ranges")
1258
+ def list_named_ranges(
1259
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1260
+ ) -> None:
1261
+ """List all named ranges in a spreadsheet."""
1262
+ service = SheetsService()
1263
+ service.list_named_ranges(spreadsheet_id=spreadsheet_id)
1264
+
1265
+
1266
+ @app.command("delete-named-range")
1267
+ def delete_named_range(
1268
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1269
+ named_range_id: Annotated[str, typer.Argument(help="Named range ID to delete.")],
1270
+ ) -> None:
1271
+ """Delete a named range."""
1272
+ service = SheetsService()
1273
+ service.delete_named_range(
1274
+ spreadsheet_id=spreadsheet_id,
1275
+ named_range_id=named_range_id,
1276
+ )
1277
+
1278
+
1279
+ # ===== Chart Updates =====
1280
+
1281
+
1282
+ @app.command("update-chart")
1283
+ def update_chart(
1284
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1285
+ chart_id: Annotated[int, typer.Argument(help="Chart ID to update.")],
1286
+ title: Annotated[
1287
+ Optional[str],
1288
+ typer.Option("--title", "-t", help="New chart title."),
1289
+ ] = None,
1290
+ chart_type: Annotated[
1291
+ Optional[str],
1292
+ typer.Option("--type", help="Chart type (LINE, BAR, COLUMN, AREA, SCATTER, PIE)."),
1293
+ ] = None,
1294
+ legend_position: Annotated[
1295
+ Optional[str],
1296
+ typer.Option("--legend", help="Legend position (TOP, BOTTOM, LEFT, RIGHT, NONE)."),
1297
+ ] = None,
1298
+ ) -> None:
1299
+ """Update a chart's title, type, or legend position."""
1300
+ service = SheetsService()
1301
+ service.update_chart(
1302
+ spreadsheet_id=spreadsheet_id,
1303
+ chart_id=chart_id,
1304
+ title=title,
1305
+ chart_type=chart_type,
1306
+ legend_position=legend_position,
1307
+ )
1308
+
1309
+
1310
+ # ===== Row/Column Movement =====
1311
+
1312
+
1313
+ @app.command("move-rows")
1314
+ def move_rows(
1315
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1316
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
1317
+ source_start: Annotated[int, typer.Argument(help="Source start row (0-indexed).")],
1318
+ source_end: Annotated[int, typer.Argument(help="Source end row (exclusive).")],
1319
+ destination_index: Annotated[int, typer.Argument(help="Destination row index (0-indexed).")],
1320
+ ) -> None:
1321
+ """Move rows to a new position."""
1322
+ service = SheetsService()
1323
+ service.move_rows(
1324
+ spreadsheet_id=spreadsheet_id,
1325
+ sheet_id=sheet_id,
1326
+ source_start=source_start,
1327
+ source_end=source_end,
1328
+ destination_index=destination_index,
1329
+ )
1330
+
1331
+
1332
+ @app.command("move-columns")
1333
+ def move_columns(
1334
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1335
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
1336
+ source_start: Annotated[int, typer.Argument(help="Source start column (0-indexed).")],
1337
+ source_end: Annotated[int, typer.Argument(help="Source end column (exclusive).")],
1338
+ destination_index: Annotated[int, typer.Argument(help="Destination column index (0-indexed).")],
1339
+ ) -> None:
1340
+ """Move columns to a new position."""
1341
+ service = SheetsService()
1342
+ service.move_columns(
1343
+ spreadsheet_id=spreadsheet_id,
1344
+ sheet_id=sheet_id,
1345
+ source_start=source_start,
1346
+ source_end=source_end,
1347
+ destination_index=destination_index,
1348
+ )
1349
+
1350
+
1351
+ # ===== Copy/Paste & Auto-Fill =====
1352
+
1353
+
1354
+ @app.command("copy-paste")
1355
+ def copy_paste(
1356
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1357
+ source_sheet_id: Annotated[int, typer.Option("--source-sheet", help="Source sheet ID.")],
1358
+ source_start_row: Annotated[int, typer.Option("--source-start-row", help="Source start row.")],
1359
+ source_end_row: Annotated[int, typer.Option("--source-end-row", help="Source end row.")],
1360
+ source_start_col: Annotated[int, typer.Option("--source-start-col", help="Source start column.")],
1361
+ source_end_col: Annotated[int, typer.Option("--source-end-col", help="Source end column.")],
1362
+ dest_sheet_id: Annotated[int, typer.Option("--dest-sheet", help="Destination sheet ID.")],
1363
+ dest_start_row: Annotated[int, typer.Option("--dest-start-row", help="Destination start row.")],
1364
+ dest_start_col: Annotated[int, typer.Option("--dest-start-col", help="Destination start column.")],
1365
+ paste_type: Annotated[
1366
+ str,
1367
+ typer.Option("--paste-type", help="What to paste (PASTE_NORMAL, PASTE_VALUES, PASTE_FORMAT)."),
1368
+ ] = "PASTE_NORMAL",
1369
+ ) -> None:
1370
+ """Copy a range and paste it to another location."""
1371
+ service = SheetsService()
1372
+ service.copy_paste(
1373
+ spreadsheet_id=spreadsheet_id,
1374
+ source_sheet_id=source_sheet_id,
1375
+ source_start_row=source_start_row,
1376
+ source_end_row=source_end_row,
1377
+ source_start_col=source_start_col,
1378
+ source_end_col=source_end_col,
1379
+ dest_sheet_id=dest_sheet_id,
1380
+ dest_start_row=dest_start_row,
1381
+ dest_start_col=dest_start_col,
1382
+ paste_type=paste_type,
1383
+ )
1384
+
1385
+
1386
+ @app.command("auto-fill")
1387
+ def auto_fill(
1388
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1389
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
1390
+ source_start_row: Annotated[int, typer.Option("--source-start-row", help="Source start row.")],
1391
+ source_end_row: Annotated[int, typer.Option("--source-end-row", help="Source end row.")],
1392
+ source_start_col: Annotated[int, typer.Option("--source-start-col", help="Source start column.")],
1393
+ source_end_col: Annotated[int, typer.Option("--source-end-col", help="Source end column.")],
1394
+ fill_start_row: Annotated[int, typer.Option("--fill-start-row", help="Fill start row.")],
1395
+ fill_end_row: Annotated[int, typer.Option("--fill-end-row", help="Fill end row.")],
1396
+ fill_start_col: Annotated[int, typer.Option("--fill-start-col", help="Fill start column.")],
1397
+ fill_end_col: Annotated[int, typer.Option("--fill-end-col", help="Fill end column.")],
1398
+ use_alternate_series: Annotated[
1399
+ bool,
1400
+ typer.Option("--alternate-series", help="Use alternate series for fill."),
1401
+ ] = False,
1402
+ ) -> None:
1403
+ """Auto-fill a range based on source data patterns."""
1404
+ service = SheetsService()
1405
+ service.auto_fill(
1406
+ spreadsheet_id=spreadsheet_id,
1407
+ sheet_id=sheet_id,
1408
+ source_start_row=source_start_row,
1409
+ source_end_row=source_end_row,
1410
+ source_start_col=source_start_col,
1411
+ source_end_col=source_end_col,
1412
+ fill_start_row=fill_start_row,
1413
+ fill_end_row=fill_end_row,
1414
+ fill_start_col=fill_start_col,
1415
+ fill_end_col=fill_end_col,
1416
+ use_alternate_series=use_alternate_series,
1417
+ )
1418
+
1419
+
1420
+ # ===== Data Cleanup =====
1421
+
1422
+
1423
+ @app.command("trim-whitespace")
1424
+ def trim_whitespace(
1425
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1426
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
1427
+ start_row: Annotated[int, typer.Option("--start-row", help="Start row (0-indexed).")] = 0,
1428
+ end_row: Annotated[
1429
+ Optional[int],
1430
+ typer.Option("--end-row", help="End row (exclusive), omit for entire sheet."),
1431
+ ] = None,
1432
+ start_col: Annotated[int, typer.Option("--start-col", help="Start column (0-indexed).")] = 0,
1433
+ end_col: Annotated[
1434
+ Optional[int],
1435
+ typer.Option("--end-col", help="End column (exclusive), omit for entire sheet."),
1436
+ ] = None,
1437
+ ) -> None:
1438
+ """Trim leading and trailing whitespace from cells."""
1439
+ service = SheetsService()
1440
+ service.trim_whitespace(
1441
+ spreadsheet_id=spreadsheet_id,
1442
+ sheet_id=sheet_id,
1443
+ start_row=start_row,
1444
+ end_row=end_row,
1445
+ start_col=start_col,
1446
+ end_col=end_col,
1447
+ )
1448
+
1449
+
1450
+ @app.command("text-to-columns")
1451
+ def text_to_columns(
1452
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1453
+ sheet_id: Annotated[int, typer.Argument(help="Sheet ID (numeric).")],
1454
+ start_row: Annotated[int, typer.Argument(help="Start row (0-indexed).")],
1455
+ end_row: Annotated[int, typer.Argument(help="End row (exclusive).")],
1456
+ source_column: Annotated[int, typer.Argument(help="Column to split (0-indexed).")],
1457
+ delimiter_type: Annotated[
1458
+ str,
1459
+ typer.Option("--delimiter", "-d", help="Delimiter type (COMMA, SEMICOLON, PERIOD, SPACE, CUSTOM, AUTODETECT)."),
1460
+ ] = "COMMA",
1461
+ custom_delimiter: Annotated[
1462
+ Optional[str],
1463
+ typer.Option("--custom", help="Custom delimiter (required if delimiter type is CUSTOM)."),
1464
+ ] = None,
1465
+ ) -> None:
1466
+ """Split text in a column into multiple columns by delimiter."""
1467
+ service = SheetsService()
1468
+ service.text_to_columns(
1469
+ spreadsheet_id=spreadsheet_id,
1470
+ sheet_id=sheet_id,
1471
+ start_row=start_row,
1472
+ end_row=end_row,
1473
+ source_column=source_column,
1474
+ delimiter_type=delimiter_type,
1475
+ custom_delimiter=custom_delimiter,
1476
+ )
1477
+
1478
+
1479
+ # ===== Banding & Filter View Updates =====
1480
+
1481
+
1482
+ @app.command("update-banding")
1483
+ def update_banding(
1484
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1485
+ banded_range_id: Annotated[int, typer.Argument(help="Banded range ID to update.")],
1486
+ header_color: Annotated[
1487
+ Optional[str],
1488
+ typer.Option("--header-color", help="Header row color (hex, e.g., '#4285F4')."),
1489
+ ] = None,
1490
+ first_band_color: Annotated[
1491
+ Optional[str],
1492
+ typer.Option("--first-color", help="First alternating band color (hex)."),
1493
+ ] = None,
1494
+ second_band_color: Annotated[
1495
+ Optional[str],
1496
+ typer.Option("--second-color", help="Second alternating band color (hex)."),
1497
+ ] = None,
1498
+ footer_color: Annotated[
1499
+ Optional[str],
1500
+ typer.Option("--footer-color", help="Footer row color (hex)."),
1501
+ ] = None,
1502
+ ) -> None:
1503
+ """Update banding colors on a range."""
1504
+ service = SheetsService()
1505
+ service.update_banding(
1506
+ spreadsheet_id=spreadsheet_id,
1507
+ banded_range_id=banded_range_id,
1508
+ header_color=header_color,
1509
+ first_band_color=first_band_color,
1510
+ second_band_color=second_band_color,
1511
+ footer_color=footer_color,
1512
+ )
1513
+
1514
+
1515
+ @app.command("update-filter-view")
1516
+ def update_filter_view(
1517
+ spreadsheet_id: Annotated[str, typer.Argument(help="Spreadsheet ID.")],
1518
+ filter_view_id: Annotated[int, typer.Argument(help="Filter view ID to update.")],
1519
+ title: Annotated[
1520
+ Optional[str],
1521
+ typer.Option("--title", "-t", help="New filter view title."),
1522
+ ] = None,
1523
+ start_row: Annotated[
1524
+ Optional[int],
1525
+ typer.Option("--start-row", help="New start row (0-indexed)."),
1526
+ ] = None,
1527
+ end_row: Annotated[
1528
+ Optional[int],
1529
+ typer.Option("--end-row", help="New end row (exclusive)."),
1530
+ ] = None,
1531
+ start_col: Annotated[
1532
+ Optional[int],
1533
+ typer.Option("--start-col", help="New start column (0-indexed)."),
1534
+ ] = None,
1535
+ end_col: Annotated[
1536
+ Optional[int],
1537
+ typer.Option("--end-col", help="New end column (exclusive)."),
1538
+ ] = None,
1539
+ ) -> None:
1540
+ """Update a filter view's title or range."""
1541
+ service = SheetsService()
1542
+ service.update_filter_view(
1543
+ spreadsheet_id=spreadsheet_id,
1544
+ filter_view_id=filter_view_id,
1545
+ title=title,
1546
+ start_row=start_row,
1547
+ end_row=end_row,
1548
+ start_col=start_col,
1549
+ end_col=end_col,
1550
+ )