使用 GSpread 在文件夹中创建电子表格

Dyl*_*gan 2 python google-sheets google-drive-api gspread google-sheets-api

我无法找到有关如何使用 GSpread 在某个 Google Drive 目录中创建 GSheet 的任何文档。

我检查了文档并查看了一些后端代码。

我目前正在使用下面的代码来创建电子表格:

worksheet = sh.add_worksheet(title='Overview', rows='100', cols='9')
Run Code Online (Sandbox Code Playgroud)

我希望能够在 google 驱动器上的目录中创建电子表格,例如:

X > Y > 电子表格

任何帮助将不胜感激,

干杯。

Tan*_*ike 8

  • 您想在特定文件夹中创建新的电子表格。
  • 您想使用 Python 来实现这一点。

如果我的理解是正确的,这个答案怎么样?

改装要点:

  • 不幸的是,Sheets API 无法实现这一点。在这种情况下,需要使用 Drive API。
  • 在您的脚本中,我认为您使用gspread.authorize()like gc = gspread.authorize(credentials)。在此修改中,credentials使用了。
  • 您问题中的脚本worksheet = sh.add_worksheet(title='Overview', rows='100', cols='9')用于向现有电子表格添加工作表。使用 gspread 创建新电子表格时,请使用sh = gc.create('A new spreadsheet').
    • 在这种情况下,将在根文件夹中创建新的电子表格。

准备:

在您使用以下脚本之前,请在 API 控制台启用 Drive API,并请添加https://www.googleapis.com/auth/drive. 如果您使用的范围是https://www.googleapis.com/auth/drive.file,请使用这个,范围不需要修改为https://www.googleapis.com/auth/drive

  • 如果您使用的是 OAuth2,请删除包含刷新令牌的文件。然后,请运行脚本并再次重新授权。这样,添加的范围就会反映到访问令牌中。

  • 如果您使用的是服务帐户,则不需要删除该文件。

模式一:

以下示例脚本为特定文件夹创建新电子表格。

示例脚本:

from apiclient import discovery

destFolderId = '###'  # Please set the destination folder ID.
title = '###'  # Please set the Spreadsheet name.

drive_service = discovery.build('drive', 'v3', credentials=credentials)  # Use "credentials" of "gspread.authorize(credentials)".
file_metadata = {
    'name': title,
    'mimeType': 'application/vnd.google-apps.spreadsheet',
    'parents': [destFolderId]
}
file = drive_service.files().create(body=file_metadata).execute()
print(file)
Run Code Online (Sandbox Code Playgroud)

模式2:

如果要将现有电子表格移动到特定文件夹,请使用以下脚本。

示例脚本:

from apiclient import discovery

spreadsheetId = '###'  # Please set the Spreadsheet ID.
destFolderId = '###'  # Please set the destination folder ID.

drive_service = discovery.build('drive', 'v3', credentials=credentials)  # Use "credentials" of "gspread.authorize(credentials)".
# Retrieve the existing parents to remove
file = drive_service.files().get(fileId=spreadsheetId,
                                 fields='parents').execute()
previous_parents = ",".join(file.get('parents'))
# Move the file to the new folder
file = drive_service.files().update(fileId=spreadsheetId,
                                    addParents=destFolderId,
                                    removeParents=previous_parents,
                                    fields='id, parents').execute()
Run Code Online (Sandbox Code Playgroud)

参考:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

编辑:

当您要共享文件夹时,请使用以下脚本。

示例脚本:

drive_service = discovery.build('drive', 'v3', credentials=credentials)  # Use "credentials" of "gspread.authorize(credentials)".
folderId = "###"  # Please set the folder ID.
permission = {
    'type': 'user',
    'role': 'writer',
    'emailAddress': '###',  # Please set the email address of the user that you want to share.
}
res = drive_service.permissions().create(fileId=folderId, body=permission).execute()
print(res)
Run Code Online (Sandbox Code Playgroud)

参考: