ayjay
ayjay

Reputation: 1382

In Google Scripts (Google Docs), how do identify if certain words in a paragraph object is in bold?

Let's say I have this paragraph object in my Google Docs:

President: Joe Obam

How do identify if the paragraph object has certain parts in it that's in bold? And further more, how do I extract which words are in bold?

Upvotes: 1

Views: 312

Answers (1)

Digvijay Singh
Digvijay Singh

Reputation: 11

function myfunction() {
    var doc = DocumentApp.getActiveDocument();
    var body = doc.getBody();
    // Analyze the first paragraph in document
    var paragraph = body.getChild(0);
    // assumes the paragraph only has text
    var txt = paragraph.asParagraph().getChild(0);
    var len = txt.asText().getText().length;
    for (var i = 0; i < len; i++) {
        var isbold = txt.asText().isBold(i);
        if (isbold) {
            Logger.log("yes");
            }
        else {
            word = "";
        }   
    }
}

This code will check your doc char by char, and it will print "yes" if even a single char is bold.

Upvotes: 1

Related Questions