Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google doc script, search and replace text string and change font (e.g., boldface)

i am new to google doc script.

in a google doc, i need to search for several text strings (e.g., "student 1" in lightface font) to replace these text strings with another text string (e.g., "Student A"), but in boldface font.

to search and replace, i use the following code:

function docReplace() {

  var body = DocumentApp.getActiveDocument().getBody();
  // change "student 1" to "Student A" in boldface
  body.replaceText("student 1", "Student A");

}

the above code only replaces "student 1" with "Student A" using the current font of google doc, but i don't know how to change the font from lightface to boldface.

i tried

body.replaceText("student 1", "<b>Student A</b>");

of course, the above code did not work.

any help would be much appreciated. thank you.

like image 293
Luke V Avatar asked Oct 19 '15 01:10

Luke V


1 Answers

a pedestrian way to replace a text string (e.g., "student 1") that has many occurrences in a google doc by a new text string (e.g., "Student A") in boldface, is two steps:

1- write a function (called, say, docReplace) to do a search and replace in regular / normal font (no boldface):

function docReplace() {

  var body = DocumentApp.getActiveDocument().getBody();
  // change "student 1" to "Student A"
  body.replaceText("student 1", "Student A");

}

2- write a function (called, say, boldfaceText) to do a search for the desired text (e.g., "Student A") and the two offset values for this text (i.e., startOffset and endOffsetInclusive) at each occurrence to set the font for the characters within these offset values to boldface:

function boldfaceText(findMe) {

  // put to boldface the argument
  var body = DocumentApp.getActiveDocument().getBody();
  var foundElement = body.findText(findMe);

  while (foundElement != null) {
    // Get the text object from the element
    var foundText = foundElement.getElement().asText();

    // Where in the Element is the found text?
    var start = foundElement.getStartOffset();
    var end = foundElement.getEndOffsetInclusive();

    // Change the background color to yellow
    foundText.setBold(start, end, true);

    // Find the next match
    foundElement = body.findText(findMe, foundElement);
  }

}

the above code for boldfaceText was inspired from that in the post Finding text (multiple times) and highlighting.

an offset value for a character is simply the integer that describes the location of that character in the document, with the very first character having the offset value 1 (it's like the coordinate of the character).

use "Student A" as argument for a call to the function boldfaceText, i.e.,

boldfaceText("Student A");

which could be embedded into the function docReplace, i.e.,

function docReplace() {

  var body = DocumentApp.getActiveDocument().getBody();
  // change "student 1" to "Student A"
  body.replaceText("student 1", "Student A");

  // set all occurrences of "Student A" to boldface
  boldfaceText("Student A");

}

in the google doc, simply run the script docReplace to change all occurrences of "student 1" into "Student A" in boldface.

the above two functions (docReplace and boldfaceText) could be a good way to introduce newbies (like me) to google doc scripts. after some time playing with google doc scripts to gain some familiarity, learn Robin's more elegant and advanced code that does the above two steps all at once.

like image 141
Luke V Avatar answered Oct 06 '22 09:10

Luke V