Skip to content

Instantly share code, notes, and snippets.

@zevaverbach
Created April 14, 2017 17:05
Show Gist options
  • Save zevaverbach/71eed69c399432ba22ed673bbdd81b82 to your computer and use it in GitHub Desktop.
Save zevaverbach/71eed69c399432ba22ed673bbdd81b82 to your computer and use it in GitHub Desktop.
Get the value of the first cell of a named range from Google Sheets.
import pygsheets
def get_value_of_named_range_from_google_sheet(sheet_name: str, worksheet_title: str, range_name: str) -> str:
"""return the value of the first cell from a named range
great for treating Google Sheets NamedRanges as variables in Python scripts"""
# https://pygsheets.readthedocs.io/en/latest/authorizing.html
gc = pygsheets.authorize(outh_file='client_secret.json', outh_nonlocal=True)
sh = gc.open(sheet_name)
wks = sh.worksheet_by_title(worksheet_title)
named_range = wks.get_named_ranges(range_name)
if named_range:
return named_range[0][0].value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment