Crystal Reports for Rational Application Developer 设计员指南

If 表达式(Crystal 语法)

If 表达式是最有用的控制结构之一。它使您得以在条件为 true 时对一系列表达式求值,而在条件不为 true 时对其他一系列表达式求值。

注意: If 语句的正确 Crystal 语法是 if <condition> then <then> else <else>,其中 <condition><then><else> 都是单个表达式。如果在 <then><else> 后面有多个表达式,则将它们转换成单个表达式(用括号将它们括起来)。例如:
Global stringVar lastValue;
if {Branch_View.Branch_ID} = lastValue
then
(lastValue := {Branch_View.Branch_ID};
crRed;)
else
(lastValue := {Branch_View.Branch_ID};
crBlack;)
注意:
  • 用条件公式进行格式设置时,始终要包含 Else 关键字;否则,不符合 If 条件的值可能不会保留原来的格式。为避免此类情况发生,可以使用 DefaultAttribute 函数 (If...Else DefaultAttribute)。
  • 当创建包含 If 表达式的记录选择公式时,应始终包含 Else 关键字;否则,可能不返回任何记录或者返回意外的记录。例如,记录选择公式 If {parameter field} = "less than 100" then {field} < 100" 将求值为 False,并且不返回任何记录。若要更正此问题,请用 Else True 完成该公式。
示例
某公司计划发给其员工 4% 的奖金,而销售部门的员工将得到 6% 的奖金。下面的公式使用 If 表达式实现这一目的:
//If example 1
If {Employee.Dept} = "Sales" Then
{Employee.Salary} * 0.06
Else
{Employee.Salary} * 0.04
在本例中,如果条件 {雇员.部门} = "Sales" 求值为 true,则处理
{Employee.Salary} * 0.06
表达式。否则处理 Else 后面的表达式,也就是
{Employee.Salary} * 0.04
表达式。
假设另一公司想要发给其员工 4% 的奖金,但奖金最低金额是 $1,000。以下示例显示了如何实现此功能。注意没有包含 Else 子句;该子句是可选的,在这种情况下并不需要。
//If example 2
Local CurrencyVar bonus := {Employee.Salary} * 0.04;
If bonus < 1000 Then
bonus := 1000;
//The final expression is just the variable 'bonus'.
//This returns the value of the variable and is the
//result of the formula
bonus
完成示例 2 的另一种方法是使用 Else 子句:
//If example 3
Local CurrencyVar bonus := {Employee.Salary} * 0.04;
If bonus < 1000 Then
1000
Else
bonus
现在假设上面的公司还要使奖金的最大金额为 $5,000。现在需要使用一个 Else If 子句。下例只有一个 Else If 子句,但可以根据需要添加任意多个。但是要注意,每个 If 表达式最多只有一个 Else 子句。如果 If 或 Else If 条件都不为 true,则执行 Else 子句。
//If example 4
Local CurrencyVar bonus := {Employee.Salary} * 0.04;
If bonus < 1000 Then
1000
Else If bonus > 5000 Then
5000
Else
bonus




Business Objects,SAP 子公司
http://www.china.businessobjects.com/
支持服务
http://www.china.businessobjects.com/support/
Web 上的产品文档
http://support.businessobjects.com/documentation/