博客地址:http://blog.csdn.net/FoxDave
Expressions
txtContent的值、样式属性和属性(attribute)属性可以以表达式的形式展示,它们会在运行时根据当前字段或列表项的上下文被解析。表达式对象可以嵌套其他表达式对象。
Excel-style表达式
所有的Excel样式表达式都以=开始。
简单的条件表达式会在@me不等于[$Author.email]时返回none,否则返回‘’:
=if(@me != [$Author.email], 'none', '')
再复杂一些的if/else条件表达式如下:
=if([$Sentiment] <= 0.3, 'sp-field-severity--blocked', if([$Sentiment] < 0.9,'sp-field-severity--warning','sp-field-severity--good'))
非条件的一元或二元操作可以像下面这样写:
=[$foo] * -7
=sin(@currentField)
=toString(60 + (sin(6.2831853 * @currentField) * 60))
抽象语法树表达式
下面的例子包含一个表达式对象,执行表达式(@currentField > 40) ? '100%' : (((@currentField * 2.5).toString() + '%')
:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"operator": "?",
"operands": [
{
"operator": ">",
"operands": [
"@currentField",
"40"
]
},
"100%",
{
"operator": "+",
"operands": [
{
"operator": "toString()",
"operands": [
{
"operator": "*",
"operands": [
"@currentField",
2.5
]
}
]
},
"%"
]
}
]
}
操作符(Operators)
操作符指定要执行的操作,可用的操作符如下:
-
-
- /
-
- <
- ==
- !=
- <=
-
=
- ||
- &&
- toString()
- Number()
- Date()
- cos
- sin
- ?
- toLocaleString()
- toLocaleDateString()
- toLocaleTimeString()
Binary operators - 标准二元运算符,需要两个运算域:
-
-
- /
-
- <
- <=
-
=
Unary operators - 标准的一元运算符:
- toString()
- Number()
- Date()
- cos
- sin
- toLocaleString()
- toLocaleDateString()
- toLocaleTimeString()
Conditional operator - 条件运算符:?
运算域
指定表达式的参数或运算域。通常是条件表达式或基本值的数组。
特殊字符串值
txtContent的值、样式和属性可以是字符串或表达式对象。一些特殊的字符串可以用于从列表的字段或用户上下文中获取值。
“@currentField”
会转换为当前字段的值。
一些字段类型表示为对象类型。从对象中输出值需要指定对象中的特定属性。例如,如果当前字段是一个用户/组类型,指定@currentField.title来获取用户的显示名,即在列表视图中显示的名称。下面会列举一些对象类型的字段以及它们的属性。
注意:@currentField.title默认返回一个人的显示名。但是如果用户字段的展示字段被调整了,它就可能改变了title属性的值。例如,一个用户字段将显示字段配置为了部门,那么我们从title取到的就是该用户的部门了。
用户字段
{
"id": "122",
"title": "Kalya Tucker",
"email": "[email protected]",
"sip": "[email protected]",
"picture": "https://contoso.sharepoint.com/kaylat_contoso_com_MThumb.jpg?t=63576928822"
}
日期/时间字段
日期/时间字段的值可以通过不同的方式获取,基于我们想要让日期以什么格式显示。以下三个方法支持将日期转换位指定的格式:
- toLocaleString() - 显示完整的日期时间。
- toLocaleDateString() - 只显示日期。
- toLocaleTimeString() - 只显示时间。
例如,下面的JSON会显示当前字段(假设是日期字段)的完整日期时间。
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"elmType": "div",
"txtContent": {
"operator": "toLocaleString()",
"operands" : ["@currentField"]
}
}
查阅项字段
{
"lookupId": "100",
"lookupValue": "North America",
}
下面的例子展示了如何使用查阅项字段。
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"elmType": "a",
"txtContent": "@currentField.lookupValue",
"attributes": {
"href": {
"operator": "+",
"operands": [
"https://contoso.sharepoint.com/teams/Discovery/Lists/Regions/DispForm.aspx?ID=",
"@currentField.lookupId"
]
},
"target": "_blank"
}
}
超链接字段
{
"desc": "SharePoint Patterns and Practices",
}
如要引用URL的值,使用@currentField。
下面的例子展示了如何使用超链接字段。
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"elmType": "a",
"txtContent": "@currentField.desc",
"attributes": {
"href": "@currentField",
"target": "_blank"
}
}
**"[ InternalName]。例如,使用字段的内部名称MarchSales获取字段的值,使用[ SalesLead.title]。
“@me”
该值会转为当前登录用户的邮箱地址。
该字段可以用来显示当前用户的邮件地址,但更多的是用在条件判断上。下面是一个为用户字段设置颜色的例子,如果当前用户跟用户字段一样则标记为红色,反之为蓝色。
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
"elmType": "div",
"txtContent": "@currentField.title",
"style": {
"color": {
"operator": "?",
"operands": [
{
"operator": "==",
"operands": [
"@me",
"@currentField.email"
]
},
"red",
"blue"
]
}
}
}
“@now”
该值会转为当前日期和时间。
“@window.innerHeight”
该值会转为在列表渲染完后等于浏览器窗口高度(像素表示)的数值。
“@window.innerWidth“
该值会转为在列表渲染完后等于浏览器窗口宽度(像素表示)的数值。