1

We are actually wondering how can you for example find Bible verses in the document text and replace them for an URL of the verse on the web.

For example if you have a "Jn 3.1" text it will be replaced for an hiperlink like this:

Text= Jn 3.1
Link= https://www.bible.com/1/jn.3.1

we though on using Body.replaceText(searchPattern, replacement) but you cant use that for insert an hyperlink.

And also we must think that the number of characters of the verse can change, for example, it can be:

Jn 1.3

that is 6 characters or can be

John 10.10

that is 10 characters. I think that this can be covered with regex (if we are be able to use them with the solution, so its irrelevant if the solution cover it.

White_King
  • 748
  • 7
  • 21

1 Answers1

2

For this kind of modifications you will have to use the Appsscript functions. They work in the same way than normal javascript functions but here you are able to work directly with the text.

for this case the replace function is: replaceText(searchPattern, replacement)

and this is how you can search the word in your document and then replace the text.

function myFunction() {
  var doc = DocumentApp.getActiveDocument();

  var word = 'example';
  var rep = 'replacement';

  var body = doc.getBody().editAsText().findText(word);
  var elem = body.getElement().asText();

  var idx = elem.editAsText().getText().indexOf(word);

  elem.replaceText(word, rep);
} 

So basically you find the element that contains the desired word, then you will get the element and then you will edit the text contained in that element.

I personally don't like to put complete urls in the text, rather i would use and inline link so in this case "Jn 1.3" would be the text of the hyperlink.

For that, instead of the replaceText line, you can use:

var result = elem.setLinkUrl(idx, idx+word.length -1, 'www.google.com');

It will be easier to read. I hope it helps.

Gerardo
  • 3,460
  • 1
  • 16
  • 18