チュートリアル: Power Automate フローからスプレッドシートを更新する – Office Scripts

このチュートリアルでは、Power Automateを通じてExcelのOffice Scriptを実行する方法を学びます。現在の時刻で2つのセルの値を更新するスクリプトを作成し、Power Automateで手動トリガーのフローに接続します。これにより、ボタンが押されるたびにスクリプトが実行されます。基本的なパターンを理解した後、他のアプリケーションを含むフローに拡張できます。チュートリアルを進める前に、Office ScriptsとPower Automateへのアクセスが必要です。最初にMyWorkbookという新しいワークブックを作成し、TutorialWorksheetというシートを追加します。

Office ScriptをPower Automateで実行するチュートリアル

このチュートリアルでは、ExcelのOffice ScriptをPower Automateを通じて実行する方法を学びます。これにより、2つのセルの値を現在の時刻で更新するスクリプトを作成し、Power Automateのボタンを選択したときにこのスクリプトが実行されるように手動でトリガーされるフローに接続します。基本的なパターンを理解すると、他のアプリケーションを含め、日常のワークフローをさらに自動化することができます。

前提条件

このチュートリアルを実施するには、Office ScriptsとPower Automateへのアクセスが必要です。自動化タブが表示されない場合は、プラットフォームのサポートを確認してください。Power Automateの開始方法については、サインアップFAQを参照してください。

ワークブックの準備

Power Automateは、ワークブックのコンポーネントにアクセスするために、相対参照(例:Workbook.getActiveWorksheet)を使用すべきではありません。それにより、Power Automateが参照できる整然とした名前のワークブックとワークシートを作成する必要があります。

  1. 新しいワークブックを作成し、名前をMyWorkbookとします。
  2. MyWorkbookワークブック内に、スクリプトで使用するワークシートTutorialWorksheetを作成します。

Office Scriptの作成

  1. Automateタブに移動し、New Scriptを選択します。
  2. 次のスクリプトに置き換えます。このスクリプトは、TutorialWorksheetワークシートの最初の2つのセルに現在の日付と時刻を追加します。
    function main(workbook: ExcelScript.Workbook) {
    let worksheet = workbook.getWorksheet("TutorialWorksheet");
    let dateRange = worksheet.getRange("A1");
    let timeRange = worksheet.getRange("B1");
    let date = new Date(Date.now());
    dateRange.setValue(date.toLocaleDateString());
    timeRange.setValue(date.toLocaleTimeString());
    }
  3. スクリプトの名前をSet date and timeに変更します。スクリプト名を選択すると変更できます。
  4. Save Scriptを選択してスクリプトを保存します。

Power Automateで自動化ワークフローを作成する

  1. Power Automateサイトにサインインします。
  2. 左側のメニューでCreateを選択します。これにより、新しいワークフローの作成方法のリストが表示されます。
  3. Start from blankセクションで、Instant flowを選択します。これは手動でトリガーされるワークフローを作成します。
  4. ダイアログウィンドウで、Flow nameテキストボックスにフローの名前を入力します。Choose how to trigger the flowの下からManually trigger a flowを選択します。そして、Createを選択して初期設定を完了します。

    手動トリガーされたフローは、さまざまなフロートタイプの1つにすぎません。次のチュートリアルでは、メールを受信したときに自動的に実行されるフローを作成します。

  5. フロービルダーで、+ボタンを選択し、Add an actionを選択します。
  6. Add an actionのタスクペインで「Excel run script」を検索します。Excel Online (Business)コネクタのRun scriptアクションを選択します。このアクションは、OneDriveのワークブックからスクリプトを実行します。チームのSharePointライブラリに格納されたスクリプトを使用する場合は、Run script from a SharePoint libraryアクションを使用します。

  7. Microsoft 365アカウントへのサインインを求められる場合は、続行するためにサインインしてください。
  8. 次に、フローのステップで使用するワークブックとスクリプトを選択します。チュートリアルでは、OneDriveに作成したワークブックを使用しますが、OneDriveやSharePointのサイトにある任意のワークブックを使用できます。

    Run scriptアクションのパラメータを次のように指定します:

    • Location: OneDrive for Business
    • Document Library: OneDrive
    • File: MyWorkbook.xlsx
    • Script: Set date and time
  9. Saveを選択します。

