Simon Marty
Simon Marty

Reputation: 11

Copy & paste automatic in a same gdoc

Inside a google doc document, I regularly have to copy-paste whole paragraphs in various places.

Is there a way to copy my paragraph so that it replicates at places defined in the google doc?

With the macros function on google sheet, I can do it very well but I need to work in a doc environment.

Upvotes: 0

Views: 79

Answers (1)

Rafa Guillermo
Rafa Guillermo

Reputation: 15377

Answer:

You can use the replaceText() method of the Body class to replace text in a document programmatically.

Code:

function replaceText(){
  const pText = "This is a long paragraph that I have to insert into my\
  document a load of times so instead of copying and pasting it each\
  time I need to use it, I will simply use the built-in replaceText()\
  method - it will replace all instances of the string {{paragraph}}.";

  var docBody = DocumentApp.getActiveDocument().getBody();
  docBody.replaceText('{{paragraph}}', pText); 
}

References:

Upvotes: 1

Related Questions