Programming with web API's is a new thing to me, and this is the first web api I have used. I have encounter a number of troubles before getting things to work and that is why I am writing this post as an additional to their guide especially for beginners like me.
I am using python to work with the api, but this could be applicable to other languages since most of the problems I am dealing with are XML related I guess. For this little & simple application that I am trying to create the operations I perform are:
- Connecting to Google Spreadsheet Service
- Opening the spreadsheet and worksheet you wish to work with
- Writing into the spreadsheet
- Reading spreadsheet
Connecting to Google Spreadsheet service is easy, here's the code:
Opening the speadsheet and worksheet
As of now, the api doesn't allow us to create a spreadsheet, we can only work on the existing spreadsheet. The first thing to do is to get the key which will tell the api what spreadsheet you want to work with. Currently I am getting the spreadsheet key by opening the document itself and you could get from the url.
You can also code your way to get your spreadsheet's key like this.
Now we need to know which worksheet we want to work with, the only way I know to get the worksheet key is to code it. We select one of the spreadsheet key from the above
Now we know the spreadsheet key and worksheet key, we can now start reading or writing.
Writing into the spreadsheet
Currently, the API provide 2 ways of writing by row and by cell. There are things you need to do and know when writing by rows. The api treats the first row as the column header, so before you write by rows you need to set the column header manually.
We need first define a dictionary first, before we can write by rows.
The keys of the dictionary will tell to which column we will write the data. As you can see spaces, capital letters, and special character are disregarded. For example, if your column header are $ Money Market and Php Money Market the keys of dictionary are moneymarket and phpmoneymarket respectively.
This where I spend I lot of making things work, underneath the api deals with a lot of XML/ATOM/RSS as I don't have any idea about it.
To check if the insert row is success, you could do something like this
Writing by cell will be very useful in setting up column header.
Same with writing into spreadsheet, you could read the spreadsheet by rows or cells. An example of reading all the rows of spreadsheet
You could search or filter rows using the gdata.spreadhseet.service.ListQuery like this
Just like writing into spreadsheet by rows, you need to provide the correct key to match the column head.
With this I was able to retrieve some data from the internet and store them in google spreadsheet for my data analysis, and I am looking for more ways of using it.