Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace the a font weight using google script?

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);
}
}
like image 391
Sebastien-Gath Avatar asked Jul 26 '26 16:07

Sebastien-Gath


2 Answers

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);
   }
}
like image 167
Sebastien-Gath Avatar answered Jul 28 '26 15:07

Sebastien-Gath


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);
like image 36
OblongMedulla Avatar answered Jul 28 '26 15:07

OblongMedulla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!