PowerApps 中的 Set 函数Set function in PowerApps

设置全局变量的值。Sets the value of a global variable.

概述Overview

使用 Set 函数设置全局变量的值,该变量暂时保留一条信息,如用户已选择某按钮的次数或数据运算的结果。Use the Set function to set the value of a global variable, which temporarily holds a piece of information, such as the number of times the user has selected a button or the result of a data operation.

全局变量可用于应用的所有屏幕。Global variables are available throughout your app on all screens. 它们是最简单的变量类型,并且可满足多数场景的需求。These are the simplest kind of variables and fill the needs of most situations. 另外,还有上下文变量,它们的作用域仅限于单个屏幕和允许对表进行行级别修改的集合。There are also context variables which are scoped to a single screen and collections that allow row level modifications to tables. 有关这些其他选项的详细信息,请参阅使用变量For more information on these other options see working with variables.

PowerApps 以公式为基础,这些公式会在用户与应用交互时自动重新计算。PowerApps are based on formulas that automatically recalculate as the user interacts with an app. 全局变量不具有此优势,因此,在应用的创建和理解上可能难度更大。Global variables don't offer this benefit and can make your app harder to create and understand. 使用变量之前,请参阅使用变量Before you use a variable, review working with variables.

说明Description

全局变量是使用 Set 函数隐式创建而成。Global variables are implicitly created by using the Set function. 无需显式声明。There is no explicit declaration required. 如果删除了全局变量的所有 Set 函数,则该全局变量将不再存在。If you remove all the Set functions for a global variable, then that global variable will cease to exist. 若要清除变量,请将其值设置为 Blank 函数的结果。To clear a variable set its value to the result of the Blank function.

在创作环境中,可以使用“文件”菜单下的“变量”视图查看变量的值、定义和使用情况。You can see your variables' values, definitions, and uses with the Variables view under the File menu in the authoring environment.

如本主题后面的示例所示,全局变量可保留多种信息,包括:As the examples later in this topic show, global variables can hold several kinds of information, including these:

  • 单个值a single value
  • 记录a record
  • a table
  • 对象引用an object reference
  • 公式的任何结果any result from a formula

全局变量可保留其值,直到应用关闭。A global variable holds its value until the app is closed. 应用关闭后,全局变量的值将丢失,重新加载应用时则必须重新创建该值。Once closed, the global variable's value will be lost and must be recreated when the app is loaded again.

全局变量使用的名称不能与已有集合或控件相同。Global variables cannot use the same name as an existing collection or control. 其使用的名称可以与上下文变量相同。It can use the same name as a context variable. 若要区分这两者,请使用消除歧义运算符To disambiguate between the two, use the disambiguation operator.

Set 没有返回值,只可以在行为公式中使用它。Set has no return value, and you can use it only within a behavior formula.

语法Syntax

Set( VariableName, Value )Set( VariableName, Value )

  • VariableName - 必需。VariableName - Required. 要创建或更新的全局变量名称。The name of a global variable to create or update.
  • Value - 必需。Value - Required. 要分配给上下文变量的值。The value to assign to the context variable.

示例Examples

公式Formula 说明Description 结果Result
Set( Counter, 1 )Set( Counter, 1 ) 创建或修改全局变量 Counter,将其值设置为 1Creates or modifies the global variable Counter, setting its value to 1. Counter 的值为 1Counter has the value 1. 可通过在公式中使用名称 Counter 来在任意屏幕上引用该变量。You can reference that variable by using the name Counter in a formula on any screen.
Set( Counter, 2 )Set( Counter, 2 ) 将上一示例中 Counter 全局变量的值设置为 2Sets the value of the Counter global variable from the previous example to 2. Counter 的值为 2Counter has the value 2.
Set( Counter, Counter + 1 )Set( Counter, Counter + 1 ) 将上一示例中 Counter 全局变量的值增加至 3Increments the value of the Counter global variable from the previous example to 3. Counter 的值为 3Counter has the value 3.
Set( Name, "Lily" )Set( Name, "Lily" ) 创建或修改全局变量 Name,将其值设置为 LilyCreates or modifies the global variable Name setting its value to Lily. Name 的值为 LilyName has the value Lily.
Set( Person, { Name: "Milton", Address: "1 Main St" } )Set( Person, { Name: "Milton", Address: "1 Main St" } ) 创建或修改全局变量 Person,将其值设置为一条记录。Creates or modifies the global variable Person, setting its value to a record. 该记录包含名为“姓名”和“地址”的两列。The record contains two columns, named Name and Address. “姓名”列为 Milton,“地址”列的值为 1 Main StThe value of the Name column is Milton, and the value of the Address column is 1 Main St. Person 的值为记录 { Name: "Milton", Address: "1 Main St" }Person has the value of record { Name: "Milton", Address: "1 Main St" }.

使用名称 Person 整体引用此记录,或使用 Person.NamePerson.Address 引用此记录的单个列。Reference this record as a whole with the name Person, or reference an individual column of this record with Person.Name or Person.Address.
Set( Person, Patch( Person, {Address: "2 Main St" } ) )Set( Person, Patch( Person, {Address: "2 Main St" } ) ) 搭配使用 Patch 函数更新 Person 全局变量,将“地址”列的值设置为 2 Main StWorks with the Patch function to update the Person global variable by setting the value of the Address column to 2 Main St. Person 现在的值为记录 { Name: "Milton", Address: "2 Main St" }Person now has the value of record { Name: "Milton", Address: "2 Main St" }.