Skip to content

Commit

Permalink
Merge pull request #1021 from burnash/bugfix/get_all_record_duplicate…
Browse files Browse the repository at this point in the history
…d_columns

Bugfix/get all record duplicated columns
  • Loading branch information
lavigne958 committed Apr 12, 2022
2 parents eb9eb72 + 691e25c commit 610ad86
Show file tree
Hide file tree
Showing 3 changed files with 885 additions and 10 deletions.
36 changes: 32 additions & 4 deletions gspread/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ def get_all_records(
allow_underscores_in_numeric_literals=False,
numericise_ignore=None,
value_render_option=None,
expected_headers=None,
):
"""Returns a list of dictionaries, all of them having the contents of
the spreadsheet with the head row as keys and each of these
Expand All @@ -400,7 +401,16 @@ def get_all_records(
be rendered in the the output. See `ValueRenderOption`_ in
the Sheets API.
.. _ValueRenderOption: https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption
.. note::
ValueRenderOption: https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption
:param list expected_headers: (optional) List of expected headers, they must be unique.
.. note::
returned dictionaries will contain all headers even if not included in this list
"""
idx = head - 1

Expand All @@ -412,9 +422,27 @@ def get_all_records(

keys = data[idx]

# Check keys are uniques
if len(keys) != len(set(keys)):
raise GSpreadException("headers must be uniques")
# if no given expected headers, expect all of them
if expected_headers is None:
expected_headers = keys

# keys must:
# - be uniques
# - be part of the complete header list
# - not contain extra headers
expected = set(expected_headers)
headers = set(keys)

# make sure they are uniques
if len(expected) != len(expected_headers):
raise GSpreadException("the given 'expected_headers' are not uniques")

if not expected & headers == expected:
raise GSpreadException(
"the given 'expected_headers' contains unknown headers: {}".format(
expected & headers
)
)

if numericise_ignore == ["all"]:
values = data[idx + 1 :]
Expand Down

0 comments on commit 610ad86

Please sign in to comment.