Home > Mobile >  Change color of text based on suits
Change color of text based on suits

Time:05-02

A guy helped me to this func, so now I want to change based on a for loop and based on green, blue, black, based on flop suits, if card is a club = green, etc, any helps TY

EXAMpLE enter image description here

function changeColor(cel, flop) {

  const spreadsheet = SpreadsheetApp.getActive();
  const sheet = spreadsheet.getSheetByName("Sheet1");
  const range = sheet.getRange(cel);
  
  const black = SpreadsheetApp.newTextStyle()
    .setForegroundColor("black")
    .build();

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

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

  const richText = SpreadsheetApp.newRichTextValue()
  .setText(flop)
  .setTextStyle(0, 2, black)
  .setTextStyle(3, 5, blue)
  .setTextStyle(6, 8, green)
  .build();

  range.setRichTextValue(richText); 
}

CodePudding user response:

Description

Assuming a cell contains text representing three cards. Each card seperated by a space. Change the color of the card based on the suit ♠ (black), ♥ (red), ♦ (blue), ♣ (green).

In my example I could have used Range.setRichTextValues() since the cell are contiguous but chose not to because I don't know the OP's intended use.

enter image description here

Script

function flop() {
  try {
    let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
    let values = sheet.getDataRange().getValues();
    for( let i=0; i<values.length; i   ) {
      let cell = 'B' (i 1).toString();
      changeColor(cell,values[i][0]);
    }
  }
  catch(err) {
    console.log(err);
  }
}

function changeColor(cell,flop) {
  try {
    const spreadsheet = SpreadsheetApp.getActive();
    const sheet = spreadsheet.getSheetByName("Sheet1");
    let cards = flop.split(' ');
    const black = SpreadsheetApp.newTextStyle()
      .setForegroundColor("black")
      .build();

    const red = SpreadsheetApp.newTextStyle()
      .setForegroundColor("red")
      .build();

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

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

    let colors = [];
    let range = [];
    let j = 0;
    for( let i=0; i<cards.length; i   ) {
      let card = cards[i];
      let suit = card.charAt(card.length-1);
      switch(suit) {
        case '♠':
          colors.push(black);
          break
        case '♥':
          colors.push(red);
          break
        case '♦':
          colors.push(blue);
          break
        case '♣':
          colors.push(green);
          break
      }
      range.push([j,j card.length]);
      j = j card.length 1;
    }

    const richText = SpreadsheetApp.newRichTextValue()
      .setText(flop)
      .setTextStyle(range[0][0], range[0][1], colors[0])
      .setTextStyle(range[1][0], range[1][1], colors[1])
      .setTextStyle(range[2][0], range[2][1], colors[2])
      .build();

    sheet.getRange(cell).setRichTextValue(richText);
  }
  catch(err) {
    console.log(err);
  }
}

Reference

  • On DB... On form Query applied..

  • Related