Skip to content

Commit

Permalink
fix(gas/gas-spreadsheet.md): ピボットテーブルを追加した
Browse files Browse the repository at this point in the history
  • Loading branch information
shotakaha committed Oct 27, 2024
1 parent ac544a5 commit c9dd9c3
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/source/gas/gas-spreadsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,45 @@ function onOpen() {

スプレッドシートにカスタムメニューを追加できます。
シートを開いたときに、メニューに追加するため``onOpen``関数の中で定義します。

## ピボットテーブルしたい(`createPivotTable`

```js
function createPivotTable() {
// スプレッドシートを取得
const book = SpreadsheetApp.getActiveSpreadsheet();

// 元データのシート
const sourceSheet = book.getSheetByName("master");
const data = sourceSheet.getDataRange();

// ピボットデータの作成場所を設定
const pivotSheet = book.getSheetByName("ピボットテーブル用")
const range = pivotSheet.getRange("A1");

// ピボットテーブルを作成
const pivotTable = range.createPivotTable(data);

// 行を設定
pivotTable.addRowGroup(1); // 元データの2列目
// 列を設定
pivotTable.addColumnGroup(2); // 元データの3列目
// 値を設定
const method = SpreadsheetApp.PivotTableSummarizeFunction.COUNTA;
pivotTable.addPivotValue(3, method); // 元データの4列目のCOUNTA数

// フィルターの設定
const criteria = SpreadsheetApp.newFilterCriteria().whenCellNotEmpty().build();
pivotTable.addFilter(4, criteria)
}
```

`createPivotTable`でピボットテーブルを作成できます。
ブラウザ操作時と同じように、
行(`addRowGroup`)、
列(`addColumnGroup`)、
値(`addPivotValue`)、
フィルター(`addFilter`
を設定します。

値の集計方法は`COUNTA`の他に、`SUM``AVERAGE`なども利用できます。

0 comments on commit c9dd9c3

Please sign in to comment.