PowerApps 中的 Blank、Coalesce、IsBlank 和 IsEmpty 函式Blank, Coalesce, IsBlank, and IsEmpty functions in PowerApps

測試值是空白,還是資料表未包含記錄,並提供方法來建立「空白」值。Tests whether a value is blank or a table contains no records, and provides a way to create blank values.

概觀Overview

空白是「無值」或「未知值」的預留位置。Blank is a placeholder for "no value" or "unknown value." 如果使用者尚未在文字輸入控制項中輸入任何字元,則該控制項會「空白」。A Text input control is blank if the user hasn't typed any characters in it. 只要使用者在其中輸入字元,相同的控制項將不再為空白。The same control is no longer blank as soon as the user types a character in it. 某些資料來源可以儲存和傳回 NULL 值,而這些 NULL 值在 PowerApps 中會呈現為「空白」。Some data sources can store and return NULL values, which are represented in PowerApps as blank.

注意

目前只有本機集合才支援儲存「空白」值。At this time, storing blank values is supported only for local collections. 我們知道有許多資料來源支援「空白」 (NULL) 值,因此正在努力提升這項限制。We know that many data sources support blank (NULL) values, and we're working to lift this limitation.

任何屬性或計算的值可以是空白。Any property or calculated value can be blank. 例如,布林值通常有兩個值之一︰truefalseFor example, a Boolean value normally has one of two values: true or false. 但除了這兩個之外,它還可以是空白。But in addition to these two, it can also be blank. 這類似於 Microsoft Excel,其中工作表儲存格一開始為空白,但在所有值之中可容納 TRUEFALSE 值。This is similar to Microsoft Excel, where a worksheet cell starts out as blank but can hold the values TRUE or FALSE, among others. 儲存格的內容隨時都可以移除,並會回到空白狀態。At any time, the contents of the cell can be removed, and it would return to a blank state.

空白特定於未包含任何記錄的資料表。Empty is specific to tables that contain no records. 資料表結構可能會保持不變,以資料行名稱完整,但是資料表中沒有資料。The table structure may be intact, complete with column names, but no data is in the table. 資料表可能會以空白為開始,採用記錄且不再為空白,然後移除記錄並再一次是空的。A table may start as empty, take on records and no longer be empty, and then have the records removed and again be empty.

說明Description

Blank 函式會傳回「空白」值。The Blank function returns a blank value. 使用這個函式可以將 NULL 值儲存至支援這些值的資料來源中,以有效地移除欄位中的任何值。Use this to store a NULL value in a data source that supports these values, effectively removing any value from the field.

IsBlank 函式會測試空白值。The IsBlank function tests for a blank value. 可在下列情況找到空白值︰Blank values are found in situations such as these:

  • Blank 函式的傳回值。The return value from the Blank function.
  • 控制項屬性並未設定任何公式。A control property has no formula set for it.
  • 文字輸入控制項中未輸入任何值,或未在清單方塊中選取項目。No value is typed into a text-input control, or no selection is made in a listbox. 您可以使用 IsBlank來提供欄位為必填的意見反應。You can use IsBlank to provide feedback that a field is required.
  • 未包含任何字元的字串具有 Len 為 0。A string that contains no characters has a Len of 0.
  • 函式中發生錯誤。An error occurred in a function. 通常,函式的其中一個引數不是有效。Often, one of the arguments to the function wasn't valid. 如果引數的值為空白,許多函式會傳回空白。Many functions return blank if the value of an argument is blank.
  • 連接的資料來源(例如 SQL Server) 可能會使用 "null" 的值。Connected data sources, such as SQL Server, may use "null" values. 這些值在 PowerApps 中會顯示為空白。These values appear as blank in PowerApps.
  • 未指定 If 函式的 else 部分,且所有條件為 falseThe else portion of an If function wasn't specified, and all conditions were false.
  • 您使用了 Update 函式,但是並未指定所有資料行的值。You used the Update function but didn't specify a value for all columns. 如此一來,便不會有任何值放入您未指定的資料行。As a result, no values were placed in the columns that you didn't specify.

Coalesce 函式會按順序評估其引數,並傳回第一個非空白值。The Coalesce function evaluates its arguments in order and returns the first value that isn't blank. 使用此函式以不同的值來取代空白值,但非空白值保持不變。Use this function to replace a blank value with a different value but leave non-blank values unchanged. 如果所有引數均為空白,則函式會傳回空白If all of the arguments are blank, then the function returns blank. Coalesce 的所有引數必須屬於相同類型;例如,不能混合數字與文字字串。All arguments to Coalesce must be of the same type; for example, you can't mix numbers with text strings. Coalesce( value1, value2 ) 相當於更精簡的 If( IsBlank( value1 ) value1, value2 ),且不需要評估 value1 兩次。Coalesce( value1, value2 ) is the more concise equivalent of If( IsBlank( value1 ) value1, value2 ) and doesn't require value1 to be evaluated twice.

