How to utilize if/else condition for JSON column formatting in SharePoint?
-
06-02-2021 - |
Question
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', '')"
}
}
La solution
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'))))"
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange