Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Working with the `google-auth` module
import gspread
from google.oauth2 import service_account
from pprint import pprint
# used to retrieve credentials from the *creds.json* file
creds = service_account.Credentials.from_service_account_file("creds.json")
scoped_credentials = creds.with_scopes(
[
"https://spreadsheets.google.com/feeds",
'https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive"
]
)
client = gspread.authorize(scoped_credentials)
# 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