Cutter
Cutter

Reputation: 1820

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

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.

Upvotes: 4

Views: 3253

Answers (3)

parapet
parapet

Reputation: 69

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

Upvotes: 3

Paul Xia
Paul Xia

Reputation: 21

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

File file = new File();

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

Upvotes: 2

Eric Koleda
Eric Koleda

Reputation: 12673

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
  });
}

Upvotes: 2

Related Questions