集成流公式使用指引

分类 公式数 公式列表
一、类型转换 5 toint / tonumber / tofloat / tostring / tobool
二、字符串操作 16 split / join / substring / contains / startsWith / endsWith / indexOf / replace_first / length / upper / lower / trim / capitalize / titleize / to_phone / smart_join
三、数学运算 8 round / max / min / sqrt / abs / ceil / floor / sum
四、日期时间 14 sysdate / now / ago / from_now / date_add / date2long / long2date / string_to_date / yweek / yday / wday / beginning_of_month / end_of_month / in_time_zone
五、列表与集合 11 first / last / count / where / include / exclude / uniq / pluck / flatten / seq.add_all / tuple
六、条件判断 4 present / presence / blank / is_empty
七、格式化与工具 6 format_map / to_currency / lookup / lookupMapping / parseJsonStr2Map

一、类型转换

在不同数据类型之间互相转换,是集成流中最基础的操作。

1. toint — 转整数

语法

toint(value)

描述

将任意值转换为整数(去掉小数部分,不是四舍五入)。

示例

输入 公式 输出
字符串 "5.1234" toint("5.1234") 5

更多示例

toint("42")        → 42
toint(3.99)        → 3     (直接截断,不是四舍五入)
toint("abc")       → 0     (无法解析时返回 0)

小技巧

  • 小数直接截断,不四舍五入。需要四舍五入用 math.round
  • 无法转换的值会返回 0,注意检查源数据
  • 如果源数据可能为 nil,先配合 presence 判断

2. tonumber — 转数字

语法

tonumber(value)

描述

将值转换为数字类型,支持小数。比 toint 更通用。

示例

输入 公式 输出
字符串 "123.45" tonumber("123.45") 123.45

更多示例

tonumber("42")         → 42
tonumber("3.14159")    → 3.14159
tonumber("")           → 0

小技巧

  • 可以保留小数精度
  • 空字符串会被转为 0
  • 需要整数时优先用 toint,需要保留精度时用 tonumber

3. tofloat — 转浮点数

语法

tofloat(value)

描述

将值转换为浮点数,与 tonumber 功能类似。

示例

输入 公式 输出
字符串 "3.14159" tofloat("3.14159") 3.14159

更多示例

tofloat("99.99")    → 99.99
tofloat(42)         → 42.0
tofloat("abc")      → 0.0

小技巧

  • 日常使用中 tonumbertofloat 效果基本一致
  • 无法解析返回 0.0

4. tostring — 转字符串

语法

tostring(value)

描述

将任意类型的值转换为字符串。数字、布尔值、日期等都可以转。

示例

输入 公式 输出
数字 123 tostring(123) "123"
布尔 true tostring(true) "true"

更多示例

tostring(3.14)         → "3.14"
tostring(false)        → "false"
tostring(nil)          → ""      (nil 转成空字符串)

小技巧

  • 拼接字符串时如果遇到类型不匹配,先用 tostring 转一下
  • nil 会变成空字符串 ""

5. tobool — 转布尔值

语法

tobool(value)

描述

将值转换为布尔值。"true"1"yes"(忽略大小写)视为 true,其他视为 false

示例

输入 公式 输出
字符串 "true" tobool("true") true
字符串 "yes" tobool("yes") true
字符串 "0" tobool("0") false

更多示例

tobool("TRUE")    → true    (忽略大小写)
tobool("YES")     → true
tobool(1)         → true
tobool(0)         → false
tobool("no")      → false
tobool("")        → false

小技巧

  • 不区分大小写:"TRUE""True" 都视为 true
  • "yes""YES" 也视为 true
  • 除了 true / 1 / yes,其他一律 false

二、字符串操作

字符串处理是集成流中用得最多的场景,掌握这些公式可以应对 90% 的数据清洗需求。

6. string.split — 字符串分割

语法

string.split(string, separator)

描述

用分隔符将字符串切割为数组,分隔符会被移除。

示例

输入 公式 输出
"a,b,c" string.split("a,b,c", ",") ["a", "b", "c"]

更多示例

string.split("张三,李四,王五", ",")    → ["张三", "李四", "王五"]
string.split("a|b|c", "|")          → ["a", "b", "c"]
string.split("hello world", " ")    → ["hello", "world"]

小技巧

  • 分隔符在结果中会被移除
  • 支持正则表达式作为分隔符
  • 和 Workato 原生 .split 是同一个公式的不同写法

7. string.join — 字符串连接

语法

string.join(array, separator)

描述

用指定分隔符将数组元素连接成一个字符串。split 的反向操作。

