Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/get all record duplicated columns #1021

Merged
merged 3 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 23 additions & 3 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 @@ -399,6 +400,7 @@ def get_all_records(
:param str value_render_option: (optional) Determines how values should
be rendered in the the output. See `ValueRenderOption`_ in
the Sheets API.
:param list expected_headers: (optional) List of expected headers, they must be unique.
lavigne958 marked this conversation as resolved.
Show resolved Hide resolved

.. _ValueRenderOption: https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption
"""
Expand All @@ -412,9 +414,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
)
)
lavigne958 marked this conversation as resolved.
Show resolved Hide resolved
lavigne958 marked this conversation as resolved.
Show resolved Hide resolved

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