Does anyone know how I'd be able to make a specific value always appear with the same formatting & colour in my sheet, based on another tab in the sheet?
The spreadsheet I'm trying to do this on is this: https://docs.google.com/spreadsheets/d/1FDmyTa4hMcBJZGYKlh8LbvLJWwLs-FW13JJEvAkp0fQ/edit?usp=sharing
They're labels for my hobby, I've set it up so that the ref tab goes into each label in order but when I reference a number it won't bring the formatting from the tab named "DMC no." with it, I was hoping someone would be able to help me with this in some way, whether it means conditional formatting on the label sheet or being able to reference a cell and the format being bought with it. Conditional formatting on the label sheet would be preferable, but whatever you can do to help me would be good!
I changed and edited the google sheet to further explain what I want to achieve, with a better explanation at the top of the labels sheet. Thank you
Best Answer
You are trying to update the value AND the background colour of a cell when a certain "DMC" number is entered. DMC information is held on a sheet ("DMC no.") and consists of cells with given numerical values and specific background colours.
Revising the DMC data format
There is no function that enables Google Sheets to get the background colour of a given cell, so I propose that the DMC information needs to be recorded in a different format - a 2 Column list. The first column is the DMC value and the second column is the hex colour value associated with that DMC value.
This function modifies the existing DMC information. I created a new sheet "DMC_data" to hold the revised information.
Custom Function - not an option
I wrote a custom function to update the background colour in the target cell (A7) and got an error:
I then read Google Apps script - change the background color of a cell with a hex value from another cell and, as they point out, "Formulas cannot modify cell format". So it's not feasible to create a function that will do this in real time.
Two options seemed available:
onEdit(e)
script. This is a far more sensible option in the circumstancesonEdit() - a logical option
onEdit(e)
needs to take advantage of the Event objects.The evaluation of edited data is in three parts:
sheetname === labelsname
: ensure that the edit took place on sheet = "Labels".modrow === 0 && modcol===0
var modrow = (+row-dmcstartrow)%3;
: this value should be zero; if not then the DMC value has not been edited.var modcol = (+col-dmcstartcol)%3;
: this value should be zero; if not, then the DMC value has not been edited.the script then loops through the DMC data looking for a match on the edited value. If a match is found, then the hex background colour is obtained from the adjacent column, and the edited cell is updated
range.setBackground(data[nn][1]);