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

get_all_records => provided headers #1527

Open
nick-youngblut opened this issue Oct 29, 2024 · 1 comment
Open

get_all_records => provided headers #1527

nick-youngblut opened this issue Oct 29, 2024 · 1 comment

Comments

@nick-youngblut
Copy link

get_all_records does not allow one to specify the headers if there is no header row. One can only skip rows via head or set the expected headers via expected_headers, but one cannot specify the header (column) names. This feature would be quite helpful when there is no header in the table.

@alifeee
Copy link
Collaborator

alifeee commented Oct 29, 2024

hi !! thanks for your request :)

the docs mention gspread.utils.to_records, which can be used like so:

apple red circle very
banana yellow stick quite
papaya seedy seedy
import gspread
SPREADSHEET_ID = "uhfa3897fu3a987f389a8jf38af"
gc = gspread.service_account(filename="./creds.json")
spreadsheet = gc.open_by_key(SPREADSHEET_ID)
worksheet = spreadsheet.sheet1

headers = ["fruit", "alternate name", "tastiness"]
values = worksheet.get()
records = gspread.utils.to_records(headers, values)
for r in records:
    print(r)
# {'fruit': 'apple', 'alternate name': 'red circle', 'tastiness': 'very'}
# {'fruit': 'banana', 'alternate name': 'yellow stick', 'tastiness': 'quite'}
# {'fruit': 'papaya', 'alternate name': 'seedy', 'tastiness': 'seedy'}

I believe this is what you desire.

However, it is clear the documentation was not clear in explaining this possibility. Perhaps it could do with an example.

Would you be willing to write an example to improve the documentation ?

Thanks !! :]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants