snapsheets.sheetο
Sheet class
- Example:
from snapsheets.sheet import Sheet
url = "https://docs.google.com/spreadsheets/d/16Sc_UgShNuxMfRnBiFsjmfThE1VfVhJf3jgmxNvFeEI/edit#gid=2015536778"
filename = "sample.csv"
description = "Sample spreadsheet for snapsheets"
datefmt = "%Y"
skip = False
s = Sheet(url=url, filename=filename, description=description, datefmt=datefmt, skip=skip)
s.snapshot()
# (printed to stdout)
# π£ Sample spreadsheet for snapsheets
# π€ Downloaded as sample.csv
# π Renamed to 2022_sample.csv
Module Contentsο
Classesο
A class for single spreadsheet |
APIο
- class snapsheets.sheet.Sheetο
A class for single spreadsheet
- url: strο
None
Copy and Pasted URL of Google Spreadsheet.
- description: strο
None
Description of the sheet.
- filename: strο
None
Output filename.
- datefmt: strο
β%Y%m%dβ
Format of datetime prefix for backup filename. (default: β%Y%m%dβ)
- skip: boolο
False
Set to True if you want to skip. (default: False)
- __post_init__() Noneο
Check if the URL is of Google spreadsheet
Check if the URL is shared
Parse export format from the given output filename
Parse key and gid from the given URL
- validate_url() Noneο
Validate the provided Google Sheets URL.
Ensure the URL is a Google Sheets URL.
Use urlparse to break down the URL into its components.
Check if the netloc (domain) is βdocs.google.comβ.
Check if the URL is accessible and shared.
Use requests.get() to attempt to access the URL.
Check if the URL is valid and whether the document is shared.
If the URL is invalid or unshared (response.ok is False), log an error and sets self.skip to True.
- get_fmt() strο
Parse and validate suffix of given output filename.
Available suffix is
xlsx,ods,csv, andtsv.Raise
ValueErrorwhen the given suffix does not match above.
Returns
str suffix of output filename
Raises
ValueError
- get_key() strο
Parse
key(=spreadsheet ID) from given URL.Returns
str spreadsheet ID
- get_gid() strο
Parse
gid(=sheet ID) from given URLSet
gid=0(=Sheet1) if not found.
Returns
str sheet ID
- get_export_url() strο
Generate export URL from given arguments.
Note
input: `https://docs.google.com/spreadsheets/d/KEY/editβ
export: `https://docs.google.com/spreadsheets/d/KEY/exportβ
Returns
str export URL
- download() Noneο
Download spreadsheet.
Download using
wgetcommandOutput filename can be configured with CLI option and config file.
- backup() Noneο
Rename downloaded file
Prefix is added to the filename using current datetime.
A datetime format of prefix can be configured with CLI option and config file.
- snapshot() Noneο
Run
download()&backup()