开发者

Use custom function to populate gSpreadsheet cell based on a XML/JSON response

开发者 https://www.devze.com 2023-02-25 05:13 出处:网络
Ok, this one has become a little tricky for me and I really need some assistance to work through it. Problem

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>
    &开发者_运维知识库nbsp;       <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

  1. Are there any gSpreadsheet examples you know of that use an API to pull data in from an external source?
  2. 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 importXML function, documented here. The formula you want will look something like this:

=importXML("http://api.klout.com/1/users/show.xml?key=SECRET&users=" + A1, "//users/user/score/kscore")


You could write a custom script with Google AppScript, but there's a simple solution to this similar to what Nick Johnson posted. I've tested this against the score function, but it could be easily adapted to the show endpoint with different XPath.

=importXML("http://api.klout.com/1/klout.xml?users="&A1&"&key=YOUR_API_KEY", "//users/user/kscore")

This presumes your Twitter IDs are in the A column.

Note, Google Docs limits the number of such importXML functions to 50 per spreadsheet. You could concatenate groups of 5 userids for each importXML call, effectively putting your limit to 250 a sheet.

This could also be adapted to a similar call in Excel that doesn't have that limit. Keep in mind the Klout ToS, though, using proper attribution and rate limits.

0

精彩评论

暂无评论...
验证码 换一张
取 消