duck
duck

Reputation: 149

Update Docs template from spreadsheet

I'm able to create a copy of the template and rename it. But I can't get the data from my spreadsheet to populate the new Doc. Error message:

TypeError: Cannot find function getBody in object File.

I'm assuming that the copy function does not open the document, and I need to open it before I can access the body, but I don't know how to open the document since I'm creating it and don't know it's ID yet.

var file = DocsList.find("Primary Conducting Sheet TEMPLATE")[0];
var copy = file.makeCopy("Primary Conducting Sheet for Upcoming Sunday");
var body = copy.getBody();
body.replaceText('{date}', sheet.getRange('B2').getValues());

Thanks in advance!

Upvotes: 0

Views: 505

Answers (1)

Serge insas
Serge insas

Reputation: 46792

You are making a confusion between DocsList file object and documentApp Document object.

When you have your copy use getId() to get its ID and then open it with the DocumentApp method.

exemple with step by step :

var file = DocsList.find("Primary Conducting Sheet TEMPLATE")[0];
var copy = file.makeCopy("Primary Conducting Sheet for Upcoming Sunday");
var copyId = copy.getId();
var docCopy = DocumentApp.openById(copyId);
var body = docCopy.getBody();
body.replaceText('{date}', sheet.getRange('B2').getValues());

EDIT : and also, read the post that Mogsdad refers to in its comment, it will definitely help you to avoid this kind of errors ;-)

Upvotes: 1

Related Questions