Convert ods to Google sheets

2k views Asked by At

I am trying to convert an ods file to google sheets file format. However if the file is a little big (~ 2mb to 3mb) it starts to present the following problem several times

GoogleJsonResponseException: API call to drive.files.insert failed with Internal Error

My code uses a try catcth to keep trying to convert. It can convert after many attempts or it doesn't even convert

var ssOrige = DriveApp.getFileById(fileId)
var ssOrigeBlob = ssOrige.getBlob()
var ssName = ssOrige.getName()

var newFile = {
    title : ssName.split('.')[0], 
    parents: [{id: folderDestinationId}]
  }
var ssDestination = null
while(!ssDestination) {
  try {
   // Cria cópia do arquivo como gSheet
   ssDestination = Drive.Files.insert(newFile, ssOrigeBlob, {convert: true})
  } catch (e) {
   Logger.log(e)
 }
}

Small files can have the same problem, but they tend to convert easier and faster

I have some ods files in a folder on the drive. I get the ods file by ID and try to convert it to google sheets format. I use Google App Script to get the file and try to do the conversion. So, I take an ods file that is already in the google drive and try to convert and save it inside the drive

1

There are 1 answers

0
Raserhin On

Instead of using the Drive API v2 try to use the Drive API v3.

Look at the Files.create and the section to upload new Google Docs types.

This code is working fine for me with multiple files of various size:

function myFunction() {
  let id = "<YOUR ODS FILE ID>";
  let file = DriveApp.getFileById(id);

  let fileBlob = file.getBlob();


  newFile = {
    name: "New File",
    mimeType: "application/vnd.google-apps.spreadsheet"
  }

  try{
    Drive.Files.create(newFile, fileBlob);
  }catch(e){
    Logger.log("Error");
    Logger.log(e);
  }
}