示例

输入 公式 输出
["a", "b", "c"] string.join(["a", "b", "c"], ",") "a,b,c"

更多示例

string.join(["张三", "李四"], "、")        → "张三、李四"
string.join(["2026", "07", "02"], "-")   → "2026-07-02"
string.join([], ",")                     → ""

小技巧

  • split 的反向操作,两者经常搭配使用
  • 空数组返回空字符串
  • 分隔符可以是任意字符串,如 " - ""、"

8. string.substring — 字符串截取

语法

string.substring(string, startIndex, endIndex)

描述

从字符串中截取指定区间(左闭右开)的子串。索引从 0 开始。

示例

输入 公式 输出
"hello world" string.substring("hello world", 0, 5) "hello"

更多示例

string.substring("hello world", 6, 11)   → "world"
string.substring("1234567890", 0, 4)     → "1234"
string.substring("hello", 1, -1)         → "ell"

小技巧

  • 索引从 0 开始,左闭右开区间 [start, end)
  • 截取到末尾可以用负索引或直接省略 endIndex
  • 常用于截取手机号后 4 位、身份证信息等

9. string.contains — 字符串包含

语法

string.contains(string, substring)

描述

判断字符串中是否包含指定的子串,返回 truefalse

示例

输入 公式 输出
"hello world" string.contains("hello world", "world") true

更多示例

string.contains("张三丰", "张")       → true
string.contains("hello", "xyz")      → false
string.contains("abc", "")           → true    (空字符串始终匹配)

小技巧

  • 区分大小写,"Hello" 不包含 "hello"
  • 常用于条件分支判断
  • 空字符串作为子串时始终返回 true

10. string.startsWith — 以…开头

语法

string.startsWith(string, prefix)

描述

判断字符串是否以指定前缀开头。

示例

输入 公式 输出
"hello world" string.startsWith("hello world", "hello") true

更多示例

string.startsWith("CN13800138000", "CN")     → true
string.startsWith("hello", "he")             → true
string.startsWith("hello", "lo")             → false

小技巧

  • 常用于判断字段前缀、编码规则等
  • 区分大小写

11. string.endsWith — 以…结尾

语法

string.endsWith(string, suffix)

描述

判断字符串是否以指定后缀结尾。

示例

输入 公式 输出
"hello.txt" string.endsWith("hello.txt", ".txt") true

更多示例

string.endsWith("report.pdf", ".pdf")        → true
string.endsWith("hello world", "world")      → true
string.endsWith("hello", "he")               → false

小技巧

  • 常用于判断文件扩展名、URL 后缀等
  • 区分大小写

12. string.indexOf — 子串位置

语法

string.indexOf(string, substring)

描述

返回子串在字符串中首次出现的位置(从 0 开始),未找到返回 -1

示例

输入 公式 输出
"hello world" string.indexOf("hello world", "world") 6

更多示例

string.indexOf("hello world", "o")     → 4
string.indexOf("hello", "xyz")         → -1
string.indexOf("abcabc", "b")          → 1     (首次出现的位置)

小技巧

  • 索引从 0 开始,第一个字符位置是 0
  • 未找到返回 -1,不是 0
  • 通常和 string.contains 搭配使用:先判断是否存在,再获取位置

13. string.replace_first — 字符串替换

语法

string.replace_first(string, old, new)

描述

替换字符串中第一个匹配的子串。

示例

输入 公式 输出
"hello world" string.replace_first("hello world", "world", "there") "hello there"

更多示例

string.replace_first("a-b-c-d", "-", "/")          → "a/b-c-d"    (只替换第一个)
string.replace_first("13800138000", "138", "CN")    → "CN00138000"

小技巧

  • 只替换首次匹配,不是全局替换
  • 需要全局替换时可能要多次调用,或结合其他方法

14. string.length — 字符串长度

语法

string.length(string)

描述

计算字符串的字符数量。

示例

输入 公式 输出
"hello" string.length("hello") 5

更多示例

string.length("你好世界")    → 4
string.length("")           → 0
string.length("a b c")      → 5

小技巧

  • 中文每个字算 1 个长度
  • 空格也算 1 个长度
  • 常用于校验手机号位数(11 位)、身份证号位数(18 位)等

15. string.upper — 转大写

语法

string.upper(string)

描述

将字符串中所有字母转为大写。

示例

输入 公式 输出
"hello world" string.upper("hello world") "HELLO WORLD"

更多示例

string.upper("Abc123")     → "ABC123"
string.upper("你好world")   → "你好WORLD"    (中文不受影响)

