PowerApps 中的 If 與 Switch 函式If and Switch functions in PowerApps

決定集合中的任何條件是否為 true (If),或者公式的結果是否符合集合中的任何值 (Switch),然後傳回結果或執行動作。Determines whether any condition in a set is true (If) or the result of a formula matches any value in a set (Switch) and then returns a result or executes an action.

說明Description

If 函式會測試一或多個條件,直到找到 true 結果為止。The If function tests one or more conditions until a true result is found. 如果找到這類結果,則會傳回對應值。If such a result is found, a corresponding value is returned. 如果找不到這類結果,則會傳回預設值。If no such result is found, a default value is returned. 在任一情況下,傳回的值可能是要顯示的字串、要評估的公式,或另一種形式的結果。In either case, the returned value might be a string to show, a formula to evaluate, or another form of result.

Switch 函式會評估公式,並判斷結果是否符合您指定的序列中的任何值。The Switch function evaluates a formula and determines whether the result matches any value in a sequence that you specify. 如果找到相符項目,則會傳回對應值。If a match is found, a corresponding value is returned. 如果找不到相符項目,則會傳回預設值。If no match is found, a default value is returned. 在任一情況下,傳回的值可能是要顯示的字串、要評估的公式,或另一種形式的結果。In either case, the returned value might be a string to show, a formula to evaluate, or another form of result.

IfSwitch 非常相似,但是您應該針對您的情況使用最佳的函式:If and Switch are very similar, but you should use the best function for your situation:

  • 使用 If 來評估單一條件。Use If to evaluate a single condition. 此函式最常見的語法是 If( Condition, ThenResult, DefaultResult ),該語法提供常見的 “if …The most common syntax for this function is If( Condition, ThenResult, DefaultResult ), which provides the common “if … then …then … else …”else …” 模式,常在其他程式設計工具中看到。pattern seen in other programming tools.
  • 使用 If 來評估多個不相關的條件。Use If to evaluate multiple unrelated conditions. 在 PowerApps (不像 Microsoft Excel) 中,您可以指定多個條件而不用讓 If 公式成為巢狀。In PowerApps (unlike Microsoft Excel), you can specify multiple conditions without having to nest If formulas.
  • 使用 Switch 針對多個可能的相符項目評估單一條件。Use Switch to evaluate a single condition against multiple possible matches. 您也可以在此案例中使用 If,但是您不需要對每個可能的相符項目重複公式。You can also use If in this case, but you'd need to repeat the formula for each possible match.

您可以在行為公式中同時使用這兩個函式,以在兩個以上的動作之間分支。You can use both of these functions in behavior formulas to branch between two or more actions. 只有一個分支會觸發動作。Only one branch will trigger an action. 條件和相符項目是按照順序進行評估,如果條件為 true 或者找到相符項目時則會停止。Conditions and matches are evaluated in order, and they stop if a condition is true or a match is found.

如果沒有條件為 true、找不到相符項目,而且您未指定預設結果,則會傳回空白。Blank is returned if no conditions are true, no matches are found, and you don't specify a default result.

語法Syntax

If ( Condition , ThenResult [, DefaultResult ] )If( Condition, ThenResult [, DefaultResult ] )
If ( Condition1 , ThenResult1 [, Condition2 , ThenResult2 , ... [ , DefaultResult ] ] )If( Condition1, ThenResult1 [, Condition2, ThenResult2, ... [ , DefaultResult ] ] )

  • Condition(s) - 必要項目。Condition(s) - Required. 要針對 true 測試的公式。Formula(s) to test for true. 此類公式通常包含比較運算子 (例如 <>=) 並測試函式,例如 IsBlankIsEmptySuch formulas commonly contain comparison operators (such as <, >, and =) and test functions such as IsBlank and IsEmpty.
  • ThenResult(s) - 必要項目。ThenResult(s) - Required. 針對評估為 true 的條件傳回的對應值。The corresponding value to return for a condition that evaluates to true.
  • DefaultResult - 選擇性。DefaultResult - Optional. 如果沒有條件評估為 true 時要傳回的值。The value to return if no condition evaluates to true. 如果您沒有指定這個引數,會傳回空白。If you don't specify this argument, blank is returned.

