Search code examples
google-apps-scriptgoogle-sheets-apigoogle-drive-realtime-api

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.


Solution

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