使用GSpred在文件夹中创建电子表格 [英] Creating A Spreadsheet In A Folder With GSpread

查看:16
本文介绍了使用GSpred在文件夹中创建电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找到有关如何使用GSpred在特定Google Drive目录中创建GSheet的任何文档时遇到问题。

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

我当前使用以下代码创建电子表格:

worksheet = sh.add_worksheet(title='Overview', rows='100', cols='9')

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

X>Y>电子表格

如有任何帮助,我们将不胜感激

干杯。

推荐答案

  • 您要在特定文件夹中创建新的电子表格。
  • 您希望使用Python来实现此目的。

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

修改点:

  • 遗憾的是,Sheets API无法做到这一点。这种情况下,需要使用Drive API。
  • 在您的脚本中,我认为您使用了gspread.authorize()Likegc = gspread.authorize(credentials)。在此修改中,使用credentials
  • 您的问题worksheet = sh.add_worksheet(title='Overview', rows='100', cols='9')中的脚本用于向现有电子表格添加工作表。当您使用Gspend创建新的电子表格时,请使用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,请删除包含刷新标记的文件。然后,请运行该脚本并重新授权。这样,添加的作用域将反映到访问令牌。

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

模式1:

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

示例脚本:

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)

模式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()

引用:

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

编辑:

如果要共享该文件夹,请使用以下脚本。

示例脚本:

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)

参考:

这篇关于使用GSpred在文件夹中创建电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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