皆さんこんにちは、マイクロソフトの川端です。
Power Automate for desktop (通称PAD) を使い始める方向けのトレーニングコンテンツを提供します。
気象庁から提供されている一般公開データをPADで自動取得する方法を題材にしています。
1. 事前準備
- 最新版のPower Automate for desktopのダウンロード:https://flow.microsoft.com/ja-jp/desktop/
- 2022年12月09日 時点の最新版は2.27です(→最新版の確認方法)。
- 最新版でない場合には以下よりインストーラーをダウンロードしお手持ちの端末環境でインストールしてください
- インストール、初期セットアップ手順: ロボ研@K2_kzyさんがまとめて頂いている内容が分かりやすいです。
- 本セッションでは出来る限り動作環境を合わせるため、ブラウザはEdgeをご利用頂き、Edgeが無い場合にはChromeをお使いください。
- データ取得対象ページ
- 気象庁天気予報データ
- 東京の天気予報(area_code=130000)
- 参考(上級編で扱います)APIによるデータ(json)取得対象(東京)
2. 上級編
上級編では、PADから気象庁で提供されているWebサービス(API)を利用しデータを取得、その後加工して、複数の地域の気象情報のデータをエクセルファイルへ出力できるようにします。
以下の構成を進めていく際には、作成途中でも、随時保存と実行を行い、正しく動くことを確認していくのがPADをはじめとするRPAツールのコツです!
Webサービス(API)の情報を取得しよう
1.APIコールでデータを取得するための変数の設定など準備を行います
- 変数の設定アクションでarea_codeという変数を設定し、地域番号(東京:130000)を設定します
- また、同様にもう一つ変数の設定アクションでarea_numという変数を設定し、初期値を0とします
2.ウェブサービスの呼び出しを設定します
-
以下の項目を設定します
- URL:https://www.jma.go.jp/bosai/forecast/data/forecast/%area_code%.json
- メソッド:Get
- 受け入れ:application/xml
- コンテンツタイプ:application/xml
- 応答を保存:テキストを変数に変換します(Webページ用)
-
実際に実行してみると、Webサービスの応答結果がWebServiceResponse変数に格納されていることが確認できます
取得したデータ(JSON形式)を加工しよう
1.Webサービスから得られたデータをJSON形式として内部で扱えるようにします
- JSONをカスタムオブジェクトに変換するアクションを追加します
- Webサービスから返ってきたデータ(WebServiceResponse)が受け渡されるようにしましょう
- 実際に得られたデータはブラウザでも確認することができますが、
- データを扱う際は、PADとは別にテキストエディタで内容が確認できるようにしましょう。Visual Studio Codeなどを使うとよいでしょう
2.JSONデータを指定し、PAD内部で扱いやすいデータ形式とする
-
変換するdatetimeに
- %JsonAsCustomObject[0]['timeSeries'][0]['timeDefines'][0]%
- を入力します。実際のJSON形式のデータとの対応関係は、テキストエディタ等で開いた実際のJSONデータとの対応関係で把握されることをお勧めします。
- 標準形式に
For eachのループを設定し、地域の気象データをまとめてエクセルファイルへ書き込もう
-
[Excelワークシート] に書き込みを追加します
-
続いてFor eachのループを設定していきます。
-
For eachループの中身としては[Excelワークシートに書き込み]アクションを以下のように4つ設定していきます(途中、アクションをコピーペーストすると作業しやすいでしょう)。
- [Excelワークシートに書き込み]アクション1(番号):
- [Excelワークシートに書き込み]アクション2(エリア情報):
- [Excelワークシートに書き込み]アクション3(天気情報):
- [Excelワークシートに書き込み]アクション4(風情報):
-
最後に、東京だけではない他のエリアでも同様の処理をするため、[変数を大きくする]アクションをFor eachループの中に追加します
3.まとめ
上級編では、Webサービスの呼び出しから得られたデータの加工、JSONオブジェクトからのループ処理によるExcelワークシートへの書き込みを行いました。RPAでは、取得したデータをうまく処理して後続のプロセスにつなげることがポイントになるケースが多く、このようなデータ操作を練習していただくことで、Power Automate Desktopのさらなる利活用につなげていただけましたら幸いです。
補足
「Webサービスを呼び出します」がNWポリシーうまくいかない場合
変数の設定 WebServiceResponseから、以下の値を設定
[{"publishingOffice":"気象庁","reportDatetime":"2022-12-16T11:00:00+09:00","timeSeries":[{"timeDefines":["2022-12-16T11:00:00+09:00","2022-12-17T00:00:00+09:00","2022-12-18T00:00:00+09:00"],"areas":[{"area":{"name":"東京地方","code":"130010"},"weatherCodes":["111","212","101"],"weathers":["晴れ 夜 くもり","くもり 夜 雨","晴れ 時々 くもり"],"winds":["北の風","北の風 後 南の風","北西の風 やや強く 後 南西の風 やや強く"],"waves":["0.5メートル","0.5メートル","1メートル"]},{"area":{"name":"伊豆諸島北部","code":"130020"},"weatherCodes":["111","214","101"],"weathers":["晴れ 夜 くもり","くもり 夕方 から 雨","晴れ 時々 くもり"],"winds":["西の風 強く 後 北東の風","北東の風 後 西の風 新島 では 後 西の風 やや強く","西の風 強く"],"waves":["3メートル 後 2メートル ただし 新島 では 3メートル","2メートル 後 1.5メートル ただし 新島 では 2メートル","2メートル 後 3メートル ただし 新島 では 2.5メートル 後 4メートル"]},{"area":{"name":"伊豆諸島南部","code":"130030"},"weatherCodes":["111","214","200"],"weathers":["晴れ 夜 くもり","くもり 昼過ぎ から 雨","くもり"],"winds":["西の風 やや強く 三宅島 では 西の風 強く","西の風 後 北西の風 強く","西の風 強く"],"waves":["3メートル","2.5メートル","3メートル 後 4メートル"]},{"area":{"name":"小笠原諸島","code":"130040"},"weatherCodes":["101","101","202"],"weathers":["晴れ 時々 くもり","晴れ 時々 くもり","くもり 一時 雨"],"winds":["北東の風","東の風 後 南の風 やや強く","南の風 やや強く 後 北の風"],"waves":["2メートル うねり を伴う","2メートル うねり を伴う","2メートル 後 2.5メートル うねり を伴う"]}]},{"timeDefines":["2022-12-16T12:00:00+09:00","2022-12-16T18:00:00+09:00","2022-12-17T00:00:00+09:00","2022-12-17T06:00:00+09:00","2022-12-17T12:00:00+09:00","2022-12-17T18:00:00+09:00"],"areas":[{"area":{"name":"東京地方","code":"130010"},"pops":["0","10","10","20","30","70"]},{"area":{"name":"伊豆諸島北部","code":"130020"},"pops":["10","10","10","20","80","80"]},{"area":{"name":"伊豆諸島南部","code":"130030"},"pops":["10","10","20","40","90","90"]},{"area":{"name":"小笠原諸島","code":"130040"},"pops":["10","10","10","10","10","20"]}]},{"timeDefines":["2022-12-16T09:00:00+09:00","2022-12-16T00:00:00+09:00","2022-12-17T00:00:00+09:00","2022-12-17T09:00:00+09:00"],"areas":[{"area":{"name":"東京","code":"44132"},"temps":["15","15","5","11"]},{"area":{"name":"大島","code":"44172"},"temps":["14","14","6","13"]},{"area":{"name":"八丈島","code":"44263"},"temps":["15","15","10","14"]},{"area":{"name":"父島","code":"44301"},"temps":["23","23","18","23"]}]}]},{"publishingOffice":"気象庁","reportDatetime":"2022-12-16T11:00:00+09:00","timeSeries":[{"timeDefines":["2022-12-17T00:00:00+09:00","2022-12-18T00:00:00+09:00","2022-12-19T00:00:00+09:00","2022-12-20T00:00:00+09:00","2022-12-21T00:00:00+09:00","2022-12-22T00:00:00+09:00","2022-12-23T00:00:00+09:00"],"areas":[{"area":{"name":"東京地方","code":"130010"},"weatherCodes":["212","101","101","101","200","202","101"],"pops":["","10","10","10","40","50","10"],"reliabilities":["","","A","A","B","C","A"]},{"area":{"name":"伊豆諸島北部","code":"130020"},"weatherCodes":["214","101","101","101","202","202","201"],"pops":["","20","20","20","50","50","20"],"reliabilities":["","","A","A","C","C","A"]},{"area":{"name":"伊豆諸島南部","code":"130030"},"weatherCodes":["214","200","201","201","202","202","200"],"pops":["","40","30","30","50","60","40"],"reliabilities":["","","A","A","C","B","B"]},{"area":{"name":"小笠原諸島","code":"130040"},"weatherCodes":["101","202","201","200","202","202","200"],"pops":["","60","30","40","50","50","40"],"reliabilities":["","","C","C","C","C","B"]}]},{"timeDefines":["2022-12-17T00:00:00+09:00","2022-12-18T00:00:00+09:00","2022-12-19T00:00:00+09:00","2022-12-20T00:00:00+09:00","2022-12-21T00:00:00+09:00","2022-12-22T00:00:00+09:00","2022-12-23T00:00:00+09:00"],"areas":[{"area":{"name":"東京","code":"44132"},"tempsMin":["","3","1","2","3","4","4"],"tempsMinUpper":["","4","3","3","4","6","6"],"tempsMinLower":["","1","-1","0","1","0","3"],"tempsMax":["","11","9","10","12","14","13"],"tempsMaxUpper":["","13","11","12","15","17","16"],"tempsMaxLower":["","8","7","8","10","10","10"]},{"area":{"name":"大島","code":"44172"},"tempsMin":["","5","5","5","7","10","8"],"tempsMinUpper":["","7","7","7","9","12","10"],"tempsMinLower":["","3","3","3","6","8","6"],"tempsMax":["","10","9","11","13","15","12"],"tempsMaxUpper":["","12","11","13","15","18","16"],"tempsMaxLower":["","9","8","10","12","14","10"]},{"area":{"name":"八丈島","code":"44263"},"tempsMin":["","9","8","8","10","12","10"],"tempsMinUpper":["","10","9","10","12","14","12"],"tempsMinLower":["","7","6","7","9","11","8"],"tempsMax":["","13","11","13","17","17","15"],"tempsMaxUpper":["","16","12","15","19","19","18"],"tempsMaxLower":["","12","10","11","15","16","13"]},{"area":{"name":"父島","code":"44301"},"tempsMin":["","19","18","17","18","19","19"],"tempsMinUpper":["","21","19","19","19","21","20"],"tempsMinLower":["","18","17","16","16","18","17"],"tempsMax":["","24","22","21","22","23","23"],"tempsMaxUpper":["","25","23","22","24","25","24"],"tempsMaxLower":["","23","21","20","21","22","22"]}]}],"tempAverage":{"areas":[{"area":{"name":"東京","code":"44132"},"min":"3.2","max":"11.5"},{"area":{"name":"大島","code":"44172"},"min":"6.0","max":"13.0"},{"area":{"name":"八丈島","code":"44263"},"min":"9.4","max":"14.9"},{"area":{"name":"父島","code":"44301"},"min":"17.7","max":"22.3"}]},"precipAverage":{"areas":[{"area":{"name":"東京","code":"44132"},"min":"0.3","max":"9.9"},{"area":{"name":"大島","code":"44172"},"min":"3.1","max":"23.3"},{"area":{"name":"八丈島","code":"44263"},"min":"18.0","max":"42.1"},{"area":{"name":"父島","code":"44301"},"min":"5.5","max":"26.6"}]}}]