PivotTable

Access and modify pivot tables.

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#addCalculatedPivotValue(String,String)` | `https://developers.google.com/apps-script/reference/spreadsheet/pivot-value` | Creates a new pivot value in the pivot table calculated from the specified `formula` with the specified `name`. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#addColumnGroup(Integer)` | `https://developers.google.com/apps-script/reference/spreadsheet/pivot-group` | Defines a new pivot column grouping in the pivot table. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#addFilter(Integer,FilterCriteria)` | `https://developers.google.com/apps-script/reference/spreadsheet/pivot-filter` | Creates a new pivot filter for the pivot table. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#addPivotValue(Integer,PivotTableSummarizeFunction)` | `https://developers.google.com/apps-script/reference/spreadsheet/pivot-value` | Defines a new pivot value in the pivot table with the specified `summarizeFunction`. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#addRowGroup(Integer)` | `https://developers.google.com/apps-script/reference/spreadsheet/pivot-group` | Defines a new pivot row grouping in the pivot table. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#asDataSourcePivotTable()` | `https://developers.google.com/apps-script/reference/spreadsheet/data-source-pivot-table|null` | Returns the pivot table as a data source pivot table if the pivot table is linked to a `https://developers.google.com/apps-script/reference/spreadsheet/data-source`, or `null` otherwise. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#getAnchorCell()` | `https://developers.google.com/apps-script/reference/spreadsheet/range` | Returns the `https://developers.google.com/apps-script/reference/spreadsheet/range` representing the cell where this pivot table is anchored. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#getColumnGroups()` | `https://developers.google.com/apps-script/reference/spreadsheet/pivot-group` | Returns an ordered list of the column groups in this pivot table. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#getFilters()` | `https://developers.google.com/apps-script/reference/spreadsheet/pivot-filter` | Returns an ordered list of the filters in this pivot table. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#getPivotValues()` | `https://developers.google.com/apps-script/reference/spreadsheet/pivot-value` | Returns an ordered list of the pivot values in this pivot table. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#getRowGroups()` | `https://developers.google.com/apps-script/reference/spreadsheet/pivot-group` | Returns an ordered list of the row groups in this pivot table. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#getSourceDataRange()` | `https://developers.google.com/apps-script/reference/spreadsheet/range` | Returns the source data range on which the pivot table is constructed. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#getValuesDisplayOrientation()` | `https://developers.google.com/apps-script/reference/spreadsheet/dimension` | Returns whether values are displayed as rows or columns. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#remove()` | `void` | Deletes this pivot table. |
| `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#setValuesDisplayOrientation(Dimension)` | `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#` | Sets the layout of this pivot table to display values as columns or rows. |

## Detailed documentation

### `addCalculatedPivotValue(name, formula)`

Creates a new pivot value in the pivot table calculated from the specified `formula` with
the specified `name`.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `name` | `String` | The name for this calculated pivot value. |
| `formula` | `String` | The formula used to calculate this value. |

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/pivot-value` --- the newly created `https://developers.google.com/apps-script/reference/spreadsheet/pivot-value`

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `addColumnGroup(sourceDataColumn)`

Defines a new pivot column grouping in the pivot table. The specified `sourceDataColumn`
indicates the column in the source data this grouping is based on.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `sourceDataColumn` | `Integer` | The number of the column this group summarizes. This index represents the absolute number of the column in the spreadsheet; `1` representing column "A," `2` representing column B, etc. |

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/pivot-group` --- the newly created `https://developers.google.com/apps-script/reference/spreadsheet/pivot-group`

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `addFilter(sourceDataColumn, filterCriteria)`

Creates a new pivot filter for the pivot table. The specified `sourceDataColumn`
indicates the column in the source data this filter operates on.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `sourceDataColumn` | `Integer` | The number of the column this group summarizes. This index represents the absolute number of the column in the spreadsheet; `1` representing column "A," `2` representing column B, etc. |
| `filterCriteria` | `https://developers.google.com/apps-script/reference/spreadsheet/filter-criteria` | The filter criteria used to perform the filtering. |

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/pivot-filter` --- the newly created `https://developers.google.com/apps-script/reference/spreadsheet/pivot-filter`

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `addPivotValue(sourceDataColumn, summarizeFunction)`

Defines a new pivot value in the pivot table with the specified `summarizeFunction`. The
specified `sourceDataColumn` indicates the column in the source data this value is based
on.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `sourceDataColumn` | `Integer` | The number of the column this group summarizes. This index represents the absolute number of the column in the spreadsheet; `1` representing column "A," `2` representing column B, etc. |
| `summarizeFunction` | `https://developers.google.com/apps-script/reference/spreadsheet/pivot-table-summarize-function` |   |

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/pivot-value` --- the newly created `https://developers.google.com/apps-script/reference/spreadsheet/pivot-value`

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `addRowGroup(sourceDataColumn)`

Defines a new pivot row grouping in the pivot table. The specified `sourceDataColumn`
indicates the column in the source data this grouping is based on.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `sourceDataColumn` | `Integer` | The number of the column this group summarizes. This index represents the absolute number of the column in the spreadsheet; `1` representing column "A," `2` representing column B, etc. |

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/pivot-group` --- the newly created `https://developers.google.com/apps-script/reference/spreadsheet/pivot-group`

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `asDataSourcePivotTable()`

Returns the pivot table as a data source pivot table if the pivot table is linked to a `https://developers.google.com/apps-script/reference/spreadsheet/data-source`, or `null` otherwise.

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/data-source-pivot-table|null` --- A data source pivot table.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `getAnchorCell()`

Returns the `https://developers.google.com/apps-script/reference/spreadsheet/range` representing the cell where this pivot table is anchored.

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/range` --- this pivot table's anchor cell

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `getColumnGroups()`

Returns an ordered list of the column groups in this pivot table.

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/pivot-group` --- the column groups in this pivot table

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `getFilters()`

Returns an ordered list of the filters in this pivot table.

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/pivot-filter` --- the filters in this pivot table

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `getPivotValues()`

Returns an ordered list of the pivot values in this pivot table.

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/pivot-value` --- the pivot values in this pivot table

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `getRowGroups()`

Returns an ordered list of the row groups in this pivot table.

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/pivot-group` --- the row groups in this pivot table

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `getSourceDataRange()`

Returns the source data range on which the pivot table is constructed.

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/range` --- The source data range of this pivot table.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `getValuesDisplayOrientation()`

Returns whether values are displayed as rows or columns.

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/dimension` --- whether values are displayed as rows or columns

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `remove()`

Deletes this pivot table. Further operations on this pivot table results in an error.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`

*** ** * ** ***

### `setValuesDisplayOrientation(dimension)`

Sets the layout of this pivot table to display values as columns or rows.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `dimension` | `https://developers.google.com/apps-script/reference/spreadsheet/dimension` | The dimension indicating how to display pivot values. |

#### Return


`https://developers.google.com/apps-script/reference/spreadsheet/pivot-table#` --- the pivot table for chaining

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/spreadsheets.currentonly`
- `https://www.googleapis.com/auth/spreadsheets`