Office Scripts

[Power Automate]Formsで回答があったときに質問を動的に変更するフロー

アンケートや投票を行う際に非常に役立つMicrosoft Formsには、現時点(2022年10月)では公式にドキュメント化されていないAPIが用意されています。

例えば下記URIではグループフォームの詳細を取得でき、

GET https://forms.office.com/formapi/api/【テナントID】/groups/【グループID】/forms('【フォームID】')

下記のようにクエリを指定することで取得項目を絞り込むこともできます。

GET https://forms.office.com/formapi/api/【テナントID】/groups/【グループID】/forms('【フォームID】')?$select=descriptiveQuestions,questions,xlWorkbookId&$expand=questions($expand=choices)

このAPIを使って質問項目を変更することもできるので、Power Automateと連携することで、受け取った回答を元に動的に質問を変更するサンプルを考えてみました。

フォームの準備

今回使用するのは下図のようなグループフォームで、テキスト形式で受け取った回答をセクションのサブタイトルとしてリスト形式で表示するようにします。

テナントID・グループID・フォームIDの取得

APIの実行に必要なテナントID、グループID、フォームIDを取得します。

テナントIDの取得方法

テナントIDはAzure AD 管理センターのプロパティページから取得できます。

グループIDの取得方法

グループIDはMicrosoft Formsから対象グループを開くことで、URLから取得できます。

フォームIDの取得方法

フォームIDは編集画面のURLから取得できます。

Formsで回答があったときに質問を動的に変更するフロー

次は実際のフローです。

フロー全体

新しい応答が送信されるとき

Formsの「新しい応答が送信されるとき」をトリガーとし、カスタム値の入力でフォームIDを指定します。

変数を初期化する

何度か使用するテナントID、グループID、フォームIDを変数として定義しておきます。

フォームIDは下記のようにトリガーの出力から取得するようにしています。

@{triggerOutputs()['body/resourceData']['formId']}

SharePoint に HTTP 要求を送信します

Forms APIを使ってフォームから必要な情報を取得します。

  • サイトのアドレス:https://forms.office.com/formapi/api
  • 方法:GET
  • URI:
  • /@{variables('テナントID')}/groups/@{variables('グループID')}/light/forms('@{variables('フォームID')}')?$select=descriptiveQuestions,questions&$expand=questions($expand=choices)

JSON の解析

APIの実行結果から必要な値を取得するため、「JSON の解析」を行います。

  • コンテンツ:
  • @{body('フォーム情報取得')}
  • スキーマ:
  • {
        "type": "object",
        "properties": {
            "descriptiveQuestions": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "groupId": {},
                        "defaultValue": {},
                        "image": {
                            "type": "object",
                            "properties": {
                                "altText": {},
                                "contentType": {},
                                "fileIdentifier": {},
                                "originalFileName": {},
                                "resourceId": {},
                                "resourceUrl": {},
                                "height": {},
                                "width": {},
                                "size": {}
                            }
                        },
                        "modifiedDate": {
                            "type": "string"
                        },
                        "status": {
                            "type": "string"
                        },
                        "subtitle": {
                            "type": "string"
                        },
                        "allowMultipleValues": {},
                        "choices": {
                            "type": "array"
                        },
                        "titleHasPhishingKeywords": {
                            "type": "boolean"
                        },
                        "subtitleHasPhishingKeywords": {
                            "type": "boolean"
                        },
                        "fileUploadSPOInfo": {},
                        "formsProRTQuestionTitle": {
                            "type": "string"
                        },
                        "formsProRTSubtitle": {
                            "type": "string"
                        },
                        "questionTagForIntelligence": {},
                        "insightsInfo": {},
                        "isFromSuggestion": {
                            "type": "boolean"
                        },
                        "imageList": {},
                        "id": {
                            "type": "string"
                        },
                        "isQuiz": {
                            "type": "boolean"
                        },
                        "order": {
                            "type": "integer"
                        },
                        "deserializedQuestionInfo": {},
                        "questionInfo": {},
                        "required": {
                            "type": "boolean"
                        },
                        "title": {
                            "type": "string"
                        },
                        "type": {
                            "type": "string"
                        },
                        "justification": {},
                        "allowCustomChoice": {},
                        "trackingId": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "groupId",
                        "defaultValue",
                        "image",
                        "modifiedDate",
                        "status",
                        "subtitle",
                        "allowMultipleValues",
                        "choices",
                        "titleHasPhishingKeywords",
                        "subtitleHasPhishingKeywords",
                        "fileUploadSPOInfo",
                        "formsProRTQuestionTitle",
                        "formsProRTSubtitle",
                        "questionTagForIntelligence",
                        "insightsInfo",
                        "isFromSuggestion",
                        "imageList",
                        "id",
                        "isQuiz",
                        "order",
                        "deserializedQuestionInfo",
                        "questionInfo",
                        "required",
                        "title",
                        "type",
                        "justification",
                        "allowCustomChoice",
                        "trackingId"
                    ]
                }
            },
            "questions": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "groupId": {},
                        "defaultValue": {},
                        "image": {
                            "type": "object",
                            "properties": {
                                "altText": {},
                                "contentType": {},
                                "fileIdentifier": {},
                                "originalFileName": {},
                                "resourceId": {},
                                "resourceUrl": {},
                                "height": {},
                                "width": {},
                                "size": {}
                            }
                        },
                        "modifiedDate": {
                            "type": "string"
                        },
                        "status": {
                            "type": "string"
                        },
                        "subtitle": {},
                        "allowMultipleValues": {},
                        "choices": {
                            "type": "array"
                        },
                        "titleHasPhishingKeywords": {
                            "type": "boolean"
                        },
                        "subtitleHasPhishingKeywords": {
                            "type": "boolean"
                        },
                        "fileUploadSPOInfo": {},
                        "formsProRTQuestionTitle": {
                            "type": "string"
                        },
                        "formsProRTSubtitle": {},
                        "questionTagForIntelligence": {
                            "type": "string"
                        },
                        "insightsInfo": {
                            "type": "string"
                        },
                        "isFromSuggestion": {
                            "type": "boolean"
                        },
                        "imageList": {},
                        "id": {
                            "type": "string"
                        },
                        "isQuiz": {
                            "type": "boolean"
                        },
                        "order": {
                            "type": "integer"
                        },
                        "deserializedQuestionInfo": {},
                        "questionInfo": {
                            "type": "string"
                        },
                        "required": {
                            "type": "boolean"
                        },
                        "title": {
                            "type": "string"
                        },
                        "type": {
                            "type": "string"
                        },
                        "justification": {},
                        "allowCustomChoice": {},
                        "trackingId": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "groupId",
                        "defaultValue",
                        "image",
                        "modifiedDate",
                        "status",
                        "subtitle",
                        "allowMultipleValues",
                        "choices",
                        "titleHasPhishingKeywords",
                        "subtitleHasPhishingKeywords",
                        "fileUploadSPOInfo",
                        "formsProRTQuestionTitle",
                        "formsProRTSubtitle",
                        "questionTagForIntelligence",
                        "insightsInfo",
                        "isFromSuggestion",
                        "imageList",
                        "id",
                        "isQuiz",
                        "order",
                        "deserializedQuestionInfo",
                        "questionInfo",
                        "required",
                        "title",
                        "type",
                        "justification",
                        "allowCustomChoice",
                        "trackingId"
                    ]
                }
            }
        }
    }