フローはこれでPower Automateを通じて実行できる状態になりました。フローエディターのTestボタンを使用してテストするか、残りのチュートリアル手順に従ってフローを実行します。

Power Automateを通じてスクリプトを実行する

  1. Power Automateのメインページから、My flowsを選択します。
  2. My flowsタブに表示されたフローのリストからMy tutorial flowを選択します。これにより、以前に作成したフローの詳細が表示されます。
  3. Runを選択します。

    フローフローを実行するためのタスクペインが表示されます。Excel Onlineへのサインインを求められた場合は、Continueを選択してください。

  4. Run flowを選択します。これによりフローが実行され、それに関連するOffice Scriptが実行されます。

  5. Doneを選択します。実行履歴が適切に更新されるのが見えるはずです。
  6. ページを更新して、Power Automateの結果を確認します。もし失敗した場合は、フローの設定を確認し、2回目を実行してみてください。
  7. ワークブックを開いて、更新されたセルを確認します。セルA1に現在の日付が表示され、セルB1に現在の時刻が表示されるはずです。Power Automateは協定世界時(UTC)を使用しているため、時刻はあなたの現地時間からオフセットされている可能性があります。

次のステップ

チュートリアル: メールからのコンテンツをワークブックに自動保存する方法を完成させてください。このチュートリアルでは、ワークフローサービスからOffice Scriptにデータを渡し、特定のイベントが発生したときにPower Automateフローを実行する方法を学びます。

————-

Tutorial: Update a spreadsheet from a Power Automate flow – Office Scripts

Source link

This tutorial guides you through running an Office Script for Excel via Power Automate. You will create a script that updates two cells with the current date and time. This script will then be linked to a manually triggered flow in Power Automate, allowing it to execute whenever you click a button within Power Automate. Once you establish this pattern, you can broaden the flow to integrate with other applications and further automate your workflow.

Prerequisites

Ensure you have access to Office Scripts and Power Automate. If the Automate tab is not visible, check the platform support. You can refer to the Power Automate sign-up FAQ for guidance on getting started.

Preparing the Workbook

  1. Create a new workbook called MyWorkbook.
  2. Inside MyWorkbook, create a worksheet named TutorialWorksheet.
  3. It’s important to avoid using relative references in Power Automate; instead, use consistent names for workbooks and worksheets.

Creating an Office Script

  1. Navigate to the Automate tab and select New Script.
  2. Replace the default script with the following code, which places the current date and time into the first two cells of the TutorialWorksheet:

    function main(workbook: ExcelScript.Workbook) {
     let worksheet = workbook.getWorksheet("TutorialWorksheet");
     let dateRange = worksheet.getRange("A1");
     let timeRange = worksheet.getRange("B1");
     let date = new Date(Date.now());
     dateRange.setValue(date.toLocaleDateString());
     timeRange.setValue(date.toLocaleTimeString());
    }
  3. Rename the script to Set date and time and save it.

Creating an Automated Workflow with Power Automate

  1. Sign in to Power Automate.
  2. From the left menu, select Create and then choose Instant flow for a manually activated workflow.
  3. Assign a name to your flow, select Manually trigger a flow, and click Create.
  4. Within the flow builder, click the + button to add an action.
  5. Search for Excel run script and select the Run script action from the Excel Online (Business) connector.
    • If prompted, log into your Microsoft 365 account.
  6. Specify the necessary parameters for the Run script action:
    • Location: OneDrive for Business
    • Document Library: OneDrive
    • File: Choose MyWorkbook.xlsx
    • Script: Select Set date and time
  7. Click Save to finalize your flow.

Running the Script via Power Automate

  1. From the main Power Automate page, navigate to My flows.
  2. Select My tutorial flow from the displayed list.
  3. Click Run. If prompted, sign in to Excel Online.
  4. Click Run flow to execute the flow, which in turn runs the Office Script.
  5. After completing the run, select Done and refresh the page to view the run history.
  6. Open MyWorkbook to verify that cell A1 contains the current date and cell B1 holds the current time.

Next Steps

Once you’ve completed this tutorial, you can proceed to explore further automation options, such as saving email content to a workbook using the next tutorial, which demonstrates how to pass data from a workflow service to your Office Script.

関連記事