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. 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! 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) 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.