Skip to content

add google sheet workflow doc #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions docs/workflows/google-sheets-with-code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Create a Google Sheets remote data block

This page will walk you through connecting a [Google Sheets](https://workspace.google.com/products/sheets/) data source. The remote block registration to display sheet records and the styling of that block is similar to the [Airtable workflow](./airtable-with-code.md). If you have not yet installed and activated the Remote Data Blocks plugin, visit [Getting Started](https://remotedatablocks.com/getting-started/).

## Google Sheets API Access

The Google Sheets API access is required for connecting to Google Sheets. The plugin uses a [service account](https://cloud.google.com/iam/docs/service-account-overview?hl=en) to authenticate requests to the Google Sheets API. The following steps are required to set up the Google Sheets API access:

- [Create a project](https://developers.google.com/workspace/guides/create-project) in Google Cloud Platform. `resourcemanager.projects.create` permission is needed to create a new project. Skip this step if you already have a project setup in your organization in Google Cloud Platform which can be used.
- Enable the Google [Sheets API](https://console.cloud.google.com/apis/library/sheets.googleapis.com) and [Drive API](https://console.cloud.google.com/apis/library/drive.googleapis.com)(required for listing spreadsheets) for your project.
- [Create a service account](https://cloud.google.com/iam/docs/service-accounts-create) which will be used to authenticate the requests to the Google Sheets API. Note down the service account email address.
- [Create a key](https://cloud.google.com/iam/docs/keys-create-delete) for the service account. This will download a JSON key file. Keep this file safe as it will be used to authenticate the block.
- Grant access of the service account email to the Google Sheet after which the service account can be used to authenticate the requests to the Google Sheets API for the given sheet.

The Service Account Keys JSON should be provided to your application securely. On WordPress VIP, we recommend using [environment variables](https://docs.wpvip.com/infrastructure/environments/manage-environment-variables/) to provide this token. The code in this example assumes that the Service Account Keys JSON has been Base64 encoded and provided securely via a constant.

## Setting up the Google Sheet

- Identify the Google Sheet that you want to connect to. If you have not created a Google Sheet yet, create one.
- Note down the Google Sheet ID from the URL. For example, in the URL `https://docs.google.com/spreadsheets/d/test_spreadsheet_id/edit?gid=0#gid=0`, the Google Sheet ID is `test_spreadsheet_id`. The Google Sheet ID is the unique identifier for the Google Sheet.
- Share the Google Sheet with the service account email address.

## Block Registration and Styling

This would be similar to the [Airtable workflow](airtable-with-code.md). Refer the following sections from that workflow -

- [Define a query](./airtable-with-code.md#define-a-query)
- [Register a block](./airtable-with-code.md#register-a-block)
- [Insert the block](./airtable-with-code.md#insert-the-block)
- [Register a list query](./airtable-with-code.md#register-a-list-query)
- [Custom patterns and styling](./airtable-with-code.md#custom-patterns-and-styling)

## Code Reference

Check out [a working example](https://github.com/Automattic/remote-data-blocks/tree/trunk/example/google-sheets/westeros-houses) of the concepts above in the Remote Data Blocks GitHub repository and feel free to open an issue if you run into any difficulty when registering or customizing your remote data blocks.

### Westeros Houses Example Setup

Follow following setup steps to get the Westeros Houses example working:

- [Configure the Google Sheet API Access](./google-sheets-with-code.md#google-sheets-api-access) and [Create a new Google Sheet](./google-sheets-with-code.md#setting-up-the-google-sheet) by following the steps above.
- Add sheet named `Houses` inside the newly created Google Sheet with columns with headers as
- House
- Seat
- Region
- Words
- Sigil (image url)
- Add some data to the sheet.
Comment on lines +42 to +48
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be nice to have screenshot for these steps 🙂

- Base64 encode the JSON key file and set it so that its available via `REMOTE_DATA_BLOCKS_EXAMPLE_GOOGLE_SHEETS_WESTEROS_HOUSES_ACCESS_TOKEN` constant.

Now the blocks with name `Westeros House` and `Westeros Houses List` should be available in the editor.