小技巧

  • 只转换英文字母,中文、数字等不受影响
  • 常用于统一编码格式、大小写不敏感的比较

16. string.lower — 转小写

语法

string.lower(string)

描述

将字符串中所有字母转为小写。

示例

输入 公式 输出
"Hello World" string.lower("Hello World") "hello world"

更多示例

string.lower("ABC")        → "abc"
string.lower("USER123")    → "user123"

小技巧

  • string.upper 互逆
  • 常用于做大小写不敏感的匹配:string.lower(a) == string.lower(b)

17. string.trim — 去除首尾空白

语法

string.trim(string)

描述

移除字符串开头和结尾的空白字符(空格、制表符、换行符等)。

示例

输入 公式 输出
" hello " string.trim(" hello ") "hello"

更多示例

string.trim("   张三   ")    → "张三"
string.trim("\nhello\n")    → "hello"
string.trim("a b c")        → "a b c"    (中间空格保留)

小技巧

  • 只去掉首尾空白,中间的空格保留
  • 从外部系统(Excel、表单等)导入数据时几乎必用
  • 可以去掉换行符 \n、制表符 \t

18. capitalize — 首字母大写

语法

capitalize(string)

描述

将字符串的第一个字符转为大写,其余保持不变。

示例

输入 公式 输出
"hello world" capitalize("hello world") "Hello world"

更多示例

capitalize("john")      → "John"
capitalize("ABC")       → "ABC"    (已经是全大写不变)
capitalize("")          → ""

小技巧

  • 只是首字母大写,不是每个单词(用 titleize 实现每个单词首字母大写)
  • 中文开头不受影响

19. titleize — 单词首字母大写

语法

titleize(string)

描述

将字符串中每个单词的首字母转为大写。

示例

输入 公式 输出
"hello world" titleize("hello world") "Hello World"

更多示例

titleize("john smith")          → "John Smith"
titleize("HELLO WORLD")         → "Hello World"
titleize("a tale of two")       → "A Tale Of Two"

小技巧

  • capitalize 的区别:capitalize 只处理整个字符串的第一个字符,titleize 处理每个单词
  • 常用于格式化人名、标题

20. to_phone — 电话字符过滤

语法

to_phone(string)

描述

过滤字符串,只保留电话号码中的有效字符:+-、数字、括号 ()

示例

输入 公式 输出
"+86 138 1234 5678" to_phone("+86 138 1234 5678") "+8613812345678"

更多示例

to_phone("138-1234-5678")         → "138-1234-5678"
to_phone("(010) 1234 5678")       → "(010)12345678"
to_phone("手机:13800138000")       → "13800138000"

小技巧

  • 会自动去掉空格、中文等非电话字符
  • +-() 会保留
  • 处理从 Excel 或表单导入的电话号码时非常实用

21. smart_join — 智能连接

语法

smart_join(array)

描述

用空格连接数组元素,自动跳过空字符串和 nil,并对每个元素修剪空格。

示例

输入 公式 输出
["hello", "", "world"] smart_join(["hello", "", "world"]) "hello world"

更多示例

smart_join(["张三", "", "李四"])       → "张三 李四"
smart_join(["a", "  b  ", "c"])      → "a b c"
smart_join(["", "", ""])             → ""

小技巧

  • string.join 更智能,自动处理空值和空格
  • 分隔符固定为空格
  • 拼接姓名、地址等场景特别好用

三、数学运算

数值四舍五入、取整、求和等基础运算。

22. math.round — 四舍五入

语法

math.round(number)

描述

对数值进行四舍五入取整。

示例

输入 公式 输出
3.6 math.round(3.6) 4

更多示例

math.round(3.4)    → 3
math.round(3.5)    → 4
math.round(-1.5)   → -1

小技巧

  • 3.5 四舍五入为 4
  • 负数也是同样规则
  • 需要指定小数位数时用 ceilfloor

23. math.max — 最大值

语法

math.max(value1, value2, ...)

描述

返回所有传入参数中的最大值。

示例

输入 公式 输出
5, 12, 3 math.max(5, 12, 3) 12

更多示例

math.max(1, 2, 3, 4)     → 4
math.max(-1, -5, -3)     → -1
math.max(100, 50)        → 100

小技巧

  • 可以传入任意数量的参数
  • 常用于取两个值中较大的那个

24. math.min — 最小值

语法

math.min(value1, value2, ...)

描述

返回所有传入参数中的最小值。

示例

输入 公式 输出
5, 12, 3 math.min(5, 12, 3) 3

更多示例