変数を初期化する

動的に変更するサブタイトルのIDを変数として定義します。

@{body('JSON_の解析')?['descriptiveQuestions'][0]['id']}

回答用Excelファイルから取得する列を指定するため、質問のタイトルを変数として定義します。

@{body('JSON_の解析')?['questions'][0]['title']}

待ち時間

回答結果がExcelファイルに反映されるまで適当に待機処理をはさみます。

スクリプトの実行

回答用Excelファイルを対象にOffice スクリプトを実行し、投稿された回答をリスト形式で取得します。

実行するスクリプトは下記の通りです。

SharePoint に HTTP 要求を送信します

最後にForms APIを使ってフォームの質問項目を変更します。

  • サイトのアドレス:https://forms.office.com/formapi/api
  • 方法:PATCH
  • URI:
  • /@{variables('テナントID')}/groups/@{variables('グループID')}/forms('@{variables('フォームID')}')/descriptiveQuestions('@{variables('セクションサブタイトルID')}')
  • ボディ:
  • {
      "subtitle": "@{outputs('スクリプトの実行')?['body/result/subtitle']}",
      "formsProRTSubtitle": "@{outputs('スクリプトの実行')?['body/result/formsProRTSubtitle']}"
    }

実行結果

実行結果は下図の通りです。

以上のように、Forms APIとPower Automateを使用することで、Formsの回答送信時に動的に質問項目(今回はセクションのサブタイトル)を変更できました。

ただ、上でも書いた通り、FormsのAPIはドキュメント化されていないものですので、保証された仕様では無い点については注意が必要です。

Windows版ExcelでOffice スクリプトを作成・実行できるようになりました。前のページ

2022年10月の人気記事次のページ

関連記事

  1. Office Scripts

    マクロの記録後に記録した操作をOffice スクリプトとしても保存できるようになりました。

    先日、Excelでマクロの記録操作を行ったところ、記録を終了した際に作…

  2. Office Scripts

    Office Scriptsの使いどころ

    これまで当ブログでOffice Scriptsについて色々と記事を書い…

  3. Power Automate for desktop

    Power Automate for desktopのアクション一覧

    Power Automate for desktopのアクションペイン…

  4. Office Scripts

    [Office Scripts]グラフを作成する方法

    今回は、Office Scriptsのスクリプトでグラフを作成する方法…

  5. Office Scripts

    [Office Scripts]外部APIを呼び出す方法

    下記サイトにあるように、Office Scriptsでは外部ライブラリ…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP