Ok, this one has become a little tricky for me and I really need some assistance to work through it.
Problem
I have a GSpreadsheet which has a list of data, in this case Twitter usernames. Using the API of a service provider (in this case the Klout API), I would like to retrieve information about that user to populate a cell within a spreadsheet.
Based on what I can work out so far, I would need to write a custom function to do this but I have no idea where to start, how I might construct it, or if there are any examples of doing this.
Scenario
The Klout API can return either an XML or JSON response (see http://developer.klout.com/docs/read/api/API), based on the string passed. For example, the URL:
http://api.klout.com/1/users/show.xml?key=SECRET&users=thewinchesterau
would return the following XML response:
<users> <user> <twitter_id>17439480</twitter_id> <twitter_screen_name>thewinchesterau</twitter_screen_name> <score> <kscore>56.63</kscore> <slope>0</slope> <description>creates content that is spread throughout their network and drives discussions.</description> <kclass_id>10</kclass_id> <kclass>Socializer</kclass> <kclass_description>You are the hub of social scenes and people count on you to find out what's happening. You are quick to connect people and readily share your social savvy. Your followers appreciate your network and generosity.</kclass_description> <kscore_description>thewinchesterau has a low level ofinfluence.</kscore_description> <network_score>58.06</network_score> <amplification_score>29.16</amplification_score> <true_reach>90</true_reach> <delta_1day>0.3</delta_1day> <delta_5day>0.5</delta_5day> </score> </user> </users>
Based on this response, I would like to be able to populate different cells with the values returned within the XML (or JSON if easier) packet.
So, for example, I would have a spreadsheet like the following which would have custom functions to go out and retrieve the value of the relevant XML element response to populate the cell:
Cell A B C D E 1 Username kscore Network score Amplification score True reach 2 thewinchester =kscore(A2) =nscore(A2) =ascore(A2) =tscore(A2)
Questions
- Are there any gSpreadsheet examples you know of that use an API to pull data in from an external source?
- How would one write a custom function to fetch the result from the API and populate a cell with a result of a specific element?
Any information, examples or helpers you have are greatly appreciated.
You want the
importXMLfunction, documented here. The formula you want will look something like this: