如何在 Google Drive API v3 中将 XLSX 转换为工作表

How to Convert XLSX to Sheets in Google Drive API v3

我想在将 xlsx 文件连同我的代码上传到 Google Drive 时自动将它们转换为 Google 电子表格。但是,虽然 csv 文件的转换成功,但我得到:

<HttpError 400 when requesting https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&alt=json returned "Bad Request">

尝试上传 xlsx 时。

这是我的代码:

def upload_service(filepath, name="", description="", fileID="", parentID=""):
    """ Uses a Resource (service) object to upload a file to drive. """

    if service == "": authenticate_service()

    if name == "":
        name = str(os.path.basename(filepath).split(os.extsep)[0])   # Get from filepath

    extension = str(os.path.basename(filepath).split(os.extsep)[1]).lower()

    if extension == "csv":                  # CSV
        mime_type = "text/csv"
    elif extension in ["xls", "xlsx"]:      # EXCEL
        mime_type = "application/ms-excel"
    else:
        return

    media_body = MediaFileUpload(filepath, mimetype=mime_type, resumable=True)

    if parentID == "":
        meta = dict(name=name, mimeType="application/vnd.google-apps.spreadsheet", description=description)
    else:
        meta = dict(name=name, mimeType="application/vnd.google-apps.spreadsheet", description=description, parents=[parentID])

    if fileID == "":   # CREATE 
        upload = service.files().create(
                                    body=meta,
                                    media_body=media_body).execute()
    else:   # REPLACE
        upload = service.files().update(
                                body=meta,
                                media_body=media_body,
                                fileId=fileID).execute()

    print ("\nFINISHED UPLOADING")

如何在 v3 中执行此操作?在 v2 中很清楚如何做到这一点,但在更新后的 API.

中却没有

根据 Official Google Documentation,您收到 400: Bad Request 表示未提供必填字段或参数,提供的值无效或提供的字段组合无效。尝试添加会在目录图中创建循环的父项时,可能会抛出此错误。

遇到此错误时,建议的操作是使用 exponential backoff。它是网络应用程序的标准错误处理策略,在该策略中,客户端会在越来越长的时间内定期重试失败的请求。

您可以使用官方 Google Docs for your reference, There is a parameter convert convert=true, 将文件转换为相应的 Google Docs 格式(默认:false)。

您还需要使用Python client library,您可以使用该库来支持上传文件。

找到这个 Stack Overflow ticket,查看社区提供的解决方案:

在 APIv3 中,您需要指定一个非常具体的 MIME 类型才能进行转换。

https://developers.google.com/drive/v3/web/manage-uploads#importing_to_google_docs_types_wzxhzdk8wzxhzdk9,您会注意到语句 "The supported conversions are available dynamically in the About resource's importFormats array"。使用

获取 importFormats 列表

GET https://www.googleapis.com/drive/v3/about?fields=importFormats&key={YOUR_API_KEY}

或转到 https://developers.google.com/drive/v3/reference/about/get#try-it 并输入 importFormats

您会在回复中注意到:

"application/vnd.ms-excel": [
   "application/vnd.google-apps.spreadsheet"
]

在您的代码中,使用:

elif extension in ["xls", "xlsx"]:      # EXCEL
    mime_type = "application/vnd.ms-excel"

(注意额外的 vnd.)它应该运行良好!

def uploadExcel(excelFileName):
   file_metadata = {'name': excelFileName, 'parents': [folderId], 'mimeType': 'application/vnd.google-apps.spreadsheet'}
   media = MediaFileUpload(excelFileName, mimetype='application/vnd.ms-excel', resumable=True)
   file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()

逻辑是:我们要从 excel 格式创建一个 电子表格

所以我们完全按照这个逻辑进行编码(C# 示例):

Google.Apis.Drive.v3.Data.File fileMetadata = new Google.Apis.Drive.v3.Data.File();
            fileMetadata.Name = System.IO.Path.GetFileName(file_being_uploaded);
            fileMetadata.Description = "File created via Google Drive API C#";
            fileMetadata.MimeType = "application/vnd.google-apps.spreadsheet";
            fileMetadata.Parents = new List<string> { _parent };    // if you want to organize in some folder       

            // File content.
            byte[] byteArray = System.IO.File.ReadAllBytes(file_being_uploaded);
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
            try
            {
                FilesResource.CreateMediaUpload request = _service.Item1.Files.Create(fileMetadata, stream, GetMimeType(file_being_uploaded));
(...)

    // gets us the Excel Mime
    private static string GetMimeType(string fileName)
    {
        string mimeType = "application/unknown";
        string ext = System.IO.Path.GetExtension(fileName).ToLower();
        Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
        if (regKey != null && regKey.GetValue("Content Type") != null)
            mimeType = regKey.GetValue("Content Type").ToString();
        return mimeType;
    }