Azure Functionsを使ってみる:[番外編] 改行コードの置換を関数で実施する

はじめに

以前に、Power Automateで改行コードを認識して<br />タグに置換する、ということをやりました。

blog.jbs.co.jp

この時は、改行コードを直接認識できずに変数を使うなど、半ば無理やり実現していましたが、Azure Functionsを使うともうちょっと素直に作れそうだったので、試してみたいと思います。

こちらの内容を参考にして作っていきたいと思います。

blog.jbs.co.jp

PowerShellでの動作確認

まず、そもそもPowerShellで改行コードを認識して正規表現で置換できるのか試してみます。

次のようなスクリプトを作成して実行してみました。

## 置換する文字列
$text = @"
abcde
abcde
"@

## 検索ルール
$findrule = '\n'

## 置換ルール
$replaserule = '<br />'

## 置換の処理
$text -replace $findrule, $replaserule

出力結果はこうなりました。ちゃんと改行コードが置換できたことがわかります。

abcde<br />abcde

Azure Functionsに実装する

PowerShellとして動いたので、これをAzure Functionsで実装します。

前回作成した正規表現の関数を元にしますが、今回は置換ルールはパラメーターではなく、関数側に持たせてしまいます。

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# 置換する際の検索ルール
$findrule = '\n'

# 置換する際の置換ルール
$replaserule = '<br />'

# 置換する元のテキストを受け取る
$text = $Request.Query.Text
if (-not $text) {
    $text = $Request.Body.Text
}

$body = $text -replace $findrule, $replaserule

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $body
})

Power Automateから利用する

それでは作成した関数をPower Automateから利用してみます。

手動トリガーで受け取った文字列を関数のルールに従って置換するだけの簡単なフローを用意しました。

この時、置換したい文字列に改行を入れておきます。

うっかりしていたのですが、通知メッセージだとhtmlタグが認識されるので改行が入ってしまいますね。

Power Automateの実行履歴で確認すると、きちんと置換されていることがわかります。

応用:置換ルールを変更する

Power Automateの中で完結していた時はあまり複雑な置換が出来なかったので改行コードをhtmlタグに変えるので精いっぱいだったのですが、正規表現が使えるなら話は別です。

応用として、各行を<p>~~</p>で囲う、という処理にしてみたいと思います。

PowerShellで試す

まず例によってPowerShellで試します。

ちょっと手間なのですが、複数回の置換を組み合わせる形で実装してみました。

## 置換する文字列
$text = @"
abcde
abcde
abcde
"@

## 検索ルール1
$findrule1 = '^'

## 置換ルール1
$replaserule1 = '<p>'

## 置換の処理1
$text = $text -replace $findrule1, $replaserule1

## 検索ルール2
$findrule2 = '\n'

## 置換ルール2
$replaserule2 = '</p><p>'

## 置換の処理2
$text = $text -replace $findrule2, $replaserule2

## 検索ルール3
$findrule3 = '$'

## 置換ルール3
$replaserule3 = '</p>'

## 置換の処理3
$text -replace $findrule3, $replaserule3

実行結果です。意図した形になっています。

<p>abcde</p><p>abcde</p><p>abcde</p>

Azure Functionsに移植する

このようにしました。

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# 置換する際の検索ルール1
$findrule1 = '^'

# 置換する際の置換ルール1
$replaserule1 = '<p>'

# 置換する際の検索ルール2
$findrule2 = '\n'

# 置換する際の置換ルール2
$replaserule2 = '</p><p>'

# 置換する際の検索ルール3
$findrule3 = '$'

# 置換する際の置換ルール3
$replaserule3 = '</p>'

# 置換する元のテキストを受け取る
$text = $Request.Query.Text
if (-not $text) {
    $text = $Request.Body.Text
}

$text = $text -replace $findrule1, $replaserule1
$text = $text -replace $findrule2, $replaserule2
$body = $text -replace $findrule3, $replaserule3

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $body
})

Power Automateからのテスト

HTTPコネクタで利用する関数を変えて実行してみました。

無事、各行を<p>タグで囲うことに成功しました。

おわりに

Azure Functions側で複雑な処理を持たせてあげると、Power Automateで出来ないことが出来たり、Power Automate側のメンテナンスコストが下がるなど、うまく利用するといろいろな活用が出来そうです。

これで一連のAzure Functionsの実験は終わりにしますが、今後も適宜利用していければと思います。

執筆担当者プロフィール
舟越 匠

舟越 匠(日本ビジネスシステムズ株式会社)

人材開発部に所属。社内向けの技術研修をメインにしつつ、JBS Tech BlogやMS認定資格取得の推進役もやっています。資格としてはAzure Solutions Architect Expertを所持。好きなサービスはPower Automate / Logic Apps。好きなアーティストはZABADAK。

担当記事一覧