Home > Mobile >  How can i REPLACE a file with another file (if their name is the same) using 'Trash' ing i
How can i REPLACE a file with another file (if their name is the same) using 'Trash' ing i

Time:11-13

I am accessing a list of folders from a shared drive.

In here, I am converting a few excel files into spreadsheet. My issue is to replace the old converted files with the new file. This is because every time i run the script the new converted file(with same name) keeps on multiplying in the same folder together with the old one.

Here is the code:

function ConvertFiles() {
var sheet = 
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var r= 2;
for(r= 2;r < sheet.getLastRow(); r  ){

// Use getValue instead of getValues
var fileId = sheet.getRange(r,1).getValue();
var folderID = sheet.getRange(r,8).getValue();

var files = DriveApp.getFileById(fileId);
var name = files.getName().split('.')[0]; 
var blob = files.getBlob();
  
var newFile = {
// Remove '_converted' from name if existing to avoid duplication of the string before adding '_converted'
// This will allow to have newly converted file "replace" the old converted file properly
  title: name.replace('_converted','')   '_converted', 
  parents: [{
    id: folderID
  }]
}; 

var destinationFolderId = DriveApp.getFolderById(folderID); 
var existingFiles = destinationFolderId.getFilesByName(newFile.title);

// GOAL #1: To replace/update the old converted file into the latest one everytime the script runs (if it has the same filename)
// Find the file with same name of the file to be converted
while(existingFiles.hasNext()) {
  // ID of the file with same converted name
  var oldConvertedFileWithSameNameID = existingFiles.next().getId();

  // Delete before writing
  Drive.Files.remove(oldConvertedFileWithSameNameID);
  //DriveApp.getFileById(oldConvertedFileWithSameNameID.getId()).setTrashed(true);

}

// Create new converted file then get ID
var newFileID = Drive.Files.insert(newFile, blob, { convert: true,supportsAllDrives: true }).id; 
Logger.log(newFileID);
//var sheetFileID = newFileID.getId();
//var Url = "https://drive.google.com/open?id="   sheetFileID;
 var Url = "https://drive.google.com/open?id="   newFileID;

// Add the ID of the converted file
sheet.getRange(r,9).setValue(newFileID);
sheet.getRange(r,10).setValue(Url);
}
}

My goal is

  1. To replace the old converted file with the new one(if they have the same name) into the shared drive folder
  2. To get to know how can i implement the setTrashed() inside the above code

I have tried using the Drive.Files.remove(oldConvertedFileWithSameNameID); but I am getting an error message GoogleJsonResponseException: API call to drive.files.delete failed with error: File not found:("fileid"). Then i saw an question on this [https://stackoverflow.com/questions/55150681/delete-files-via-drive-api-failed-with-error-insufficient-permissions-for-this]...so i guess that method is not suitable to implemented in shared folder.

So i how can i use setTrashed() method inside the above code?

CodePudding user response:

I think you need to set the supportsAllDrives parameter:

Drive.Files.remove(oldConvertedFileWithSameNameID, {supportsAllDrives: true});

References:

  • Related