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:
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.
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.
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.
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.
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:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With