0

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!

1
  • 1
    See this answer about Class / Method matching. Commented Dec 4, 2013 at 18:31

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Serge, I knew you'd answer the call. Works like a charm. Thanks for the explanation and the code.
You're welcome :-). Just a question: why are you using getValues with an s to get a single cell value?
Because I have no background in coding and am teaching myself by searching these forums! Does it work just as well to do .getValue() without the 's'? Am I hurting anything by using with the 's' and only getting single cell? Thanks!
Good answer ! (no kidding) The thing is that getValues returns a matrix of values (in this case an array with only one value in it) instead of a single value and that might cause some troubles sometimes. Look eventually at the logger.log feature and the execution transcript, you'll see some [brackets] that indicates the "array type". I have a lot of sympathy for self learning people, keep going and good luck ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.