Pages

Tuesday, May 25, 2010

Importing data from Google Spreadsheets as List based Feed

The Google Spreadsheets Data API allows client applications to view and update Spreadsheets content in the form of Google Data API feeds. Your client application can request a list of a user's spreadsheets, edit or delete content in an existing Spreadsheets worksheet, and query the content in an existing Spreadsheets worksheet.
A given worksheet generally contains multiple rows, each containing multiple cells. You can request data from the worksheet as a list-based feed, in which each entry represents a row. The list feed makes some assumptions about how the data is laid out in the spreadsheet.
In particular, the list feed treats the first row of the worksheet as a header row; Spreadsheets dynamically creates XML elements named after the contents of header-row cells. Users who want to provide Data API feeds should not put any data other than column headers in the first row of a worksheet.
The list feed contains all rows after the first row up to the first blank row. The first blank row terminates the data set. If expected data isn't appearing in a feed, check the worksheet manually to see whether there's an unexpected blank row in the middle of the data. In particular, if the second row of the spreadsheet is blank, then the list feed will contain no data.
http://code.google.com/apis/spreadsheets/data/3.0/developers_guide_java.html

Classes to be imported from gdata library java/lib/gdata-spreadsheet-1.0.jar and java/lib/gdataclient-1.0.jar jar files. And don't forget to include the jar file from the deps folder.


http://code.google.com/apis/gdata/javadoc/com/google/gdata/data/spreadsheet/Column.html

Additional Link: http://wiki.developerforce.com/index.php/Google_Spreadsheets_API

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");
URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
// Obtaining the SpreadSheet Feede
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.getTitle().getPlainText());
// Looping the no. of times, no. of columns present  
 for (String tag : listEntry.getCustomElements().getTags()) {
   System.out.println("  " + listEntry.getCustomElements().getValue(tag) + "");
 }
}

}
}










No comments:

Post a Comment