表单结构:
订单表(t_pn9k4l5mdj5) → 交易记录表(子表格 pay_item)
我想实现:
交易记录表里,输入税前单价(f_cs8s2h34b23)和计划数量(f_k9aa0eucny7)之后可以自动计算出税前总价(f_p8gi46f8g4s)。
我在1.0版本的时候使用字段联动里的赋值表达式,当时自动计算功能一切正常
在2.0版本里,字段联动没有赋值表达式功能,在 ”字段值“ 里选择 ”交易记录表/税前总价“,将计算方式改为RunJS,并尝试了以下代码均无法正确计算:
尝试的公式:
第一种:
// 1. 用 ctx.getVar 获取单价
const unitPrice = Number(await ctx.getVar(‘ctx.formValues.f_cs8s2h34b23’)) || 0;
// 2. 用 ctx.getVar 获取数量
const quantity = Number(await ctx.getVar(‘ctx.formValues.f_k9aa0eucny7’)) || 0;
// 3. 计算并返回总价
return unitPrice * quantity;
结果全为0
第二种:
{{ (Number(f_cs8s2h34b23) || 0) * (Number(f_k9aa0eucny7) || 0) }}
以及
{{ ($row.f_cs8s2h34b23 || 0) * ($row.f_k9aa0eucny7 || 0) }}
以及
{{ (Number($record.f_cs8s2h34b23) || 0) * (Number($record.f_k9aa0eucny7) || 0) }}
结果全为空
第三种:
// 1. 用 ctx.getVar 获取单价(参考你的 total_count_en 取值方式)
const unitPrice = Number(await ctx.getVar(‘ctx.formValues.f_cs8s2h34b23’)) || 0;
// 2. 用 ctx.getVar 获取数量
const quantity = Number(await ctx.getVar(‘ctx.formValues.f_k9aa0eucny7’)) || 0;
// 3. 计算并返回总价
return unitPrice * quantity;
结果全为0
大佬们求教,RunJS里怎么实现这个需求?到底用什么函数可以获取到当前行的字段值?
参考 ,v2表单字段使用runjs功能计算赋值
const total_count_en = await ctx.getVar(‘ctx.formValues.total_count_en’);
const total_count_cn = await ctx.getVar(‘ctx.formValues.total_count_cn’);
return total_count_en+total_count_cn;
const item = await ctx.getVar(‘ctx.formValues.m2m.item’);
return ctx.libs.formula.SUM(item);
await ctx.getVar(‘ctx.formValues.)这种方式我尝试过了,第一种方式就是这个,只能读取到当前表单的数据,我想读取的是 当前表单里的子表单的 当前对象。v1版本的表达式里可以直接选择当前对象。v2版本用runjs的哪个函数可以指定当前对象?v2 runjs帮助文档中没找到具体说明。
用上这配合 AI 写代码算明细表的合计金额了,前端直接能合计就很好哈
把所有工作流里聚合查询,更新数据,再查询数据全去掉了
const a = await ctx.getVar(‘ctx.formValues.f_mb4atpi4o79.f_xp1kw3l1dtz’);
const b = await ctx.getVar(‘ctx.formValues.f_mb4atpi4o79.f_ol49ggo1j3a’);
const c = await ctx.getVar(‘ctx.formValues.f_mb4atpi4o79.f_6447ny3ezjq’);
const d = await ctx.getVar(‘ctx.formValues.f_mb4atpi4o79.f_rba8hn0rer7’);
return a+b+c+d;
qooboy
6
const quantity = parseFloat(await ctx.getVar(‘ctx.formValues.6465e533de8.46e822010a0’) || 0);
const price = parseFloat(await ctx.getVar(‘ctx.formValues.6465e533de8.eb52033d63f’) || 0);
const total = quantity * price;
return total.toFixed(2);
目前返回0,其中6465e533de8为子表单UID,46e822010a0和eb52033d63f字段UID,通过日志输出,
[log] quantityRaw: type: undefined
[log] priceRaw: type: undefined
[log] quantity parsed: 0
[log] price parsed: 0
[log] total: 0
[info] 执行成功
qooboy
7
const quantityRaw = await ctx.getVar(‘ctx.formValues.cigaret_or_details.quantity’);
const priceRaw = await ctx.getVar(‘ctx.formValues.cigaret_or_details.price’);
cigaret_or_details为子表名称,后面是字段名称,日志返回
[log] quantityRaw: type: undefined
[log] priceRaw: type: undefined