0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Power Automate】Excel テーブルを YAML ファイルに変換する

Last updated at Posted at 2025-05-21

【Power Automate】Excel テーブルを YAML ファイルに変換する

目的

Excel ブック内に存在するすべてのテーブルを、それぞれ YAML 形式に変換し、SharePoint ライブラリの指定フォルダに個別の YAML ファイルとして保存します。

Power Automate (クラウドフロー) には、JSON 変換のアクションはありますが、YAML 変換のアクションは現状ありません。(2025/05 現在)

Office スクリプトは Excel for the Web 上で実行される安全なサンドボックス環境のため、Office スクリプト 単体ではローカルファイルやクラウドストレージに直接ファイルを書き出すことはできません。Office スクリプトPower Automate を連携させることで実現します。

処理の流れ

  1. Office スクリプト で YAML 文字列を生成する
  2. Power AutomateOffice スクリプト を呼び出し、変換された YAML 文字列を元にファイルを作成し、SharePoint ライブラリに格納する

シーケンス

ステップ 1:Office スクリプトの作成

image.png

Excel ブック内のすべてのテーブルを取得し、それぞれを YAML 形式に変換し、配列として返すスクリプトを作成します。

スクリプトのポイント

  • 空セルや null 値は "null" として出力
  • ファイル名は {テーブル名}.yaml
  • Power Automate で扱いやすいように配列形式で返す
type TableInfo = {
    BaseName: string;
    FileName: string;
    Content: string;
};

function main(workbook: ExcelScript.Workbook): TableInfo[] {
    const tables = workbook.getTables();
    const result: TableInfo[] = [];

    for (const table of tables) {
        const yaml = convertTableToYAML(table);
        result.push({
            BaseName: `${table.getName()}`,
            FileName: `${table.getName()}.yaml`,
            Content: yaml
        });
    }

    return result;
}

function convertTableToYAML(table: ExcelScript.Table): string {
    const headers = table.getHeaderRowRange().getValues()[0];
    const rows = table.getRangeBetweenHeaderAndTotal().getValues();

    let yaml = "";

    rows.forEach((row) => {
        yaml += "- ";
        row.forEach((cell, colIndex) => {
            const key = headers[colIndex];
            let value: string;

            if (cell === null || cell === "") {
                value = "null";
            } else if (typeof cell === "string") {
                value = `"${cell}"`;
            } else if (typeof cell === "boolean") {
                value = cell ? "true" : "false";
            } else {
                value = cell.toString();
            }

            yaml += `${colIndex === 0 ? "" : "  "}${key}: ${value}\n`;
        });
    });

    return yaml;
}

ステップ 2:Power Automate フローの作成

選択したファイルの場合

  • トリガー名: 【モバイルのフロー ボタン】「フローを手動でトリガーする」

パラメーター:

  • 場所: SharePoint サイト
  • ドキュメント ライブラリ: 対象のライブラリ

スクリプトの実行

  • アクション名:【Excel Online (Business)】「スクリプトを実行する」

パラメーター:

  • 場所: SharePoint サイト
  • ドキュメント ライブラリ: 対象のライブラリ
  • ファイル: Office スクリプトを作成したファイル名
  • スクリプト: 上記で作成した Office スクリプト

ファイルの作成

  • アクション名:「Apply to each (各要素に適用)
    • 前のステップから出力を選択します:スクリプトの戻り値 (配列)
      outputs('スクリプトの実行')?['body/result']
  • ループ内のアクション:「ファイルの作成(SharePoint)
    • サイトのアドレス: 対象の SharePoint サイト
    • フォルダー パス: 例 /Shared Documents/YAML
    • ファイル名items('Apply_to_each')?['filename']
    • ファイル コンテンツitems('Apply_to_each')?['content']
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?