在条件中使用表达式来查看多个值Use expressions in conditions to check multiple values

本演练将介绍如何使用表达式和条件在高级模式下比较多个值。In this walkthrough, you'll learn to use expressions and Conditions to compare multiple values in Advanced mode.

创建流时,可以在基本模式下使用条件卡将单个值和其他值进行快速比较。When you create a flow, you can use the Condition card in basic mode to quickly compare a single value with another value. 但是,有时候需比较多个值。However, there're times when you need to compare multiple values. 例如,可能需要查看电子表格或数据库表中几个列的值。For example, you may want to check the value of a few columns in a spreadsheet or database table.

可以在条件中使用以下逻辑表达式的任何组合。You can use any combination of the following logical expressions in your conditions.

表达式Expression 说明Description 示例Example
andand 采用两个参数,如果两个值均为 true,则返回 true。Takes two arguments and returns true if both values are true.
注意:两个参数都必须为布尔型。Note: Both arguments must be Booleans.
以下表达式返回 false:This expression returns false:
and(greater(1,10),equals(0,0))and(greater(1,10),equals(0,0))
oror 采用两个参数,如果其中一个参数为 true,则返回 true。Takes two arguments and returns true if either argument is true.
注意:两个参数都必须为布尔型。Note: Both arguments must be Booleans.
以下表达式返回 true:This expression returns true:
or(greater(1,10),equals(0,0))or(greater(1,10),equals(0,0))
equalsequals 如果两个值相等,则返回 true。Returns true if two values are equal. 例如,如果 parameter1 为 someValue,则以下表达式返回 true:For example, if parameter1 is someValue, this expression returns true:
equals(parameters('parameter1'), 'someValue')equals(parameters('parameter1'), 'someValue')
lessless 采用两个参数,如果第一个参数小于第二个参数,则返回 true。Takes two arguments and returns true if the first argument is less than the second argument.
注意:支持的类型为整数、浮点和字符串。Note: The supported types are integer, float, and string.
以下表达式返回 true:This expression returns true:
less(10,100)less(10,100)
lessOrEqualslessOrEquals 采用两个参数,如果第一个参数小于或等于第二个参数,则返回 true。Takes two arguments and returns true if the first argument is less than or equal to the second argument.
注意:支持的类型为整数、浮点和字符串。Note: The supported types are integer, float, and string.
以下表达式返回 true:This expression returns true:
lessOrEquals(10,10)lessOrEquals(10,10)
greatergreater 采用两个参数,如果第一个参数大于第二个参数,则返回 true。Takes two arguments and returns true if the first argument is greater than the second argument.
注意:支持的类型为整数、浮点和字符串。Note: The supported types are integer, float, and string.
以下表达式返回 false:This expression returns false:
greater(10,10)greater(10,10)
greaterOrEqualsgreaterOrEquals 采用两个参数,如果第一个参数大于或等于第二个参数,则返回 true。Takes two arguments and returns true if the first argument is greater than or equal to the second argument.
注意:支持的类型为整数、浮点和字符串。Note: The supported types are integer, float, and string.
以下表达式返回 false:This expression returns false:
greaterOrEquals(10,100)greaterOrEquals(10,100)
emptyempty 如果对象、数组或字符串为空,则返回 true。Returns true if the object, array, or string is empty. 以下表达式返回 true:This expression returns true:
empty('')empty('')
notnot 返回布尔值的相反值。Returns the opposite of a boolean value. 以下表达式返回 true:This expression returns true:
not(contains('200 Success','Fail'))not(contains('200 Success','Fail'))
ifif 如果表达式生成 true 或 false,则返回特定值。Returns a specific value if the expression results in true or false. 以下表达式返回“yes”:This expression returns "yes":
if(equals(1, 1), 'yes', 'no')if(equals(1, 1), 'yes', 'no')

先决条件Prerequisites

  • 有权访问 Microsoft Flow。Access to Microsoft Flow.
  • 一个电子表格,其中的表在本演示后面有介绍。A spreadsheet with the tables described later in this walkthrough. 确保将电子表格保存在 Microsoft Flow 能够访问的某个位置,例如 Dropbox 或 Microsoft OneDrive。Be sure to save your spreadsheet in a location such as Dropbox or Microsoft OneDrive so that Microsoft Flow can access it.
  • Microsoft Office 365 Outlook(虽然我们使用 Office 365 Outlook,但你可以在流中使用任何受支持的电子邮件服务。)Microsoft Office 365 Outlook (While we use Office 365 Outlook, you can use any supported email service in your flows.)

使用 or 表达式Use the or expression

有时候,如果某个项的值为 valueA 或 valueB,则工作流需采取操作。Sometimes your workflow needs to take an action if the value of an item is valueA or valueB. 例如,你可能在跟踪电子表格表中任务的状态。For example, you may be tracking the status of tasks in a spreadsheet table. 假设该表有一个名为“状态”的列,“状态”列的可能值为:Assume that the table has a column named Status and the possible values in the Status column are:

  • 已完成completed
  • 已阻止blocked
  • 不必要unnecessary
  • 未启动not started

