How to write a blob to an existing file?

2,483 views
Skip to first unread message

Bob Alexander

unread,
Feb 3, 2023, 9:03:15 PM2/3/23
to Google Apps Script Community
Hi,

If a file already exists, how can I replace its contents with a blob?

I know that if the file did not exist yet, I could do:

        file = DriveApp.createFile(blob);
        file.setName(fileName);

And if I were replacing an existing file's contents with a string, I could do:

          let file = DriveApp.getFilesByName(fileName).next();
         file.setContent(str);

But I can't figure out how to replace an existing file's contents with a blob.

Thank you.

Tanaike

unread,
Feb 3, 2023, 9:25:30 PM2/3/23
to Google Apps Script Community
In that case, it is required to use Drive API. The simple sample script is as follows. Please enable Drive API at Advanced Google services.

const originalFileId = "###"; // Please set the file ID of the original.
const newFileId = "###"; // Please set the file ID of the new file.
const blob = DriveApp.getFileById(newFileId).getBlob();
Drive.Files.update(null, originalFileId, blob);


When this script is run, the file of `originalFileId` is overwritten with the file of `newFileId`. Please be careful about this.

This is a simple sample script. So, error handling is not included. Please be careful about this.

Bob Alexander

unread,
Feb 4, 2023, 10:37:45 AM2/4/23
to Google Apps Script Community
Thank you!

Scott Robinson

unread,
Jun 1, 2023, 12:44:55 PM6/1/23
to Google Apps Script Community
This was a very interesting thread to stumble upon.  I can see there hasn't been any activity here, but I wanted to see if Tanaike was still available to answer a question?

So,  I thought I could use this method to copy the contents of a template google document over another already existing google document.  It is important that I don't just create a new file as I want to keep the same docid for the destination document.

Unfortunately, although the blob copy works, the destination document does not keep the formatting from the source template document.  You can see that the elements are there, but things like images and watermark information is scrambled and all over the place.

I thought that a blob copy would give me an exact reproduction though?  Is this possible Tanaike?

Thank you.

Tanaike

unread,
Jun 2, 2023, 1:05:23 AM6/2/23
to Google Apps Script Community
I think that when a blob is retrieved from Google Document, the mimeType of the retrieved blob is "application/pdf". I thought that this might be the reason for your current issue. In this case, how about overwritten by DOCX converted from the Google Document instead of PDF data? But, I think that Google Document is not the same as DOCX. So, I'm not sure whether this approach can be used for your situation. I apologize for this. And, about your "copy", in this case, the existing Google Document is overwritten by a blob without changing the file ID. Is this your expected result?

Scott Robinson

unread,
Jun 5, 2023, 4:53:32 AM6/5/23
to Google Apps Script Community
Yes Tanaike,  it is important that the file id is kept the same.

It seems that .isGoogleType()  property is returning false for a google document blob despite the mimetype being  application/vnd.google-apps.document

Alex

unread,
Jun 5, 2023, 5:58:08 AM6/5/23
to Google Apps Script Community
Google Docs files are not a Blob container. Update is not work fine for this. But Update is a single method for safe id of the file.

Alex

unread,
Jun 5, 2023, 6:06:49 AM6/5/23
to Google Apps Script Community
And yeppp... isGoogleType always returns false

  const blob = DriveApp.getFileById('12eJE-Atj0YxSLO1z95ScH9KXupIzarckxvuP5bV3lTU').getBlob();
  console.log(blob.isGoogleType(), blob.getContentType());

> Info false 'application/pdf'


Cheers!

Scott Robinson

unread,
Jun 5, 2023, 6:08:03 AM6/5/23
to Google Apps Script Community
I have decided to step through the document children and do a step by step copy using insertElementType instead.  I think the blobcopy operations were not designed for Google filetypes...

Alex

unread,
Jun 5, 2023, 6:13:12 AM6/5/23
to Google Apps Script Community
Really! Very smart Scott!

It looks like it.

Andrea Guerri

unread,
Aug 21, 2023, 3:54:11 AM8/21/23
to Google Apps Script Community
I had a problem where I was creating a PDF file with today's date. The folder was shared on the PC. If I ran the script on the same day, on the PC it creates a duplicate with (1) ... (2) etc. So at first I tried deleting the file with setTrashed and then adding a new one. The problem that during the execution it didn't have time to delete the file on the folder in the PC and kept creating a ..(1) file. But following Tanaike's advice I adapted the solution like this:
  try{
    let oldFile = DriveApp.getFolderById(id_Folder).getFilesByName(nameFile).next().getId(),
        filePDF = DriveApp.getFileById(ssID).getAs('application/pdf'),
        newFile = DriveApp.getFolderById(id_Folder).createFile(filePDF).setName("clone_file");

    Drive.Files.update(null, oldFile, newFile.getBlob());
    DriveApp.getFileById(newFile.getId()).setTrashed(true);
 
  }catch{
    let filePDF = DriveApp.getFileById(ssID).getAs('application/pdf');
    DriveApp.getFolderById(id_Folder).createFile(filePDF).setName(nameFile);
  }
Solution: With try I take the id of the file with today's date, if the file exists in the folder, then I create a clone of the day's file and with Drive.Files.Update I update the already existing file. Finally I delete the clone file. In case the file does not exist with Catch I create it with today's date. Thank you Tanaike for this inspiration. I haven't found anything that simple on the web.

Reply all
Reply to author
Forward
0 new messages