Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new blob in memory using Apps Script

I want to create a new blob in memory, put the content in the blob, name it, and finally save it as a file in Drive.

I know how to create a file in Drive from blobs. The only thing I'm looking for is creating the empty new blob to start with.

like image 954
Alejandro Silvestri Avatar asked May 06 '26 02:05

Alejandro Silvestri


1 Answers

You can create a new blob in memory with the Utilities Service:

function createNewBlob() {

  var blobNew = Utilities.newBlob("initial data");
  blobNew.setName("BlobTest");

  Logger.log(blobNew.getName());

  blobNew.setDataFromString("Some new Content");

  Logger.log(blobNew.getDataAsString())

  var newFileReference = DriveApp.createFile(blobNew);
  newFileReference.setName('New Blob Test');
};

You can also create a new file, with some small amount of data, then change the content. In this example, a file is created in the root directory with the contents "abc". Then the content of the blob is set to something else.

function createNewBlob() {

  // Create an BLOB file with the content "abc"
  var blobNew = DriveApp.createFile('BlobTest', 'abc', MimeType.Blob);
  Logger.log(blobNew.getName());

  blobNew.setContent("Some new Content");
  Logger.log(blobNew.getBlob().getDataAsString())
};

You could create data in memory, then create the blob with the finished data.

like image 106
Alan Wells Avatar answered May 08 '26 14:05

Alan Wells