I'm looking for some conditional formatting solution where a new cell background color is set to a unique cell text. The image below shows this.
The tricky part is that I won't know any of the values within the list beforehand.
I'm looking for some conditional formatting solution where a new cell background color is set to a unique cell text. The image below shows this.
The tricky part is that I won't know any of the values within the list beforehand.
onEdit
onEdit
function should compare your new entry (the last row of a predefined column) against the already existing values in the column with indexOf()indexOf
either this color is not contained in the already present background colorsSample:
function onEdit(e) {
if(e.range.getColumn()==1){
var text = e.value;
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var range = sheet.getRange(1,1,sheet.getLastRow(),1);
var values = range.getValues();
var array = [];
var row = e.range.getRow();
for (var i =0; i <values.length; i++){
if(row!=(i+1))
{
array.push(values[i][0]);
}
}
if(array.indexOf(text)==-1){
var backgrounds = range.getBackgrounds();
var color = getRandomColor();
while(backgrounds.indexOf(color)>-1){
color = getRandomColor();
}
buildConditionalFormatting(text, color)
}
}
}
function getRandomColor() {
var letters = '0123456789abcdef';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function buildConditionalFormatting(text, color){
var sheet = SpreadsheetApp.getActiveSheet();
var formattingRange = sheet.getRange("A:A");
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenTextEqualTo(text)
.setBackground(color)
.setRanges([formattingRange])
.build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);
}
if(e.range.getColumn()==2)
var range = sheet.getRange(1,1,sheet.getLastRow(),2);
var formattingRange = sheet.getRange("A:B");
Why isn't working? –
Somali if(e.range.getColumn()==1 || e.range.getColumn()==2)
var range = sheet.getRange(1,1,sheet.getLastRow(),2);
var formattingRange = sheet.getRange("A:B");
. Thanks again. I'll try to figure out next how to make is accept any column. –
Somali © 2022 - 2024 — McMap. All rights reserved.
onEdit
trigger. – Hesperus