Reputation: 847
I am using a list based feed to access my spreadsheet in google docs. How can I get a list of the column names/ columnHeaders?
I figured out the answer to this myself, but since I couldn't find the answer on StackOverflow, or in the java doc for the google api, I thought would contribute my solution.
Upvotes: 3
Views: 4912
Reputation: 1407
You could use the CellFeed and iterate through all cells with a row value of 1.
However, you'll need to strip out all characters that aren't alphanumeric, decimals or dashes, and also strip out any leading digits too, which can be done with a regex and the String.replaceAll()
method.
URL cellFeedUrl = worksheet.getCellFeedUrl();
CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);
String[] columnNames = new String[worksheet.getColCount()];
for (CellEntry entry : cellFeed.getEntries())
if (entry.cell.getRow() == 1)
{
String val = entry.cell.getValue().toLowerCase();
String parsed = val.replaceAll('^[0-9]*|[^a-z0-9\-\.]', '');
columnNames[entry.cell.getCol()] = parsed;
}
Upvotes: 0
Reputation: 847
I assume you know how to access the worksheet to begin with, so you are responsible for writing the following two functions.
SpreadsheetService service = getspreadsheetService();
WorksheetEntry worksheet = getGoogleWorksheet();
Here is how you get your list of columnHeaders:
URL url = worksheet.getListFeedUrl();
ListFeed rows = service.getFeed(url, ListFeed.class);
ListEntry row = rows.getEntries().get(0);
Set<String> columnHeadings = row.getCustomElements().getTags();
Upvotes: 2