Currently I can connect using gspread and create/share a spreadsheet. I can also read data from said spreadsheet but I have to request it by name. Is there a way to list of all spreadsheets shared with the OAuth account being used.
I may not know all the names of the spreadsheets that will be shared with the account so I want to have them listed off programmatically if possible.
All I have been able to do so far is select a sheet by name. But I need to be able to pull a list of all sheets shared with my service_account
.
Everything I have found so far online is related to getting a list of sheets in a workbook but not a list of all workbooks available.
Documentation is welcome though all the stuff I have found so far has not helped.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import tkinter as tk
class Main(tk.Tk):
def __init__(self):
super().__init__()
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.geometry('1000x400')
self.txt = tk.Text(self)
self.txt.grid(row=0, column=0, sticky='nsew')
self.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"]
self.txt.insert('end', '{}\n'.format(self.scope))
print('Scope: ', self.scope)
self.after(2000, self.testing)
def testing(self):
creds = ServiceAccountCredentials.from_json_keyfile_name('creds.json', self.scope)
client = gspread.authorize(creds)
try:
sh = client.create('Created By App')
sh.share('my.account@workemail.com', perm_type='user', role='writer')
except Exception as e:
print('Error: ', e)
try:
print('Client: ', client)
try:
self.txt.insert('end', '{}\n'.format('Running: client.list_permissions("TestingSheet")'))
self.txt.insert('end', '{}\n\n'.format(client.list_permissions("TestingSheet")))
except Exception as e:
self.txt.insert('end', 'Error: {}\n\n'.format(e))
print('Error: ', e)
try:
self.txt.insert('end', '{}\n'.format('Running: client.list_spreadsheet_files()'))
self.txt.insert('end', '{}\n\n'.format(client.list_spreadsheet_files()))
except Exception as e:
self.txt.insert('end', 'Error: {}\n\n'.format(e))
print('Error: ', e)
try:
self.txt.insert('end', '{}\n'.format('Running: client.session()'))
self.txt.insert('end', '{}\n\n'.format(client.session()))
except Exception as e:
self.txt.insert('end', 'Error: {}\n\n'.format(e))
print('Error: ', e)
# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("TestingSheet").sheet1
self.txt.insert('end', '{}\n\n'.format(sheet))
print('Sheet: ', sheet)
# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
self.txt.insert('end', '{}\n\n'.format(list_of_hashes))
print('list_of_hashes: ', list_of_hashes)
except Exception as e:
self.txt.insert('end', 'Error: {}\n\n'.format(e))
print('Error: ', e)
creds.json is formated like this:
{
"type": "service_account",
"project_id": "XXXXXXXXXXXXXXXXXXXXXXXX",
"private_key_id": "XXXXXXXXXXXXXXXXXXXXXXXX",
"private_key": "-----BEGIN PRIVATE KEY-----\nXXXXXXXXXXXXXXXXXXXXXXXX\n-----END PRIVATE KEY-----\n",
"client_email": "project-service-account@XXXXXXXXXXXXXXXXXXXXXXXX.iam.gserviceaccount.com",
"client_id": "XXXXXXXXXXXXXXXXXXXXXXXX",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/project-service-account@XXXXXXXXXXXXXXXXXXXXXXXX.iam.gserviceaccount.com"}