You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求修改SharePoint列表条件格式代码,实现百分比两位小数显示

SharePoint Column Formatting: Display as 2-Decimal Percentage with Conditional Highlighting

Got it, let's tweak your code to show the numeric value as a percentage with two decimal places, while keeping the conditional cell highlighting working correctly. Here's the revised code:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "debugMode": true,
  "elmType": "div",
  "attributes": {
    "class": "=if(@currentField >= 0.5, 'sp-field-severity--good', if(@currentField >= 0.25,'sp-field-severity--warning', if(@currentField < 0.25,'sp-field-severity--blocked', '')))"
  },
  "children": [
    {
      "elmType": "span",
      "txtContent": "=@currentField.toString('P2')"
    }
  ]
}

Key Changes Made:

  • Fixed syntax error: Added the missing closing quote for the class attribute (your original code was missing this, which would break the formatting entirely).
  • Formatted as percentage with 2 decimals: Changed the txtContent to use @currentField.toString('P2') — SharePoint's column formatting natively supports this method to convert numeric values to percentage strings, with P2 enforcing exactly two decimal places (e.g., 0.5 becomes 50.00%, 0.333 becomes 33.30%).
  • Removed mismatched icon logic: Your original code had an icon section checking for text values like 'Scheduled', which doesn't align with a numeric percentage field. I removed that since it wouldn't function with your numeric values. If you want to add icons tied to the percentage ranges, you can re-add the span and adjust the iconName logic to use the numeric conditions (e.g., =if(@currentField >= 0.5, 'CheckMark', if(@currentField >=0.25, 'Warning', 'Error'))).

How It Works:

  • Values ≥ 50% get the green "good" highlight
  • Values between 25% and 49.99% get the yellow "warning" highlight
  • Values <25% get the red "blocked" highlight
  • All values display as a percentage with exactly two decimal places

内容的提问来源于stack exchange,提问作者Szymek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 09:02:30