如何使用Google API for python在特定文件夹下创建工作表? [英] How to create a sheet under a specific folder with google API for python?

查看:97
本文介绍了如何使用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)

推荐答案

  • 您要在特定文件夹中创建新的电子表格.
  • 您要使用带有python的google-api-python-client实现此功能.
  • 如果我的理解正确,那么这个答案如何?

    If my understanding is correct, how about this answer?

    很遗憾,在当前阶段,不能使用Sheets API将新的电子表格直接创建到Google云端硬盘的特定文件夹中.在这种情况下,必须使用Drive API.

    Unfortunately, in the current stage, new Spreadsheet cannot be directly created to the specific folder of Google Drive using Sheets API. In this case, Drive API is required to be used.

    在运行脚本之前,请设置文件夹ID.

    Before you run the script, please set the folder ID.

    在这种模式下,新的电子表格将直接创建到Google云端硬盘中的特定文件夹.为了创建电子表格,使用了application/vnd.google-apps.spreadsheet的mimeType.

    In this pattern, the new Spreadsheet is directly created to the specific folder in your Google Drive. In order to create Spreadsheet, the mimeType of application/vnd.google-apps.spreadsheet is used.

    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)
    

    模式2:

    在这种模式下,通过Sheets API创建新的电子表格后,电子表格将移至Google云端硬盘中的特定文件夹.

    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.

    # 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.添加范围后,请删除创建的凭证文件(包括刷新令牌),然后再次授权.这样,其他作用域就会反映到刷新令牌中.
    • 如果要使用共享驱动器,请进行以下修改.
      • 对于模式1
        • file_metadata = {'name': 'sampleName','parents': ['### folderId ###'],'mimeType': 'application/vnd.google-apps.spreadsheet','driveId': "###"}
        • res = drive.files().create(body=file_metadata, supportsAllDrives=True).execute()
        • Note:

          • For both samples, please add a scope of https://www.googleapis.com/auth/drive. 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
              • 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()
                • Files: create
                • Files: update

                如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

                If I misunderstood your question and this was not the direction you want, I apologize.

                这篇关于如何使用Google API for python在特定文件夹下创建工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