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

Webform提交中访问元素:邮件内数值计算与输出需求

用Webform Computed Twig字段实现邮件中的数值计算与内容输出

嘿,我刚好处理过类似的Webform邮件计算需求,给你一套完整的实现方案,一步步来:

1. 先添加并配置Computed Twig字段

首先在你的Webform里新增一个Computed Twig类型的字段(比如给它起个好记的名字,比如calculated_summary),记得把「显示在表单中」设为隐藏——毕竟我们只需要它在邮件里输出内容,不需要用户在表单里看到。字段的「Twig模板」区域就是我们写逻辑的核心地方。

2. 获取Table元素里的字段值

假设你的Table元素机器名是product_details_table,表格里的输入框机器名分别是textfield1、textfield1_2这类,你可以通过Webform的Twig变量轻松拿到这些值:

{# 先获取表格的每一行数据 #}
{% set row1 = webform_token('[webform_submission:values:product_details_table:0]') %}
{% set row2 = webform_token('[webform_submission:values:product_details_table:1]') %}
{% set row3 = webform_token('[webform_submission:values:product_details_table:2]') %}

{# 提取每行的数值型字段,先转成数字避免计算出错 #}
{% set tf1 = row1['textfield1']|float %}
{% set tf1_2 = row1['textfield1_2']|float %}
{% set tf2 = row2['textfield2']|float %}
{% set tf2_2 = row2['textfield2_2']|float %}
{% set tf3 = row3['textfield3']|float %}
{% set tf3_2 = row3['textfield3_2']|float %}

小提示:如果你的Table是允许用户动态添加行的,那别用上面的固定行写法,改用循环遍历所有行更稳妥,比如{% for row in webform_submission.values.product_details_table %}。

3. 写数值计算逻辑

比如你要算每行的合计,或者所有行的总计,直接在Twig里写算术就行:

{# 计算每行的小计 #}
{% set row1_total = tf1 + tf1_2 %}
{% set row2_total = tf2 + tf2_2 %}
{% set row3_total = tf3 + tf3_2 %}

{# 计算所有行的总计 #}
{% set grand_total = row1_total + row2_total + row3_total %}

4. 构建邮件要展示的内容

把计算结果和原表格、HTML描述字段整合起来,用HTML格式写(毕竟邮件支持HTML,排版更美观):

{# 先输出原表格+计算结果 #}
<h3>产品明细汇总</h3>
<table border="1" cellpadding="6" cellspacing="0" style="border-collapse: collapse;">
  <tr>
    <th>col1</th>
    <th>col2</th>
    <th>col3</th>
    <th>col4</th>
    <th>行小计</th>
  </tr>
  <tr>
    <td>{{ row1['textfield1'] }}</td>
    <td>{{ row1['Text1'] }}</td>
    <td>{{ row1['Text1_2'] }}</td>
    <td>{{ row1['textfield1_2'] }}</td>
    <td>{{ row1_total|number_format(2) }}</td>
  </tr>
  <tr>
    <td>{{ row2['textfield2'] }}</td>
    <td>{{ row2['Text2'] }}</td>
    <td>{{ row2['Text2_2'] }}</td>
    <td>{{ row2['textfield2_2'] }}</td>
    <td>{{ row2_total|number_format(2) }}</td>
  </tr>
  <tr>
    <td>{{ row3['textfield3'] }}</td>
    <td>{{ row3['Text3'] }}</td>
    <td>{{ row3['Text3_2'] }}</td>
    <td>{{ row3['textfield3_2'] }}</td>
    <td>{{ row3_total|number_format(2) }}</td>
  </tr>
  <tr style="background-color: #f0f0f0;">
    <td colspan="4" align="right"><strong>总计:</strong></td>
    <td><strong>{{ grand_total|number_format(2) }}</strong></td>
  </tr>
</table>

{# 输出你之前添加的HTML/Markup产品描述字段 #}
<h3>产品详细描述</h3>
{{ webform_submission.values.product_description }}

这里用|number_format(2)是为了让数值保留两位小数,看起来更专业,根据你的需求调整就行。

5. 在邮件模板里引用这个Computed字段

最后一步,编辑Webform的邮件模板,在你想要展示内容的位置插入[webform_submission:values:calculated_summary](替换成你实际的Computed字段机器名),这样邮件发送时就会自动渲染出你写好的带计算结果的内容了。

动态行场景的补充代码

如果你的Table是用户可以动态添加行的,那用下面的循环代码更灵活,不管用户加多少行都能正确计算:

{% set grand_total = 0 %}
<h3>产品明细汇总</h3>
<table border="1" cellpadding="6" cellspacing="0" style="border-collapse: collapse;">
  <tr>
    <th>col1</th>
    <th>col2</th>
    <th>col3</th>
    <th>col4</th>
    <th>行小计</th>
  </tr>
  {% for row in webform_submission.values.product_details_table %}
    {% set row_total = (row['textfield']|float + row['textfield_2']|float) %}
    {% set grand_total = grand_total + row_total %}
    <tr>
      <td>{{ row['textfield'] }}</td>
      <td>{{ row['Text'] }}</td>
      <td>{{ row['Text_2'] }}</td>
      <td>{{ row['textfield_2'] }}</td>
      <td>{{ row_total|number_format(2) }}</td>
    </tr>
  {% endfor %}
  <tr style="background-color: #f0f0f0;">
    <td colspan="4" align="right"><strong>总计:</strong></td>
    <td><strong>{{ grand_total|number_format(2) }}</strong></td>
  </tr>
</table>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:24:01