Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing color of cell and font

It's my first time working with Google scripts. I want to change the background color of a row and text when the row contains certain values. I've had some success using this piece of code:

function onEdit() 
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1');
  var rows = sheet.getRange('a1:z');
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  for (var i = 0; i <= numRows - 1; i++)
  {
    var n = i+1;
    var backgroundColor;
    var textColor;

    if(values[i].indexOf('Won'))
    {
      backgroundColor = 'red';
      textColor = 'blue';
    }
    else if(values[i].indexOf('Lost'))
    {
      backgroundColor = 'green';
      textColor = 'yellow';
    }

    sheet.getRange('a'+n+':z'+n).setBackgroundColor(backgroundColor);    
    sheet.getRange('a'+n+':z'+n).setFontColor(textColor); 
  }
}

So now rows containing Won turn green with yellow text, and rows containing Lost turn red with blue text.

A couple of problems:

  1. Every empty row also turn red- how do I fix this?
  2. My if/else function seems to be inverted, yet it works perfectly. The way I understand it is that if the row contains Won it should actually turn red with blue text. Can anyone help me understand this part better?
like image 821
Code Vader Avatar asked Jan 29 '15 14:01

Code Vader


People also ask

How do you make a cell change color based on data?

On the Home tab, in the Style group, click the arrow next to Conditional Formatting, and then click Highlight Cells Rules. Select the command you want, such as Between, Equal To Text that Contains, or A Date Occurring. Enter the values you want to use, and then select a format.

How do I keep the Font color in Excel?

Select the Home tab in the toolbar at the top of the screen and click on the Font Color button in the Font group. Select the color from the popup window.

How do I use multiple Font colors or fonts in one cell in Excel?

Select the cell you want to apply multiple font colors or fonts, then get into the cell with pressing the F2 key (or just double click the cell). 2. Select the text you need to use different font colors or fonts, click Home > Font Color or Font in the Font group, then specify the certain font color or font you need.


2 Answers

I tried this and it worked for me, but superb answer by redpandasuit (in the comments below) for anyone who want to try it through conditional formatting. Here is the code version (Google scripts):

function onEdit() 
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('ZB Squash 2015');
  var rows = sheet.getRange('a1:z');
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  for (var i = 0; i <= numRows - 1; i++)
  {
    var n = i+1;
    var backgroundColor;
    var textColor;


      if(values[i].indexOf('Won') == -1)
      {
        backgroundColor = 'red';
        textColor = 'yellow';
      }

      if(values[i].indexOf('Lost') == -1)
      {
        backgroundColor = 'green';
        textColor = 'yellow';
      }

      if(values[i].indexOf('Won') && values[i].indexOf('Lost'))
      {
        backgroundColor = 'white';
      }

    sheet.getRange('a'+n+':z'+n).setBackgroundColor(backgroundColor);    
    sheet.getRange('a'+n+':z'+n).setFontColor(textColor); 
  }
}

Rows where the user won now turns green, rows where the user lost turns red, and all others remains white.

like image 165
Code Vader Avatar answered Sep 20 '22 20:09

Code Vader


I would try using conditional formatting to solve this issue. To do so highlight your desired rows, right click them and select Conditional Formatting.

In the drop down box select Custom formula and try

=RegExMatch($A1,"(?i)Won")

In this formula the A1 signifies which column you will be entering the text in. Change it as need be.

you can then select the text and background colour from the options available next to the formula box and select your range to add constraints to what is coloured.

Then repeat the process and add a second custom formula for "Lost"

=RegExMatch($A1,"(?i)Lost")

These formulas can be further customized to have multiple trigger words, case sensitivity and much more. Check out this link for ideas: https://support.google.com/a/answer/1371417?hl=en

I hope this helps:)

like image 36
redpandasuit Avatar answered Sep 22 '22 20:09

redpandasuit