Search code examples
google-apps-scriptgoogle-sheetsgoogle-drive-api

Add convert xls to Google sheets feature to function of download


I have working code for downloading any file in Google docs by link. Most of the files are in .xlx or .xlsx format and need to be converted in Google sheets type. I've tried to use some methods, that I find in Internet, but they don't work for me. In code below, I comment with code which I test. And I guess, that Google had changed some documentation.

 function downloadFile(fileURL,folder) {
            
  var fileName = "";
  var fileSize = 0;
  var  fileId = "";
  
  var response = UrlFetchApp.fetch(fileURL, {muteHttpExceptions: true});
  var rc = response.getResponseCode();
  
  if (rc == 200) {
    var fileBlob = response.getBlob() //.getAs(MimeType.GOOGLE_SHEETS) - get error
    var folder = DriveApp.getFolderById(folder);
    if (folder != null) {
      var file = folder.createFile(fileBlob);//.getAs(MimeType.GOOGLE_SHEETS) - get error
      fileName = file.getName();
      fileSize = file.getSize();
      fileId = file.getId();
    }
  }
  //file.setMimeType("application/vnd.google-apps.spreadsheet") - not work
  //makeCopy('ssssss', folder, {convert: true}) - get error

  var fileInfo = [ rc, fileName, fileSize, fileId ];
  return fileInfo;
}

I call function like this:

downloadFile("http://www.beltools.ru/prais_rar/price%20TD%20RI.xls","0B_E2P3ZhQySBY3BBMzdlazBLcTA") 

Solution

  • In order to convert Excel to Spreadsheet, DriveApp cannot do it. So Drive API has to be used. You can use Drive API v2 from Advanced Google services. "Drive API v2" can be used at Google Apps Script by enabling Drive API of Advanced Google services and of Google API Console.

    How to use it is as follows.

    1. In the script editor, select Resources > Advanced Google services

    2. In the dialog that appears, click the on/off switch for Drive API v2.

    3. At the bottom of the dialog, click the link for the Google API Console.

    4. In the console, click into the filter box and type part of the name of the API "Drive API", then click the name once you see it.

    5. On the next screen, click Enable API.

    6. Close the Developers Console and return to the script editor. Click OK in the dialog. The advanced service you enabled is now available in autocomplete.

    The detail information is https://developers.google.com/apps-script/guides/services/advanced.

    At the sample script, at first, a file is downloaded by fileURL, which is Excel file, as blob. The blob data is uploaded to Google Drive using Drive API. In this case, access token is not required. For downloadFile(), the Input data and output data are same to your downloadFile(). File name is retrieved from fileURL.

    Script :

    function downloadFile(fileURL, folder) {
      var filename = fileURL.match(".+/(.+?)([\?#;].*)?$")[1];
      var response = UrlFetchApp.fetch(fileURL);
      var rc = response.getResponseCode();
      var blob = response.getBlob();
      var resource = {
        "mimeType": "application/vnd.google-apps.spreadsheet",
        "parents": [{id: folder}],
        "title": filename
      };
      var res = Drive.Files.insert(resource, blob);
      var fileInfo = [rc, res.title, blob.getBytes().length, res.id];
      return fileInfo;
    }
    

    Result :

    [
        200,
        sample.xlsx,
        10000.0,
        ## file id ##
    ]
    

    If I misunderstand your question, I'm sorry.