math.min(1, 2, 3, 4)     → 1
math.min(-1, -5, -3)     → -5

小技巧

  • math.max 搭配使用可实现范围限制

25. math.sqrt — 平方根

语法

math.sqrt(number)

描述

计算数值的平方根。

示例

输入 公式 输出
9 math.sqrt(9) 3.0

更多示例

math.sqrt(16)    → 4.0
math.sqrt(2)     → 1.414...
math.sqrt(0)     → 0.0

小技巧

  • 返回浮点数
  • 负数会报错,调用前确保值 ≥ 0

26. math.abs — 绝对值

语法

math.abs(number)

描述

返回数值的绝对值(去掉负号)。

示例

输入 公式 输出
-5 math.abs(-5) 5

更多示例

math.abs(10)     → 10
math.abs(-3.14)  → 3.14
math.abs(0)      → 0

小技巧

  • 计算差额、偏差时常用

27. ceil — 向上取数

语法

ceil(number, precision)

描述

按指定精度向上取数。不传精度时取整到整数(不小于原数的最小整数)。

示例

输入 公式 输出
5.126 ceil(5.126, 2) 5.13

更多示例

ceil(5.126)         → 6     (不传精度,向上取整)
ceil(5.129, 2)      → 5.13
ceil(5.001, 2)      → 5.01
ceil(-1.5)          → -1

小技巧

  • 不传第二个参数时等同于向上取整
  • 第二个参数表示保留几位小数
  • 常用于"至少需要 N 个"的计算场景

28. floor — 向下取数

语法

floor(number, precision)

描述

按指定精度向下取数。不传精度时取整到整数(不大于原数的最大整数)。

示例

输入 公式 输出
5.129 floor(5.129, 2) 5.12

更多示例

floor(5.129)         → 5     (不传精度,向下取整)
floor(5.999, 2)      → 5.99
floor(-1.5)          → -2

小技巧

  • ceil 相反
  • 常用于"最多给 N 个"的场景

29. sum — 求和

语法

sum(array)

描述

对数组中的所有数值元素求和。

示例

输入 公式 输出
[1, 2, 3, 4] sum([1, 2, 3, 4]) 10

更多示例

sum([10, 20, 30])    → 60
sum([])              → 0
sum([-1, 2, -3, 4])  → 2

小技巧

  • 空数组返回 0
  • 常用于合计金额、数量等
  • 结合 pluck 可以先提取字段再求和

四、日期时间

集成流中处理时间戳、日期格式转换、时区换算等高频操作。

30. sysdate — 系统日期时间

语法

sysdate()

描述

返回当前系统的日期时间,格式为 yyyy-MM-dd HH:mm:ss

示例

输入 公式 输出
(无) sysdate() 2026-04-02 10:00:00

小技巧

  • 不需要参数
  • 返回的是格式化字符串,不是时间戳
  • 需要时间戳用 now

31. now — 当前时间戳

语法

now()

描述

获取当前系统时间的时间戳(毫秒)。

示例

输入 公式 输出
(无) now() 1743565200000(当前时间戳)

小技巧

  • 返回毫秒级时间戳(13 位数字)
  • 常用于计算时间差、记录操作时间等
  • 需要通过 long2date 才能转换为可读日期

32. ago — 过去时间

语法

ago(amount, unit)

描述

计算当前时间往前推指定时间量,返回时间戳。unit 支持 "day""hour""minute""second""month""year" 等。

示例

输入 公式 输出
往前 3 天 ago(3, "day") 1743523200000(当前时间往前 3 天的时间戳)

更多示例

ago(1, "hour")       → 一小时前的时间戳
ago(7, "day")        → 七天前的时间戳
ago(1, "month")      → 一个月前的时间戳

小技巧

  • 常用场景:查询近 7 天数据、获取昨天的时间戳
  • 搭配 from_now 可表示时间区间

33. from_now — 未来时间

语法

from_now(amount, unit)

描述

计算当前时间往后推指定时间量,返回时间戳。是 ago 的"未来"版本。

示例

输入 公式 输出
往后 3 天 from_now(3, "day") 1743955200000(当前时间往后 3 天的时间戳)

更多示例

from_now(1, "hour")      → 一小时后的时间戳
from_now(30, "minute")   → 30 分钟后的时间戳
from_now(1, "year")      → 一年后的时间戳

小技巧

  • ago 相反方向
  • 常用于设置截止时间、过期时间

34. date_add — 日期加减

语法

date_add(timestamp, amount, unit)

描述

对指定日期时间戳进行加减操作。unit 支持 "day""month""year""hour" 等。

示例

输入 公式 输出
指定日期加 5 天 date_add(1743523200000, 5, "day") 1743955200000

更多示例

date_add(1743523200000, -1, "day")     → 1743436800000    (减一天)
date_add(1743523200000, 1, "month")    → 1746035200000    (加一个月)
date_add(1743523200000, 1, "year")     → 1775059200000    (加一年)

小技巧

  • amount 可以为负数,实现减法效果
  • ago/from_now 更灵活,可以基于任意时间戳计算

35. date2long — 日期转时间戳

语法

date2long(dateString, format, unit)

描述

将日期字符串按指定格式转换为时间戳。

示例

输入 公式 输出
"2026-04-02 10:00:00" date2long("2026-04-02 10:00:00", "yyyy-MM-dd HH:mm:ss", "ms") 1743565200000

更多示例

date2long("2026-04-02", "yyyy-MM-dd", "ms")             → 1743523200000
date2long("04/02/2026", "MM/dd/yyyy", "ms")             → 1743523200000
date2long("20260402", "yyyyMMdd", "ms")                 → 1743523200000

小技巧

  • 第三个参数 unit"ms" 表示毫秒,"s" 表示秒
  • format 必须和日期字符串的实际格式一致
  • 常用格式符号:yyyy=年,MM=月,dd=日,HH=时,mm=分,ss=秒

36. long2date — 时间戳转日期

语法

long2date(timestamp, format, unit)

描述

将时间戳按指定格式转换为可读的日期字符串。date2long 的反向操作。

示例

输入 公式 输出
1743565200000 long2date(1743565200000, "yyyy-MM-dd HH:mm:ss", "ms") "2026-04-02 10:00:00"

更多示例

long2date(1743565200000, "yyyy-MM-dd", "ms")              → "2026-04-02"
long2date(1743565200000, "yyyy/MM/dd", "ms")              → "2026/04/02"
long2date(1743565200000, "yyyy年MM月dd日", "ms")            → "2026年04月02日"

小技巧

  • date2long 的反向函数,两者是一对
  • format 可以自定义任意格式

37. string_to_date — 字符串转日期

语法

string_to_date(dateString, format)

描述

将字符串按指定格式解析为日期对象。

示例

输入 公式 输出
"2026-04-02" string_to_date("2026-04-02", "yyyy-MM-dd") 2026-04-02 00:00:00

更多示例

string_to_date("2026/04/02", "yyyy/MM/dd")          → 2026-04-02 00:00:00
string_to_date("20260402", "yyyyMMdd")              → 2026-04-02 00:00:00

小技巧

  • 返回的是日期对象,不是时间戳
  • 如果不需要时间戳只需要日期判断,用这个即可

38. yweek — 年内第几周

语法

yweek(timestamp)

描述

返回日期在一年中是第几周(1-53)。

示例

输入 公式 输出
1743523200000 yweek(1743523200000) 14(2026 年 4 月 2 日是年内第 14 周)

更多示例

yweek(now())       → 当前是年内第几周

小技巧

  • 返回 1-53 的整数
  • 常用于按周统计报表

39. yday — 年内第几天

语法

yday(timestamp)

描述

返回日期在一年中是第几天(1-366)。

示例

输入 公式 输出
1743523200000 yday(1743523200000) 92(2026 年 4 月 2 日是年内第 92 天)

更多示例

yday(now())       → 当前是年内第几天

小技巧

  • 1 月 1 日是第 1 天
  • 闰年最多到 366

40. wday — 星期几

语法

wday(timestamp)

描述

返回星期几。周日 = 0,周一 = 1,…,周六 = 6。

示例

输入 公式 输出
1743523200000 wday(1743523200000) 4(2026 年 4 月 2 日是星期四)

更多示例

wday(now())       → 今天是星期几(数字)

小技巧

  • 星期从 0 开始:周日是 0,周一是 1
  • 判断工作日:wday(now()) >= 1 and wday(now()) <= 5

41. beginning_of_month — 月初

语法

beginning_of_month(timestamp)

描述

返回该月第一天 00:00:00 的时间戳。

示例

输入 公式 输出
1743523200000 beginning_of_month(1743523200000) 1748966400000(2026 年 5 月 1 日 00:00:00)

更多示例

beginning_of_month(now())   → 本月第一天 00:00:00 的时间戳

小技巧

  • 注意:示例中输入是 4 月的日期,返回的是 5 月 1 日
  • 常用于按月查询的起始时间

42. end_of_month — 月末

语法

end_of_month(timestamp)

描述

返回该月最后一天 23:59:59 的时间戳。

示例

输入 公式 输出
1748966400000 end_of_month(1748966400000) 1751366399000(2026 年 5 月 31 日 23:59:59)

更多示例

end_of_month(now())   → 本月最后一天 23:59:59 的时间戳

小技巧

  • 返回的是 23:59:59,覆盖到最后一秒
  • beginning_of_month 搭配定义月度时间区间

43. in_time_zone — 时区转换

语法

in_time_zone(timestamp, fromZone, toZone)

描述

将时间戳从一个时区转换到另一个时区,返回转换后的时间戳。

示例

输入 公式 输出
上海 → 纽约 in_time_zone(1743523200000, "Asia/Shanghai", "America/New_York") 1743494640000

更多示例

in_time_zone(now(), "Asia/Shanghai", "Asia/Tokyo")           → 东京时间
in_time_zone(now(), "Asia/Shanghai", "Europe/London")        → 伦敦时间

小技巧

  • 时区名使用标准格式:"Asia/Shanghai""America/New_York"
  • 返回的仍然是时间戳,需要用 long2date 格式化

五、列表与集合

集成流中数据经常以数组/列表形式出现,这些公式帮你高效处理。

44. first — 获取首个元素

语法

first(array, n?)

描述

获取列表的第一个元素。如果传入第二个参数 n,则返回前 n 个元素组成的数组。

示例

输入 公式 输出
[1, 2, 3] first([1, 2, 3]) 1
[1, 2, 3] first([1, 2, 3], 2) [1, 2]

更多示例

first(["张三", "李四", "王五"])        → "张三"
first(["张三", "李四", "王五"], 1)     → ["张三"]     (传 1 返回数组)
first([])                            → nil

小技巧

  • 空数组返回 nil,注意做空值判断
  • n 和不传 n 的返回类型不同:前者返回元素,后者返回数组
  • 在线索列表、取最新的记录时非常常用

45. last — 获取末尾元素

语法

last(array, n?)

描述

获取列表的最后一个元素。如果传入第二个参数 n,则返回后 n 个元素。

示例

输入 公式 输出
[1, 2, 3] last([1, 2, 3]) 3
[1, 2, 3] last([1, 2, 3], 2) [2, 3]

更多示例

last(["张三", "李四", "王五"])      → "王五"
last([])                          → nil

小技巧

  • first 类似,空数组返回 nil
  • 常用于获取最新记录

46. count — 计数

语法

count(array)

描述

返回数组或集合中元素的数量。

示例

输入 公式 输出
[1, 2, 3, 4] count([1, 2, 3, 4]) 4

更多示例

count([])           → 0
count(["a", "b"])   → 2
count("hello")      → 5     (字符串也可以用)

小技巧

  • 字符串也可以用,返回字符数
  • 空数组返回 0

47. where — 条件过滤

语法

where(array, condition)

描述

按条件过滤列表,只保留满足条件的元素。条件中用 item 代表当前元素。

示例

输入 公式 输出
[1, 2, 3, 4] where([1, 2, 3, 4], "item > 2") [3, 4]

更多示例

where([1, 2, 3, 4], "item % 2 == 0")                    → [2, 4]      (偶数)
where(["a", "ab", "abc"], "string.length(item) > 1")    → ["ab", "abc"]

小技巧

  • 条件中统一用 item 指代当前元素
  • 条件写法类似编程中的布尔表达式
  • 对象数组可按字段过滤:"item.age > 18"

48. include — 集合包含

语法

include(array, value)

描述

判断数组或字符串中是否包含指定元素/子串。

示例

输入 公式 输出
[1, 2, 3] include([1, 2, 3], 2) true

更多示例

include(["张三", "李四"], "张三")     → true
include("hello", "he")              → true
include([1, 2, 3], 5)              → false

小技巧

  • 对字符串使用时等同于 string.contains
  • 对数组使用时判断元素是否存在

49. exclude — 不包含检查

语法

exclude(collection, value)

描述

检查字符串或数组是否不包含指定元素。和 include 结果相反。

示例

