使用Apps脚本在内存中创建新blob

时间:2015-02-07 04:57:43

标签: javascript google-apps-script blob

我想在内存中创建一个新blob,将内容放入blob中,为其命名,最后将其另存为Drive中的文件。

我知道如何在Blob中创建驱动器中的文件。我唯一想要的就是创建一个空的新blob。

1 个答案:

答案 0 :(得分:3)

您可以使用Utilities Service在内存中创建新blob:

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');
};

您还可以使用少量数据创建新文件,然后更改内容。在此示例中,将在根目录中创建一个文件,其内容为“abc”。然后将blob的内容设置为其他内容。

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())
};

您可以在内存中创建数据,然后使用完成的数据创建blob。