Converts a range defined in A1 notation to a dict representing a `GridRange`_. All indexes are zero-based. Indexes are half open, e.g the start index is inclusive and the end index is exclusive: [startIndex, endIndex). Missing indexes indicate the range is unbounded on that side.
(name: str, sheet_id: Optional[int] = None)
| 470 | |
| 471 | |
| 472 | def a1_range_to_grid_range(name: str, sheet_id: Optional[int] = None) -> Dict[str, int]: |
| 473 | """Converts a range defined in A1 notation to a dict representing |
| 474 | a `GridRange`_. |
| 475 | |
| 476 | All indexes are zero-based. Indexes are half open, e.g the start |
| 477 | index is inclusive and the end index is exclusive: [startIndex, endIndex). |
| 478 | |
| 479 | Missing indexes indicate the range is unbounded on that side. |
| 480 | |
| 481 | .. _GridRange: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/other#GridRange |
| 482 | |
| 483 | Examples:: |
| 484 | |
| 485 | >>> a1_range_to_grid_range('A1:A1') |
| 486 | {'startRowIndex': 0, 'endRowIndex': 1, 'startColumnIndex': 0, 'endColumnIndex': 1} |
| 487 | |
| 488 | >>> a1_range_to_grid_range('A3:B4') |
| 489 | {'startRowIndex': 2, 'endRowIndex': 4, 'startColumnIndex': 0, 'endColumnIndex': 2} |
| 490 | |
| 491 | >>> a1_range_to_grid_range('A:B') |
| 492 | {'startColumnIndex': 0, 'endColumnIndex': 2} |
| 493 | |
| 494 | >>> a1_range_to_grid_range('A5:B') |
| 495 | {'startRowIndex': 4, 'startColumnIndex': 0, 'endColumnIndex': 2} |
| 496 | |
| 497 | >>> a1_range_to_grid_range('A1') |
| 498 | {'startRowIndex': 0, 'endRowIndex': 1, 'startColumnIndex': 0, 'endColumnIndex': 1} |
| 499 | |
| 500 | >>> a1_range_to_grid_range('A') |
| 501 | {'startColumnIndex': 0, 'endColumnIndex': 1} |
| 502 | |
| 503 | >>> a1_range_to_grid_range('1') |
| 504 | {'startRowIndex': 0, 'endRowIndex': 1} |
| 505 | |
| 506 | >>> a1_range_to_grid_range('A1', sheet_id=0) |
| 507 | {'sheetId': 0, 'startRowIndex': 0, 'endRowIndex': 1, 'startColumnIndex': 0, 'endColumnIndex': 1} |
| 508 | """ |
| 509 | start_label, _, end_label = name.partition(":") |
| 510 | |
| 511 | start_row_index, start_column_index = _a1_to_rowcol_unbounded(start_label) |
| 512 | |
| 513 | end_row_index, end_column_index = _a1_to_rowcol_unbounded(end_label or start_label) |
| 514 | |
| 515 | if start_row_index > end_row_index: |
| 516 | start_row_index, end_row_index = end_row_index, start_row_index |
| 517 | |
| 518 | if start_column_index > end_column_index: |
| 519 | start_column_index, end_column_index = end_column_index, start_column_index |
| 520 | |
| 521 | grid_range = { |
| 522 | "startRowIndex": start_row_index - 1, |
| 523 | "endRowIndex": end_row_index, |
| 524 | "startColumnIndex": start_column_index - 1, |
| 525 | "endColumnIndex": end_column_index, |
| 526 | } |
| 527 | |
| 528 | filtered_grid_range: Dict[str, int] = { |
| 529 | key: value for (key, value) in grid_range.items() if isinstance(value, int) |
no test coverage detected
searching dependent graphs…