SharePoint列格式化中@currentField.lookupValue遇空查找值报错
我特意针对你的需求写了适配的列格式化JSON代码,完美匹配你提到的5种取值(Red、Amber、Green、Gray、空值),直接复制粘贴到列的“列格式化”设置框里就能用:
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "style": { "padding": "6px 12px", "border-radius": "4px", "display": "flex", "align-items": "center", "gap": "8px", "width": "100%" }, "children": [ { "elmType": "div", "style": { "width": "12px", "height": "12px", "border-radius": "50%", "background-color": "=if(@currentField.lookupValue == 'Red', '#dc3545', if(@currentField.lookupValue == 'Amber', '#ffc107', if(@currentField.lookupValue == 'Green', '#28a745', if(@currentField.lookupValue == 'Gray', '#6c757d', 'transparent'))))" } }, { "elmType": "span", "txtContent": "=if(@currentField.lookupValue, @currentField.lookupValue, '未设置')", "style": { "font-weight": "500", "color": "=if(@currentField.lookupValue == 'Red', '#dc3545', if(@currentField.lookupValue == 'Amber', '#212529', if(@currentField.lookupValue == 'Green', '#28a745', if(@currentField.lookupValue == 'Gray', '#6c757d', '#6c757d'))))" } } ] }
代码说明
- 针对查找列的特性,用
@currentField.lookupValue获取实际状态值,避免匹配错误 - 每种状态对应不同的圆形色块和文本颜色:
- Red:红色块+红色文本
- Amber:黄色块+黑色文本(保证可读性)
- Green:绿色块+绿色文本
- Gray:灰色块+灰色文本
- 空值:透明色块+灰色的“未设置”文本
- 整体样式做了居中对齐、内边距和圆角,和SharePoint的默认风格更协调
如果需要调整样式(比如色块大小、文本字体),直接修改style里的参数就行,比如把width: "12px"改成width: "16px"就能放大色块。
内容的提问来源于stack exchange,提问作者Kirill Gonchar
相关产品推荐
相关产品推荐

