Skip to content

Commit

Permalink
Merge pull request #1353 from burnash/1352-expected-headers
Browse files Browse the repository at this point in the history
fix 1352 expected headers broken
  • Loading branch information
alifeee authored Nov 20, 2023
2 parents 385e5d0 + 02484c2 commit 0932358
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 260 deletions.
26 changes: 12 additions & 14 deletions gspread/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ def get_records( # noqa: C901 # this comment disables the complexity check for
the Sheets API.
:type value_render_option: :namedtuple:`~gspread.utils.ValueRenderOption`
:param list expected_headers: (optional) List of expected headers, they must be unique.
:param list expected_headers: (optional) Set this to allow reading a spreadsheet with duplicate headers. Set this to a list of unique headers that you want to read. Other headers not included in this list may be overwritten and data lost.
.. note::
Expand Down Expand Up @@ -660,26 +660,24 @@ def get_records( # noqa: C901 # this comment disables the complexity check for
)[0]

if expected_headers is None:
expected_headers = keys
# all headers must be unique
header_row_is_unique = len(keys) == len(set(keys))
if not header_row_is_unique:
raise GSpreadException("the header row in the worksheet is not unique")
else:
# all expected headers must be unique
expected_headers_are_unique = len(expected_headers) == len(
set(expected_headers)
)
if not expected_headers_are_unique:
raise GSpreadException("the given 'expected_headers' are not uniques")

# validating the headers in the worksheet
header_row_is_unique = len(keys) == len(set(keys))
if not header_row_is_unique:
raise GSpreadException("the header row in the worksheet is not unique")

# validating that the expected headers are part of the headers in the worksheet
if not all(header in keys for header in expected_headers):
raise GSpreadException(
"the given 'expected_headers' contains unknown headers: {}".format(
set(expected_headers) - set(keys)
# expected headers must be a subset of the actual headers
if not all(header in keys for header in expected_headers):
raise GSpreadException(
"the given 'expected_headers' contains unknown headers: {}".format(
set(expected_headers) - set(keys)
)
)
)

values = self.get_values(
"{first_index}:{last_index}".format(
Expand Down
Loading

0 comments on commit 0932358

Please sign in to comment.