Querying/Accessing/Retrieving Header row in Google Spreadsheet

1.7k Views Asked by At

I'm accessing a public Google Docs Spreadsheet using the Google Sheets API. The API says that when you query a list-feed for a worksheet, you get back a list of rows excluding the first row, which is the header row (by convention).

Is there a way to access the header row? I see that you can use the cells feed to request specific cells:

// Fetch column 4, and every row after row 1.
URL cellFeedUrl = new URI(worksheet.getCellFeedUrl().toString()
    + "?min-row=2&min-col=4&max-col=4").toURL();
CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);

Is there another way that is more explicit, to retrieve the header row?

2

There are 2 best solutions below

0
Vivin Paliath On BEST ANSWER

I searched long and hard, but it appears there is no semantically explicit way to grab headers out of a spreadsheet. As I mentioned in the question, you can use the cell feed so this is what I did:

URL cellFeedUrl = new URL(worksheet.getCellFeedUrl().toString() + "?min-row=1&max-row=1");
CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);
for(CellEntry cellEntry : cellFeed.getEntries()) {
    System.out.print(cellEntry.getCell().getValue() + ",");
}
System.out.println();

The important part is the ?min-row=1&max-row=1 part. This gives you all the cells in the first row of the sheet. By convention, the very first row in a worksheet is treated as the header.

1
eddyparkinson On

getTags() this might return an iterable with "name", "address", "manager", "employeeid"."

https://developers.google.com/gdata/javadoc/com/google/gdata/data/spreadsheet/CustomElementCollection#getTags()

Example - ListDemo.java

  public void printAndCacheEntry(ListEntry entry) {

    // We only care about the entry id, chop off the leftmost part.
    // I.E., this turns http://spreadsheets.google.com/..../cpzh6 into cpzh6.
    String id = entry.getId().substring(entry.getId().lastIndexOf('/') + 1);

    // Cache all displayed entries so that they can be updated later.
    entriesCached.put(id, entry);

    out.println("-- id: " + id + "  title: " + entry.getTitle().getPlainText());

    for (String tag : entry.getCustomElements().getTags()) {
      out.println("     <gsx:" + tag + ">"
          + entry.getCustomElements().getValue(tag) + "</gsx:" + tag + ">");
    }
  }

http://gdata-java-client.googlecode.com/svn-history/r497/trunk/java/sample/spreadsheet/list/ListDemo.java

I have never used this, so not 100% sure. But looks like what you want.