问题
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