0

I am trying to format the color of a column based on the selection of another column in the same View. But, I am having trouble writing the JSON to check for multiple values in the other column. Here is my code so far that only checks for one selection, "Red". I want it to check for the other possible options, which are "Green", "Blue", "Amber", and "Gray".

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "background-color": "=if([$OtherColumn] == 'Red', 'red', '')"
  }
}

1 Answer 1

4

You can do it similar to that in Excel :)

You can checkout the below sample to get an idea:

"background-color": "= if([$OtherColumn] == 'Red', 'red', 
if([$OtherColumn] == 'Green', 'green', 
if([$OtherColumn] == 'Amber', 'amber', 
if([$OtherColumn] == 'Gray', 'gray', 'blue'))))"
4
  • I tried this, but it's giving me the "Please enter valid column-formatting JSON." error.
    – wahle509
    Commented Mar 12, 2019 at 14:37
  • 1
    I got it to work! It all had to be in one line (no line breaks for each "if"). Thanks!
    – wahle509
    Commented Mar 12, 2019 at 17:07
  • ahh, i used them for better reading experience, but used the actual no-line breaks in real code, facepalm :( . Happy to help. You can also accept it as answer by ticking the checkmark besides the answer, cheers :) !! Commented Mar 12, 2019 at 17:13
  • Thank you Ganesh, Unfortunately it does not seem to work. Maybe I should have precised that I'm using SP online? An imge that coould help: ![enter image description here](i.sstatic.net/s9Nva.png)
    – Anto
    Commented Jan 12, 2021 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.