3

I've got doc.getBody().replaceText(oldregex,newstring) working fine in a Google Document script at the minute, and was hoping to set some bold/italic on newstring. This looks harder than I thought it would be. Has anyone found a tidy way to do this?

I'm currently thinking I'll need to...

  • Build newtext as a range with rangeBuilder
  • Find oldtext and select it as a range (somehow...)
  • Clear the oldtext range and insert the newtext range at the find location

This seems like a lot of work for something that would be trivial with HTML-like tags. I'm definitely missing something. Would really appreciate any suggestions.

Richie
  • 77
  • 2
  • 4
  • Possible duplicate of [google doc script, search and replace text string and change font (e.g., boldface)](https://stackoverflow.com/questions/33205269/google-doc-script-search-and-replace-text-string-and-change-font-e-g-boldfac) – user202729 Oct 28 '19 at 04:05

1 Answers1

4

Since replaceText only changes the plain text content, leaving formatting in place, the goal can be achieved by applying formatting before the replacement. First, findText goes through the text and sets bold to every match; then replaceText performs the replacement.

There are two cases to consider: only a part of text in an element is matched (which is typical) and entire element is matched. The property isPartial of RangeElement class distinguishes between these.

function replaceWithBold(pattern, newString) {
  var body = DocumentApp.getActiveDocument().getBody();
  var found = body.findText(pattern);
  while (found) {
    var elem = found.getElement();
    if (found.isPartial()) {
      var start = found.getStartOffset();
      var end = found.getEndOffsetInclusive();
      elem.setBold(start, end, true);
    }
    else {
      elem.setBold(true);
    }
    found = body.findText(pattern, newString);
  }
  body.replaceText(pattern, newString);
}

This seems like a lot of work for something that would be trivial

This is both correct and typical for working with Google Documents using Apps Script.

  • That's really useful, thanks! Unfortunately I was hoping to replace a placeholder with a block of dynamically-produced, formatted text, so it sounds like too much work. Would it be easier to create a new, formatted document from scratch then just cut and paste it manually? If that's going to by tricky too then I might just go back to using LaTeX or HTML... – Richie May 13 '18 at 20:00
  • Apps Script cannot insert formatted text; all insert methods take a string. But one can first insert a string at the offsets identified with findText and apply the formatting to containing Element with the offsets, so that formatting gets applied to the inserted text. The placeholder can be removed at the end of the process. But if this sounds like too much work, then sure, use other solutions. –  May 13 '18 at 20:59
  • replaceText does not leave the formatting in place. – ThePunisher May 17 '18 at 08:54
  • 3
    *Note*: The second parameter in the second `findText` should be `found`. - [documentation](https://developers.google.com/apps-script/reference/document/body#findText(String,RangeElement)). – user202729 Oct 28 '19 at 04:04