Converting .xls to google spreadsheet in google apps script

google-apps-script, google-drive-api, google-sheets

Solution

Updated 2024-03-22 - NEW for Drive API v3

var xlsxBlob = xlsxFile.getBlob();
var resource = { 
  name : newFileName, 
  mimeType : MimeType.GOOGLE_SHEETS, 
  parents: [driveFolderId] 
}; 
const file = Drive.Files.create(resource, xlsxBlob); 

Docs:

- Drive.Files.create: developers.google.com/drive/api/reference/rest/v3/files/create

- File parameters: developers.google.com/drive/api/reference/rest/v3/files#File

Below: DEPRECATED - applicable only for Drive API v2.

This is now possible using the Advanced Drive service:

https://developers.google.com/apps-script/advanced/drive

When using Drive.Files.insert, simply set the optional parameter "convert" to "true".

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

This was also obtained from the above given issue

Problem

I have an .xls file stored in Google Drive. I want to convert it to the Google Sheets spreadsheet file format from Google Apps Script. Is there any way to do this without external solutions?

Original source