- 文字や配列が空になっているか調べたい
- 変数や配列を空にしたい
この記事では、そんな方に空の調べ方と空にする方法を紹介します。

この記事を読むと、変数の判定に悩まずフローを作ることができます。
空白・Nullを判定する
空白・Nullを判定する関数は4種類あります。
【Empty関数】空白・Nullを判定 ・・空白・Nullならtrue。それ以外はfalse
↑基本はこれでOK
【length関数】空白を判定 ・・空白なら0。
【coalesce関数】Nullを判定 ・・NullならNull。それ以外はその値
【equals関数】空白やNullを判定 ・・一致すればtrue。それ以外はfalse
今回はそれぞれ4種類使ったパターンを紹介します。
細かいですが、変数の型は6種類あり空白やNullが存在しない型があります。
当たり前ですが、空白が存在しない型は空白の判定はできません。
Nullが存在しない型はNullの判定はできません。
空白 | Null | |
---|---|---|
ブール(はい/いいえ) | × | △ |
整数 | × | △ |
float(小数) | × | △ |
文字列 | ○ | △ |
オブジェクト(JSON) | × | ○ |
アレイ(配列) | × | △ |
△はNullを指定すると型毎に決められた初期値が自動で設定されます。
(1)Emptyで文字列の空白判定
文字列を初期化します。
値は空欄にします。文字列の初期値は空白文字なのでこれで準備はOKです。

Empty関数の中に文字列の変数「テスト」を指定しました。
Empty(variables('テスト'))

フローを動かすと結果にtrueが表示されたので、Emptyで空白文字を判定できました。

(2)lengthで文字列の空白判定
length関数の中に文字列の変数「テスト」を指定しました。
length(variables('テスト'))

フローを動かすと結果に0が表示されたので、lengthで空白文字を判定できました。

(3)equalsで文字列の空白判定
equals関数の中に空白文字を表す「 ” 」と文字列の変数「テスト」を指定しました。
equals('',variables('テスト'))

フローを動かすと結果にtrueが表示されたので、equalsで空白文字を判定できました。

(4)coalesceでJSONのNull判定
JSONを初期化します。
値は空欄にします。オブジェクト(JSON)の初期値はNullなのでこれで準備はOKです。

coalesce関数の中にJSONの変数「テスト」を指定しました。
coalesce(variables('テスト'))

フローを動かすと「クリックしてダウンロードします。」が表示されました。

クリックすると「about:blank」となりました。これはNullを表しています。

true/falseで表示させたい場合はequalsを組み合わせます。
equals(coalesce(variables('テスト')),null)

フローを動かすと結果にtrueが表示されたので、equalsとcoalesceの組み合わせでNullを判定できました。

判定してからの処理
空白であることが分かった場合に合わせて処理を変える方法を2つ紹介します。
- if文を使う
- 条件を使う
if文の場合は、空白だった場合とそうでない場合の処理を書きます。
if(Empty(variables('テスト')),'空白でした。','空白ではありません。')

フローを動かすと「空白でした。」と表示されました。

条件を使う場合、片方に下記式を入力し、もう片方は「true」にします。
Empty(variables('テスト'))

フローを動かすと「はいの場合」が実行され「空白でした。」と表示されました。


作っているフローに合わせてif文と条件を使い分けましょう。
変数や配列を空にする
どの変数でもNullを使うと値を空にできます。
初期値が指定されている型の場合は初期値になります。
変数の初期化を使い文字列の変数を作ります。

変数の設定で値にnullを指定します。

この時、値に直接nullと入力するのではなく、式でnullを入力して表示されたnullを選択しましょう。

フローを動かすと値が空白文字になりました。

nullを指定しましたが、文字列の初期値である空白文字になります。
null=初期値と覚えておくといいでしょう。
例では変数の型を文字列にしましたが、その他の型でも初期値にすることができます。
文字列については初期値が空白文字なので、下記を指定することで初期値に戻すことができます。
String('')
空白・nullを調べる関数と結果一覧
こちらの表は関数の返す値を調査した結果です。
空白文字 | 空白文字の変数(文字列) | 空白文字の変数(アレイ) | 空白文字の変数(JSON) | |
---|---|---|---|---|
Empty | true | true | false | false |
length | 0 | 0 | 1 | エラー※2 |
coalesce | 空白文字※1 | 空白文字※1 | [ “” ] | { “”: “” } |
equals(null,) | false | false | false | false |
equals(”,) | true | true | false | false |
※1 結果欄には何も表示されないが、仕様では最初に見つかったnull以外の文字を返すので空白文字である。
※2 lengthで指定できる引数は文字列か配列のみなのでエラーとなった。以下エラーメッセージ。
InvalidTemplate. Unable to process template language expressions in action ‘作成_14’ inputs at line ‘0’ and column ‘0’: ‘The template language function ‘length’ expects its parameter to be an array or a string. The provided value is of type ‘Object’. Please see https://aka.ms/logicexpressions#length for usage details.’.
null | nullの変数(文字列) | nullの変数(配列) | nullの変数(JSON) | |
---|---|---|---|---|
Empty | true | true | true | true |
length | エラー※2 | 0 | 0 | エラー※2 |
coalesce | null | 空白文字※1 | [] | null |
equals(null,) | true | false | false | false |
equals(”,) | false | true | false | false |
※1 結果欄には何も表示されないが、仕様では最初に見つかったnull以外の文字を返すので空白文字である。
※2 lengthで指定できる引数は文字列か配列のみなのでエラーとなった。以下エラーメッセージ。
InvalidTemplate. Unable to process template language expressions in action ‘作成_14’ inputs at line ‘0’ and column ‘0’: ‘The template language function ‘length’ expects its parameter to be an array or a string. The provided value is of type ‘Object’. Please see https://aka.ms/logicexpressions#length for usage details.’.
まとめ
PowerAutomateで空の調べ方と空にする方法を紹介しました。
空の処理は使おうとすると忘れていることが多いと思います。空の判定(てい)はEmpty(てぃ)。空にする(する)はNull(ぬる)と覚えておきましょう。
PowerAutomateのおすすめ記事
人気ページ
Apply to eachの使い方
アレイのフィルター処理の使い方
日付関数の使い方
エラーを無視する方法
メールの内容をExcelへ転記
メール本文から名前を抽出
>>PowerAutomateの記事一覧を見る<<
お気に入り必須!公式ページ
Microsoft Power Automate
式関数のリファレンス
コネクタのリファレンス
Excel Online (Business)のリファレンス