burnash / gspread

Google Sheets Python API
https://docs.gspread.org
MIT License
7.11k stars 948 forks source link

get_all_records => provided headers #1527

Open nick-youngblut opened 2 weeks ago

nick-youngblut commented 2 weeks ago

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 commented 2 weeks ago

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 !! :]

nick-youngblut commented 2 weeks ago

Thanks @alifeee for the info!

Could get_all_records be updated so that users can specify custom headers, so that developers can just use get_all_records for cases in which a header exists or does not?

alifeee commented 2 weeks ago

generally we are trying to stop feature creep for get_all_records. See https://github.com/burnash/gspread/issues/1367 and surrounding issues/PRs for more information, but we had to pull back the scope because of bugs.

There is potential that this feature is "simple enough" to be a good cause for addition, but I think the "workaround" (using utils.to_records is a perfectly fine solution, but perhaps needs more signposting in the documentation for the method (i.e., with an example) as I said

so... no ;]

lavigne958 commented 1 week ago

Hi as alifee said, we stopped adding features to get_all_records. We want it to be as simple as possible and it's already quiet complex as is.

Closing this issue.

alifeee commented 1 week ago

@lavigne958 thank you. But the creation of an issue is an example that our documentation has room for improvement, so we should improve it, thus I open the issue for this purpose

lavigne958 commented 1 week ago

Agreed, sorry for the prompt action. You're right 👍