Google 表格颜色不同的单元格字符串

Google Sheets Color diferent cel strings

我想用 3 种颜色的文本为我的单元格着色,可以通过 JS 的条件格式实现吗?泰!

示例。

8♠ 6♦ 5♣

确实你要用RichTextValueBuilderclass。 您定义颜色,然后定义这些颜色生效的位置。

脚本示例:

这将在 Sheet1A1 单元格中生成所需的结果:

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);
}

输出:

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);
  }
}