Data sync from DB to Gsheet— part 1

Siva Gollapalli
2 min readNov 27, 2018

Datastore is an important component in every software. The data store could be Gsheets or database. Many legacy software run Gsheets as their primary data store to maintain their data. But the problem with the Gsheet is that it is not a scalable solution. Though you want to migrate your data store from Gsheet to database it can’t be done in a single day. For some days you have to use both Gsheets and database to run your business seamlessly. Then how you maintain data sync between Gsheets and database? In this post let’s solve this problem step by step:

Step-1 : Registering a service account with Google

Whenever we communicate with Google, we have to register a service account which will authenticate & authorize our API call before accessing any Google service like Gsheet. Here is the link to register a service account.

Step - 2: Adding data to Gsheet

Before proceeding, we need to add google-api-client gem to Gemfile .

Step-3: Generic service for talking to Gsheet

The above code snippet takes respective changes on a given object and appends/update the data in a Gsheet. We don’t need to specify a row number if you want to add data to Gsheet. But if you want to update then we need to specify range ie., users!A1:A10 which will update the Gsheet called users from range A1 to A10 .

Note: While dealing with Gsheet, column numbers are very important since Gsheet just overrides the data.

Step-4: Add/Update data in Gsheet

The above code snippet will appends data to Gsheet if data doesn’t exists otherwise it just updates data in Gsheet. Here is the video demo.

Video demo for data sync between rails DB and Gsheet

I hope you have learned about syncing between rails and gsheet. In next part we will discuss about how to sync from gsheet to a rails app.

Any feedback would be welcome and encouraging!!!

--

--