How to convert an already-uploaded Excel file to Google's spreadsheet format?

别说谁变了你拦得住时间么 提交于 2021-02-18 16:42:25

问题


I need to convert Excel files already uploaded to Google Drive into Google's spreadsheet format. What is the best way to do that programmatically?

I've found this post (#15), but I can't figure out how to use this code. And it seems intended for Excel files not already stored on Drive. Uploading it again would be redundant.


回答1:


Unfortunately you cannot convert a file in-place, and you'll need to re-upload it. Here's a sample showing how to use an XLSX file already in your drive:

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



回答2:


For those that stumble upon this: This is possible in both Drive API v2 & v3. Python example:

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()



回答3:


In Google Driver V3 API, just need to set MimeType.

File file = new File();

file.MimeType = "application/vnd.google-apps.spreadsheet"


来源:https://stackoverflow.com/questions/23410166/how-to-convert-an-already-uploaded-excel-file-to-googles-spreadsheet-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!