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

Sheet

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
  1. Check if the URL is of Google spreadsheet

  2. Check if the URL is shared

  3. Parse export format from the given output filename

  4. 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, and tsv.

  • Raise ValueError when 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 URL

  • Set 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 wget command

  • Output 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()