SharePoint自动生成JSON格式化字符串包含匹配问题求助
1. 计算字段Display包含"LOCATED"的匹配实现
原来的精确匹配逻辑仅识别完全等于"LOCATED"的情况,改用indexOf函数判断字符串包含关系即可——只要返回值≥0,就说明目标字符串存在:
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/row-formatting.schema.json", "additionalRowClass": "=if(indexOf([$Display], 'LOCATED') >= 0, 'sp-field-severity--warning', '')" }
- 注意:
[$Display]要替换成该计算字段的内部名称(不是显示名,可在列表设置→字段设置的地址栏中找到Field=xxx的xxx部分)。 - 若不想用内置样式类,可直接定义自定义背景色:
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/row-formatting.schema.json", "style": { "background-color": "=if(indexOf([$Display], 'LOCATED') >= 0, '#fff3cd', '')" } }
- 需要忽略大小写时,统一转换为小写/大写后再判断:
=if(indexOf(toLower([$Display]), 'located') >= 0, 'sp-field-severity--warning', '')
2. 选择字段精确匹配的修复
选择字段在JSON格式化中返回的是对象类型,而非纯字符串,直接匹配字符串会失败,必须访问其value属性:
假设选择字段的内部名称是Status,要匹配"已完成"选项:
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/row-formatting.schema.json", "additionalRowClass": "=if([$Status].value == '已完成', 'sp-field-severity--success', '')" }
- 如果是多选的选择字段,需用
includes函数判断数组中是否包含目标值:
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/row-formatting.schema.json", "additionalRowClass": "=if(includes([$MultiChoiceStatus], '已完成'), 'sp-field-severity--success', '')" }
关键提醒
- 所有字段必须使用内部名称,不要用显示名,这是最容易踩的坑;
- 不确定字段返回值时,可在格式化预览中临时输出字段值(比如
"style": {"content": "=[$Display]"}),确认逻辑是否正确。
内容的提问来源于stack exchange,提问作者EXL
相关产品推荐
相关产品推荐

