Python 使用驱动API在Google Drive中创建空电子表格
我想在Google Drive中创建一个空的Google工作表(仅使用元数据创建)。当我提到Google文档时,它说要使用DocumentsList API,但它不推荐使用,而是要求我使用Google Drive API。在Drive API文档中,我找不到任何创建空工作表的方法。有人知道怎么做吗?您可以通过将应用程序设置为Python 使用驱动API在Google Drive中创建空电子表格,python,google-drive-api,google-sheets-api,google-api-python-client,Python,Google Drive Api,Google Sheets Api,Google Api Python Client,我想在Google Drive中创建一个空的Google工作表(仅使用元数据创建)。当我提到Google文档时,它说要使用DocumentsList API,但它不推荐使用,而是要求我使用Google Drive API。在Drive API文档中,我找不到任何创建空工作表的方法。有人知道怎么做吗?您可以通过将应用程序设置为应用程序/vnd.google apps.spreadsheet来完成此操作: 要在Python中执行此操作,请执行以下操作: from apiclient.discover
应用程序/vnd.google apps.spreadsheet来完成此操作:
要在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)
去医院试试吧 建议的方法对我不起作用,但以下代码起作用:
# requires: uid='example@gmail.com', pass1='password',
# name_spr='name_of_spreadsheet'
import gdata.docs.client
docs_client = gdata.docs.client.DocsClient()
docs_client.ClientLogin(uid, pass1, 'any')
document = gdata.docs.data.Resource(type='spreadsheet', title=name_spr)
resource = docs_client.CreateResource(document)
full_id = resource.resource_id.text # returned by gdata
gs_id = full_id[len('spreadsheet:'):]
创建电子表格的api参考位于
创建新电子表格的代码段如下所示
String[] SCOPES = { SheetsScopes.SPREADSHEETS };
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
getApplicationContext(),
Arrays.asList(SCOPES)).setBackOff(new ExponentialBackOff());
credential.setSelectedAccountName("your_google_account@gmail.com");
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
com.google.api.services.sheets.v4.Sheets service =
new com.google.api.services.sheets.v4.Sheets.Builder(
transport,
jsonFactory,
credential)
.setApplicationName("Google Sheets API Android Quickstart")
.build();
Spreadsheet spreadsheet = new Spreadsheet();
SpreadsheetProperties properties = new SpreadsheetProperties();
properties.setTitle("SpreadSheetTitle");
spreadsheet.setProperties(properties);
service.spreadsheets().create(spreadsheet).execute()
(2016年7月)的上述答案仍然有效(因为驱动器API v2尚未被弃用)。然而,下面是做同样事情的更现代的方法和一些帮助理解的视频:
下面两个示例的授权样板文件
from googleapiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
注意:要创建API项目和OAuth2凭据,并将这些凭据下载到client\u secret.json
(或client\u id.json
)文件,请转到
- 要了解如何使用开发者控制台,请参阅
- 要浏览此样板授权代码,请参阅。(注意:上面的样板文件比视频中的代码稍新/改进)
- 要获得使用Google Drive API v2(列出您的驱动器文件)的介绍,请参阅
- 要了解Google Drive API v3(启动/下载文件),请参阅。(注意:v2和v3并行运行…v2尚未弃用;v3:API调用更少,性能比v2更好)
- 要了解Google Sheets API v4(将SQL数据迁移到工作表),请参阅
使用v3和v2创建新的/空白工作表
创建带有v4的新/空白工作表
# above: SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
SHEETS = discovery.build('sheets', 'v4', http=creds.authorize(Http()))
data = {'properties': {'title': 'My new Sheet'}}
sheet = SHEETS.spreadsheets().create(body=data).execute()
现在您可能会问,“为什么有两种不同的方式创建空白工作表?”简而言之,Sheets API主要用于面向电子表格的操作,即插入数据、读取电子表格行、单元格格式、创建图表、添加透视表等,而不是像创建/删除和导入/导出这样面向文件的请求,其中驱动器API是要使用的正确API。恰好create是两者兼而有之,因此有两种方法。你也可以试试我写的库,它当然提供了比创建电子表格更多的功能
import pygsheets
gc = pygsheets.authorize(outh_file='client_secret.json')
# Open spreadsheet and then worksheet
gc.create('my new sheet')
这应该会有帮助:我如何获得OAuth 2.0凭据??“去谷歌API浏览器试试!”我试过了,它在谷歌API浏览器中运行。但我想从python代码开始,在授权部分遇到了困难。它不起作用,我可以使用“text/plain”MIME类型创建文本文件..但不是电子表格谢谢,不是因为这个问题。因为这是我发现的第一个关于如何发布模型的示例。我找到的所有文档都使用get和list示例好消息:这使用了最新版本的Sheets API v4。坏消息:OP请求一个Python示例。我可以指定在google驱动器的哪个文件夹中创建此电子表格吗?可以。查看有关如何执行此操作的文档:查找“parents”属性。
import pygsheets
gc = pygsheets.authorize(outh_file='client_secret.json')
# Open spreadsheet and then worksheet
gc.create('my new sheet')