Dec 23 2019 08:37 AM
I am trying to utilize JSON to conditionally add a hyphen to a single line text column based on the length of the data in the field. I have validation on the same column that only allows entry of 6 or 7 characters. If 7 characters are entered the hyphen should display after the 3rd character, if 6 characters are entered it should display after the 2nd character. This is what I have so far but it doesn't work...
{
"$schema": "<a href="https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json" target="_blank">https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json</a>",
"elmType": "div",
"children": [
{
"elmType": "span",
"txtContent": {
"operator": "?",
"operands": [
{
"operator": "==",
"operands": [
"@currentField.length",
"7"
]
},
{
"operator": "?",
"operands": [
{
"operator": "+",
"operands": [
"@currentField.0",
"@currentField.1",
"@currentField.2",
"-",
"@currentField.3",
"@currentField.4",
"@currentField.5",
"@currentField.6"
]
},
{
"operator": "+",
"operands": [
"@currentField.0",
"@currentField.1",
"-",
"@currentField.2",
"@currentField.3",
"@currentField.4",
"@currentField.5"
]
}
]
}
]
}
}
]
}
Jan 02 2020 01:01 PM - edited Jan 02 2020 01:04 PM
Solutionfigured it out myself. I was making it way too hard.
{
"$schema": "<a href="https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json" target="_blank">https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json</a>",
"elmType": "div",
"txtContent": "=if(@currentField.length != 0,substring(@currentField,@currentField.length-4,0) + '-' + substring(@currentField,@currentField.length-4,8),'')",
}
Jan 02 2020 01:01 PM - edited Jan 02 2020 01:04 PM
Solutionfigured it out myself. I was making it way too hard.
{
"$schema": "<a href="https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json" target="_blank">https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json</a>",
"elmType": "div",
"txtContent": "=if(@currentField.length != 0,substring(@currentField,@currentField.length-4,0) + '-' + substring(@currentField,@currentField.length-4,8),'')",
}