gspread icon indicating copy to clipboard operation
gspread copied to clipboard

get_all_records => provided headers

Open nick-youngblut opened this issue 1 year ago • 6 comments

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.

nick-youngblut avatar Oct 29 '24 00:10 nick-youngblut

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

alifeee avatar Oct 29 '24 14:10 alifeee

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?

nick-youngblut avatar Oct 30 '24 15:10 nick-youngblut

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 ;]

alifeee avatar Oct 30 '24 19:10 alifeee

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.

lavigne958 avatar Oct 31 '24 08:10 lavigne958

@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

alifeee avatar Oct 31 '24 09:10 alifeee

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

lavigne958 avatar Oct 31 '24 09:10 lavigne958