PowerApps 中的 Update 和 UpdateIf 函数Update and UpdateIf functions in PowerApps

更新数据源中的记录Updates records in a data source.

说明Description

Update 函数Update function

使用 Update 函数可替换数据源中的整个记录,Use the Update function to replace an entire record in a data source. 而使用 UpdateIfPatch 函数则可修改记录中的一个或多个值,对其他值不作处理。In contrast, the UpdateIf and the Patch functions modify one or more values in a record, leaving the other values alone.

对于集合来说,整个记录必须匹配。For a collection, the entire record must match. 集合允许重复记录,因此可能会匹配多个记录。Collections allow duplicate records, so multiple records might match. 可以使用 All 参数更新某个记录的所有副本;否则只会更新记录的一个副本。You can use the All argument to update all copies of a record; otherwise, only one copy of the record is updated.

如果数据源自动生成列的值,则该的值必须再次进行确认。If the data source generates a column's value automatically, the value of that column must be reaffirmed.

UpdateIf 函数UpdateIf function

使用 UpdateIf 函数可以在一个或多个记录中修改一个或多个值,而这些记录则与一个或多个条件匹配。Use the UpdateIf function to modify one or more values in one or more records that match one or more conditions. 条件可以是其结果为 truefalse 的任意公式,并且可以通过名称引用数据源的列。The condition can be any formula that results in a true or false and can reference columns of the data source by name. 函数会评估每个记录的条件,并修改结果为 true 的任何记录。The function evaluates the condition for each record and modifies any record for which the result is true.

若要指定某个修改,请使用包含新的属性值的更改记录。To specify a modification, use a change record that contains new property values. 如果使用大括号以内联方式提供此更改记录,则属性公式可以引用要修改的记录的属性。If you provide this change record inline with curly braces, property formulas can reference properties of the record that's being modified. 可以通过此行为根据公式修改记录。You can use this behavior to modify records based on a formula.

就像使用 UpdateIf 一样,也可使用 Patch 函数更改记录的特定列,而不影响其他列。Similar to UpdateIf, you can also use the Patch function to change specific columns of a record without affecting other columns.

UpdateUpdateIf 都以的形式返回修改的数据源。Both Update and UpdateIf return the modified data source as a table. 必须在行为公式中使用任一函数。You must use either function in a behavior formula.

委派Delegation

当这些函数与数据源一起使用时,不能委派这些函数。When used with a data source, these functions can't be delegated. 将只检索数据源的第一个部分,然后检索应用的函数。Only the first portion of the data source will be retrieved and then the function applied. 结果可能并不是事实的全部。The result may not represent the complete story. 系统会在你创作时显示一个蓝点,提醒注意此限制,并建议尽可能改用可委托的替代项。A blue dot will appear at authoring time to remind you of this limitation and to suggest switching to delegable alternatives where possible. 有关详细信息,请参阅委托概述For more information, see the delegation overview.

语法Syntax

Update( DataSource, OldRecord, NewRecord [, All ] )Update( DataSource, OldRecord, NewRecord [, All ] )

  • DataSource – 必需。DataSource – Required. 数据源,其中包含要替换的记录。The data source that contains the record that you want to replace.
  • OldRecord - 必需。OldRecord – Required. 要替换的记录。The record to replace.
  • NewRecord - 必需。NewRecord – Required. 替换记录。The replacement record. 这不是更改记录。This isn't a change record. 将替换整个记录,缺失的属性将包含“空”。The entire record is replaced, and missing properties will contain blank.
  • All - 可选。All – Optional. 在集合中,同一记录可能出现多次。In a collection, the same record may appear more than once. 指定 All 参数即可删除记录的所有副本。Specify the All argument to remove all copies of the record.

