Python APIv4谷歌工作表按名称打开工作表

Python APIv4谷歌工作表按名称打开工作表,python,google-sheets,google-drive-api,Python,Google Sheets,Google Drive Api,如何使用新的APIv4按名称而不是按ID打开工作表?似乎所有示例都有spreadsheetID=key,但不知道如何按名称执行。我需要按姓名打开工作表并获取ID 基本上,我要做的是使用下面的代码在复制新工作表后对其进行操作: drive.auth.service.files().copy(fileId=g.tkey,body={“parents”:[{“kind”:“drive#fileLink”,“id”:g.mfolder}],“title”:copy_name})。execute() 新工

如何使用新的APIv4按名称而不是按ID打开工作表?似乎所有示例都有
spreadsheetID=key
,但不知道如何按名称执行。我需要按姓名打开工作表并获取ID

基本上,我要做的是使用下面的代码在复制新工作表后对其进行操作:

drive.auth.service.files().copy(fileId=g.tkey,body={“parents”:[{“kind”:“drive#fileLink”,“id”:g.mfolder}],“title”:copy_name})。execute()


新工作表的名称为
copy\u name
,如何获取该工作表的ID?

在Google Sheets API v4上,它无法从文件名直接打开电子表格。工作表API v4的所有API都要求电子表格ID使用工作表。但您可以使用文件名检索电子表格ID。因此,您可以通过使用文件名检索电子表格ID,从文件名打开电子表格

为了从文件名中检索电子表格ID,它使用。python脚本如下所示。驱动器API v3用于此。它可以添加一个作用域(
https://www.googleapis.com/auth/drive.file

脚本:

res = service.files().list(q="trashed=false and name='" + copy_name + "'", fields="files(id)").execute()
{'files': [{'id': '#####'}]}
结果:

res = service.files().list(q="trashed=false and name='" + copy_name + "'", fields="files(id)").execute()
{'files': [{'id': '#####'}]}

您可以将结果检索为JSON。使用此ID,您可以使用电子表格。

我得到一个
无效查询“
HttpError。我找到了。。。它应该是
res=drive.auth.service.files().list(q=“title=”+copy\u name+”,fields=“items(id)”).execute()
。可能是API的不同版本。字段语法也已更改。使用
title
items
作为文件名,数组为版本2。“对不起,我没注意到。”皮诺伊谢谢你的帮助。我忘了垃圾箱的事了。