Patrick
Patrick

Reputation: 11

How to fix "Blob object must have non-null data for this operation. (line 6, file "Code")" in google script

I want to create a duplicate file and then send it via email, but I'm getting error:

Blob object must have non-null data for this operation. (line 6, file "Code")

I've tried to change:

var duplicatedFileId = sourceFile.makeCopy(sourceFile+" copy", sourceFolder).getId(); 

Into:

var duplicatedFileId = sourceFile.makeCopy(sourceFile+" copy", sourceFolder).getId().toString();

But I got another error.

function myFunction() {
  var sourceFolder = DriveApp.getFolderById("1DiqneJbyPN90SvE7uGaesdrd7Po6NJLl");
  var sourceFile = DriveApp.getFileById("1qAabbU8MlWmV8J1rdI5A8eEvUPLXAqKnz6fp1vxUOzQ");
  var duplicatedFileId = sourceFile.makeCopy(sourceFile+" copy", sourceFolder).getId();
  var duplicatedFile = DriveApp.getFileById(duplicatedFileId);
  GmailApp.sendEmail("[email protected]", "Testing attaching a file", "This is a test email", {
    attachments:[duplicatedFile.getAs(MimeType.GOOGLE_DOCS)],
    name:sourceFile+" copy"
  });
}

I expected the duplicate file to be sent, but I got error:

Blob object must have non-null data for this operation. (line 6, file "Code").

Upvotes: 1

Views: 2051

Answers (1)

Cooper
Cooper

Reputation: 64102

Try this:

function runTwo() {
  var sourceFolder=DriveApp.getFolderById("1DiqneJbyPN90SvE7uGaesdrd7Po6NJLl");
  var sourceFile=DriveApp.getFileById("1qAabbU8MlWmV8J1rdI5A8eEvUPLXAqKnz6fp1vxUOzQ");
  var duplicatedFileId = sourceFile.makeCopy(sourceFile.getName() + "_copy", sourceFolder).getId();
  var duplicatedFile = DriveApp.getFileById(duplicatedFileId);
  GmailApp.sendEmail("[email protected]", "Testing attaching a file", "This is a test email", {attachments:[duplicatedFile]});
}

Changed this line var duplicatedFileId = sourceFile.makeCopy(sourceFile+" copy", sourceFolder).getId(); to this line var duplicatedFileId = sourceFile.makeCopy(sourceFile.getName() + "_copy", sourceFolder).getId();

and this line GmailApp.sendEmail("[email protected]", "Testing attaching a file", "This is a test email", { attachments:[duplicatedFile.getAs(MimeType.GOOGLE_DOCS)], name:sourceFile+" copy" }); to this line GmailApp.sendEmail("[email protected]", "Testing attaching a file", "This is a test email", {attachments:[duplicatedFile]});

Upvotes: 1

Related Questions