以下示例展示了该电子表格的外观:Here's an example of what the spreadsheet might look like:

示例电子表格

对于前面的电子表格,你希望使用 Microsoft Flow 删除“状态”列设置为“已完成”或“不必要”的所有行。Given the preceding spreadsheet, you want to use Microsoft Flow to remove all rows with a Status column that's set to completed or unnecessary.

让我们创建该流。Let's create the flow.

从空白流开始Start with a blank flow

  1. 登录到 Microsoft FlowSign into Microsoft Flow.

    登录

  2. 选择“我的流”选项卡。Select the My flows tab.

    选择我的流

  3. 选择“从零开始创建”。Select Create from blank.

    从空白创建

向流添加触发器Add a trigger to your flow

  1. 搜索“计划”,然后选择“计划 - 重复周期”触发器Search for Schedule, and then select the Schedule - Recurrence trigger

    计划触发器

  2. 将计划设置为每天运行一次。Set the schedule to run once daily.

    设置计划

选择电子表格,获取所有行Select the spreadsheet and get all rows

  1. 选择“新建步骤” > “添加操作”。Select New step > Add an action.

    新建步骤

  2. 搜索“行”,然后选择“Excel - 获取行”。Search for rows, and then select Excel - Get rows.

    注意:请选择对应于要使用的电子表格的“获取行”操作。Note: Select the "get rows" action that corresponds to the spreadsheet that you're using. 例如,如果要使用 Google Sheets,则选择“Google Sheets - 获取行”。For example, if you're using Google Sheets, select Google Sheets - Get rows.

    获取行

  3. 选择“文件名”框中的文件夹图标,浏览到相应的文件夹,然后选择包含你的数据的电子表格。Select the folder icon in the File name box, browse to, and then select the spreadsheet that contains your data.

    选择电子表格

  4. 从“表名”列表中选择包含你的数据的表。Select the table that contains your data from the Table name list.

    选择表

查看每一行的状态列Check the status column of each row

  1. 选择“新建步骤” > “更多” > “向每个都添加一个应用”。Select New step > More > Add an apply to each.

    选择表

  2. 向“从先前的步骤中选择一个输出”框添加“值”令牌。Add the Value token to the Select an output from previous steps box.

    选择表

  3. 选择“添加条件” > “在高级模式下编辑”。Select Add a condition > Edit in advanced mode.

  4. 添加以下 or 表达式。Add the following or expression. 此 or 表达式检查表中每个行的值(在表达式中访问时,行称为项)。This or expression checks the value of each row in the table (a row is known as an item when accessed in a expression). 如果“状态”列的值为“已完成”或“不必要”,则 or 表达式的求值结果为“true”。If the value of the status column is completed or unnecessary, the or expression evaluates to "true".

    or 表达式如下所示:The or expression appears as shown here:

    @or(equals(item()?['status'], 'unnecessary'), equals(item()?['status'], 'completed'))

    “条件”卡如下图所示:Your Condition card resembles this image:

    or 表达式图像

从电子表格中删除匹配的行Delete matching rows from the spreadsheet

  1. 选择该条件的“如果是,则不执行任何操作”分支上的“添加操作”。Select Add an action on the IF YES, DO NOTHING branch of the condition.

  2. 搜索“删除行”,然后选择“Excel - 删除行”。Search for Delete row, and then select Excel - Delete row.

    删除行图像

  3. 在“文件名”框中搜索并选择包含要删除数据的电子表格文件。In the File name box, search for, and select the spreadsheet file that contains the data you want to delete.

  4. 从“表名”列表中选择包含你的数据的表。In the Table name list, select the table that contains your data.

  5. 将“行 ID”令牌置于“行 ID”框中。Place the Row id token in the Row id box.

    电子表格文件

命名流并将其保存Name the flow and save it

  1. 给流命名,然后选择“创建流”按钮。Give your flow a name and then select the Create flow button.

    保存流

使用 or 表达式运行流Run the flow with the or expression

流在保存后运行。The flow runs after you save it. 如果已创建此前在本演示中显示的电子表格,则下面是在运行完成后该电子表格的情况:If you created the spreadsheet shown earlier in this walkthrough, here's what the it looks like after the run completes:

or 表达式完成

注意,“状态”列为“已完成”或“不必要”的行中的所有数据均已删除。Notice all data from rows that had "completed" or "unnecessary" in the Status column were deleted.

使用 and 表达式Use the and expression

假设电子表格表有两个列。Assume you have a spreadsheet table with two columns. 列名称为“状态”和“已分配”。The column names are Status and Assigned. 另假设需要删除“状态”列的值为“已阻止”、“已分配”列的值为“John Wonder”的所有行。Assume also that you want to delete all rows if the Status column's value is "blocked" and the Assigned column's value is "John Wonder". 若要完成此任务,请执行本演示中前面介绍的所有步骤,但在以高级模式编辑“条件”卡时,请使用 and 表达式,如下所示:To accomplish this task, follow all steps earlier in this walkthrough, however, when you edit the Condition card in advanced mode, use the and expression shown here:

