Skip to content

Commit 255cf84

Browse files
authored
Merge pull request #1461 from nbwzx/dev
Some updates on `get_notes`
2 parents 4eb7c6d + 24411dc commit 255cf84

File tree

4 files changed

+1302
-1887
lines changed

4 files changed

+1302
-1887
lines changed

gspread/worksheet.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,24 +2606,46 @@ def unmerge_cells(self, name: str) -> JSONResponse:
26062606

26072607
return self.client.batch_update(self.spreadsheet_id, body)
26082608

2609-
def get_notes(self, default_empty_value: Optional[str] = None) -> List[List[str]]:
2610-
"""Returns a list of lists containing all notes in the sheet, or the empty list if the
2611-
sheet does not have a note.
2609+
def get_notes(self, default_empty_value: Optional[str] = "") -> List[List[str]]:
2610+
"""Returns a list of lists containing all notes in the sheet.
26122611
26132612
.. note::
26142613
2615-
The resulting matrix is as long as the last row holding a note
2616-
(could be smaller than the last data row)
2614+
The resulting matrix is not necessarily square.
2615+
The matrix is as tall as the last row with a note,
2616+
and each row is only as long as the last column in that row with a note.
26172617
2618-
The resulting matrix is as large as the last column holding a note
2619-
(could be smaller than the last data column)
2618+
2619+
Please see the example below.
2620+
To ensure it is square, use `gspread.utils.fill_gaps`,
2621+
for example like `utils.fill_gaps(arr, len(arr), max(len(a) for a in arr), None)`
26202622
26212623
:param str default_empty_value: (optional) Determines which value to use
26222624
for cells without notes, defaults to None.
2625+
2626+
Examples::
2627+
2628+
# Note data:
2629+
# A B
2630+
# 1 A1 -
2631+
# 2 - B2
2632+
2633+
# Read all notes from the sheet
2634+
>>> arr = worksheet.get_notes()
2635+
>>> print(arr)
2636+
[
2637+
["A1"],
2638+
["", "B2"]
2639+
]
2640+
>>> print(gspread.utils.fill_gaps(arr, len(arr), max(len(a) for a in arr), None))
2641+
[
2642+
["A1", ""],
2643+
["", "B2"]
2644+
]
26232645
"""
26242646
params: ParamsType = {"fields": "sheets.data.rowData.values.note"}
26252647
res = self.client.spreadsheets_get(self.spreadsheet_id, params)
2626-
data = res["sheets"][self.index]["data"][0].get("rowData", [])
2648+
data = res["sheets"][self.index]["data"][0].get("rowData", [{}])
26272649
notes: List[List[str]] = []
26282650
for row in data:
26292651
notes.append([])

0 commit comments

Comments
 (0)