在谷歌云端硬盘中使用Drive API创建空的电子表格

2024-06-23 03:41:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Google驱动器中创建一个空的Google工作表(只使用元数据创建)。当我提到GoogleSpreadSheet API文档时,它说要使用DocumentsList API,但它不推荐使用,而是要求我使用googledriveapi。在Drive API文档中,我找不到任何创建空工作表的方法。有人知道怎么做吗?


Tags: 数据方法文档apigoogledrive驱动器googledriveapi
3条回答

创建电子表格的api引用位于https://developers.google.com/sheets/reference/rest/v4/spreadsheets/create

创建新电子表格的代码段如下所示。

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月)BossyLobster的回答仍然有效(因为驱动器API v2尚未被弃用)。然而,下面是更现代的方法做同样的事情和一些视频帮助理解:

下面两个示例的授权模板

from apiclient 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_secret.json(或client_id.json)文件,请转到Google Developers Console

  • 要了解如何使用开发人员控制台,请参见this video
  • 要浏览此样板授权代码,请参见this video。(注意:上面的样板文件比视频中的代码稍有更新/改进)
  • 要获得使用googledriveapiv2(列出驱动器文件)的简介,请参见this video
  • 要了解Google Drive APIv3(上传/下载文件),请参见this blogpost & video。(注意:v2&v3并排带电。。。v2还未被弃用;v3:与v2相比,API调用更少,性能更好)
  • 要了解googlesheetsapiv4(将SQL数据迁移到工作表),请参见this blogpost & video

创建新的/空白工作表w/Google Drive APIv3(&v2)

# above: SCOPES = 'https://www.googleapis.com/auth/drive.file'
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
data = {
    'name': 'My new Sheet',
    'mimeType': 'application/vnd.google-apps.spreadsheet',
}
sheet = DRIVE.files().create(body=data).execute() # insert() for v2

创建新的/空白工作表w/Google Sheets APIv4

# 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主要用于面向电子表格的操作,即插入数据、读取电子表格行、单元格格式、创建图表、添加透视表等,而不是像create/delete和import/export这样的面向文件的请求,其中Drive API是正确的。碰巧create两者都有,所以有两种方法。

通过将MIME type设置为application/vnd.google-apps.spreadsheet,可以使用Drive API完成此操作:

要在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)

Google APIs Explorer试一试!

相关问题 更多 >

    热门问题