Overwrite an Image File with Google Apps Script

后端 未结 2 816
孤独总比滥情好
孤独总比滥情好 2020-12-10 18:14

Can I overwrite an image file with Google Apps Script? I\'ve tried:

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

Neither of

2条回答
  •  执笔经年
    2020-12-10 18:33

    An image file can be overwritten with Google Apps Script and the DriveAPI using the update() method:

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

    Where file resource is:

    var myFileName = 'fileName' + '.jpg';
    
    var file = {
      title: myFileName,
      mimeType: 'image/jpeg'
    };
    

    I'm getting the file ID with the DriveApp service, and the Blob is what was uploaded by the user.

    In order to use DriveAPI, you need to add it through the Resources, Advanced Google Services menu. Set the Drive API to ON.

    var allFilesByName,file,myFolder,myVar,theFileID,thisFile;
       //Define var names without assigning a value
    
    file = {
      title: myFileName,
      mimeType: 'image/jpeg'
    };
    
    myFolder = DriveApp.getFolderById('Folder ID');
    
    allFilesByName = myFolder.getFilesByName(myFileName);
    
    while (allFilesByName.hasNext()) {
      thisFile = allFilesByName.next();
      theFileID = thisFile.getId();
      //Logger.log('theFileID: ' + theFileID);
      
      myVar = Drive.Files.update(file, theFileID, uploadedBlob);
    };
    

提交回复
热议问题