I have moved over to using google forms for data entry to try and improve data integrity. However, the conditional formatting is thrown off after ever new entry is added onto the bottom of the sheet. It means constantly having to change the formatting range manually, which isnt a lot of work but a bit frustrating when you want to leave the sheet running by itself.
I am new to apps script (and coding in general!) but have been trying to piece together something that might work. I want the cells in column A (A4:A) to change colour depending on the value of the cell. So far I have the below - which works for Row 4 but none of the other rows. Im struggling to get the code to recognise that there is more data below the first row.
The error I'm getting says:
"The number of rows in the data does not match the number of rows in the range. The data has 1 but the range has 196."
function conditionalformatting(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Warehouse Queries');
var range = sheet.getRange(4,1,sheet.getLastRow(),1);
var cellvalues= range.getValues();
var colors = [];
if (cellvalues = 'Warehouse Admin to Action') {
colors.push(["#6db05f"]);}
else if (cellvalues = 'Warehouse Coordinator to Action') {
colors.push(["#f0c784"]);}
else if (cellvalues = 'POD required - Supply Chain to source') {
colors.push(["#84bef0"]);}
else {colors.push([""]);}
range.setBackgrounds(colors);
}
Thanks!
Modification points:
colors.push(["#6db05f"])
is always run. Becausecellvalues = 'Warehouse Admin to Action'
is used as true. And,cellvalues = 'Warehouse Admin to Action'
means that a text of'Warehouse Admin to Action'
is substituted to a variblecellvalues
. If you want to compare the value, please use==
or===
instead of=
.colors
is always["#6db05f"]
.var range = sheet.getRange(4,1,sheet.getLastRow(),1);
might bevar range = sheet.getRange(4, 1, sheet.getLastRow() - 3, 1);
. And, I think thatvar range = sheet.getRange("A4:A" + sheet.getLastRow());
might be able to be also used.'Warehouse Admin to Action'
,'Warehouse Coordinator to Action'
,'POD required - Supply Chain to source'
, it is required to use a loop.When these points are reflected in your script, how about the following modification?
Modified script:
When this script is run, the cell values are retrieved from "A4:A", and the background colors are set using
obj
. And, the background color of "A4:A" is changed.In order to search the values and set the color code, I used
obj
.Reference: