Skip to content

Instantly share code, notes, and snippets.

@dat-adi
Last active January 17, 2021 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dat-adi/c8cc33a6209e1a9aeb8d03b5e46e6483 to your computer and use it in GitHub Desktop.
Save dat-adi/c8cc33a6209e1a9aeb8d03b5e46e6483 to your computer and use it in GitHub Desktop.
A small snippet of code to exhibit the functionality of Google Sheets in Python.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
#sheet access
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
# used to retrieve credentials from the *creds.json* file
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
client = gspread.authorize(creds)
# We're accessing the sheet 1 of the tester document
sheet = client.open("tester").sheet1
# retrieves all records
print("\nRetrieving all records...")
data = sheet.get_all_records()
pprint(data)
# retrieving specific row values
print("\nRetrieving information from row 3...")
row = sheet.row_values(3)
pprint(row)
# retrieving specific column values
print("\nRetrieving information from column 3...")
col = sheet.col_values(3)
pprint(col)
# retrieving specific cell
print("\nRetrieving value of a cell placed at (1, 2) in the sheet...")
cell = sheet.cell(1, 2).value
pprint(cell)
# inserting details into the sheet
print("\nInserting details into the sheet...")
insertDetails = ["Adithya", "testemail@gmail.com", "33YEP4554"]
sheet.insert_row(insertDetails, 1)
# updating details in the sheet
print("\nUpdating details in the sheet...")
sheet.update_cell(2, 2, "dat.adithya@gmail.com")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment