文档首页 / 条件渲染

条件渲染

根据条件动态修改单元格样式、颜色、字体、分页等视觉属性

概述

条件渲染(渲染项 renderItems)允许在满足特定条件时,动态修改单元格的视觉属性——包括背景色、字体颜色、字体大小、边框、强制分页等——无需额外的单元格或复杂的版面设计。

使用场景

  • 负数显示为红色
  • 超出预警值的行标红背景
  • 库存不足时字体加粗
  • 每组数据切换时强制分页
  • 条件控制列宽/行高变化

作用范围(scope)

说明
cell仅当前单元格
row整行(常用于行变色)
column整列

可修改的属性类型

类型可设置属性说明
backgroundbackgroundColor单元格/行背景色(#RRGGBB
colorfontColor字体颜色
fontfontFamily / fontSize / bold / italic / underline字体样式
borderleftBorder / rightBorder / topBorder / bottomBorder边框
heightheight动态行高
widthwidth动态列宽
newValuenewValue(表达式)替换单元格显示值
pagingpagingType: "before" / "after"动态强制分页

条件运算符

条件中 operator 支持以下值:

equals notEquals greatThen EqualsGreatThen lessThen equalsLessThen contains notContains startsWith endsWith isEmpty isNotEmpty

示例:负数标红

$$value < 0 时,将字体颜色改为红色:

renderItems: [{
  name: "负数红色",
  enabled: true,
  conditions: [{
    joinType: "and",
    itemType: "common",
    leftValue: "$$value",
    operator: "lessThen",
    rightValueType: "Number",
    rightValue: 0
  }],
  contents: [{
    type: "color",
    scope: "cell",
    fontColor: "#FF0000"
  }]
}]

示例:整行背景交替

通过 seq() 序号奇偶实现隔行变色:

conditions: [{
  leftValue: "$$value",  // 用表达式类型:seq() % 2 == 0
  operator: ...,
  rightValueType: "Expression",
  rightValue: "seq() % 2 == 0"
}],
contents: [{
  type: "background",
  scope: "row",
  backgroundColor: "#F5F5F5"
}]

示例:分组切换时强制分页

当分组字段值变化时(即每进入新分组),在该行之前强制分页:

contents: [{
  type: "paging",
  scope: "cell",
  pagingType: "before"   // 在此单元格之前分页
}]

提示:通常将分组条件渲染加在分组标题单元格(aggregateType=group),触发条件为 &A2 > 1(跳过第一组,避免第一页前产生空白页)。

联系我们

请填写您的信息,我们将在 1 个工作日内与您联系。