Switch ( Formula , Match1 , Result1 [, Match2 , Result2 , ... [, DefaultResult ] ] )Switch( Formula, Match1, Result1 [, Match2, Result2, ... [, DefaultResult ] ] )

  • Formula - 必要項目。Formula - Required. 評估相符項目的公式。Formula to evaluate for matches. 此公式只評估一次。This formula is evaluated only once.
  • Match(s) - 必要項目。Match(s) - Required. 與「公式」的結果進行比較的值。Values to compare with the result from Formula. 如果找到完全相符的項目,則會傳回對應的結果。If an exact match is found, the corresponding Result is returned.
  • Result(s) - 必要項目。Result(s) - Required. 找到完全相符的項目時傳回的對應值。The corresponding value to return when an exact match is found.
  • DefaultResult - 選擇性。DefaultResult - Optional. 如果找不到完全相符的項目,則會傳回此值。If an exact match isn't found, this value is returned. 如果您沒有指定這個引數,會傳回空白。If you don't specify this argument, blank is returned.

範例Examples

公式中的值Values in formulas

在下列範例中,名為 Slider1滑桿控制項具有值 25In the following examples, a Slider control (named Slider1) has a value of 25.

公式Formula 說明Description 結果Result
If( Slider1.Value = 25, "Result1" )If( Slider1.Value = 25, "Result1" ) 條件是 true,並傳回對應的結果。The condition is true, and the corresponding result is returned. "Result1""Result1"
If( Slider1.Value = 25, "Result1", "Result2" )If( Slider1.Value = 25, "Result1", "Result2" ) 條件是 true,並傳回對應的結果。The condition is true, and the corresponding result is returned. "Result1""Result1"
If( Slider1.Value > 1000, "Result1" )If( Slider1.Value > 1000, "Result1" ) 條件是 false ,且不會提供 DefaultResult。The condition is false, and no DefaultResult was provided. 空白blank
If( Slider1.Value > 1000, "Result1", "Result2" )If( Slider1.Value > 1000, "Result1", "Result2" ) 條件是 false ,且已提供並傳回 DefaultResult。The condition is false, a DefaultResult was provided, and it's returned. "Result2""Result2"
If( Slider1.Value = 25, "Result1", Slider1.Value > 0, "Result2" )If( Slider1.Value = 25, "Result1", Slider1.Value > 0, "Result2" ) 第一個條件是 true,並傳回對應的結果。The first condition is true, and the corresponding result is returned. 第二個條件也是 true,但是不會評估它,因為它會比評估為 true 的條件更晚出現在引數清單中。The second condition is also true, but it isn't evaluated because it appears later in the argument list than a condition that evaluates to true. "Result1""Result1"
If( IsBlank( Slider1.Value ), "Result1", IsNumeric( Slider1.Value ), "Result2" )If( IsBlank( Slider1.Value ), "Result1", IsNumeric( Slider1.Value ), "Result2" ) 第一個條件是 false,因為滑桿不是空白。The first condition is false because the slider isn't blank. 第二個條件是 true,因為滑桿的值是數字,並傳回對應的結果。The second condition is true because the slider's value is a number, and the corresponding result is returned. "Result2""Result2"
If( Slider1.Value > 1000, "Result1", Slider1.Value > 50, "Result2", "Result3")If( Slider1.Value > 1000, "Result1", Slider1.Value > 50, "Result2", "Result3") 第一個和第二個條件都是 false ,且已提供並傳回 DefaultResult。Both the first and second conditions are false, a DefaultResult was provided, and it's returned. "Result3""Result3"
Switch( Slider1.Value, 25, "Result1" )Switch( Slider1.Value, 25, "Result1" ) 滑桿的值符合要檢查的第一個值,並且傳回對應的結果。The slider's value matches the first value to be checked, and the corresponding result is returned. "Result1""Result1"
Switch( Slider1.Value, 20, "Result1", 25, "Result2", 30, "Result3" )Switch( Slider1.Value, 20, "Result1", 25, "Result2", 30, "Result3" ) 滑桿的值符合要檢查的第二個值,並且傳回對應的結果。The slider's value matches the second value to be checked, and the corresponding result is returned. "Result2""Result2"
Switch( Slider1.Value, 20, "Result1", 10, "Result2", 0, "Result3", "DefaultResult" )Switch( Slider1.Value, 20, "Result1", 10, "Result2", 0, "Result3", "DefaultResult" ) 滑桿的值不符合要檢查的任何值。The slider's value doesn't match any value to be checked. 提供 DefaultResult,因此傳回該值。A DefaultResult was provided, so it's returned. "DefaultResult""DefaultResult"

