Pages

Tuesday, May 25, 2010

Importing data from Google Spreadsheets from Particular columns

In the Previous Post, http://aintmeekcoder.blogspot.com/2010/05/importing-data-from-google-spreadsheets.html which gives details of retrieving data as lists from Google spreadsheets make the following changes to retrieve data only for particular fields say mobile.


import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.URL;
import java.util.List;




public class Interact 
{
public static void main(String args[]) throws IOException, ServiceException
{
SpreadsheetService myService = new SpreadsheetService("exampleCo-exampleApp-1");
myService.setUserCredentials("username", "password");
// SpreadSheets Feed URL
URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
// Obtaining the SpreadSheet Feed
SpreadsheetFeed feed = myService.getFeed(metafeedUrl, SpreadsheetFeed.class);
List spreadsheets = feed.getEntries();
// Obtaining that particular SpreadSheet...
SpreadsheetEntry  entry = spreadsheets.get(10);
/* First obtain the list feed URL from a WorksheetEntry and request 
this feed from our authenticated SpreadsheetService object. 
The SpreadsheetServicereturns a ListFeed, which contains a list 
of all the rows in this worksheet. Each row is represented as 
a ListEntry object.*/


// Create a list of all the Worksheets
List worksheets = entry.getWorksheets();
// Retreive the first Worksheet
WorksheetEntry worksheet = worksheets.get(0);

// Retrieve List Feed URL
URL listFeedUrl = worksheet.getListFeedUrl();
// Get Feed from the ListFeedURL
ListFeed listfeed = myService.getFeed(listFeedUrl, ListFeed.class);

// Examine Each List Entry from the ListFeed obtained
for (ListEntry listEntry : listfeed.getEntries()) {
    System.out.println(listEntry.getCustomElements().getValue("mobile"));
}

}
}

No comments:

Post a Comment