2

I'd like to copy a Google Docs document and add text to the copy. This is my code:

function main() {
    var template = DriveApp.getFileById(TEMPLATE_DOC_ID);
    var copy = template.makeCopy('copied file');
    var form = DocumentApp.openById(copy.getId());
    form.insertText(0, 'Inserted text.\n');
  }

When I run main(), I get the following error: TypeError: Cannot find function insertText in object Document. (line 5, file "Code")

1 Answer 1

2
  • You want to put a text using insertText().

If my understanding is correct, how about this modification?

Pattern 1:

When you want to put the text to the body, please modify as follows.

From:
form.insertText(0, 'Inserted text.\n');
To:
form.getBody().editAsText().insertText(0, 'Inserted text.\n');

Pattern 2:

When you want to put the text to the paragraph, please modify as follows.

From:
form.insertText(0, 'Inserted text.\n');
To:
form.getBody().getParagraphs()[0].insertText(0, 'Inserted text.\n');
  • In this case, the text is put to the first paragraph.

References:

If I misunderstood your question and this was not the result you want, I apologize.

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

Comments

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.