user12736831
user12736831

Reputation:

How to insert text at specified position in google doc using google apps script?

I have document which contains the text. I want to find the word (li) and insert new word by adding new line. I have found the following script that finds the text:

matchPosition = theDoc.getBody().findText("put stuff here").getStartOffset();
theDoc.getBody().editAsText().insertText(matchPosition, "The new stuff");

But this does not work properly because it gives the position of word from the starting line and when we insert a new text it counts position from the start of the document instead of the start of that line. Hope you understand what I am saying.

So to summarize, Is there any way from which we can find the first occurrence of the word anywhere in the document and add a new word on the next line of the found word?

Upvotes: 5

Views: 3852

Answers (2)

MarcoP
MarcoP

Reputation: 1776

You can search the specified text in the document and do different things.

You can add a new line with the \n caracter.

Replace the text:

function insert_text_after_pattern() {
  // get the document's body
  var body = DocumentApp.getActiveDocument().getBody();
  // the patten to find in the text
  var pattern = "my pattern" // you can use Regular Expressions
  // the text to insert
  var str = "[new text]"

  // Replace the text
  body.replaceText(pattern, pattern + str);
}

Append the text to the paragraph, matching the style:

function insert_text_after_pattern() {
  // get the document's body
  var body = DocumentApp.getActiveDocument().getBody();
  // the patten to find in the text
  var pattern = "my pattern" // you can use Regular Expressions
  // the text to insert
  var str = "[new text]"

  // find the position where to insert the text
  var matchPosition = body.findText(pattern);
  // get the text element at position
  var textElement = matchPosition.getElement();
  // append text
  textElement.appendText(str);

Append the text to the paragraph, with its own style:

function insert_text_after_pattern() {
  // get the document's body
  var body = DocumentApp.getActiveDocument().getBody();
  // the patten to find in the text
  var pattern = "my pattern" // you can use Regular Expressions
  // the text to insert
  var str = "[new text]"

  // find the position where to insert the text
  var matchPosition = body.findText(pattern);
  // get the text element at position
  var textElement = matchPosition.getElement();
  // get its parent (paragraph)
  var parent = textElement.getParent();
  // append text to paragraph
  var newText = parent.appendText(str);
  // set style
  newText.setBold(true);
}

Upvotes: 4

user12736831
user12736831

Reputation:

I just found a way to insert text at a specified position:

for (var i = 0; i < paragraphs.length; i++) {
    var text = paragraphs[i].getText();
        
    if (text.includes("a href")==true) 
    
    {
      body.insertParagraph(i, "Test Here")
     }
 }

This loops through all the paragraphs' text and when it finds the matching word, it adds a new paragraph at the specified position in which you can add your desired word.

Upvotes: 2

Related Questions