// This module is included in the following assemblies:
// as_connecting-to-google-sheets.adoc

[id='add-google-sheets-connection-add-pivot-table_{context}']
= Adding a pivot table to a sheet

In the middle of a flow, or to finish a simple integration, 
you can add a pivot table to a Google Sheets spreadsheet.
A pivot table lets you aggregate, sort, or apply a function to 
spreadsheet data and display the results in the same spreadsheet. 
To add a pivot table, add a Google Sheets connection to the middle of a flow 
or as a simple integration's finish connection. Then add a data mapping
step before the connection. In the data mapping step, you set 
options that determine the location, properties, and content of the 
pivot table. 

In this release, the *Add pivot table* action is limited to defining 
one value group, one row pivot group, and one column pivot group. 
Support for multiple groups in a single action is expected to be 
added in a future release. As a workaround, you can add multiple
Google Sheets connections that add pivot tables based on the same
source spreadsheet. 

.Prerequisites
* You created a Google Sheets connection that is authorized to access
the Google spreadsheet that you want to add a pivot table to.
* You have access to the ID for the spreadsheet that contains the source
data for the pivot table. 
* You are creating or editing a flow and {prodname} is prompting you
to add to the integration.  Or, {prodname} is prompting you to choose the finish
connection for a simple integration. 

.Procedure
. On the *Add to Integration* page, click the plus sign where you 
want to add the connection. Skip this step if {prodname} is
prompting you to choose the finish connection. 
. Click a Google Sheets connection that is authorized to access
the spreadsheet that you want to add the pivot table to.
. On the *Choose an action* page, select *Add pivot tables*.
. To configure the *Add pivot tables* action, in the 
*SpreadsheetId* field, do one of the following: 
+
* Enter the ID of the spreadsheet that you want to add a pivot table to. 
* Leave the field blank. In the data mapper step that you will add just
before this connection, you will map the spreadsheet ID from 
a previous connection to this connection. The previous connection must 
access the spreadsheet that you want to add a pivot table to in this connection.

. Click *Next* to add the connection to the flow.
The connection appears in the integration visualization 
where you added it.
. If you want to add any other connections to this flow, add them 
now. Then return to these instructions. 
. In the integration visualization, click the plus sign that is
just before the connection that adds a pivot table to a sheet.
. Click *Data Mapper*.
+
In the data mapper, map source properties, source fields, or source constants to the target 
Google Sheets connection, which is adding a pivot table. The following instructions
assume that there is a source field, source property, or source constant that contains the 
value that you want to map to each target field. 
+
If there is no source
for the required value, then create a source property
in the data mapper by clicking the + sign that is to the right of the 
*Properties* folder. In the *Create Property* dialog, give the property
a name that makes it easy to map to the correct target field, for example, 
specify *`valueLayout`* as the property name. 
Specify the value that you want the connection to use to create the 
pivot table. The pivot table's value layout field requires a value of `HORIZONTAL`
or `VERTICAL` so you might specify `VERTICAL` as the value of the 
*valueLayout* property. Save the new property. 

. In the data mapper, configure a new pivot table as follows: 

.. Map a source *spreadsheetId* field to the target *spreadsheetId* field. 
The connection uses source data in this spreadsheet to add the pivot 
table to a sheet in this same spreadsheet. The source data and the pivot 
table can be in two different sheets in the same spreadsheet but they
cannot be in two different spreadsheets. 
.. Identify the sheet to add the pivot table to by mapping an integer to the 
*sheetId* field. The default is `0`, which identifies the first sheet
in the spreadsheet. 
For subsequent sheets, the sheet ID is at the end of the URL when the 
sheet is visible in a browser. For example, at the end of the following URL, 
you can `gid=206589330`: 
+
`\https://docs.google.com/spreadsheets/d/1pa...ngQbKkM/edit#gid=206589330`. 
+
This indicates that the sheet ID is `206589330` and that is the value 
that you would map to *sheetId*. 

.. Identify the sheet that provides the data for generating the pivot table by mapping 
its integer sheet ID to the target *sourceSheetId* field.  
If you do not map a value to *sourceSheetId*, 
the connection uses the *sheetId* value, or `0` if no value is mapped
to *sheetId*. `0` identifies the first sheet in the spreadsheet.
.. Set the source data range for generating the pivot table 
by mapping an A1 notation value to the target *sourceRange* field. 
For example, *A2:D5*. 
.. Set the value layout to be used on the pivot table by mapping to the
target *valueLayout* field. The value must be `VERTICAL` or `HORIZONTAL`, which
is the default. `HORIZONTAL` specifies that values are laid out as columns. 
.. Set the top left cell of the pivot table by mapping an A1 notation
cell coordinate to the target *start* field. 
When the connection adds the pivot table to the sheet that contains 
the source data, if you do not map a value to the *start* field, 
the default is that the top left cell of the pivot table is the cell 
that is in the first row of the source data range and in the first 
empty column after the source data range. When the source data is 
in one sheet and the pivot table will be in another sheet, the default is *A1*. 

.. To add a value group, expand *valueGroups* 
in the target panel, and map values to the 
fields under *valueGroups*:
... Map a value group name to the target *name* field.
... Map a function, which you want to apply to the value group, to the target *function* field. 
The value must be `SUM`, `COUNT`, `AVERAGE`, `MAX`, `MIN`, or `CUSTOM`. 
The default is `SUM`. Map `CUSTOM` to *function* when you are defining a formula.
... Map a custom formula, which you want to apply to the value group, to the target 
*formula* field if you mapped the `CUSTOM` value to the *function* field. 
... Map a column name, which is the coordinate that builds the value group, 
to the target *sourceColumn* field.

.. To add a row pivot group, expand *columnGroups* 
in the target panel, and map values to the 
fields under *columnGroups*:
... Map a row pivot group name to the target *label* field.
... Map the sort order that you want to apply to the row pivot group to the
target *sortOrder* field. The value must be `ASCENDING` or `DESCENDING`.
The default is `ASCENDING`.
... Map `true` or `false` to the target *showTotals* field. The default 
is `true`, which enables the display of totals for the row pivot group. 
... Map a column name, which is the coordinate that builds the row pivot group,
to the target *sourceColumn* field.

.. To add a column pivot group, expand *rowGroups* 
in the target panel, and map values to the 
fields under *rowGroups*:
... Map a column pivot group name to the target *label* field.
... Map the sort order that you want to apply to the column pivot group to the
target *sortOrder* field. The value must be `ASCENDING` or `DESCENDING`.
The default is `ASCENDING`.
... Map `true` or `false` to the target *showTotals* field. The default 
is `true`, which enables the display of totals for the column pivot group. 
... Map a column name, which is the coordinate that builds the column pivot group,
to the target *sourceColumn* field. 

.. In the upper right, click *Done* to add the data mapper step.

.Additional resource
link:https://developers.google.com/sheets/api/guides/pivot-tables[Google developers documentation for pivot tables]
