# Working with the Google Sheets connector

## Introduction

Having installed the Google Sheets connector and added required instances (using credentials obtained via the [Google Sheets APIs and Services portal](https://doc.wearepatchworks.com/product-documentation/connectors-and-instances/patchworks-connectors/google-sheets-prebuilt-connector)), you can add this connector to process flows, syncing data to/from your spreadsheets.

This page provides guidance and techniques for using the Google Sheets connector in process flows.

## Connector settings

Having added a connector shape to your process flow and selected a Google Sheets `source instance` and `endpoint`, you'll see that variables are available - for example:&#x20;

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FGlaNABAyXwRKnvG2c23F%2Fconnector%20settings%20-%20drawer%201.png?alt=media&#x26;token=74719d60-1be0-4b11-9878-766d7d233b15" alt="" width="333"><figcaption></figcaption></figure></div>

Specific variables will vary according to which source endpoint is selected but essentially, this is where you define [which spreadsheet to access](#finding-your-spreadsheet-id) and then additional details (for example, target cell range).

## Finding your spreadsheet id

Whichever endpoint you select, you'll always need to enter a value for a `spreadsheet id` variable. Every Google Sheets spreadsheet has a unique id, which you can find by opening the spreadsheet and checking the URL. Here you'll find a long string of characters after base URL - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FBQM8yGjwJwHIpAl2e5sQ%2Fconnector%20settings%20-%20spreadsheet%20id.png?alt=media&#x26;token=3b89d644-5d79-4032-a01a-785e343b86e6" alt=""><figcaption></figcaption></figure></div>

This string of characters (everything between the two `/` characters) is your `spreadsheet id`.

{% hint style="info" %}
More information is available in Google's documentation, [here](https://developers.google.com/sheets/api/guides/concepts).
{% endhint %}

## Example: Retrieving data

For this example, we have a simple product list in a spreadsheet:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FYNwlmGdeN3Y9Z1cVTDvP%2Fspreadsheet%20-%20plain%202.png?alt=media&#x26;token=5cd292fc-cfd4-4ee5-b890-87e6dcc91cf7" alt=""><figcaption></figcaption></figure>

In the steps below we'll walk through the process of sending data from columns **E**, **F**, **G**, **H**, and **I** into a destination database. &#x20;

### Prerequisites

* Ensure you've completed the [required setup in your Google Workspace to obtain OAuth 2.0 credentials](https://doc.wearepatchworks.com/product-documentation/connectors-and-instances/patchworks-connectors/google-sheets-prebuilt-connector).
* Ensure that the Google Sheets connector is [installed from the Patchworks marketplace](https://doc.wearepatchworks.com/product-documentation/connectors-and-instances/working-with-connectors/installing-a-connector).
* Ensure that [at least one instance has been created](https://doc.wearepatchworks.com/product-documentation/process-flows/cross-reference-lookups/adding-a-cross-reference-lookup) for this connector, using your OAuth 2.0 credentials for authentication.

### The steps

**Step 1**\
Create a new process flow and add a connector shape. Configure the&#x20;

<table><thead><tr><th width="157">Field</th><th>Setting</th><th>Notes</th></tr></thead><tbody><tr><td><code>Source instance</code></td><td>Select the Google Sheets connector instance that you want to use.</td><td>For information about obtaining OAuth 2.0 credentials required to create instances for the Google Sheets connector, see <a href="">Google Sheets (prebuilt connector)</a></td></tr><tr><td><code>Source endpoint</code></td><td>Select <code>GET Method</code></td><td>This endpoint retrieves data from a given spreadsheet.</td></tr><tr><td>Spreadsheet i<code>d</code></td><td>Enter the <code>id</code> associated with the Google Sheets spreadsheet that you want to access.</td><td>This id forms part of the URL for the spreadsheet. For more information please see <a href="#finding-your-spreadsheet-id">Finding your spreadsheet id</a>.</td></tr><tr><td><code>Range</code></td><td><p>Enter the range of data that you want to retrieve. If your spreadsheet includes multiple sheets, you should prefix the range with the sheet name.</p><p></p><p>For our example, we're targeting a specific sheet named <code>fruit</code> and we're interested in data from column <code>A</code> to column <code>I</code>, rows <code>2</code> to <code>13</code>. This is expressed as: <code>fruit!A2:I13</code>.</p></td><td><p>If your spreadsheet is comprised of a single sheet, you don't need to specify the sheet name - any range given is taken from the first visible sheet.</p><p></p><p>For detailed information about range notation see Google's documentation, <a href="https://developers.google.com/sheets/api/guides/concepts">here</a>.</p></td></tr></tbody></table>

For example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FL03VGAjkoH0ijFfLofU5%2Fconnector%20settings.png?alt=media&#x26;token=bdb50ff5-57d3-4f5f-8546-8360936c902b" alt="" width="334"><figcaption></figcaption></figure></div>

**Step 2**\
If we run the flow now and check `payload` information for the connector step, we can see data from the spreadsheet:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FEZVShHKRq5aG6SorC5Le%2Frun%20flow%201.png?alt=media&#x26;token=5a0127a0-b3f6-447c-869c-fcd18e4fda6e" alt=""><figcaption></figcaption></figure></div>

The first thing to note is that the `majorDimension` is set to `ROWS`:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FH1K4454ShPBqFfkzdYOt%2Flogs%20-%20dimension.png?alt=media&#x26;token=33d9cc18-2adc-431d-820c-a4eb63dfb64a" alt="" width="563"><figcaption></figcaption></figure></div>

`majorDimension` is a Google Sheets API parameter that determines how values are structured - it might be set to `ROWS` or `COLUMNS` (`ROWS` is the default if nothing is defined). In Patchworks, this value is set in the [schema](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/4-endpoints/endpoint-options/schema-taxonomy) for each Google Sheets endpoint.

{% hint style="info" %}
You can read more about this field in Google's documentation, [here](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values)
{% endhint %}

Taking our example, we can see that each row in our sheet is output as an array containing items for each column. Let's look at this structure in a bit more detail.&#x20;

In our spreadsheet, columns are organised from `A` to `I`:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FDGUQGs7akCGoPn7skI7c%2Fspreadsheet%20-%20single%20item%20b.png?alt=media&#x26;token=f08d1a2d-66c3-4fa4-acae-17488157290a" alt=""><figcaption></figcaption></figure></div>

But in our payloads, these column labels aren't shown:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FHiY3vEb2sG47wnbcdWHF%2Fpayload%20-%20plain.png?alt=media&#x26;token=3ca17145-5896-43d8-a78c-f8a51d13274b" alt="" width="563"><figcaption></figcaption></figure></div>

To target columns or rows, we use numbers. For rows this is straightforward - by default, every row in a sheet has a unique number. But for columns we must translate existing alpha labels to numbers, starting at `0`. So our columns `A` to `I` can be referenced as `0` to `8`:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FxIBv9EcOFJvlLpzaC8Lu%2Flogs%20-%20single%20entry%20b.png?alt=media&#x26;token=4708a4b5-5667-4a06-a8b4-1b0044ea66c5" alt="" width="563"><figcaption></figcaption></figure></div>

You'll see how this works in subsequent steps.

**Step 3**\
Now we've connected to our Google Sheet and pulled a single payload that contains all rows. To send this data to our destination system, we need to break that payload into smaller batches, with one record (i.e. row) per payload. To achieve this, we add and configure a [flow control shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/flow-control-shape):

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FWKjdmJFIfDVbcA7ThjyY%2Fflow%20control%201.png?alt=media&#x26;token=d857a6ca-8581-4539-aed5-3f00a6adc93a" alt="" width="334"><figcaption></figcaption></figure></div>

Here, we're saying that we want to target records within the `values` array and create batches of `1` - i.e. create one payload for every row. We also toggle the `Do not wrap single items in an array` option ON, because our destination system requires this.

Now if we run the flow and check the output, we have multiple payloads:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FA0O1tJ2zilXyRiDOGL4P%2Fpayloads%20a.png?alt=media&#x26;token=2b470e0f-777d-43bf-82b9-edd298e2cbc6" alt=""><figcaption></figcaption></figure></div>

...each containing data for a single row:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fjkz1udTvZmdDu1Cklh2l%2Fpayloads%20b.png?alt=media&#x26;token=e63bc42b-4488-47bd-a857-0db71937d25b" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 4**\
Next, we add a connector for our destination system:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FfX6RnMGTOUCpPGROUiVu%2Fdestination%20connector.png?alt=media&#x26;token=fd9fe0b9-7819-4c2f-81c3-30d8bcb3b658" alt=""><figcaption></figcaption></figure></div>

**Step 5**\
Finally, we add a [map shape](https://doc.wearepatchworks.com/product-documentation/developer-hub/patchworks-core-api/core-api-spotlights/initialising-a-process-flow-and-sending-data-via-the-patchworks-api/the-steps/mapping-payload-data) and define field mappings to determine where our column values are placed in the destination system - this is where our numbering system for column labels is needed:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FTfwVd7N7suAThrOjJmmZ%2Fmap%201a.png?alt=media&#x26;token=59f6253c-d9ce-4430-a678-b6ccc01f6f6b" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 6**\
At the end of the mappings page, we toggle the `wrap input payload` option to ON, so our data is in the required format for our destination system.&#x20;

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FtTiUk0SzsXDqMCUpEUaC%2Fmap%202.png?alt=media&#x26;token=b279a3f3-32ef-4bb4-bd3c-42cd8772b373" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 7**\
Having saved field mappings we can run the flow and check resulting payloads for the map step - here we see that our spreadsheet columns are successfully mapped to destination fields.

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FqxLve38DywPSQ5NopHdl%2Fmap%203.png?alt=media&#x26;token=22131048-4a57-42d5-920e-a59792d1e426" alt=""><figcaption></figcaption></figure></div>

**Step 8**\
If we check our destination database, all spreadsheet rows have been added:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FBbtCazmRXWQeuxuXiCEQ%2Fdestination.png?alt=media&#x26;token=e77019e8-1412-4d5d-9406-0fd09c3e048d" alt=""><figcaption></figcaption></figure>

## Useful information

* [Google Sheets API overview](https://developers.google.com/sheets/api/guides/concepts)&#x20;
