Collaxd
Collaxd

Reputation: 181

Google Sheets Color diferent cel strings

I want to color my cels with 3 colors of text, is possible via conditional formating of JS? TY !

Example.

8♠ 6♦ 5♣

3 colors in one cell

Upvotes: 1

Views: 49

Answers (2)

Collaxd
Collaxd

Reputation: 181

const spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
const sheet = spreadsheet.getSheetByName('Sheet1')

function changeColor() {
  const range = sheet.getRange("A1:A5");
  const values = range.getValues()

  black = SpreadsheetApp.newTextStyle()
    .setForegroundColor("black")
    .build();

  blue = SpreadsheetApp.newTextStyle()
    .setForegroundColor("blue")
    .build();

  green = SpreadsheetApp.newTextStyle()
    .setForegroundColor("green")
    .build();

  for (value of values){
    richText = SpreadsheetApp.newRichTextValue()
    .setText(value)
    .setTextStyle(0, 2, black)
    .setTextStyle(3, 5, blue)
    .setTextStyle(6, 8, green)
    .build();
    
    range.setRichTextValue(richText);
  }
}

Upvotes: 0

Marios
Marios

Reputation: 27350

Indeed you have to use the RichTextValueBuilder class. You define the colors and then the positions that these colors take effect.

Script example:

This will generate the desired result in the cell A1 of Sheet1:

function changeColor() {
   var spreadsheet = SpreadsheetApp.getActive();
   var sheet = spreadsheet.getSheetByName("Sheet1");
   var range = sheet.getRange("A1");
   var black = SpreadsheetApp.newTextStyle()
      .setForegroundColor("black")
      .build();

   var blue = SpreadsheetApp.newTextStyle()
      .setForegroundColor("blue")
      .build();

   var green = SpreadsheetApp.newTextStyle()
      .setForegroundColor("green")
      .build();

   var richText = SpreadsheetApp.newRichTextValue()
      .setText("8♠ 6♦ 5♣")
      .setTextStyle(0, 2, black)
      .setTextStyle(3, 5, blue)
      .setTextStyle(6, 8, green)
      .build();

   range.setRichTextValue(richText);
}

Output:

enter image description here

Upvotes: 1

Related Questions