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. 如果结果为 true,将返回相应的值。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.

虽然 If 和 Switch 函数非常相似,但应使用最适合自身情况的函数: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 中,可以指定多个条件,而无需嵌套 If 公式(不同于 Microsoft Excel)。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。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. 此类公式通常包含比较运算符(如 <、> 和 =)和测试函数(如 IsBlankIsEmpty)。Such 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”)的值为“25”。In 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,因为滑块不是 blank。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 为 blank,此公式不会产生任何影响。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”匹配的项,所以应用将转到 Screen3。A match is found with "John", so the app navigates to Screen3. truetrue

切换显示 Screen3。The display is changed to Screen3.

分步操作Step by step

  1. 添加“文本输入”控件,再将它命名为“Text1”(如果默认名称不是这样的话)。Add a Text input control, and name it Text1 if it doesn't have that name by default.
  2. 在“Text1”中,键入“30”。In 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”中,键入“50”。In Text1, type 50.

    此时,“标签”控件显示所键入的值,因为“Text1”的值大于 40。The Label control shows the value that you typed because it's more than 40.