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

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions gspread/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,33 @@ def export(self, file_id: str, format: str = ExportFormat.PDF) -> bytes:

return self.http_client.export(file_id=file_id, format=format)

def export_worksheet(self, file_id, sheet_id: str, format: str = ExportFormat.PDF) -> bytes:
"""Export the spreadsheet in the given format.

:param str file_id: The key of the spreadsheet to export

:param str sheet_id: The key of the worksheet to export

:param str format: The format of the resulting file.
Possible values are:

* ``ExportFormat.PDF``
* ``ExportFormat.EXCEL``
* ``ExportFormat.CSV``
* ``ExportFormat.OPEN_OFFICE_SHEET``
* ``ExportFormat.TSV``
* ``ExportFormat.ZIPPED_HTML``

See `ExportFormat`_ in the Drive API.

:type format: :class:`~gspread.utils.ExportFormat`

:returns bytes: The content of the exported file.

.. _ExportFormat: https://developers.google.com/drive/api/guides/ref-export-formats
"""
return self.http_client.export_worksheet(file_id, sheet_id, format)

def copy(
self,
file_id: str,
Expand Down
52 changes: 52 additions & 0 deletions gspread/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
SPREADSHEET_VALUES_BATCH_URL,
SPREADSHEET_VALUES_CLEAR_URL,
SPREADSHEET_VALUES_URL,
SPREADSHEET_DRIVE_URL,
)
from .utils import ExportFormat, convert_credentials, quote

Expand Down Expand Up @@ -358,6 +359,57 @@ def export(self, file_id: str, format: str = ExportFormat.PDF) -> bytes:
r = self.request("get", url, params=params)
return r.content

def export_worksheet(self, file_id, sheet_id: str, format: str = ExportFormat.PDF) -> bytes:
"""Export the worksheet in the given format.

:param str file_id: The key of the spreadsheet to export

:param str sheet_id: The key of the worksheet to export

:param str format: The format of the resulting file.
Possible values are:

* ``ExportFormat.PDF``
* ``ExportFormat.EXCEL``
* ``ExportFormat.CSV``
* ``ExportFormat.OPEN_OFFICE_SHEET``
* ``ExportFormat.TSV``
* ``ExportFormat.ZIPPED_HTML``

See `ExportFormat`_ in the Drive API.

:type format: :class:`~gspread.utils.ExportFormat`

:returns bytes: The content of the exported file.

.. _ExportFormat: https://developers.google.com/drive/api/guides/ref-export-formats
"""

if format not in ExportFormat:
raise UnSupportedExportFormat

if format not in ExportFormat:
raise UnSupportedExportFormat
alifeee marked this conversation as resolved.
Show resolved Hide resolved

format_str = "pdf"
if format == ExportFormat.PDF:
format_str = "pdf"
Comment on lines +391 to +393
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the default value is PDf, then you don't have to check if it's PDF in format

elif format == ExportFormat.CSV:
format_str = "csv"
elif format == ExportFormat.ZIPPED_HTML:
format_str = "zip"
Comment on lines +396 to +397
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one does not work.
as well as you don't handle the value: OPEN_OFFICE_SHEET which results as PDF due to default value set at the start of the ifs here.

elif format == ExportFormat.EXCEL:
format_str = "xlsx"
elif format == ExportFormat.TSV:
format_str = "tsv"
lavigne958 marked this conversation as resolved.
Show resolved Hide resolved

url = "{}/export?gid={}&format={}".format(SPREADSHEET_DRIVE_URL % file_id, sheet_id, format_str)
lavigne958 marked this conversation as resolved.
Show resolved Hide resolved

params: ParamsType = {"mimeType": format}

r = self.request("get", url, params=params)
return r.content

def insert_permission(
self,
file_id: str,
Expand Down
24 changes: 24 additions & 0 deletions gspread/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
numericise_all,
rowcol_to_a1,
to_records,
ExportFormat,
)

if TYPE_CHECKING is True:
Expand Down Expand Up @@ -295,6 +296,29 @@ def _get_sheet_property(self, property: str, default_value: Optional[T]) -> T:

return sheet.get(property, default_value)

def export(self, format=ExportFormat.PDF):
"""Export the worksheet in the given format.

:param format: The format of the resulting file.
Possible values are:

``ExportFormat.PDF``,
``ExportFormat.EXCEL``,
``ExportFormat.CSV``,
``ExportFormat.OPEN_OFFICE_SHEET``,
``ExportFormat.TSV``,
and ``ExportFormat.ZIPPED_HTML``.

See `ExportFormat`_ in the Drive API.
Default value is ``ExportFormat.PDF``.
:type format: :class:`~gspread.utils.ExportFormat`

:returns bytes: The content of the exported file.

.. _ExportFormat: https://developers.google.com/drive/api/guides/ref-export-formats
"""
return self.client.export_worksheet(self.spreadsheet_id, str(self.id), format)

def acell(
self,
label: str,
Expand Down