# PowerApps 中的 ForAll 函数ForAll function in PowerApps

## 说明Description

ForAll 函数针对表中的所有记录对公式求值。The ForAll function evaluates a formula for all records of a table. 该公式可以计算值并/或执行操作，例如修改数据或使用连接。The formula can calculate a value and/or perform actions, such as modifying data or working with a connection.

### 替代方法Alternatives

PowerApps 中的许多函数可以通过使用单列表一次处理多个值。Many functions in PowerApps can process more than one value at a time through the use of a single-column table. 例如，Len 函数可以采用与 ForAll 相同的方式处理包含文本值的表，返回包含长度的表。For example, the Len function can process a table of text values, returning a table of lengths, in the same manner that ForAll could. 这样，在很多情况下将不需要使用 ForAll，因此可以更高效并且更容易阅读。This can eliminate the need to use ForAll in many cases, can be more efficient, and is easier to read.

## 语法Syntax

ForAll( Table, Formula )ForAll( Table, Formula )

• Table - 必需。Table - Required. 要对其执行操作的表。Table to be acted upon.
• Formula - 必需。Formula - Required. 用于对 Table 中的所有记录进行求值的公式。The formula to evaluate for all records of the Table.

## 示例Examples

### 计算Calculations

• ClearCollect( Squares, [ "1", "4", "9" ] )ClearCollect( Squares, [ "1", "4", "9" ] )

ForAll( Squares, Sqrt( Value ) )ForAll( Squares, Sqrt( Value ) )

Sqrt( Squares )Sqrt( Squares )

ForAll( Squares, Power( Value, 3 ) )ForAll( Squares, Power( Value, 3 ) ) 针对输入表中的所有记录计算 Value 列的三次幂。For all the records of the input table, raises the Value column to the third power. Power 函数不支持单列表。The Power function does not support single-column tables. 因此，在本例中必须使用 ForAllTherefore, ForAll must be used in this case.

### 使用连接Using a connection

• ClearCollect( Expressions, [ "Hello", "Good morning", "Thank you", "Goodbye" ] )ClearCollect( Expressions, [ "Hello", "Good morning", "Thank you", "Goodbye" ] )

ForAll( Expressions, MicrosoftTranslator.Translate( Value, "es" ) )ForAll( Expressions, MicrosoftTranslator.Translate( Value, "es" ) ) 针对 Expressions 表中的所有记录将 Value 列的内容翻译为西班牙语（缩写为“es”）。For all the records in the Expressions table, translate the contents of the Value column into Spanish (abbreviated "es").
ForAll( Expressions, MicrosoftTranslator.Translate( Value, "fr" ) )ForAll( Expressions, MicrosoftTranslator.Translate( Value, "fr" ) ) 针对 Expressions 表中的所有记录将 Value 列的内容翻译为法语（缩写为“fr”）。For all the records in the Expressions table, translate the contents of the Value column into French (abbreviated "fr").

### 复制表Copying a table

• 同一信息存在两个副本意味着它们有可能不同步。Two copies of the same information means that one of them can fall out of sync.
• 创建副本会消耗大量的计算机内存、网络带宽和/或时间。Making a copy can consume a lot of computer memory, network bandwidth, and/or time.
• 对于大多数数据源，复制无法委派，这限制了可以移动的数据量。For most data sources, copying cannot be delegated, limiting how much data can be moved.

• ClearCollect( Products, Table( { Product: "Widget", 'Quantity Requested': 6, 'Quantity Available': 3 }, { Product: "Gadget", 'Quantity Requested': 10, 'Quantity Available': 20 }, { Product: "Gizmo", 'Quantity Requested': 4, 'Quantity Available': 11 }, { Product: "Apparatus", 'Quantity Requested': 7, 'Quantity Available': 6 } ) )ClearCollect( Products, Table( { Product: "Widget", 'Quantity Requested': 6, 'Quantity Available': 3 }, { Product: "Gadget", 'Quantity Requested': 10, 'Quantity Available': 20 }, { Product: "Gizmo", 'Quantity Requested': 4, 'Quantity Available': 11 }, { Product: "Apparatus", 'Quantity Requested': 7, 'Quantity Available': 6 } ) )

#### 按需进行表整形Table shaping on demand

• ShowColumns( AddColumns( Filter( Products, 'Quantity Requested' > 'Quantity Available' ), "Quantity To Order", 'Quantity Requested' - 'Quantity Available' ), "Product", "Quantity To Order" )ShowColumns( AddColumns( Filter( Products, 'Quantity Requested' > 'Quantity Available' ), "Quantity To Order", 'Quantity Requested' - 'Quantity Available' ), "Product", "Quantity To Order" )

FilterAddColumns 函数分别会创建一个记录范围来使用每条记录的 'Quantity Requested''Quantity Available' 字段执行比较和减法操作。A record scope is created by the Filter and AddColumns functions to perform the comparison and subtraction operations, respectively, with the 'Quantity Requested' and 'Quantity Available' fields of each record.

#### 按需 ForAllForAll on demand

• ForAll( Products, If( 'Quantity Requested' > 'Quantity Available', { Product: Product, 'Quantity To Order': 'Quantity Requested' - 'Quantity Available' } ) )ForAll( Products, If( 'Quantity Requested' > 'Quantity Available', { Product: Product, 'Quantity To Order': 'Quantity Requested' - 'Quantity Available' } ) )

ForAll 的任何部件都不可委派。No part of the ForAll is delegable. 只会对 Products 表的第一部分进行评估，如果此表非常大，这可能是一个问题。Only the first portion of the Products table will be evaluated, which could be a problem if this table is very large. 因为 Filter 在上一示例中是可委派的，因此它可能更适用于大型数据集。Because Filter could be delegated in the previous example, it could work better with large data sets.

#### 收集结果Collect the result

• ClearCollect( NewOrder, ShowColumns( AddColumns( Filter( Products, 'Quantity Requested' > 'Quantity Available' ), "Quantity To Order", 'Quantity Requested' - 'Quantity Available' ), "Product", "Quantity To Order" ) )ClearCollect( NewOrder, ShowColumns( AddColumns( Filter( Products, 'Quantity Requested' > 'Quantity Available' ), "Quantity To Order", 'Quantity Requested' - 'Quantity Available' ), "Product", "Quantity To Order" ) )
• ClearCollect( NewOrder, ForAll( Products, If( 'Quantity Requested' > 'Quantity Available', { Product: Product, 'Quantity To Order': 'Quantity Requested' - 'Quantity Available' } ) ) )ClearCollect( NewOrder, ForAll( Products, If( 'Quantity Requested' > 'Quantity Available', { Product: Product, 'Quantity To Order': 'Quantity Requested' - 'Quantity Available' } ) ) )

ClearCollectCollect 无法委派。ClearCollect and Collect can't be delegated. 因此，可以通过此方式移动的数据量是有限的。As a result the amount of data that can be moved in this manner is limited.

#### ForAll 内的 CollectCollect within ForAll

• Clear( ProductsToOrder ); ForAll( Products, If( 'Quantity Requested' > 'Quantity Available', Collect( NewOrder, { Product: Product, 'Quantity To Order': 'Quantity Requested' - 'Quantity Available' } ) ) )Clear( ProductsToOrder ); ForAll( Products, If( 'Quantity Requested' > 'Quantity Available', Collect( NewOrder, { Product: Product, 'Quantity To Order': 'Quantity Requested' - 'Quantity Available' } ) ) )