IsEmpty 函式會測試資料表是否包含任何記錄。The IsEmpty function tests whether a table contains any records. 它相當於使用 CountRows 函式,並檢查是否為零。It's equivalent to using the CountRows function and checking for zero. 您可以合併使用 IsEmptyErrors 函式,以檢查資料來源錯誤。You can check for data-source errors by combining IsEmpty with the Errors function.

IsBlankIsEmpty 的傳回值都是布林值 truefalseThe return value for both IsBlank and IsEmpty is a Boolean true or false.

語法Syntax

Blank()Blank()

Coalesce( Value1 [, Value2, ... ] )Coalesce( Value1 [, Value2, ... ] )

  • Value(s) – 必要項目。Value(s) – Required. 要測試的值。Values to test. 每個值會依序進行評估,直到發現非空白值。Each value is evaluated in order until a non-blank value is found. 第一個非空白值之後的值不會受評估。Values after the first non-blank value aren't evaluated.

IsBlank( Value )IsBlank( Value )

  • Value – 必要項目。Value – Required. 要測試的值。Value to test.

IsEmpty( Table )IsEmpty( Table )

  • Table - 必要項目。Table - Required. 要測試記錄的資料表。Table to test for records.

範例Examples

BlankBlank

注意

下列範例目前只適用於本機集合。At this time, the following example only works for local collections. 我們知道有許多資料來源支援「空白」 (NULL) 值,因此正在努力提升這項限制。We know that many data sources support blank (NULL) values and We are working to lift this limitation.

  1. 從頭建立應用程式,並新增按鈕控制項。Create an app from scratch, and add a Button control.
  2. 將按鈕的 OnSelect 屬性設為以下公式:Set the button's OnSelect property to this formula:

    ClearCollect( Cities, { Name: "Seattle", Weather: "Rainy" } )ClearCollect( Cities, { Name: "Seattle", Weather: "Rainy" } )

  3. 預覽應用程式,並按一下或點選您已新增的按鈕,然後關閉 [預覽]。Preview your app, click or tap the button that you added, and then close Preview.
  4. 在 [檔案] 功能表上,按一下或點選 [集合]。On the File menu, click or tap Collections.

    即會出現 Cities 集合,其中顯示一筆含有 "Seattle" 和 "Rainy" 的記錄:The Cities collection appears, showing one record with "Seattle" and "Rainy":

    顯示 Seattle 的天氣為 Rainy 的集合

  5. 按一下或點選上一步箭頭以返回預設工作區。Click or tap the back arrow to return to the default workspace.
  6. 新增標籤控制項,並將其 Text 屬性設定為下列公式:Add a Label control, and set its Text property to this formula:

    IsBlank( First( Cities ).Weather )IsBlank( First( Cities ).Weather )

    標籤會顯示 false,因為 Weather 欄位包含值 ("Rainy")。The label shows false because the Weather field contains a value ("Rainy").

  7. 新增第二個按鈕,並將其 OnSelect 屬性設定為下列公式:Add a second button, and set its OnSelect property to this formula:

    Patch( Cities, First( Cities ), { Weather: Blank() } )Patch( Cities, First( Cities ), { Weather: Blank() } )

  8. 預覽應用程式,並按一下或點選您已新增的按鈕,然後關閉 [預覽]。Preview your app, click or tap the button that you added, and then close Preview.

    Cities 中第一筆記錄的 Weather 欄位會取代為「空白」,以移除先前的 "Rainy"。The Weather field of the first record in Cities is replaced with a blank, removing the "Rainy" that was there previously.

    顯示 Seattle 的 Weather 欄位空白的集合

    標籤會顯示 true,因為 Weather 欄位不再包含值。The label shows true because the Weather field no longer contains a value.

聯合Coalesce

公式Formula 說明Description 結果Result
Coalesce( Blank(), 1 )Coalesce( Blank(), 1 ) 測試 Blank 函式的傳回值,一律會傳回「空白」值。Tests the return value from the Blank function, which always returns a blank value. 因為第一個引數為空白,會繼續進行下一個引數評估,直到找到非空白值。Because the first argument is blank, evaluation continues with the next argument until a non-blank value is found. 11
Coalesce( Blank(), Blank(), Blank(), Blank(), 2, 3 )Coalesce( Blank(), Blank(), Blank(), Blank(), 2, 3 ) Coalesce 會從引數清單的開頭開始,依序評估每個引數,直到找到非空白值為止。Coalesce starts at the beginning of the argument list and evaluates each argument in turn until a non-blank value is found. 在此案例中,前四個引數均傳回空白,因此評估會繼續至第五個引數。In this case, the first four arguments all return blank, so evaluation continues to the fifth argument. 第五個引數為非空白,因此評估到此為止。The fifth argument is non-blank, so evaluation stops here. 會傳回第五個引數的值,而第六個引數則不受評估。The value of the fifth argument is returned, and the sixth argument isn't evaluated. 22

