Alejandro Silvestri
Alejandro Silvestri

Reputation: 3774

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.

Upvotes: 3

Views: 9376

Answers (1)

Alan Wells
Alan Wells

Reputation: 31300

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.

Upvotes: 5

Related Questions