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

如何在保留链接的同时将多值Lookup列值显示在一行

多值Lookup列:保留跳转链接+单行显示+无末尾多余逗号

SharePoint里的Lookup列默认会把值做成链接,点击可跳转至对应列表项;要是开启「允许多个值」,每个值就会单独占一行。

之前为了让值单行显示,用了这段列格式代码,虽然实现了逗号分隔,但所有链接都丢失了:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=join(@currentField.lookupValue, ', ')"
}

现在要实现的效果是:每个值保留跳转链接,同时单行排列,最后一个值后无多余逗号。


解决思路

利用forEach循环遍历每个Lookup项,通过判断当前项的索引是否为最后一项,控制逗号的显示:

  • 用loopIndex('lookupItem')获取当前项的索引(从0开始计数)
  • 用length(@currentField)获取总项数
  • 若当前索引等于「总项数-1」,则隐藏逗号;否则显示「, 」作为分隔

最终可用代码

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "forEach": "lookupItem in @currentField",
      "elmType": "span",
      "children": [
        {
          "elmType": "a",
          "txtContent": "=[$lookupItem.lookupValue]",
          "attributes": {
            "target": "_blank",
            "href": "='https://tenant-name.sharepoint.com/sites/site-name/Lists/Some%20List/DispForm.aspx?ID=' + [$lookupItem.lookupId]"
          }
        },
        {
          "elmType": "span",
          "txtContent": ", ",
          "style": {
            "display": "=if(loopIndex('lookupItem') == length(@currentField) - 1, 'none', 'inline')"
          }
        }
      ]
    }
  ]
}

代码说明

  1. 通过forEach遍历每个Lookup值,迭代器命名为lookupItem
  2. 每个值拆分为两个子元素:带跳转链接的文本、分隔用的逗号
  3. 用条件判断控制逗号显示:最后一项的逗号设置display: none隐藏,其余项正常显示
  4. 跳转链接通过拼接lookupId构造,确保能精准跳转到对应列表项的详情页

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 01:24:31