@and(equals(item()?['Status'], 'blocked'), equals(item()?['Assigned'], 'John Wonder'))

“条件”卡如下图所示:Your Condition card resembles this image:

and 表达式图像

使用 and 表达式运行流Run the flow with the and expression

如果按步骤操作,则电子表格如下图所示:If you followed along, your spreadsheet resembles this image:

在 and 运行之前

在流运行后,电子表格如下图所示:After your flow runs, your spreadsheet resembles this image:

在 and 运行之后

使用 empty 表达式Use the empty expression

注意,电子表格中现在有多个空行。Notice that there are several empty rows in the spreadsheet now. 若要删除这些空行,请使用 empty 表达式确定在“已分配”和“状态”列中没有任何文本的所有行。To remove them, use the empty expression to identify all rows that don't have any text in the Assigned and Status columns.

若要完成此任务,请执行本演示中前面的“使用 and 表达式”部分列出的所有步骤,但在以高级模式编辑“条件”卡时,请按以下方式使用 empty 表达式:To accomplish this task, follow all steps listed in Use the and expression section earlier in this walkthrough, however, when you edit the Condition card in advanced mode, use the empty expression this way:

@and(empty(item()?['Status']), empty(item()?['Assigned']))

“条件”卡如下图所示:Your Condition card resembles this image:

empty 表达式图像

在流运行后,电子表格如下图所示:After your flow runs, the spreadsheet resembles this image:

在 empty 运行之后

注意,已从表中删除额外的行。Notice extra lines are removed from the table.

使用 greater 表达式Use the greater expression

假设你为同事购买了棒球票,于是使用电子表格来确保每个人都付清款项。Imagine you've bought baseball tickets for your coworkers and you're using a spreadsheet to ensure you're reimbursed by each person. 你可以快速创建一个流,向每个尚未支付全款的人每日发送一封电子邮件。You can quickly create a flow that sends a daily email to each person who hasn't paid the full amount.

使用 greater 表达式确定尚未支付全款的员工。Use the greater expression to identify the employees who haven't paid the full amount. 然后即可自动向那些尚未全额支付的员工发送一封善意的提醒邮件。You can then automatically send a friendly reminder email to those who haven't paid in full.

下面是该电子表格的视图:Here's a view of the spreadsheet:

电子表格的视图

下面显示了如何实现 greater 表达式,以便确定尚未全额支付所欠款项的所有人员:Here's the implementation of the greater expression that identifies all persons who have paid less than the amount due from them:

@greater(item()?['Due'], item()?['Paid'])

使用 less 表达式Use the less expression

假设你为同事购买了棒球票,于是使用电子表格来确保每个人在应允支付的日期之前都付清款项。Imagine you've bought baseball tickets for your coworkers, and you're using a spreadsheet to ensure you're reimbursed by each person by the date to which everyone agreed. 可以创建一个流,在当前日期离应允支付日期不到一天时,向每个尚未支付全款的人发送一封提醒邮件。You can create a flow that sends a reminder email to each person who hasn't paid the full amount if the current date is less than one day before the due date.

请将 and 表达式和 less 表达式配合使用,因为有两个条件需要验证:Use the and expression along with the less expression since there are two conditions being validated:

要验证的条件Condition to validate 要使用的表达式expression to use 示例Example
是否已全额支付到期的款项?Has the full amount due been paid? greatergreater @greater(item()?['Due'], item()?['Paid'])@greater(item()?['Due'], item()?['Paid'])
是否离到期日期只有不到一天的时间?Is the due date less than one day away? lessless @less(item()?['DueDate'], addDays(utcNow(),1))@less(item()?['DueDate'], addDays(utcNow(),1))

在 and 表达式中将 greater 和 less 表达式组合到一起Combine the greater and less expressions in an and expression

使用 greater 表达式来确定尚未全额支付欠款的员工,使用 less 表达式来确定当前日期离应允支付的日期是否不到一天。Use the greater expression to identify the employees who have paid less than the full amount due and use the less expression to determine if the payment due date is less than one day away from the current date. 然后即可执行“发送电子邮件”操作,向那些尚未全额支付且离应允支付日期不到一天的人员发送善意的提醒邮件。You can then the Send an email action to send friendly reminder email to those who haven't paid in full and the due date is less than one day away.

下面是该电子表格表的视图:Here's a view of the spreadsheet table:

电子表格的视图

下面显示了如何实现 and 表达式,以便确定尚未全额支付所欠款项且当前日期离应允支付日期不到一天的所有人员:Here's the implementation of the and expression that identifies all persons who have paid less than the amount due from them and the due date is less than one day away from the current date:

@and(greater(item()?['Due'], item()?['Paid']), less(item()?['dueDate'], addDays(utcNow(),1)))

了解详情Learn more

了解其他表达式Learn about other expressions