Google sheets as backend datastore
Introduction
Unlock the secret weapon for your next app: Google Sheets! Discover how to transform this familiar tool into a powerful, cost-free backend. With this guide, you'll breeze through tedious authentication google process and crafting a script that effortlessly taps into your Sheets' data. It's innovation without the price tag!
This blog will be less words and more picture to clearly guide the users.
Without further a do, let's look at the outline for the blog.
Outline
We will go through the following steps.
Enable the Google Sheets API in Google Cloud Console.
Create Service Account
Create Service Account key
Create Google Sheet
Connect Service account with Google sheet
Get the spreadsheet id
Code
Enable Google Sheets API
To be able to access Google sheets via API, you will first need to enable it in the Google Cloud Console.
Google Cloud Console it like you window to programmatically interacting with different Google products.
Click on this link. A screen like this will open up.
Please make sure you are on the intended Google account before continuing.
Go ahead and click on "Create Project".
- Next Provide a name for your project "myproject"
Click "Create" button
Make sure you have selected the correct project by clicking on the drop down on the top
Click out after verifying the correct project.
Click "Next" button to select the project
- Click "Enable" button to Enable API on the following page.
All right your Google sheets API is now enabled.
Next we will create a service account.The service account will help us to enable connection from our Golang App to Google sheet.
Create a service account
Click on the hamburger menu on the top left of the screen
Select "IAM & Admin" from the drop down
Select "Service Accounts" from second drop down.
- Click on "Create Service Account" on the next page.
Provide a name for your service account as "myserviceaccount".
Click "Create and Continue" button.
- Click on "Select a role" dropdown
- Select "Owner" from the drop down.
- Click on "Continue" after selecting the role
- Finally click on "Done" to create your service account.
Your service account is now created.
Copy the service account email address and keep it safe. We will need it shortly.
Create Service Account key
Next we need to create a key for our service account. This key will serve as our secret credentials file to authenticate into Google to access Sheets.
Open you service accounts page once again and click on your newly created service account
Click on the "Keys" tab on the top
Click on the "Add Key" drop down.
Select "Create new key".
Select JSON from the pop up
Click "Create" button. This will download the json key to your local system.
Be sure to keep this json file safe and not check this in git.
You are now done creating a service account.
You should how have two artifacts from previous steps.
Service account key in for of JSON file.
Service account email address.
Create Google Sheet
I am skipping screenshots to create a google sheet.
Please make sure your Google sheet looks like so.
Name is "myspreadsheet"
Top two rows are populated as shown.
Connect Service account with Google sheet
Go ahead and hit the Share button on the top left corner of the screen on your google sheet.
Paste the service account email to text bot of share pop up
Grant Editor access.
Your Share pop should look something like this with your email and service account email.
Now your service account has access to this google sheet.
Get Sheet id
Click on the "Copy link" button from your Share pop.
The share link should look something like this https://docs.google.com/spreadsheets/d/1G5XaW7qJSDFfU1V7emy9VFrOe5i1UacDo5YxSXps2J2PY/edit?usp=sharing
The bold section in your url will be your sheet id.
Copy the sheet id and keep it safe.
Next we will work on a small golang code base to read data from this Google sheet.
Code
Get the code base
You can clone the git repo
git clone
https://github.com/guptaachin/sheets-api-example.git
You can also download the code zip file from here
cd into the code base
cd sheets-api-example
Create a file called
credentials.json
and copy your service account key in it.run code -
./run.sh <SheetID>
% ./run.sh 1G5XaW7qJSDFfU1V7emy9VFrOe5i1UacDo5YxSXps2J2PY
Starting connection to Google Sheets.
Reading data from Sheet
[A1 B1]
[A2 B2]
That's it you have now completed Google setup to start using Google Sheets as your backend.
References
Subscribe to my newsletter
Read articles from Achin Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by