Timogavk
Timogavk

Reputation: 869

How to set textstyle for specific words in a cell of Google sheets?

I have a Google sheet with default textstyle cell A1. I want to set a new text value to the cell with different textstyles:

function setTextToCell (){
let comment = Browser.inputBox('Your comment');
let commentTextStyle = SpreadsheetApp.newTextStyle().setForegroundColor('red');
  comment = SpreadsheetApp.newRichTextValue().setText(comment).setTextStyle(commentTextStyle).build();
  SpreadsheetApp.getActiveSheet().getRange('A1').setValue(`My comment - ${comment}`)


}

I expect to see default font color for My comment - and red for text from input box.

It's does not work. Where I'm wrong and how to fix it?

Upvotes: 0

Views: 300

Answers (1)

TheMaster
TheMaster

Reputation: 50482

.setValue only sets the value. To set rich text, use .setRichTextValue. Text style should also be offseted as necessary.

Snippet:

richComment = SpreadsheetApp
    .newRichTextValue()
    .setText(`My comment - ${comment}`)// modified
    .setTextStyle(13, 13+comment.length-1, commentTextStyle)//modified
    .build();

SpreadsheetApp
  .getActiveSheet()
  .getRange('A1')
  .setRichTextValue(richComment)//modified

Upvotes: 2

Related Questions