Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use Google Spreadsheet as a backend database

I am trying to develop an application, where I where fetching data from multiple clients related to some transactions. Now I want to make some analysis using the Google Spreadsheet. Is there any way by which I can achieve this using the ASP.Net MVC(using C#). Google provides OAuth and When I implement the code and when I am trying to send the data to Google spreadsheet which I receive form the clients. However, when I am doing that, for every new client, Google asks for login credential. Client enter their own credentials, and the Google sheet instead a common sheet, client own sheet is used. Thus, my purpose is not solved. Is there any way possible to do this. I does not seem to find proper documentation on Google as well. Please provide some suggestions to implement this. Thanks in advance.

like image 538
Jagdeep Chawla Avatar asked Jan 22 '16 04:01

Jagdeep Chawla


2 Answers

This can be done very easily if you just want a read only database. You must publish your Google Sheet to the web and, from its url, copy its id.

For example my sheet has this url: https://docs.google.com/spreadsheets/d/1IHF0mSHs1HdYpIlIzYKG3O8SnAhKU_a6nEJSz04Togk/edit

The long alphanumeric string in the middle is my sheet id. Copy it and place it instead of XXXX in the following url, as follows:

https://spreadsheets.google.com/feeds/list/XXXX/1/public/basic?alt=json"

So the final url would look like this: https://spreadsheets.google.com/feeds/list/1IHF0mSHs1HdYpIlIzYKG3O8SnAhKU_a6nEJSz04Togk/1/public/basic?alt=json

Then you can simply access this url and get all your data as json. Using jQuery:

var $url = 'https://spreadsheets.google.com/feeds/list/1IHF0mSHs1HdYpIlIzYKG3O8SnAhKU_a6nEJSz04Togk/1/public/basic?alt=json';
$.getJSON($url,function(data){
    alert(JSON.stringify(data.feed.entry));
});

You will get a long json structure. The relevant data is in data.feed.entry. There you'll have many entries. On each one you'll have a "content" property and within it, a "$t" one. These will give you all the cells. So for getting the first row, you will have to get data.feed.entry[0].content.$t.

Hope it helps.

like image 103
user876508 Avatar answered Sep 28 '22 11:09

user876508


This can be accomplished using Google Apps Script. In particular, you can achieve this with a "bound" script in Google Sheets (i.e. a script that was created in the context of the sheet that you wish to create as the "backend") that you then publish as a "web app" script. When you publish it, you can make it execute with the authority of the owner of the spread sheet (rather than the authority of the user who invokes the url), which will not require the end user to explicitly authorize themselves (since it is the script publisher's credentials, not the user's credentials, that are being used).

It should be noted that, while this (and generally building on top of Google Apps Script) is a reasonable approach for small-to-medium apps, you'll probably find using Google Cloud Platform (and, in this particular case, the Cloud Datastore) as the better, more scalable solution for small-to-large apps. That is, if you are prototyping or creating an internal tool that is unlikely to catch fire overnight, I'd go with whichever approach you find more convenient / simpler; if you are creating an app that could potentially experience a "success disaster", I'd go with Cloud Platform, instead.

like image 24
Michael Aaron Safyan Avatar answered Sep 28 '22 09:09

Michael Aaron Safyan