( by Aram Donabedian )
I was wondering if there was a way to assign conditional formatting based on the number of comma separators in a cell? IE: A,=green B,C,=Blue D,E,F,=Yellow
The letter values won't always be fixed and will be different from cell to cell so I want the number of commas in the cell to be the condition.
Can I do that?
Thanks
Have a look at the following animated screenshot:
Have a look at the following code:
///////////////////////////////////////
// This script has been developed by Kishan
// For more visit: iGoogleDrive.blogspot.com
function onEdit(e) {
condition();
};
function condition() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
if(s.getName() == "Sheet1")
{
var cell = s.getActiveCell();
var a1_Notation = cell.getA1Notation();
if(a1_Notation=="A1") {
var str = cell.getValue().toString();
var count = str.split(",").length - 1;
if(count==1) cell.setBackground("lightgreen");
else if(count==2) cell.setBackground("blue");
else if(count==3) cell.setBackground("yellow");
else cell.setBackground("white");
}
}
};
///////////////////////////////////////
put the above code in your script editor and then whenever you edit on cell "A1" of sheet "Sheet1", this script will count the number of comma and place the color accordingly.
And If you are not much familiar with scripts then check out the following link:http://igoogledrive.blogspot.com/2012/08/how-to-write-script-in-google.html
I hope the above solution will help you, and if you need more help then please do comment below on this blog itself, I will try to help you out.
I also take up private and confidential projects:
If this blog post was helpful to you, and if you think you want to help me too and make my this blog survive then please donate here: http://igoogledrive.blogspot.com/2012/09/donate.html
This comment has been removed by the author.
ReplyDeleteOk I think I got it running! How do I extend it to an entire column and or range of Columns? IE: Columns C through H?
ReplyDeleteHi Donabedian,
DeleteTry the following code:
function onEdit(e) {
condition();
};
function condition() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
if(s.getName() == "Sheet1")
{
var cell = s.getActiveCell();
var Range = ss.getRange("C2:H24");
if( Range.getLastRow() >= cell.getRow() &&
Range.getRow() <= cell.getLastRow() &&
Range.getLastColumn() >= cell.getColumn() &&
Range.getColumn() <= cell.getLastColumn() ) {
var str = cell.getValue().toString();
var count = str.split(",").length - 1;
if(count==1) cell.setBackground("lightgreen");
else if(count==2) cell.setBackground("blue");
else if(count==3) cell.setBackground("yellow");
else cell.setBackground("white");
}
}
};