Leonard Niehaus
Leonard Niehaus

Reputation: 530

Google Apps Script: Cannot find function insertText in object Document

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

Upvotes: 2

Views: 336

Answers (1)

Tanaike
Tanaike

Reputation: 201513

  • 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.

Upvotes: 2

Related Questions