Overwrite image file with Google Apps Script

Can I overwrite an image file using Google Apps Script? I tried:

file.setContent(newBlobImage);
file.replace(newBlobImage);

None of them work. .setContent()will delete all the data in the file, and it looks like it just writes the variable name in the form of text or something like that. I assume that both .setContent()and .replace()are used for text documents, and perhaps why they do not work.

If it was a text file or a spreadsheet, I could clear it and then add new content.

I can destroy the file and then create a new one, but I would prefer if there is another way.

If I write a file with the same name, it will not overwrite the existing file, it creates another file with the same name.

The only way I was able to delete the file was DocsList, and the only success I had when creating the image file was DriveApp. So I have to delete the file using DocsList, and then create another file with DriveApp.

Well, I figured out how to delete a file without sending it to the trash, so I won’t need to empty the trash later. Google Drive SDKinside the Script application has a method removethat did not send the file to the trash, it just disappeared.

var myFolder = DriveApp.getFolderById('3Bg2dKau456ySkhNBWB98W5sSTM');

thisFile = myFolder.getFilesByName(myFileName);

while (thisFile.hasNext()) {
  var eachFile = thisFile.next();
  var idToDLET = eachFile.getId();
  Logger.log('idToDLET: ' + idToDLET);

  var rtrnFromDLET = Drive.Files.remove(idToDLET);
};

, DriveApp DriveAPI, , . DriveAPI.remove , , , , . , , , , , .

, DriveAPI Patch Update.

.patch(resource, fileId, optionalArgs)

Google .

resource - , , . fileId . , optionalArgs , HTTP-? .

, . - PUT,

, .

. Patch, . , , , .

Patch, Update:

.update(resource, fileId, mediaData)

mediaData blob. , , . , resource. .

+4
2

. Google Apps Script DriveAPI. :

.update(File resource, String fileId, Blob mediaData)

file resource:

var myFileName = 'fileName' + '.jpg';

var file = {
  title: myFileName,
  mimeType: 'image/jpeg'
};

DriveApp, Blob - , .

DriveAPI, Resources, Advanced Google Services. Drive API .

var file = {
  title: myFileName,
  mimeType: 'image/jpeg'
};

var myFolder = DriveApp.getFolderById('Folder ID');

var allFilesByName = myFolder.getFilesByName(myFileName);

while (allFilesByName.hasNext()) {
  var thisFile = allFilesByName.next();
  var theFileID = thisFile.getId();
  Logger.log('theFileID: ' + theFileID);

  var myVar = Drive.Files.update(file, theFileID, uploadedBlob);
};
+6

!

: .

"Resource Script Editor" Drive script.

function spreadsheetCopy() {
  // Below is the file to be copied with a bound form.
  var fileToCopy = DriveApp.getFileById("file_key"); // key is fileId
  var saveFolder = DriveApp.getFolderById("folder_key"); // key is folderId
  var currentFolder = "";
  ( fileToCopy.getParents().next() ) ? currentFolder = fileToCopy.getParents().next() : currentFolder = DriveApp.getRootFolder();  
  Logger.log(currentFolder)
  var copyFile = fileToCopy.makeCopy(saveFolder),
      copyName = copyFile.getName(); 
  Utilities.sleep(30000);
 moveFormCopy(currentFolder, saveFolder, copyName);
}

function moveFormCopy(currentFolder, saveFolder, copyName) {  
  var formsInFolder = currentFolder.getFilesByType(MimeType.GOOGLE_FORMS);
  var form, copyForm, copyFormMimeType, copyFormName, copyFormId;
  while ( formsInFolder.hasNext() ) {
    form = formsInFolder.next();
    if ( copyName === form.getName() ) {
      copyForm = form;
      copyFormMimeType = copyForm.getMimeType();
      copyFormName = copyForm.getName();
      copyFormId = copyForm.getId();
      break;
    }
  };  
  var resource = {title: copyName, mimeType: copyFormMimeType};
  Drive.Files.patch(resource, copyFormId, {addParents: saveFolder.getId(), removeParents: currentFolder.getId()})
}
+2

Source: https://habr.com/ru/post/1544612/


All Articles