[英]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)
If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样?
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。
Before you run the script, please set the folder ID.在运行脚本之前,请设置文件夹 ID。
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。
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)
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)
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.这样,额外的范围就会反映到刷新令牌中。file_metadata = {'name': 'sampleName','parents': ['### folderId ###'],'mimeType': 'application/vnd.google-apps.spreadsheet','driveId': "###"}
res = drive.files().create(body=file_metadata, supportsAllDrives=True).execute()
res = drive.files().update(fileId=results['spreadsheetId'], body={'driveId': "###"}, addParents=folderId, removeParents='root', supportsAllDrives=True).execute()
If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.