【问题标题】:How to convert an already-uploaded Excel file to Google's spreadsheet format?如何将已上传的 Excel 文件转换为 Google 的电子表格格式?
【发布时间】:2014-06-18 01:50:54
【问题描述】:

我需要将已上传到 Google Drive 的 Excel 文件转换为 Google 的电子表格格式。以编程方式执行此操作的最佳方法是什么?

我找到了this post (#15),但我不知道如何使用此代码。它似乎适用于尚未存储在云端硬盘上的 Excel 文件。再次上传将是多余的。

【问题讨论】:

    标签: google-apps-script google-spreadsheet-api google-drive-realtime-api


    【解决方案1】:

    很遗憾,您无法就地转换文件,您需要重新上传它。以下示例展示了如何使用驱动器中已有的 XLSX 文件:

    function convert(xlsxFileId) { 
      var xlsxBlob = DriveApp.getFileById(xlsxFileId).getBlob();
      var file = {
        title: 'Converted Spreadsheet'
      };
      file = Drive.Files.insert(file, xlsxBlob, {
        convert: true
      });
    }
    

    【讨论】:

    • 它有效,谢谢!但是你能解释一下insert 方法的用法吗?和here描述的方法一样吗?为什么那里没有描述您使用的参数?
    • 是的,方法相同。 Apps 脚本对参数的排序略有不同,即:resource、requiredArgs、mediaData、optionalArgs
    • 由于某种原因,此代码不再起作用...生成的文件是 0 字节大小的空 XLS 之一。我仍然启用了 Drive API。但是在脚本执行日志中,有这行:[14-05-01 23:55:30:289 CEST] File.getBlob() [0 secondes] 显然getBlob() 方法不再起作用了。我该如何解决这个问题?
    • ... 它又可以恢复了(我没有更改代码,也没有更改我的驱动器内容)。知道是什么导致了这个问题吗?
    【解决方案2】:

    Google Driver V3 API中,只需设置MimeType即可。

    File file = new File();
    
    file.MimeType = "application/vnd.google-apps.spreadsheet"
    

    【讨论】:

      【解决方案3】:

      对于那些偶然发现的人:这在 Drive API v2 和 v3 中都是可能的。 Python 示例:

      API v2:

      drive_service_v2.files().copy(body=body, fileId="0n3xrtmjFpuG3V1g3h24tVHM2a33", convert="true").execute()
      

      API v3:

      new_mime_type = "application/vnd.google-apps.spreadsheet"
      file_id_of_Excel_file = "0n3xrtmjFpuG3V1g3h24tVHM2a33"
      body = {'name': 'New File Name', 'mimeType': new_mime_type}        
      drive_service.files().copy(body=body, fileId=file_id_of_Excel_file).execute()
      

      【讨论】:

      • 谢谢你!对 Python 非常有帮助,这正是我前进所需要的。
      猜你喜欢
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      相关资源
      最近更新 更多