Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add header via Google Spreadsheet API

I am unable to insert a header (or any regular data) into a newly created worksheet via Java API. The standard way to insert data is to provide a header and value e.g

newEntry.getCustomElements().setValueLocal(header, value2);

This question similar to this one posted on stackoverflow:

How to add header in Google spreadsheet

The suggested answer was to try use cell based feed as opposed to list based in example above. I have been unable to do this as any query on cells or attempts at getting the cell feed seem to return 0 as there is no data in any cell to return...at least this is what I think is happening...

e.g.

    public void getCells(){
         cellFeedUrl = worksheetEntry.getCellFeedUrl();
        CellFeed feed;
        try {
            feed = spreadSheetService.getFeed(cellFeedUrl, CellFeed.class);

        for (CellEntry cell : feed.getEntries()) {
          System.out.println(worksheetEntry.getTitle().getPlainText());
          String shortId = cell.getId().substring(cell.getId().lastIndexOf('/') + 1);
          System.out.println(" -- Cell(" + shortId + "/" + cell.getTitle().getPlainText()
              + ") formula(" + cell.getCell().getInputValue() + ") numeric("
              + cell.getCell().getNumericValue() + ") value("
              + cell.getCell().getValue() + ")");
        }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

and

public void queryHeader(){

    CellQuery query = new CellQuery(cellFeedUrl);
    query.setMinimumRow(1);
    query.setMaximumRow(1);
    query.setMinimumCol(1);
    query.setMaximumCol(1);
    try {
        CellFeed feed = spreadSheetService.query(query, CellFeed.class);

        List<CellEntry> entries = feed.getEntries();

        for(CellEntry cell : entries){

            cell.changeInputValueLocal("new");
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ServiceException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
like image 329
user932456 Avatar asked Sep 07 '11 10:09

user932456


1 Answers

Your suspicion about the empty cells not being return is correct. You can override this default setting by adding the following line:

query.setReturnEmpty(true);

like image 171
G__ Avatar answered Sep 29 '22 20:09

G__