3
votes

Hello I try to detect specific string in my google doc and set it in "bold". Ive tried this :

function bold() {
var body = DocumentApp.getActiveDocument().getBody();
var foundElement = body.findText("Câbles");

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

    // Change the weight
    foundText.setFontWeight("bold");

    // Find the next match
    foundElement = body.findText("Câbles", foundElement);
}
}

The script return me an error : TypeError: Fonction setFontWeight not found in Text object.

Can you help me? Thanks.

EDIT>

Ive tried using this, but the bold style was now applied to the whole paragraph not only the text string...

function bold() {
var body = DocumentApp.getActiveDocument().getBody();
var foundElement = body.findText("test");

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

    // Set Bold
    foundText.setBold(true);

    // Find the next match
    foundElement = body.findText("test", foundElement);
}
}
2

2 Answers

3
votes

Ok, i did this :

function bold() {
var body = DocumentApp.getActiveDocument().getBody();
var foundElement = body.findText("test");

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();

    // Set Bold
    foundText.setBold(start, end, true);

    // Find the next match
    foundElement = body.findText("test", foundElement);
   }
}
2
votes

It looks like that is supported via sheets- not docs

in docs you could try to change font size?

 // Change the weight
   foundText.setFontSize(99);