如何使用Drive API创建空白的Google电子表格?

时间:2016-11-30 15:52:42

标签: javascript google-sheets google-drive-api google-spreadsheet-api

我创建了一个网络界面,我正在尝试使用Drive API创建一个空白的Google电子表格。

如何制作空白的Google电子表格?

1 个答案:

答案 0 :(得分:0)

Drive API设置为application/vnd.google-apps.spreadsheet

,尝试使用MIME Type
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

注意:使用files.insert创建快捷方式的应用必须指定MIME类型application / vnd.google-apps.drive-sdk。