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

如何用JSON设置SharePoint日历视图指定相对日期项的背景色

SharePoint日历视图条件格式化:设置7天内到期项红色背景

问题分析

原代码中直接将@now + 604800000放入Date()操作数里无法被识别,核心原因是SharePoint格式化语法需要正确的表达式嵌套,同时原代码的日期转换逻辑过于冗余,多次调用toDateString()和Date()导致计算异常。

修改后的可用代码

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/row-formatting.schema.json",
  "additionalEventClass": {
    "operator": ":",
    "operands": [
      {
        "operator": "&&",
        "operands": [
          {
            "operator": ">=",
            "operands": [
              "[$Expiration_x0020_Date]",
              "@now"
            ]
          },
          {
            "operator": "<=",
            "operands": [
              "[$Expiration_x0020_Date]",
              "@now + 604800000"
            ]
          }
        ]
      },
      "=if(@isSelected == true, 'sp-css-color-WhiteFont sp-css-backgroundColor-BgDarkRed' , 'sp-css-backgroundColor-BgCoral sp-css-color-CoralFont')",
      ""
    ]
  }
}

关键修改点

  • 移除冗余的日期转换嵌套:SharePoint支持直接对日期列[$Expiration_x0020_Date]和时间戳@now做数值比较,无需多次调用Date()和toDateString()。
  • 正确计算7天后的时间:604800000是7天对应的毫秒数(7×24×60×60×1000),SharePoint能直接识别@now + 604800000这个表达式并计算出7天后的时间戳。
  • 保留原有选中状态样式:未选中项用珊瑚红背景,选中项切换为深红背景,维持原交互逻辑。

验证说明

把上述代码替换原条件格式化代码后,日历视图中ExpirationDate处于今日至7天后的列表项会自动应用对应背景色,完全符合需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 15:07:14