简体   繁体   English

电子表格未找到错误 - 尝试访问 Google 表格

[英]Spreadsheet Not Found Error - Trying to Access Google Sheets

I am testing this code.我正在测试这段代码。

import gspread
from oauth2client.service_account import ServiceAccountCredentials

# use creds to create a client to interact with the Google Drive API

scope = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive.readonly']

creds = ServiceAccountCredentials.from_json_keyfile_name('C:\\my_path\\client_secret.json', scope)
client = gspread.authorize(creds)

# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Test_Sheet").sheet1

# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)

I am getting this error:我收到此错误:

SpreadsheetNotFound

I followed the instructions from the link below.我按照以下链接中的说明进行操作。

https://medium.com/datadriveninvestor/use-google-sheets-as-your-database-using-python-77d40009860f https://medium.com/datadriveninvestor/use-google-sheets-as-your-database-using-python-77d40009860f

Perhaps I missed a step in the link, but I don't think so.也许我错过了链接中的一步,但我不这么认为。 Maybe something changed in GCP since that article was published, a little over 1 year ago.也许自从那篇文章发表后 GCP 发生了一些变化,大约一年前。 This is likely, as the cloud is a very dynamic place.这很有可能,因为云是一个非常动态的地方。 What is the best way to trouble shoot this kind of issue?解决此类问题的最佳方法是什么? I would really love to get this thing working, mostly as a learning exercise, but I can also see this being very useful, quite soon, in my line of work.我真的很想让这件事发挥作用,主要是作为一种学习练习,但我也很快就会看到这在我的工作中非常有用。 Thanks.谢谢。

Ok, I finally figured it out.好吧,我终于想通了。 Face plant here...在这里面对植物...

In the file named 'statup_funding.json', you have to grab the email that is generated, then click the 'Share' button on the Spreadsheet, and paste THAT EMAIL ADDRESS into the window that opens.在名为“statup_funding.json”的文件中,您必须获取生成的电子邮件,然后单击电子表格上的“共享”按钮,并将该电子邮件地址粘贴到打开的窗口中。 Then, everything works as expected.然后,一切都按预期进行。 Here is a final version of my now-working code.这是我现在工作代码的最终版本。

import gspread
#Service client credential from oauth2client
from oauth2client.service_account import ServiceAccountCredentials
# Print nicely
import pprint
#Create scope
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
#create some credential using that scope and content of startup_funding.json
creds = ServiceAccountCredentials.from_json_keyfile_name('startup_funding.json',scope)
#create gspread authorize using that credential
client = gspread.authorize(creds)
#Now will can access our google sheets we call client.open on StartupName
sheet = client.open('Test_Sheet').sheet1
pp = pprint.PrettyPrinter()
results = sheet.get_all_records()
results

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM