我想在Google Drive中创建一个只包含元数据的空白Google Sheet。当我查阅Google SpreadSheet API文档时发现,它建议使用DocumentsList API,但是该API已经被弃用,并且要求我使用Google Drive API。在Drive API文档中,我找不到任何创建空白电子表格的方法。是否有人知道如何做到这一点?
我想在Google Drive中创建一个只包含元数据的空白Google Sheet。当我查阅Google SpreadSheet API文档时发现,它建议使用DocumentsList API,但是该API已经被弃用,并且要求我使用Google Drive API。在Drive API文档中,我找不到任何创建空白电子表格的方法。是否有人知道如何做到这一点?
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尝试一下吧!
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)
client_secret.json
(或client_id.json
)文件中,请访问Google开发者控制台。
# 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
# 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()
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()
import pygsheets
gc = pygsheets.authorize(outh_file='client_secret.json')
# Open spreadsheet and then worksheet
gc.create('my new sheet')
我尝试了提出的方法,但是以下代码可以工作:
# 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:'):]