【问题标题】:NetSuite FreeMarker Advanced PDF HTML Template IF ELSE ErrorNetSuite FreeMarker 高级 PDF HTML 模板 IF ELSE 错误
【发布时间】:2019-07-18 18:58:51
【问题描述】:

我有以下 NetSuite 高级 PDF HTML 模板代码给我一个错误:

<#if record.item?has_content>

<table class="itemtable" style="width: 100%;"><!-- start items --><#list record.item as item><#if item_index==0>
<thead>
  <tr>
  <th colspan="4">Item Code</th>
  <th colspan="12">Item Description</th>
  <th align="right" colspan="2">UOM1</th>
  <th align="right" colspan="3">${item.quantity@label}</th>
  <th align="right" colspan="3">UOM2</th>
  <th align="right" colspan="4">Unit Price (excl. VAT)</th>
  <th align="right" colspan="3">${item.amount@label}</th>
  </tr>
</thead>
</#if><tr>
  <td colspan="4">${item.item}</td>
  <td colspan="12">${item.description}</td>
  <td align="right" colspan="2">${item.custcolsyn_uom}&nbsp;${item.custcolsyn_unit_measure}</td>
  <td align="right" colspan="3">${item.quantity}</td>
  <td align="right" colspan="3">${item.units}</td>
  <td align="right" colspan="4"><#if item.rate?has_content>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else>&nbsp;</#if></td>
  <td align="right" colspan="3">${item.amount}</td>
  </tr>
  </#list><!-- end items --></table>
</#if>

问题出在这行:

<td align="right" colspan="4"><#if item.rate?has_content>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else>&nbsp;</#if></td>

看起来 FreeMarker 正在评估以下部分

${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}

即使订单项没有任何价格信息。没错

<#if item.rate?has_content>

应该阻止这种评估的发生。我试图只保留 2 位小数的货币数据,而我尝试的所有其他方法都丢失了货币符号。

我们使用的是最新版本的 NetSuite (2018.2)。

错误信息是:

The template cannot be printed due to the following errors: 

Error on line 239, column 95 in template.

Detail...

Range start index 0 is out of bounds, because the sliced string has only 0 character(s). (Note that indices are 0-based).
The blamed expression:
==> 0..1 [in template "template" at line 239, column 128]

----
FTL stack trace ("~" means nesting-related):
- Failed at: ${item.rate?keep_after_last(".")[0..1]} [in template "template" at line 239, column 95]
----


Please contact your administrator.

任何人对我做错了什么或如何解决此问题有任何想法?

【问题讨论】:

  • 如果费率以. 开头,您可以获得此异常
  • 利率为空/空/空白
  • item.rate 是数字还是字符串?试试:${item.rate?is_number?c。 (或者如果 NetSuite 是从某个古老的 FM 版本中分叉出来的,因此不知道 ?c,那么使用 ?string 而不是 ?c
  • ${item.rate?is_number?c} 返回 true,即使数字前面明显有 R 的货币符号。
  • 但是如果你只写${1},它没有R?那么,我猜${item.rate?string('#.00')} 也失去了R,还有${item.rate + 1}。可以?我只是想知道他们是如何实现这一点的。使用TemplateNumberFormat 是可行的,但他们不妨修改 FreeMarker...AFAK 他们有自己的封闭源代码分支。

标签: netsuite freemarker


【解决方案1】:

既然你说 rate null 你可以使用 ??测试操作员:

<#if item.rate??></#if>

如果你想检查多个条件,你可以检查&lt;#if item.rate?? &amp;&amp; item.rate?has_content&gt;&lt;/#if&gt;

对于货币格式,您可以参考此链接docs。如果你想要自定义格式,你可以试试this

${item.rate:M2}

【讨论】:

  • 这意味着item 本身为空
  • item 不为空,它会在其他列中打印值。 item.rate 然而为空
  • 我认为您可以使用item.rate?string.currency 而不是往返。您可以替换该行
  • 正如我在文中所述,item.rate?string.currency 没有任何成功,因为它丢失了货币符号。
  • 只需为该列添加您的预期输出。我会尝试以其他方式解决它。看来我们做错了。
【解决方案2】:

Freemarker ?has_content 函数可能会产生意想不到的结果 - 特别是当您不控制底层数据模型时(NetSuite 就是这种情况)。基本上正在发生的事情是 item.rate 被传递不是 null 并且不符合 ?has_content 对“空”的定义,即使它看起来是空的。您可以改用?length gt 0

  <td align="right" colspan="4"><#if item.rate?length gt 0>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else>&nbsp;</#if></td>

如果由于某种原因将item.rate 传递给没有“.”的模板,这仍然是一个问题,因为它仍然会以空字符串结尾,[0..1] 索引将是无效的。

因此,您可以测试 item.rate ?contains 是否改为小数分隔符:

<td align="right" colspan="4"><#if item.rate?contains(".")>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else>&nbsp;</#if></td>

但最简单的做法可能是改用 Freemarker 内置的货币字符串格式:

<td align="right" colspan="4">${item.rate?string.currency}</td>

【讨论】:

  • 谢谢! &lt;td align="right" colspan="4"&gt;&lt;#if item.rate?contains(".")&gt;${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}&lt;#else&gt;${item.rate}&lt;/#if&gt;&lt;/td&gt; 成功了。出于某种原因使用string.currency 会返回$ 而不是R
  • ?string.currency 的输出取决于运行 Freemarker 的区域设置。这应该在 NetSuite 中的 Setup>Company>Company Information>Currency Locale 中进行设置。或者,您可以覆盖模板内的语言环境设置:&lt;#setting locale="en_ZA"&gt;&lt;td align="right" colspan="3"&gt;${item.rate?string.currency}&lt;/td&gt;.
  • 你可以避免#if-s,只使用一个表达式:${item.rate?replace(r'(\d\.\d{2})\d+', '$1', 'r')}。当然,现在将其放入带有描述性名称的#function 仍然很丑陋......(或者坦率地说,如果 Netsuite 的人需要一个功能来正确处理这些带注释的数字,最好出现在 FM 邮件列表中,就像这样 ?string('0.00') 将与他们一起工作......)
猜你喜欢
  • 2014-05-08
  • 2018-10-20
  • 2017-02-19
  • 2010-09-22
  • 2023-01-31
  • 2018-01-10
  • 2022-12-04
  • 2019-11-09
  • 2018-01-01
相关资源
最近更新 更多