表のサンプル

これらのサンプルでは、Excel テーブルとの一般的な相互作用について説明します。

並べ替えられたテーブルを作成する

このサンプルでは、現在のワークシートの使用範囲からテーブルを作成し、最初の列に基づいて並べ替えます。

function main(workbook: ExcelScript.Workbook) {
  // Get the current worksheet.
  const selectedSheet = workbook.getActiveWorksheet();

  // Create a table with the used cells.
  const usedRange = selectedSheet.getUsedRange();
  const newTable = selectedSheet.addTable(usedRange, true);

  // Sort the table using the first column.
  newTable.getSort().apply([{ key: 0, ascending: true }]);
}

テーブルをフィルター処理する

このサンプルでは、いずれかの列の値を使用して既存のテーブルをフィルター処理します。

function main(workbook: ExcelScript.Workbook) {
  // Get the table in the workbook named "StationTable".
  const table = workbook.getTable("StationTable");

  // Get the "Station" table column for the filter.
  const stationColumn = table.getColumnByName("Station");

  // Apply a filter to the table that will only show rows 
  // with a value of "Station-1" in the "Station" column.
  stationColumn.getFilter().applyValuesFilter(["Station-1"]);
}

ヒント

を使用して、ブック全体でフィルター処理された情報を Range.copyFromコピーします。 スクリプトの末尾に次の行を追加して、フィルター処理されたデータを含む新しいワークシートを作成します。

  workbook.addWorksheet().getRange("A1").copyFrom(table.getRange());

1 つの値を除外する

前のサンプルでは、含まれる値の一覧に基づいてテーブルをフィルター処理します。 テーブルから特定の値を除外するには、列内の他のすべての値の一覧を指定する必要があります。 このサンプルでは、 関数 columnToSet を使用して、列を一意の値のセットに変換します。 その後、そのセットには除外された値 ("Station-1") が削除されます。

function main(workbook: ExcelScript.Workbook) {
  // Get the table in the workbook named "StationTable".
  const table = workbook.getTable("StationTable");

  // Get the "Station" table column for the filter.
  const stationColumn = table.getColumnByName("Station");

  // Get a list of unique values in the station column.
  const stationSet = columnToSet(stationColumn);

  // Apply a filter to the table that will only show rows
  // that don't have a value of "Station-1" in the "Station" column. 
  stationColumn.getFilter().applyValuesFilter(stationSet.filter((value) => {
      return value !== "Station-1";
  }));
}

/**
 * Convert a column into a set so it only contains unique values.
 */
function columnToSet(column: ExcelScript.TableColumn): string[] {
    const range = column.getRangeBetweenHeaderAndTotal().getValues() as string[][];
    const columnSet: string[] = [];
    range.forEach((value) => {
        if (!columnSet.includes(value[0])) {
            columnSet.push(value[0]);
        }
    });

    return columnSet;
}

テーブル列フィルターを削除する

このサンプルでは、アクティブなセルの場所に基づいて、テーブル列からフィルターを削除します。 スクリプトは、セルがテーブルの一部であるかどうかを検出し、テーブル列を決定し、そのセルに適用されているフィルターをクリアします。

すぐに使用できるブックの table-with-filter.xlsx をダウンロードします。 サンプルを自分で試すには、次のスクリプトを追加します。

function main(workbook: ExcelScript.Workbook) {
  // Get the active cell.
  const cell = workbook.getActiveCell();

  // Get the tables associated with that cell.
  // Since tables can't overlap, this will be one table at most.
  const currentTable = cell.getTables()[0];

  // If there's no table on the selection, end the script.
  if (!currentTable) {
    console.log("The selection is not in a table.");
    return;
  }

  // Get the table header above the current cell by referencing its column.
  const entireColumn = cell.getEntireColumn();
  const intersect = entireColumn.getIntersection(currentTable.getRange());
  const headerCellValue = intersect.getCell(0, 0).getValue() as string;

  // Get the TableColumn object matching that header.
  const tableColumn = currentTable.getColumnByName(headerCellValue);

  // Clear the filters on that table column.
  tableColumn.getFilter().clear();
}

列フィルターをクリアする前に (アクティブなセルに注目してください)

列フィルターをクリアする前のアクティブ セル。

列フィルターをクリアした後

列フィルターをクリアした後のアクティブ セル。

ヒント

フィルターをクリアする前にフィルターを保存する方法の詳細を確認する (後で再適用する) 場合は、「 フィルターを保存してテーブル間で行を移動する」を参照してください。詳細なサンプルです。

テーブル値を動的に参照する

このスクリプトでは、"@COLUMN_NAME" 構文を使用して、テーブル列に数式を設定します。 テーブル内の列名は、このスクリプトを変更せずに変更できます。

function main(workbook: ExcelScript.Workbook) {
  // Get the current worksheet.
  const table = workbook.getTable("Profits");

  // Get the column names for columns 2 and 3.
  // Note that these are 1-based indices.
  const nameOfColumn2 = table.getColumn(2).getName();
  const nameOfColumn3 = table.getColumn(3).getName();

  // Set the formula of the fourth column to be the product of the values found
  // in that row's second and third columns.
  const combinedColumn = table.getColumn(4).getRangeBetweenHeaderAndTotal();
  combinedColumn.setFormula(`=[@[${nameOfColumn2}]]*[@[${nameOfColumn3}]]`);
}

スクリプトの前

Month Price 販売単位 合計
Jan 45 5
Feb 45 3
03 月 45 6

スクリプトの後

Month Price 販売単位 合計
Jan 45 5 225
Feb 45 3 135
03 月 45 6 270