Google sheets as backend datastore

Google sheets as backend datastore

Talk to Google Sheets from Golang script

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.

  1. Enable the Google Sheets API in Google Cloud Console.

  2. Create Service Account

  3. Create Service Account key

  4. Create Google Sheet

  5. Connect Service account with Google sheet

  6. Get the spreadsheet id

  7. 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.

  1. Click on this link. A screen like this will open up.

  2. Please make sure you are on the intended Google account before continuing.

  3. Go ahead and click on "Create Project".

  1. Next Provide a name for your project "myproject"

  1. Click "Create" button

  2. Make sure you have selected the correct project by clicking on the drop down on the top

  1. Click out after verifying the correct project.

  2. Click "Next" button to select the project

  1. 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

  1. Click on the hamburger menu on the top left of the screen

  2. Select "IAM & Admin" from the drop down

  3. Select "Service Accounts" from second drop down.

  1. Click on "Create Service Account" on the next page.

  1. Provide a name for your service account as "myserviceaccount".

  2. Click "Create and Continue" button.

  1. Click on "Select a role" dropdown

  1. Select "Owner" from the drop down.

  1. Click on "Continue" after selecting the role

  1. Finally click on "Done" to create your service account.

  1. Your service account is now created.

  2. 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.

  1. Open you service accounts page once again and click on your newly created service account

  2. Click on the "Keys" tab on the top

  3. Click on the "Add Key" drop down.

  4. Select "Create new key".

  1. Select JSON from the pop up

  2. Click "Create" button. This will download the json key to your local system.

  3. 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.

  1. Service account key in for of JSON file.

  2. 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.

  1. Name is "myspreadsheet"

  2. Top two rows are populated as shown.

Connect Service account with Google sheet

  1. Go ahead and hit the Share button on the top left corner of the screen on your google sheet.

  2. Paste the service account email to text bot of share pop up

  3. Grant Editor access.

  4. 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

  1. Click on the "Copy link" button from your Share pop.

  2. The share link should look something like this docs.google.com/spreadsheets/d1G5XaW7qJSDFfU1V7emy9VFrOe5i1UacDo5YxSXps2J2PY/edit?usp=sharing

  3. The bold section in your url will be your sheet id.

  4. 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

  1. Get the code base

    1. You can clone the git repo
      git clonehttps://github.com/guptaachin/sheets-api-example.git

    2. You can also download the code zip file from here

  2. cd into the code base cd sheets-api-example

  3. Create a file called credentials.json and copy your service account key in it.

  4. 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