使用Drive API在Google云端硬盘中创建空白电子表格(在Python中) [英] Creating empty spreadsheets in Google Drive using Drive API (in Python)

本文介绍了使用Drive API在Google云端硬盘中创建空白电子表格(在Python中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Google云端硬盘中创建一个空的Google表格(仅用元数据创建)。当我参考Google SpreadSheet API 文档时,它说要使用DocumentsList API,但它已被弃用,而是要求我使用Google Drive API。在Drive API文档中,我找不到任何方法来创建空白工作表。任何人都有关于如何做到这一点的线索?

解决方案

您可以使用 Drive API ,方法是设置 MIME type to application / vnd.google-apps.spreadsheet



在Python中执行此操作:

  from apiclient.discovery import build 
service = build('drive' ,'v2')

导入httplib2
凭证= ...#获取OAuth 2.0凭证
http = credentials.authorize(httplib2.Http())

body = {
'mimeType':'application / vnd.google-apps.spreadsheet',
'title':'电子表格名称',
}
文件= service.files()。insert(body = body).execute(http = http)
#或者对于版本3,它将是
#file = service.files()。create(body = body ).execute(http = http)

转到 Google APIs Explorer 来试用它!


I want to create an empty Google Sheet (created only with metadata) in Google Drive. When I referred to the Google SpreadSheet API documentation, it says to use the DocumentsList API, but it's deprecated and instead asks me to use the Google Drive API. In the Drive API docs, I could not find any way to create an empty Sheet. Anyone have a clue on how to do this?

解决方案

You can do this using the Drive API by setting the MIME type to application/vnd.google-apps.spreadsheet:

To do this in Python:

from apiclient.discovery import build
service = build('drive', 'v2')

import httplib2
credentials = ... # Obtain OAuth 2.0 credentials
http = credentials.authorize(httplib2.Http())

body = {
  'mimeType': 'application/vnd.google-apps.spreadsheet',
  'title': 'Name of Spreadsheet',
}
file = service.files().insert(body=body).execute(http=http)
# or for version 3 it would be
# file = service.files().create(body=body).execute(http=http)

Head over to the Google APIs Explorer to try it out!

这篇关于使用Drive API在Google云端硬盘中创建空白电子表格(在Python中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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