Home > Back-end >  I have an error code in my getBody() function and I dont know why
I have an error code in my getBody() function and I dont know why

Time:01-18

So I'm creating this code where the template is selected through a series of If statements. so it is a changing variable. I am simply trying to now replace text in the selected template and I keep getting the same error that getBody() is not a function. Any help is greatly appreciated!

var doc = DriveApp.getFileById(templateId);
var copy = doc.makeCopy();
var destination = DriveApp.getFolderById('1mGCx4yXX_NnLHsHsGWBGkzwAVhG-cTrc');
destination.addFile(copy);
copy.setName(regno   ' statistical analysis');
var copiedTemplateId = copy.getId();

var body = doc.getBody(); 
var header = doc.getHeader();

CodePudding user response:

DriveApp.getFileById returns a File. So, doc is of type File. There is no "getBody" function for a File, at least it doesn't exist in the documentation: https://developers.google.com/apps-script/reference/drive/file

CodePudding user response:

This works:

function myfunk() {
  const regno = "test";
  var file = DriveApp.getFileById("fileid");
  var destination = DriveApp.getFolderById("folderid");
  let name = regno   ' statistical analysis';
  var copy = file.makeCopy(name, destination);
  var copiedTemplateId = copy.getId();
  let doc = DocumentApp.openById(copiedTemplateId);
  var body = doc.getBody();
  var header = doc.getHeader();
  Logger.log('id: %s, body: %s, header: %s',copiedTemplateId,body,header);
}

File was a Google Document and a copied was created in the appropriate directory and renamed correctly.

  • Related