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 元。現在需要使用的是 ElseIf 子句。以下範例只有一個 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.taiwan.businessobjects.com/
支援服務
http://www.taiwan.businessobjects.com/support/
產品說明文件網站
http://support.businessobjects.com/documentation/