В настоящее время я могу подключиться с помощью gspread и создать / поделиться таблицей. Я также могу читать данные из указанной таблицы, но мне нужно запрашивать их по имени. Есть ли способ составить список всех электронных таблиц, к которым используется учетная запись OAuth.
Я могу не знать всех имен таблиц, к которым будет предоставлен доступ к учетной записи, поэтому я хочу, чтобы они указывались программно, если это возможно.
Все, что мне удалось сделать, это выбрать лист по имени. Но мне нужно получить список всех листов, к которым мой service_account
.
Все, что я нашел до сих пор в Интернете, связано с получением списка листов в книге, но не списка всех доступных книг.
Документация приветствуется, хотя все, что я нашел до сих пор, не помогло.
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('[email protected]', 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 имеет следующий формат:
{
"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"}