IsBlankIsBlank

  1. 從頭開始建立應用程式,並新增文字輸入控制項,然後將它命名為 FirstNameCreate an app from scratch, add a text-input control, and name it FirstName.
  2. 新增標籤,並將其 Text 屬性設定為下列公式:Add a label, and set its Text property to this formula:

    If( IsBlank( FirstName.Text ), "First Name is a required field." )If( IsBlank( FirstName.Text ), "First Name is a required field." )

    根據預設,文字輸入控制項的 Text 屬性設定為 "Text input"By default, the Text property of a text-input control is set to "Text input". 屬性包含值,因此它不是空白,且標籤不會顯示任何訊息。Because the property contains a value, it isn't blank, and the label doesn't display any message.

  3. 從文字輸入控制項移除所有字元,包含任何空格。Remove all the characters from the text-input control, including any spaces.

    因為 Text 屬性不再包含任何字元,所以其「空白」和 IsBlank( FirstName.Text )trueBecause the Text property no longer contains any characters, it's blank, and IsBlank( FirstName.Text ) will be true. 會顯示必要欄位的訊息。The required field message is displayed.

如需如何使用其他工具來執行驗證的資訊,請參閱 Validate 函式和使用資料來源For information about how to perform validation by using other tools, see the Validate function and working with data sources.

其他範例︰Other examples:

公式Formula 說明Description 結果Result
IsBlank( Blank() )IsBlank( Blank() ) 測試 Blank 函式的傳回值,一律會傳回「空白」值。Tests the return value from the Blank function, which always returns a blank value. truetrue
IsBlank( "" )IsBlank( "" ) 未包含任何字元的字串。A string that contains no characters. truetrue
IsBlank( "Hello" )IsBlank( "Hello" ) 包含一個或多個字元的字串。A string that contains one or more characters. falsefalse
IsBlank( AnyCollection )IsBlank( AnyCollection ) 集合存在,因此它不是空白,即使它不包含任何記錄。Because the collection exists, it isn't blank, even if it doesn't contain any records. 若要檢查空白集合,請改用 IsEmptyTo check for an empty collection, use IsEmpty instead. falsefalse
IsBlank( Mid( "Hello", 17, 2 ) )IsBlank( Mid( "Hello", 17, 2 ) ) Mid 的起始字元超過字串的結尾。The starting character for Mid is beyond the end of the string. 結果是空白字串。The result is an empty string. truetrue
IsBlank( If( false, false ) )IsBlank( If( false, false ) ) If 函式沒有 ElseResultAn If function with no ElseResult. 因為條件一律為 false,這個 If 一律會傳回空白。Because the condition is always false, this If always returns blank. truetrue

IsEmptyIsEmpty

  1. 從頭建立應用程式,並新增按鈕控制項。Create an app from scratch, and add a Button control.
  2. 將按鈕的 OnSelect 屬性設為以下公式:Set the button's OnSelect property to this formula:

    Collect( IceCream, { Flavor: "Strawberry", Quantity: 300 }, { Flavor: "Chocolate", Quantity: 100 } )Collect( IceCream, { Flavor: "Strawberry", Quantity: 300 }, { Flavor: "Chocolate", Quantity: 100 } )

  3. 預覽應用程式,並按一下或點選您已新增的按鈕,然後關閉 [預覽]。Preview your app, click or tap the button that you added, and then close Preview.

    已建立名為 IceCream 的集合,其中包含這項資料︰A collection named IceCream is created and contains this data:

    此集合有兩筆記錄,而並非空白。This collection has two records and isn't empty. IsEmpty( IceCream ) 會傳回 falseCountRows( IceCream ) 會傳回 2IsEmpty( IceCream ) returns false, and CountRows( IceCream ) returns 2.

  4. 新增第二個按鈕,並將其 OnSelect 屬性設定為下列公式:Add a second button, and set its OnSelect property to this formula:

    Clear( IceCream )Clear( IceCream )

  5. 預覽應用程式,並按一下或點選第二個按鈕,然後關閉 [預覽]。Preview your app, click or tap the second button, and then close Preview.

    集合現在是空的︰The collection is now empty:

    Clear 函式會從集合中移除所有記錄,產生空的集合。The Clear function removes all the records from a collection, resulting in an empty collection. IsEmpty( IceCream ) 會傳回 trueCountRows( IceCream ) 會傳回 0IsEmpty( IceCream ) returns true, and CountRows( IceCream ) returns 0.

您也可以使用 IsEmpty 來測試計算的資料表是否為空白,如下列範例所示︰You can also use IsEmpty to test whether a calculated table is empty, as these examples show:

公式Formula 說明Description 結果Result
IsEmpty( [ 1, 2, 3 ] )IsEmpty( [ 1, 2, 3 ] ) 單一資料行資料表包含三個記錄,因此不是空白。The single-column table contains three records and, therefore, isn't empty. falsefalse
IsEmpty( [ ] )IsEmpty( [ ] ) 單一資料行資料表不包含記錄,因此為空白。The single-column table contains no records and is empty. truetrue
IsEmpty( Filter( [ 1, 2, 3 ], Value > 5 ) )IsEmpty( Filter( [ 1, 2, 3 ], Value > 5 ) ) 單一資料行資料表不包含任何大於 5 的值。The single-column table contains no values that are greater than 5. 來自篩選的結果不包含任何記錄,因此是空白的。The result from the filter doesn't contain any records and is empty. truetrue