0
我創建了一個Web界面,我試圖用Drive API創建一個空白的Google Spreadsheet。如何使用Drive API創建空白Google電子表格?
如何創建空白Google Spreadsheet?
我創建了一個Web界面,我試圖用Drive API創建一個空白的Google Spreadsheet。如何使用Drive API創建空白Google電子表格?
如何創建空白Google Spreadsheet?
試圖通過設置MIME Type到application/vnd.google-apps.spreadsheet
from apiclient import errors
from apiclient.http import MediaFileUpload
# ...
def insert_file(service, title, description, parent_id, mime_type, filename):
"""Insert new file.
Args:
service: Drive API service instance.
title: Title of the file to insert, including the extension.
description: Description of the file to insert.
parent_id: Parent folder's ID.
mime_type: MIME type of the file to insert.
filename: Filename of the file to insert.
Returns:
Inserted file metadata if successful, None otherwise.
"""
media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True)
body = {
'title': title,
'description': description,
'mimeType': mime_type
}
# Set the parent folder.
if parent_id:
body['parents'] = [{'id': parent_id}]
try:
file = service.files().insert(
body=body,
media_body=media_body).execute()
# Uncomment the following line to print the File ID
# print 'File ID: %s' % file['id']
return file
except errors.HttpError, error:
print 'An error occured: %s' % error
return None
注意使用Drive API:與files.insert應用程序創建快捷方式必須指定MIME類型application/vnd.google-apps.drive-SDK。