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', '')"

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'))))"
| improve this answer | |
  • I tried this, but it's giving me the "Please enter valid column-formatting JSON." error. – wahle509 Mar 12 '19 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 Mar 12 '19 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 :) !! – Gautam Sheth Mar 12 '19 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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