Insert Line breaks before text in Google Apps Script

I need to insert some line breaks before certain text in a Google Document.

Tried this approach but get errors:

var body = DocumentApp.getActiveDocument().getBody();
var pattern = "WORD 1";
var found = body.findText(pattern);
var parent = found.getElement().getParent();
var index = body.getChildIndex(parent);
// or parent.getChildIndex(parent);
body.insertParagraph(index, "");

Any idea on how to do this?

Appreciate the help!

149 thoughts on “Insert Line breaks before text in Google Apps Script”

  1. It’s appropriate time to make some plans for the future and it is time
    to be happy. I’ve read this post and if I could I desire to suggest you some interesting
    things or tips. Perhaps you can write next articles referring to this article.
    I desire to read more things about it!

    Reply
  2. First off I want to say excellent blog! I had a quick question which I’d
    like to ask if you don’t mind. I was interested
    to find out how you center yourself and clear your mind prior to writing.

    I have had difficulty clearing my mind in getting my ideas out.
    I do enjoy writing but it just seems like the first 10
    to 15 minutes are lost just trying to figure out how to begin. Any ideas or tips?
    Many thanks!

    Reply
  3. I’m really enjoying the design and layout of
    your site. It’s a very easy on the eyes which
    makes it much more pleasant for me to come here and visit more often. Did you hire out a developer to create
    your theme? Superb work!

    Reply
  4. Having read this I thought it was very informative.
    I appreciate you spending some time and effort to put this information together.
    I once again find myself spending way too much time both
    reading and posting comments. But so what, it was still
    worthwhile!

    Reply
  5. Hey there I am so thrilled I found your website, I really
    found you by error, while I was looking on Yahoo for something else, Regardless
    I am here now and would just like to say cheers for a remarkable post and a all
    round entertaining blog (I also love the theme/design), I
    don’t have time to read it all at the moment but I have book-marked
    it and also included your RSS feeds, so when I have time I will be
    back to read a lot more, Please do keep up the great b.

    Reply
  6. An impressive share! I’ve just forwarded this onto a co-worker who
    has been doing a little homework on this. And he actually ordered me lunch simply because I
    found it for him… lol. So let me reword this…. Thank YOU for the meal!!

    But yeah, thanx for spending some time to discuss this topic
    here on your internet site.

    Reply
  7. Pingback: clomid 100g
  8. Pingback: mulnopiravir
  9. Pingback: buy nolvadex
  10. Pingback: nolvadex 10mg
  11. Pingback: clomid 50g

Leave a Comment