LoginSignup
7
6

Power Automate で高速に差集合を求める方法 by Office Scripts

Last updated at Posted at 2021-07-15

概要

Power Automateで差集合を求める方法は・・

  1. 二重ループで処理
  2. 二重ループの内側を、Filter Arrayで代用
  3. Office Scriptsで、Excepts()を実装して処理

この最後のやり方を記録しておくことで、簡単に再利用できるといいな、と
image.png

以下を利用した方が簡単で高速です

差集合 by Filter Array

まずは速度比較

方法  504件での記録
二重ループ + Filter Array 3 [min] 1
Office Scripts で実装 8 [sec]
20倍以上の速度差で、ストレス軽減 :relaxed:
image.png

Automate での実装比較

  • ループの面倒な点
    • 比較結果を別途変数に残す必要がある
    • Items(), Item() から、プロパティを選択するのは結構手作業で、ついついミスが・・
    • コメント書いておかないと、このループなんだっけ?ってのが分かりにくくなる

image.png

で、Office Scripts での実装例

Excepts
function main(
	workbook: ExcelScript.Workbook,
	json1st: object,	// 基礎となるJSON
	json2nd: object,	// 除外するJSON
	key: string			// 比較するプロパティ名
) {
	let array1st = []
	// convert json to array by 'map'
	Object.keys(json1st).map((index) => {
		array1st.push(json1st[index])
	});
	let array2nd = []
	// convert json to array by 'for'
	for (let index in json2nd) {
		array2nd.push(json2nd[index]);
	}
	// Automateなら二重ループでの処理
	let excepts2 = array1st.filter(f => !array2nd.find(f2 => f2[key] == f[key]));

	return JSON.stringify(excepts2)
}

Office Scripts 簡単な説明

引数

引数 概要 補足
json1st 基礎となるJSON
json2nd 除外するJSON
key 比較するプロパティ名 各Objectを一致判断させる対象。PrimaryKey的な。複数対応は面倒だったのでやってない・・

Object to Array

  • 二種類のやり方(Object.keys と for) にしているのは、Type Script自体初心者なので備忘録として。指摘歓迎 :laughing:

あとがき

  • 一旦Office Scripts で実装すると、以下のようなことも簡単になるのでお勧め。ってことで以降は次回へ?
    • 排他的論理和
      image.png

    • 置換

description

Comparing two JSON Array to get differences immediately

参考

  1. 180 [s]以上

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6