我想在Google Drive中创建一个空的Google工作表(仅使用元数据创建)。当我参考谷歌SpreadSheet API文档时,它告诉我使用DocumentsList应用程序接口,但它被弃用了,并要求我使用Google Drive API。在Drive API文档中,我找不到任何创建空表的方法。有人知道该怎么做吗?
发布于 2013-01-05 03:21:02
您可以通过将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试试吧!
发布于 2016-07-14 03:10:48
(2016年7月)BossyLobster上面的回答仍然有效(因为Drive 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_secret.json (或client_id.json)文件,请转到Google Developers Console。
要了解如何使用开发人员控制台,请参阅this video.
调用更少,性能更好)
使用 v3 (& 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使用 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主要用于面向电子表格的操作,即插入数据、读取电子表格行、单元格格式、创建图表、添加透视表等,而不是像创建/删除和导入/导出这样的面向文件的请求,其中Drive API是正确使用的。碰巧create是两者兼而有之的,因此有两种方法可以做到这一点。
发布于 2016-07-06 10:34:23
创建电子表格的应用编程接口参考位于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()https://stackoverflow.com/questions/12741303
复制相似问题