helpdoc
helpdoc

Reputation: 1990

Extract Bold word in text from Google Sheet

Can anyone know how to solve my Google Sheet Problem? I have a cell with a text, some word in text are bold, i want to extract only bold words from text, how can i do that?

Text in Cell:

I want to register for a course?

Expected Output:

register, course

Thanks

Upvotes: 0

Views: 1335

Answers (1)

Aerials
Aerials

Reputation: 4419

You can use the RichTextValue class of a range, then you want to get the "runs" and verify if they are bold by using the TextStyle.isBold() method.

Example:

In a bound script to a sheet which contains the value "I want to register for a course?" in cell A1 run the following function:

function myFunction() {
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
  let runs = sheet.getRange('A1').getRichTextValue().getRuns()
  let boldWords = []
  runs.forEach((run)=>{
    if(run.getTextStyle().isBold()){
      boldWords.push(run.getText())
      
    }
    
  })
  console.log(boldWords.join(','))
}
// Output: register,course

Upvotes: 2

Related Questions