1

I am trying to find text within a google doc and replace with a subscript notation - replace "a3" with a3 but with the 3 now formatted as a subscript.

based on the answer here I wrote some code that is working but only replaces the 1st instance of any occurrence (some are repeated).

I wrote the following:

  for (var k=0; k<subscriptsReplace.length; k++) {
      subscript = ' a'+subscriptsReplace[k];
      find = ' a'+subscriptsReplace[k]+' ';
             Logger.log(find)
  var element = body.findText(find);
  if(element){ // if found a match
    var start = element.getStartOffset();
    var text = element.getElement().asText();
    text.replaceText(find, subscript);
    text.setTextAlignment(start+2, start+2, DocumentApp.TextAlignment.SUBSCRIPT);
    Logger.log("found one");
} // else do nothing
}

note that subscriptsReplace is an array that contains all the numbers of the subscripts throughout the document.

I cannot figure out why it's not getting the repeats, by looking at the logs, I know that it's not running the conditional on the repeats - so it's not re-replacing the same subscript it already replaced.

can someone see what's going on? THank you!

Community
  • 1
  • 1
aShumays
  • 69
  • 12
  • @Serge insas I'm basing this off of your answer (see link in question) in that you wrote "If you have more than one then you should iterate the whole doc content to find each or them and replace them all." what do you mean by "the whole doc"? Is that my problem? how would I do that? thank you! – aShumays Sep 19 '15 at 23:57

2 Answers2

1

Ultimately the issue was that using replaceText() was replacing all the occurences of the text throughout the document and therefor, it wasn't available to find and replace the formatting after the 1st iteration.

Here's the code that replaced all occurences:

  for (var k=0; k<subscriptsReplace.length; k++) {
       find = 'a'+subscriptsReplace[k]+'_';
       var element = body.findText(find);
  if(element){ // if found a match
       var start = element.getStartOffset();
       var text = element.getElement().asText();
       text.setTextAlignment(start+1, start+1, DocumentApp.TextAlignment.SUBSCRIPT);
       text.deleteText(start+2, start+2);
  } // else do nothing
}

you'll see that rather than replacing, I added a special character "_" as a marker to find and then used deleteText() to get rid of them 1 at a time as I reformatted into subscripts

aShumays
  • 69
  • 12
0

You can replace everything in the entire body with this:

function testReplace() {
  var docBody = DocumentApp.getActiveDocument().getBody();
  docBody.replaceText(searchPattern, replacement);
};

Google Documentation - Replace Text

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • thank you for your response. Unfortunately, that won't work because the replacement is just a string, and what I want to change is the formatting. the string does not need to change. or maybe I misunderstand what you're suggesting? – aShumays Sep 20 '15 at 04:48
  • yes, but your response actually got me thinking about it in a different way that helped me find my own problem :) – aShumays Sep 21 '15 at 00:00
  • Thank you for sharing the answer. I'm sure it will help others. – Alan Wells Sep 21 '15 at 01:04