更改新创建的谷歌电子表格的默认共享

时间:2021-06-19 19:41:53

标签: python google-sheets google-drive-api google-sheets-api gspread

是否有任何选项或设置可以设置新创建的谷歌电子表格的默认共享,以便服务帐户可以使用 gspread api 读取它们?我不想手动共享每个新电子表格。

谢谢您的回答

1 个答案:

答案 0 :(得分:1)

我相信你的目标如下。

  • 来自Is there any option or setting to set the default sharing of newly created google spreadsheet, in order that a service account can read them using gspread api?
    • 您想创建使用服务帐户写入新电子表格的权限。
  • 您想使用 gspread 实现这一目标。
  • 您已经能够使用 Sheets API 获取和放置电子表格的值,此外,您还可以使用 Drive API。

在这种情况下,我认为gspread的insert_permission方法可能可以使用。当它用于电子表格时,它变成如下。 insert_permission 使用 Drive API。

示例脚本:

client = gspread.authorize(credentials) # Please use your "credentials" here.

newSpreadsheetTitle = 'sample' # Please set the new Spreadsheet title.
emailAddress = '###' # Please set the email address of the service account.
folderId = '###' # If you want to create new Spreadsheet to the specific folder. Please use this.

spreadsheetId = client.create(newSpreadsheetTitle, folder_id=folderId).id
client.insert_permission(spreadsheetId, emailAddress, 'user', 'writer')
  • 运行上述脚本时,会创建一个新的电子表格,并为创建的电子表格创建权限。

参考:

相关问题