行為公式中的分支Branching in behavior formulas

在這些範例中,名為 FirstName文字輸入控制項已在其中輸入 "John" 的值。In these examples, a Text input control named FirstName has the value "John" typed into it.

公式Formula 說明Description 結果Result
If( !IsBlank( FirstName.Text ), Navigate( Screen1, ScreenTransition.None ) )If( ! IsBlank( FirstName.Text ), Navigate( Screen1, ScreenTransition.None ) ) 條件是 true,因此執行 Navigate 函式。The condition is true, so the Navigate function runs. 您可以使用 IsBlank 函式來測試是否已填入必要的表單欄位。You can use the IsBlank function to test whether a required form field has been filled in. 如果 FirstName空白,此公式不會有任何作用。If FirstName were blank, this formula would have no effect. truetrue

顯示變更為 Screen1The display is changed to Screen1.
If( IsBlank( FirstName.Text ), Navigate( Screen1, ScreenTransition.None ), Back() )If( IsBlank( FirstName.Text ), Navigate( Screen1, ScreenTransition.None ), Back() ) 不含 !Without the ! 運算子,條件是 false,因此並未執行 Navigate 函式。operator, the condition is false, so the Navigate function doesn't run. 提供 Back 函式作為 DefaultResult ,因此它會執行。The Back function was provided as a DefaultResult, so it runs. truetrue

顯示會回到先前所示的畫面。The display goes back to the screen that was previously shown.
Switch( FirstName.Text, "Carlos", Navigate( Screen1, ScreenTransition.None ), "Kirstin", Navigate( Screen2, ScreenTransition.None ), "John", Navigate( Screen3, ScreenTransition.None ) )Switch( FirstName.Text, "Carlos", Navigate( Screen1, ScreenTransition.None ), "Kirstin", Navigate( Screen2, ScreenTransition.None ), "John", Navigate( Screen3, ScreenTransition.None ) ) FirstName.Text 的值會根據 "Carlos"、"Kirstin" 和 "John" 的順序進行比較。The value of FirstName.Text is compared against "Carlos", "Kirstin", and "John" in that order. 找到 "John" 的相符項目,因此應用程式會瀏覽至 Screen3A match is found with "John", so the app navigates to Screen3. truetrue

顯示變更為 Screen3The display is changed to Screen3.

逐步解說Step by step

  1. 新增文字輸入控制項,如果它依預設沒有該名稱,則將其命名為 Text1Add a Text input control, and name it Text1 if it doesn't have that name by default.
  2. Text1 中,輸入 30In Text1, type 30.
  3. 新增標籤控制項,並將其 Text 屬性設為以下公式:Add a Label control, and set its Text property to this formula:
    If( Value(Text1.Text) < 20, "Order MANY more!", Value(Text1.Text) < 40, "Order more!", Text1.Text ) If( Value(Text1.Text) < 20, "Order MANY more!", Value(Text1.Text) < 40, "Order more!", Text1.Text )

    標籤控制項會顯示訂購更多!The Label control shows Order more! 因為 Text1 的值超過 20 但少於 40。because the value of Text1 is more than 20 but less than 40.

  4. Text1 中,輸入 15In Text1, type 15.

    標籤控制項會顯示再訂購更多!The Label control shows Order MANY more! 因為 Text1 的值少於 20。because the value of Text1 is less than 20.

  5. Text1 中,輸入 50In Text1, type 50.

    標籤控制項會顯示您輸入的值,因為它超過 40。The Label control shows the value that you typed because it's more than 40.