Home > Net >  Is it possible to call RichTextValueBuilder.setTextStyle( ) multiple times programmatically?
Is it possible to call RichTextValueBuilder.setTextStyle( ) multiple times programmatically?

Time:12-22

I got this these values.

enter image description here

And I want to have this result.

enter image description here

So I made the following test code and tried it to the first cell.

function test2() {
  const ss = SpreadsheetApp.getActive();
  const sheet = ss.getSheetByName("richText3");
  const range1 = sheet.getRange("A1");
  const text1 = range1.getValue();
  Logger.log(text1);
  const re = new RegExp(/\([ a-zA-Z\/']*\)\?/dg);  
  const redBold = SpreadsheetApp.newTextStyle().setBold(true).setForegroundColor('red').build();
  let array;
  while ((array = re.exec(text1)) !== null) {
    const [start, end] = array.indices[0];
    const richTxtValBlder = SpreadsheetApp.newRichTextValue()
        .setText(text1)
        .setTextStyle(start, end, redBold)
        .build();
    range1.setRichTextValue(richTxtValBlder);   
  }  
}

After first try, I got this result.

enter image description here

I checked the enter image description here

To:

enter image description here

References:

  • Related