Class DataSourcePivotTable  |  Apps Script  |  Google for Developers (original) (raw)

Class DataSourcePivotTable

Stay organized with collections Save and categorize content based on your preferences.

DataSourcePivotTable

Access and modify existing data source pivot table. To create a new data source pivot table, use[Range.createDataSourcePivotTable(dataSource)](/apps-script/reference/spreadsheet/range#createDataSourcePivotTable%28DataSource%29).

Only use this class with data that's connected to a database.

Methods

Method Return type Brief description
addColumnGroup(columnName) PivotGroup Adds a new pivot column group based on the specified data source column.
addFilter(columnName, filterCriteria) PivotFilter Adds a new filter based on the specified data source column with the specified filter criteria.
addPivotValue(columnName) PivotValue Adds a new pivot value based on the specified data source column without any summarize function.
addPivotValue(columnName, summarizeFunction) PivotValue Adds a new pivot value based on the specified data source column with the specified summarize function.
addRowGroup(columnName) PivotGroup Adds a new pivot row group based on the specified data source column.
asPivotTable() PivotTable Returns the data source pivot table as a regular pivot table object.
cancelDataRefresh() DataSourcePivotTable Cancels the data refresh associated with this object if it's currently running.
forceRefreshData() DataSourcePivotTable Refreshes the data of this object regardless of the current state.
getDataSource() DataSource Gets the data source the object is linked to.
getStatus() DataExecutionStatus Gets the data execution status of the object.
refreshData() DataSourcePivotTable Refreshes the data of the object.
waitForCompletion(timeoutInSeconds) DataExecutionStatus Waits until the current execution completes, timing out after the provided number of seconds.

Detailed documentation

addColumnGroup(columnName)

Adds a new pivot column group based on the specified data source column.

Parameters

Name Type Description
columnName String The data source column name the pivot group is based on.

Return

[PivotGroup](/apps-script/reference/spreadsheet/pivot-group) — The new pivot group.

Scripts that use this method require authorization with one or more of the following scopes:


addFilter(columnName, filterCriteria)

Adds a new filter based on the specified data source column with the specified filter criteria.

Parameters

Name Type Description
columnName String The data source column name the filter is based on.
filterCriteria FilterCriteria The criteria the filter uses.

Return

[PivotFilter](/apps-script/reference/spreadsheet/pivot-filter) — The new filter.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


addPivotValue(columnName)

Adds a new pivot value based on the specified data source column without any summarize function. For Looker measures only.

The following code shows how you can add a Looker measure as a pivot value.

// TODO(developer): Replace with your spreadsheet ID which has a Looker data // source. const spreadsheet = SpreadsheetApp.openById('abcd1234'); const datasource = spreadsheet.getDataSources()[0]; const pivotTable = datasource.createDataSourcePivotTableOnNewSheet();

pivotTable.addPivotValue('columnName');

This method is only available for Looker data sources.

Parameters

Name Type Description
columnName String The data source column name the pivot value is based on.

Return

[PivotValue](/apps-script/reference/spreadsheet/pivot-value) — The new pivot value.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


addPivotValue(columnName, summarizeFunction)

Adds a new pivot value based on the specified data source column with the specified summarize function.

In order to add pivot values for Looker measures, use [addPivotValue(columnName)](#addPivotValue%28String%29).

Parameters

Name Type Description
columnName String The data source column name the pivot value is based on.
summarizeFunction PivotTableSummarizeFunction The summarize function the pivot value uses.

Return

[PivotValue](/apps-script/reference/spreadsheet/pivot-value) — The new pivot value.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


addRowGroup(columnName)

Adds a new pivot row group based on the specified data source column.

Parameters

Name Type Description
columnName String The data source column name the pivot group is based on.

Return

[PivotGroup](/apps-script/reference/spreadsheet/pivot-group) — The new pivot group.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


asPivotTable()

Returns the data source pivot table as a regular pivot table object.

Return

[PivotTable](/apps-script/reference/spreadsheet/pivot-table) — The pivot table.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


cancelDataRefresh()

Cancels the data refresh associated with this object if it's currently running.

This example shows how to cancel a formula refresh.

const spreadsheet = SpreadsheetApp.getActive(); const formula = spreadsheet.getDataSourceFormulas()[0]; // Cancel the ongoing refresh on the formula. formula.cancelDataRefresh();

Throws an exception if the data source type is not enabled. Use SpreadsheetApp#enable...Execution() methods to enable data execution for specific data source type.

Return

[DataSourcePivotTable](#) — The data object.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


forceRefreshData()

Refreshes the data of this object regardless of the current state. See [refreshData()](#refreshData%28%29) for more details. If you want to cancel a currently running refresh of this object, see [cancelDataRefresh()](#cancelDataRefresh%28%29).

Throws an exception if the data source type is not enabled. Use SpreadsheetApp#enable...Execution() methods to enable data execution for specific data source type.

Return

[DataSourcePivotTable](#) — The data object.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getDataSource()

Gets the data source the object is linked to.

Return

[DataSource](/apps-script/reference/spreadsheet/data-source) — The data source.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getStatus()

Gets the data execution status of the object.

Return

[DataExecutionStatus](/apps-script/reference/spreadsheet/data-execution-status) — The data execution status.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


refreshData()

Refreshes the data of the object.

Throws an exception if currently in [error](/apps-script/reference/spreadsheet/data-execution-state#ERROR) state. Use [DataSource#updateSpec()](/apps-script/reference/spreadsheet/data-source#updateSpec%28DataSourceSpec%29) to update the specification. The method is preferred over [forceRefreshData()](#forceRefreshData%28%29) to prevent unexpected edits on data source.

Throws an exception if the data source type is not enabled. Use SpreadsheetApp#enable...Execution() methods to enable data execution for specific data source type.

Return

[DataSourcePivotTable](#) — The data object.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


waitForCompletion(timeoutInSeconds)

Waits until the current execution completes, timing out after the provided number of seconds. Throws an exception if the execution is not completed when timing out, but does not cancel the data execution.

Parameters

Name Type Description
timeoutInSeconds Integer The time to wait for data execution, in seconds. The maximum is 300 seconds.

Return

[DataExecutionStatus](/apps-script/reference/spreadsheet/data-execution-status) — The data execution status.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-12-03 UTC.