简体   繁体   English

如何使用谷歌 API 为 python 在特定文件夹下创建工作表?

[英]How to create a sheet under a specific folder with google API for python?

I am able to create a sheet with the code below in the root of 'My Drive' but how do I create the sheet under a folder in “My Drive” or “Shared drives”?我可以在“我的云端硬盘”的根目录中使用以下代码创建工作表,但是如何在“我的云端硬盘”或“共享驱动器”的文件夹下创建工作表?

from googleapiclient.discovery import build

service = build(‘sheets’, ‘v4’, credentials=creds)
sheet = service.spreadsheets()
body = {}
results = sheet.create(body=body).execute()
pprint(results)
  • You want to create new Spreadsheet in the specific folder.您想在特定文件夹中创建新的电子表格。
  • You want to achieve this using google-api-python-client with python.您想使用带有 python 的 google-api-python-client 来实现此目的。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样?

Issue:问题:

Unfortunately, in the current stage, new Spreadsheet cannot be directly created to the specific folder of Google Drive using Sheets API.不幸的是,在当前阶段,无法使用 Sheets API 将新的电子表格直接创建到 Google Drive 的特定文件夹中。 In this case, Drive API is required to be used.在这种情况下,需要使用驱动器 API。

Sample script:示例脚本:

Before you run the script, please set the folder ID.在运行脚本之前,请设置文件夹 ID。

Pattern 1:模式一:

In this pattern, the new Spreadsheet is directly created to the specific folder in your Google Drive.在这种模式下,新的电子表格会直接创建到您 Google Drive 中的特定文件夹中。 In order to create Spreadsheet, the mimeType of application/vnd.google-apps.spreadsheet is used.为了创建电子表格,使用了application/vnd.google-apps.spreadsheet的 mimeType。

Script: 脚本:
 drive = build('drive', 'v3', credentials=creds) file_metadata = { 'name': 'sampleName', 'parents': ['### folderId ###'], 'mimeType': 'application/vnd.google-apps.spreadsheet', } res = drive.files().create(body=file_metadata).execute() print(res)

Pattern 2:模式二:

In this pattern, after the new Spreadsheet is created by Sheets API, the Spreadsheet is moved to the specific folder in your Google Drive.在此模式中,在 Sheets API 创建新的电子表格后,电子表格将移动到您的 Google Drive 中的特定文件夹。

Script: 脚本:
 # Create Spreadsheet to the root folder. service = build('sheets', 'v4', credentials=creds) sheet = service.spreadsheets() body = {} results = sheet.create(body=body).execute() pprint(results) # Move the created Spreadsheet to the specific folder. drive = build('drive', 'v3', credentials=creds) folderId = '### folderId ###' res = drive.files().update(fileId=results['spreadsheetId'], addParents=folderId, removeParents='root').execute() print(res)

Note:笔记:

  • For both samples, please add a scope of https://www.googleapis.com/auth/drive .对于这两个示例,请添加https://www.googleapis.com/auth/drive的 scope 。 And when the scopes are added, please remove the created credential file including the refresh token and authorize again.添加范围后,请删除创建的包含刷新令牌的凭据文件并重新授权。 By this, the additional scopes are reflected to the refresh token.这样,额外的范围就会反映到刷新令牌中。
  • If you want to use the shared Drive, please modify as follows.如果您想使用共享云端硬盘,请进行如下修改。
    • For pattern 1对于模式 1
      • file_metadata = {'name': 'sampleName','parents': ['### folderId ###'],'mimeType': 'application/vnd.google-apps.spreadsheet','driveId': "###"}
      • res = drive.files().create(body=file_metadata, supportsAllDrives=True).execute()
    • For pattern 2对于模式 2
      • res = drive.files().update(fileId=results['spreadsheetId'], body={'driveId': "###"}, addParents=folderId, removeParents='root', supportsAllDrives=True).execute()

References:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

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

相关问题 Python - 通过 Google 表格创建 Google 表格并将其发布到网站 API - Python - Create & Publish Google Sheet to Website via Google Sheet API 如何使用 Google Drive API 和 Python 将文件上传到特定文件夹? - How to upload file into specific folder with Google Drive API and Python? 如何在 Python 中使用 Google Drive API 创建新文件夹? - How can I create a new folder with Google Drive API in Python? Python + 谷歌表 | 如何更新特定单元格 - Python + Google Sheet | How to Update specific cells python google api 创建有权限的文件夹 - python google api create folder with permission 用Python创建Google表格 - Create Google Sheet in Python 在驱动器中的特定文件夹中创建Google电子表格 谷歌驱动器api - create google spreadsheet in specific folder in drive | Google drive api Google drive api python - 如何将文件夹或文件夹中的所有文件下载到特定的本地目标路径 - Google drive api python - How to download all the files inside a folder or the folder to a specific local destination path 如何在Python中的特定文件夹中创建一个csv文件? - How to create a csv file in a specific folder in Python? 如何使用 Python 将文件插入/上传到 Google Drive API v3 中的特定文件夹 - How can I insert/upload files to a specific folder in Google Drive API v3 using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM