0

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

helpdoc
  • 1,910
  • 14
  • 36
  • Does this answer your question? [Google Sheets App Script - Custom Function to find RichText (Bold/Italic) locations](https://stackoverflow.com/questions/63408312/google-sheets-app-script-custom-function-to-find-richtext-bold-italic-locati) – Kos Sep 09 '21 at 09:40
  • also see https://stackoverflow.com/a/64600172 – Kos Sep 09 '21 at 09:41

1 Answers1

2

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
Aerials
  • 4,231
  • 1
  • 16
  • 20