我想在Google云端硬盘中创建一个空的Google表格(仅使用元数据创建)。当我提到Google SpreadSheet API文档时,它说要使用DocumentsList API,但它已被弃用,而是要求我使用Google Drive API。在Drive API文档中,我找不到任何方法来创建空Sheet。任何人都知道如何做到这一点?
答案 0 :(得分:28)
您可以使用Drive API将MIME type设置为application/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)
前往Google APIs Explorer尝试一下!
答案 1 :(得分:8)
(2016年7月) BossyLobster上面的答案仍然有效(因为Drive 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。
创建新/空白工作表w / Google Drive API 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
使用Google Sheets API 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是正确使用的API。事实上,创造是两者兼而有之,因此有两种方法可以做到这一点。
答案 2 :(得分:1)
创建电子表格的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()
答案 3 :(得分:0)
建议的方法对我不起作用,但以下代码有效:
# 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:'):]
答案 4 :(得分:0)
你也可以尝试我写的pygsheets库,它可以提供比创建电子表格更多的功能,
import pygsheets
gc = pygsheets.authorize(outh_file='client_secret.json')
# Open spreadsheet and then worksheet
gc.create('my new sheet')