gspread icon indicating copy to clipboard operation
gspread copied to clipboard

Export only one worksheet by it's Id from all document added

Open ShevchenkoVadim opened this issue 1 year ago • 6 comments

Added the ability to export one page from the entire document by ID

ShevchenkoVadim avatar Jul 16 '24 20:07 ShevchenkoVadim

you can run these commands to check that the workflow will pass :)

pip install -r lint-requirements.txt
tox -e lint
# optionally but you have not changed tests/docs
pip install -r docs/requirements.txt
pip install -r test-requirements.txt
tox -e doc # build docs
tox -e py # run tests

is this a feature that you think is possible to add a test for, or not?

alifeee avatar Jul 18 '24 09:07 alifeee

@alifeee what do you think of this detail ?

I'm not fully sure why this feature is desired. What can gspread currently do, and what is missing?

Could someone provide a (pseudo)code snippet example of what is desired with this function?

alifeee avatar Jul 23 '24 14:07 alifeee

@alifeee what do you think of this detail ?

I'm not fully sure why this feature is desired. What can gspread currently do, and what is missing?

I believe it's desired when you wish to export (to PDF, to CSV, ...) a single sheet instead of a the whole spreadsheet (which creates multiple pages PDF for example).

Could someone provide a (pseudo)code snippet example of what is desired with this function?

sure, I tried it, here it is :upside_down_face:

client = gspread.service_account()
file = client.open("xxxxxxxx")
res = file.sheet1.export(gspread.utils.ExportFormat.PDF)
with open("result.pdf", "wb") as fp:
    fp.write(res)

My major concerns are:

  1. the error handling, which returns HTML code, and not a JSON
  2. the exported format that are not fully covered in this MR. (this is easily fixed with a new Enum)

lavigne958 avatar Jul 23 '24 20:07 lavigne958

Hi I thought about this feature and I really don't like the use of an HTML base API. that is dedicated to server content for humans on a web browser and mostly because we can't handle errors from this URL.

it should not be part of gspread unfortunately until the API allows us to export a single sheet.

For that matter I still want to help you and I found a way for you to download your spreadsheet and extract a single sheet using this pure python library that can handle PDFs: https://github.com/py-pdf/pypdf

You can find the appropriate documentation here to read a pdf, choose page then write that page only to a new PDF file. https://pypdf.readthedocs.io/en/stable/user/cropping-and-transforming.html#cropping-and-transforming-pdfs

With this solution that can solve your problem I suggest we don't merge this feature but I still want to wait for @alifeee opinion on this, so what do you think ?

lavigne958 avatar Oct 20 '24 21:10 lavigne958

I see it being quite useful. I don't know what exists already to export a sheet (say, if you want to download PDF as a file). If you want a CSV or TSV, then you can probably just use worksheet.get just fine.

But, perhaps if you want a PDF, you would want to customise how it looks, so you would want to use the GUI on the Google Sheets web browser anyway.

As for errors: I do not know what errors exist when trying to export, apart from an incorrect format string (or if Google changes which are accepted)

So, in the end: I see this adding a nice feature, but at a cost of bad errors if anything goes wrong, which is a maintenance burden cost. I say we could leave this open, or close it without merging, and if other people come requesting the same feature, then we can reconsider.?

alifeee avatar Oct 21 '24 10:10 alifeee

I'm ok with that. Let's leave it open for now.

lavigne958 avatar Oct 22 '24 07:10 lavigne958