This project automates the process of appending data to a specific Google Sheet using a Google Cloud Function. It provides a webhook where you can send any JSON record as POST request to and it will be added as a new row in the Sheet. The mapping will be defined in a separate tab in the Sheet. It leverages the Google Sheets API to append new data sent via HTTP requests to a predefined spreadsheet. This solution is ideal for integrating various data sources into a centralized Google Sheet for reporting, analysis, or aggregation purposes.
Before deploying this project, ensure you have the following prerequisites:
- Google Cloud Platform (GCP) Account: You need a GCP account with billing enabled.
- Terraform: Terraform is used for deploying infrastructure as code. Install Terraform by following the official installation guide.
- Google Cloud SDK: The Google Cloud SDK is required for managing resources within your GCP account. Install it from the Google Cloud SDK documentation.
To configure the project for your environment, you need to create a terraform.tfvars file from the provided terraform.tfvars.example. Replace the example values with your specific project details:
project_id = "yourProjectID"
region = "yourPreferredRegion"
function_name = "yourFunctionName"
spread_sheet_url= "https://docs.google.com/spreadsheets/d/yourSpreadsheetID"Note, you can simply copy the Sheet url from the browser.
Follow these steps to deploy the project:
- Initialize Terraform: Navigate to the project directory and run
terraform initto initialize the Terraform project. - Apply Terraform Configuration: Deploy the infrastructure by executing
terraform apply. Confirm the action by typingyeswhen prompted. - Note the Output: After successful deployment, Terraform displays outputs, including the service account email. You'll need this to set permissions on your Google Sheet.
To use this automation, prepare your Google Sheet with the following tabs and column names. Keep exact capitalization on tab and column names:
- 'data' Tab: This is where the data will be appended. Define the column names based on the data you expect to receive.
- 'mapping' Tab: Use this tab to map JSON keys from the incoming requests to your column names in the Data tab. It should have two columns:
json_keyandcolumn_header. Add here for each key in the json of the request the column name that you would like to value to be added. Note, you don't have to add all json keys nor all column names if you don't want to fill them.
Share the Google Sheet with the service account email provided by Terraform, granting it Editor access.