Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:param file_format: File format (:class:`ExportType`)
:param path: Path to where the file should be stored. (default: current working directory)
:param filename: Name of the file. (default: Spreadsheet Id)
"""
request = None
tmp = None
mime_type, file_extension = getattr(file_format, 'value', file_format).split(':')
if isinstance(sheet, Spreadsheet):
if (file_format == ExportType.CSV or file_format == ExportType.TSV) and len(sheet.worksheets()) > 1:
for worksheet in sheet:
self.export(worksheet, file_format, path=path, filename=filename + str(worksheet.index))
return
else:
request = self._export_request(sheet.id, mime_type)
elif isinstance(sheet, Worksheet):
if sheet.index != 0:
tmp = sheet.index
try:
sheet.index = 0
except HttpError:
raise Exception("Can only export first sheet in readonly mode")
request = self._export_request(sheet.spreadsheet.id, mime_type)
import io
file_name = str(sheet.id or tmp) + file_extension if filename is None else filename + file_extension
fh = io.FileIO(path + file_name, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
# logging.info('Download progress: %d%%.', int(status.progress() * 100)) TODO fix this
"""
import logging
import warnings
from pygsheets.worksheet import Worksheet
from pygsheets.datarange import DataRange
from pygsheets.exceptions import (WorksheetNotFound, RequestError,
InvalidArgumentValue, InvalidUser)
from pygsheets.custom_types import *
class Spreadsheet(object):
""" A class for a spreadsheet object."""
worksheet_cls = Worksheet
def __init__(self, client, jsonsheet=None, id=None):
"""The spreadsheet is used to store and manipulate metadata and load specific sheets.
:param client: The client which is responsible to connect the sheet with the remote.
:param jsonsheet: The json-dict representation of the spreadsheet as returned by Google Sheets API v4.
:param id: Id of this spreadsheet
"""
if type(jsonsheet) != dict and jsonsheet is not None:
raise InvalidArgumentValue("jsonsheet")
self.logger = logging.getLogger(__name__)
self.client = client
self._sheet_list = []
self._jsonsheet = jsonsheet
self._id = id
self._title = ''
:param rows: Number of rows which should be initialized (default 100)
:param cols: Number of columns which should be initialized (default 26)
:param src_tuple: Tuple of (spreadsheet id, worksheet id) specifying the worksheet to copy.
:param src_worksheet: The source worksheet.
:param index: Tab index of the worksheet.
:returns: :class:`Worksheets `.
"""
jsheet = dict()
if src_tuple:
jsheet['properties'] = self.client.sheet.sheets_copy_to(src_tuple[0], src_tuple[1], self.id)
wks = self.worksheet_cls(self, jsheet)
wks.title = title
elif src_worksheet:
if type(src_worksheet) != Worksheet:
raise InvalidArgumentValue("src_worksheet")
jsheet['properties'] = self.client.sheet.sheets_copy_to(src_worksheet.spreadsheet.id, src_worksheet.id, self.id)
wks = self.worksheet_cls(self, jsheet)
wks.title = title
else:
request = {"addSheet": {"properties": {'title': title, "gridProperties": {"rowCount": rows, "columnCount": cols}}}}
if index is not None:
request["addSheet"]["properties"]["index"] = index
result = self.client.sheet.batch_update(self.id, request, fields='replies/addSheet')
jsheet['properties'] = result['replies'][0]['addSheet']['properties']
wks = self.worksheet_cls(self, jsheet)
self._sheet_list.append(wks)
return wks
raise Exception("Can only export first sheet in readonly mode")
request = self._export_request(sheet.spreadsheet.id, mime_type)
import io
file_name = str(sheet.id or tmp) + file_extension if filename is None else filename + file_extension
fh = io.FileIO(path + file_name, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
# logging.info('Download progress: %d%%.', int(status.progress() * 100)) TODO fix this
logging.info('Download finished. File saved in %s.', path + file_name)
if tmp is not None:
sheet.index = tmp + 1
if isinstance(sheet, Worksheet):
sheet.refresh(False)