使用单个"gspread"命令从多个 google 工作表中获取值



我写这段代码是为了访问一个名为"同学们"以及其中的两个工作表;学校;以及";拼贴;每个都具有文件句柄";ws1";以及";ws2";。

import gspread
jf=gspread.service_account("E:PyProjGsheetscreds.json")
wb=jf.open("Classmates")
ws1=wb.worksheet("School")
ws2=wb.worksheet("Collage")
inp=int(input("What data you want to gett:"))
if inp==1:
ws="ws1"
elif inp==2:
ws="ws2"
else:
print("Try again")
data=ws.get_all_values()        #line 14 with error
print(data)

现在,我试图根据选择从两张表中的任何一张表中获取_all_values。它给出以下错误

line 14, in <module>
data=ws.get_all_values()
AttributeError: 'str' object has no attribute 'get_all_values'

我不想写单独的代码来获取值。由于我使用的是Gspread,所以请从Gspread API参考而不是Google Sheets API中建议解决方案。谢谢

我试了一个变通办法,它能在上工作

import gspread
jf=gspread.service_account("E:PyProjGsheetscreds.json")
wb=jf.open("Classmates")
def getvalue(sheetname):
ws1=wb.worksheet(sheetname)
lis_of_name=ws1.get_all_values()
return lis_of_name

inp=int(input("What data you want to gett:"))
if inp==1:
data=getvalue("School")
elif inp==2:
data=getvalue("Collage")
else:
print("Try again")
print("BACK IN MAIN :-)")
print(data)

与其将文件句柄名称保存在";ws";,我发送工作表的名称作为工作表((的参数。

最新更新