SharePoint列表格式化异常求助:按Ganztägig字段控制内容显示
原代码存在的问题
- 布尔值判断逻辑错误:Ganztägig作为复选框字段,直接调用
[$Ganztägig]即可获取布尔值;若使用.displayValue,返回的是字符串"TRUE"/"FALSE",不能直接和布尔值TRUE做比较。 - 未满足需求中的显示要求:原代码仅返回日期,未添加指定的“ganztägig”字样。
- 冗余括号可能影响代码解析。
修正后的格式化代码
方案一(推荐,直接使用字段布尔值)
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "txtContent": "=if([$Ganztägig], toLocaleDateString(@currentField) + ' ganztägig', toLocaleString(@currentField))" }
方案二(针对字段值为字符串的特殊场景)
{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "txtContent": "=if([$Ganztägig.displayValue] == 'TRUE', toLocaleDateString(@currentField) + ' ganztägig', toLocaleString(@currentField))" }
代码说明
- 当
Ganztägig为true时,调用toLocaleDateString仅提取日期,再拼接“ ganztägig”字样; - 当
Ganztägig为false时,调用toLocaleString返回包含日期和时间的完整格式。
内容的提问来源于stack exchange,提问作者ZBHorizon
相关产品推荐
相关产品推荐