输入 公式 输出
"hello world" exclude("hello world", "wor") false(因为包含 "wor"
"hello" exclude("hello", "xyz") true

更多示例

exclude([1, 2, 3], 5)         → true     (不包含 5)
exclude(["张三"], "张三")       → false    (包含)

小技巧

  • exclude(x, y) 等于 !include(x, y)
  • 逻辑上很好理解:排除 = 不包含

50. uniq — 去重

语法

uniq(array)

描述

返回数组中唯一元素组成的数组,自动去除重复值。

示例

输入 公式 输出
[1, 2, 2, 3, 1] uniq([1, 2, 2, 3, 1]) [1, 2, 3]

更多示例

uniq(["a", "b", "a", "c"])       → ["a", "b", "c"]
uniq([1])                        → [1]
uniq([])                         → []

小技巧

  • 保持首次出现的顺序
  • 空数组返回空数组

51. pluck — 提取字段

语法

pluck(array, fieldName)

描述

从对象数组中提取指定字段的值,返回一个新数组。

示例

输入 公式 输出
[{"name": "张三"}, {"name": "李四"}] pluck([{"name": "张三"}, {"name": "李四"}], "name") ["张三", "李四"]

更多示例

pluck([{"age": 25}, {"age": 30}], "age")        → [25, 30]
pluck([{"name": "张三", "age": 25}], "name")     → ["张三"]

小技巧

  • 非常高频的公式,几乎每个集成流都会用到
  • 常和 sumwhereuniq 等链式使用

52. flatten — 扁平化

语法

flatten(array)

描述

将嵌套的数组(二维、三维等)展平为一维数组。

示例

输入 公式 输出
[[1, 2], [3, [4, 5]]] flatten([[1, 2], [3, [4, 5]]]) [1, 2, 3, 4, 5]

更多示例

flatten([[1], [2, 3], [4]])       → [1, 2, 3, 4]
flatten([1, 2, 3])                → [1, 2, 3]     (已扁平,不变)

小技巧

  • 处理多级嵌套,会递归展平所有层
  • 从多个接口聚合数据后常用

53. seq.add_all — 列表连接

语法

seq.add_all(array1, array2)

描述

将两个列表连接成一个。

示例

输入 公式 输出
[1, 2][3, 4] seq.add_all([1, 2], [3, 4]) [1, 2, 3, 4]

更多示例

seq.add_all(["a"], ["b", "c"])         → ["a", "b", "c"]
seq.add_all([], [1, 2])               → [1, 2]

小技巧

  • 只支持两个数组连接,多个需要多次调用
  • 本质上是把两个数组首尾拼接

54. tuple — 集合构造

语法

tuple(value1, value2, ...)

描述

将多个值按顺序组装为数组。

示例

输入 公式 输出
1, 2, 3 tuple(1, 2, 3) [1, 2, 3]

更多示例

tuple("张三", 25, true)       → ["张三", 25, true]
tuple(a, b)                  → [a的值, b的值]

小技巧

  • 可以混合不同类型
  • 常用于将散乱的变量组合成一个数组,再传给其他公式处理

六、条件判断

用于条件分支、数据校验、空值处理等逻辑控制。

55. present — 存在检查

语法

present(value)

描述

判断值是否"存在"(非 nil、非空字符串、非 false、非空集合)。存在返回 true,否则 false

示例

输入 公式 输出
"hello" present("hello") true
"" present("") false

更多示例

present(0)          → true     (0 是有效值,存在)
present(nil)        → false
present([])         → false    (空数组不"存在")
present(" ")        → true     (空格字符串不算空)

小技巧

  • blank 互斥:present(x) 等于 !blank(x)
  • 0" "(空格)视为存在,这点容易踩坑
  • 常用于 if 条件分支:"字段有值才继续执行"

56. presence — 存在值返回

语法

presence(value)

描述

如果值存在(非空)则返回原值,否则返回 nil

示例

输入 公式 输出
"hello" presence("hello") "hello"
"" presence("") nil

更多示例

presence(nil)        → nil
presence("张三")      → "张三"
presence(0)          → 0      (0 视为存在,返回原值)

小技巧

  • present 的区别:present 返回布尔值,presence 返回原值或 nil
  • 常用于提供默认值的场景:presence(field) or "默认值"

57. blank — 空值检查

语法

blank(value)

描述

判断值是否为空(nil、空字符串、false、空集合)。空返回 true

示例

输入 公式 输出
"" blank("") true
"hello" blank("hello") false

更多示例

blank(nil)         → true
blank(false)       → true
blank([])          → true
blank(0)           → false
blank(" ")         → false    (空格不算 blank)

小技巧

  • present 结果互斥
  • blank(0) 返回 false,这和某些语言不同
  • 常用于数据校验:"字段为空则报错"

58. is_empty — 是否为空

语法

is_empty(value)

描述

判断集合、字符串是否为空。

示例

输入 公式 输出
[] is_empty([]) true
"hello" is_empty("hello") false

更多示例

is_empty("")           → true
is_empty([1, 2, 3])    → false

小技巧

  • blank 类似,但判断逻辑稍窄
  • 日常使用 present / blank 更常见

七、格式化与工具

数据格式化、映射查找、JSON 解析等实用工具公式。

59. format_map — 格式化映射

语法

format_map(template, objectArray)

描述

将对象数组按模板规则映射为格式化字符串列表,支持 %{fieldName} 占位符。

示例

输入 公式 输出
对象数组 [{"name":"张三","age":13}] format_map("他的名字是%{name},年龄是%{age}。", obj) ["他的名字是张三,年龄是13。"]

更多示例

format_map("%{name} - %{amount}元", [{"name":"商品A", "amount":100}, {"name":"商品B", "amount":200}])
→ ["商品A - 100元", "商品B - 200元"]

小技巧

  • %{字段名} 会被替换为对象中对应字段的值
  • 适用于批量生成格式化文本,如通知消息、报表行等
  • 一次可以对多个对象进行映射,返回数组

60. to_currency — 货币格式化

语法

to_currency(number, symbol?, precision?)

描述

将数字格式化为货币显示格式(加千分位分隔符和货币符号)。默认使用人民币符号 ¥

示例

输入 公式 输出
1234.56 to_currency(1234.56) "¥1,234.56"
1234.56 to_currency(1234.56, "$", 2) "$1,234.56"

更多示例

to_currency(1000000)            → "¥1,000,000.00"
to_currency(99.9, "€", 2)      → "€99.90"

小技巧

  • 默认货币符号为 ¥(人民币)
  • 自动添加千分位逗号
  • 常用于报价单、发票等场景

61. lookup — 查找表

语法

lookup(map, key)

描述

在 Map(键值对)中根据键名查找对应的值。

示例

输入 公式 输出
{"name": "张三", "age": 25} lookup({"name": "张三", "age": 25}, "name") "张三"

更多示例

lookup({"status": "已审核", "code": 200}, "code")     → 200
lookup({"a": 1}, "b")                                → nil    (键不存在)

小技巧

  • 键不存在时返回 nil
  • 类似于编程中的 map[key]
  • 常用于从配置对象中取值

62. lookupMapping — 查找映射对象

语法

lookupMapping(objectName, mappingCategory, mappingValue, convertType)

描述

从数据映射对象中查找匹配记录并返回 _id。参数包括:对象名、映射分类、映射值、转换类型。

示例

输入 公式 输出
部门映射 lookupMapping("绑定的对象(只读)", "部门映射", "研发部", "1") "12345"(返回匹配记录的 ID)

小技巧

  • 这是集成流平台特有的公式,依赖平台中配置的数据映射表
  • 需要先在平台中建立映射关系才能使用
  • 常用于不同系统间的数据字典转换

63. parseJsonStr2Map — JSON 字符串转 Map

语法

parseJsonStr2Map(jsonString)

描述

将 JSON 格式的字符串解析为 Map 对象,方便后续用 lookup 等公式处理。

示例

输入 公式 输出
'{"name":"张三","age":13}' parseJsonStr2Map('{"name":"张三","age":13}') {name: "张三", age: 13}

更多示例

parseJsonStr2Map('{"status":"ok","data":[1,2,3]}')
→ {status: "ok", data: [1, 2, 3]}

小技巧

  • JSON 字符串必须是合法的 JSON 格式
  • 解析后即可用 lookup.字段名 访问内部数据
  • 从 API 获取的响应经常是 JSON 字符串,这个公式是解析的第一步

附录:常见组合用法

以下是初阶开发者最容易上手的公式组合套路:

套路 1:从 API 响应提取数据

解析 JSON → 提取字段 → 格式化输出
parseJsonStr2Map(resp)  →  pluck(data, "name")  →  format_map("姓名:%{name}", ...)

套路 2:日期区间筛选

获取时间范围 → 过滤数据
beginning_of_month(now())  ~  end_of_month(now())  →  where(items, "item.date >= start and item.date <= end")

套路 3:空值安全处理

presence(field) or "默认值"

套路 4:数据清洗流水线

去空格 → 过滤空值 → 去重
string.trim(field)  →  where(list, "not blank(item)")  →  uniq(list)

套路 5:字符串 ↔ 数组互转

字符串  →  split  →  [数组处理]  →  join  →  字符串
"a,b,c" → ["a","b","c"] → 去重/过滤/排序 → "a,b"

提示:建议在实际集成流中动手测试每条公式,遇到报错时先检查参数类型是否匹配、空值是否处理。

2026-07-03
0 0