UpdateIf( DataSource, Condition1, ChangeRecord1 [, Condition2, ChangeRecord2, ... ] )UpdateIf( DataSource, Condition1, ChangeRecord1 [, Condition2, ChangeRecord2, ... ] )

  • DataSource – 必需。DataSource – Required. 数据源,其中包含要修改的一个或多个记录。The data source that contains the record or records that you want to modify.
  • Condition(s) - 必需。Condition(s) – Required. 一个公式,对于一个或多个需修改的记录,该公式的求值结果为 trueA formula that evaluates to true for the record or records that you want to modify. 可以在公式中使用 DataSource 的列名。You can use column names of DataSource in the formula.
  • ChangeRecord(s) - 必需。ChangeRecord(s) - Required. 对于每个相应的条件,将对符合条件的 DataSource 记录应用新属性值的更改记录。For each corresponding condition, a change record of new property values to apply to records of DataSource that satisfy the condition. 如果使用大括号以内联方式提供记录,则可在属性公式中使用现有记录的属性值。If you provide the record inline using curly braces, property values of the existing record can be used in the property formulas.

示例Examples

在以下示例中,你将替换或修改某个数据源中的记录。该数据源名为 IceCream 且以下表中的数据开头:In these examples, you'll replace or modify records in a data source that's named IceCream and that starts with the data in this table:

公式Formula 说明Description 结果Result
Update( IceCream,
First( Filter( IceCream, Flavor="Chocolate" ) ), { ID: 1, Flavor: "Mint Chocolate", Quantity:150 } )
Update( IceCream,
First( Filter( IceCream, Flavor="Chocolate" ) ), { ID: 1, Flavor: "Mint Chocolate", Quantity:150 } )
替换数据源中的某条记录。Replaces a record from the data source.

修改了 IceCream 数据源。The IceCream data source has been modified.
UpdateIf( IceCream, Quantity > 175, { Quantity: Quantity + 10 } )UpdateIf( IceCream, Quantity > 175, { Quantity: Quantity + 10 } ) 修改其 Quantity 大于 150 的记录。Modifies records that have a Quantity that is greater than 150. Quantity 字段按 10 递增,其他字段不进行修改。The Quantity field is incremented by 10, and no other fields are modified.

修改了 IceCream 数据源。The IceCream data source has been modified.
Update( IceCream,
First( Filter( IceCream, Flavor="Strawberry" ) ),
{ ID: 3, Flavor: "Strawberry Swirl"} )
Update( IceCream,
First( Filter( IceCream, Flavor="Strawberry" ) ),
{ ID: 3, Flavor: "Strawberry Swirl"} )
替换数据源中的某条记录。Replaces a record from the data source. 替换记录中未提供 Quantity 属性,因此在结果中该属性将为“空”。The Quantity property hasn't been supplied in the replacement record, so that property will be blank in the result.

修改了 IceCream 数据源。The IceCream data source has been modified.
UpdateIf( IceCream, true, { Quantity: 0 } )UpdateIf( IceCream, true, { Quantity: 0 } ) 将数据源中所有记录的 Quantity 属性的值设置为 0。Sets the value of the Quantity property for all records in the data source to 0.

修改了 IceCream 数据源。The IceCream data source has been modified.

分步操作Step by step

  1. 导入或创建名为 Inventory 的集合,让其显示在库中,如在库中显示数据所述。Import or create a collection named Inventory, and show it in a gallery as Show data in a gallery describes.
  2. 将库命名为 ProductGalleryName the gallery ProductGallery.
  3. 添加名为 UnitsSold 的滑块,将其 Max 属性设置为以下表达式:Add a slider named UnitsSold, and set its Max property to this expression:
    ProductGallery.Selected.UnitsInStockProductGallery.Selected.UnitsInStock
  4. 添加一个按钮,然后将其 OnSelect 属性设置为以下公式:Add a button, and set its OnSelect property to this formula:
    UpdateIf(Inventory, ProductName = ProductGallery.Selected.ProductName, {UnitsInStock:UnitsInStock-UnitsSold.Value})UpdateIf(Inventory, ProductName = ProductGallery.Selected.ProductName, {UnitsInStock:UnitsInStock-UnitsSold.Value})
  5. 按 F5,在库中选择一个产品,使用滑块指定一个值,然后选择该按钮。Press F5, select a product in the gallery, specify a value with the slider, and then select the button.

    所指定产品的库存单元数下降指定的数量。The number of units in stock for the product you specified decreases by the amount that you specified.