Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create the first line in a new Google Spreadsheet using the API?

This is how I create the spreadsheet:

DocsService client= new DocsService ("idea");
client.useSsl ();
client.setOAuthCredentials (oauthParameters, new OAuthHmacSha1Signer ());

DocumentListEntry newEntry= new com.google.gdata.data.docs.SpreadsheetEntry ();
newEntry.setTitle (new PlainTextConstruct ("GIdeaDB"));
DocumentListEntry insertedEntry= client.insert (new URL (
  "https://docs.google.com/feeds/default/private/full/?xoauth_requestor_id="+ userEmail), newEntry);

Now I want to write the first line in it.

But unfortunately all API calls seam to base on the fact, that there already is a first line, for you insert name-value-pairs (where the name is the headline I want to create). http://code.google.com/apis/spreadsheets/data/3.0/developers_guide.html#CreatingTableRecords

Any ideas how I can create the first line? The one which defines the field names.

like image 648
JochenJung Avatar asked Dec 03 '10 18:12

JochenJung


People also ask

How do I get the last row in Google Sheets API?

You can set the range to "A2:D" and this would fetch as far as the last data row in your sheet.


2 Answers

Finaly found it. You have to do it cell by cell:

  oauthParameters= new GoogleOAuthParameters ();
  oauthParameters.setOAuthConsumerKey (CONSUMER_KEY);
  oauthParameters.setOAuthConsumerSecret (CONSUMER_SECRET);
  oauthParameters.setOAuthType (OAuthType.TWO_LEGGED_OAUTH);
  oauthParameters.setScope ("https://spreadsheets.google.com/feeds/");

  SpreadsheetService spreadsheetService= new SpreadsheetService ("appname");
  spreadsheetService.useSsl ();
  spreadsheetService.setOAuthCredentials (oauthParameters,
    new OAuthHmacSha1Signer ());

  URL feedUrl= new URL (
    "https://spreadsheets.google.com"
      + "/feeds/spreadsheets/private/full?title=Spreadsheetname&xoauth_requestor_id="
      + userEmail);

  SpreadsheetFeed resultFeed= spreadsheetService.getFeed (feedUrl,
    SpreadsheetFeed.class);

  List <SpreadsheetEntry> spreadsheets= resultFeed.getEntries ();
  SpreadsheetEntry spreadsheetEntry= spreadsheets.get (0);

  URL worksheetFeedUrl= spreadsheetEntry.getWorksheetFeedUrl ();
  log.severe (worksheetFeedUrl.toString ());
  WorksheetFeed worksheetFeed= spreadsheetService.getFeed (
    worksheetFeedUrl, WorksheetFeed.class);

  List <WorksheetEntry> worksheetEntrys= worksheetFeed.getEntries ();
  WorksheetEntry worksheetEntry= worksheetEntrys.get (0);

  // Write header line into Spreadsheet
  URL cellFeedUrl= worksheetEntry.getCellFeedUrl ();
  CellFeed cellFeed= spreadsheetService.getFeed (cellFeedUrl,
    CellFeed.class);

  CellEntry cellEntry= new CellEntry (1, 1, "headline1");
  cellFeed.insert (cellEntry);
  cellEntry= new CellEntry (1, 2, "headline2");
  cellFeed.insert (cellEntry);
like image 123
JochenJung Avatar answered Sep 21 '22 11:09

JochenJung


I haven't tried it, but it looks to me like that is described in the section on "Creating a table":

TableEntry tableEntry = new TableEntry();

FeedURLFactory factory = FeedURLFactory.getDefault();
URL tableFeedUrl = factory.getTableFeedUrl(spreadsheetEntry.getKey());

// Specify a basic table:
tableEntry.setTitle(new PlainTextConstruct("New Table"));
tableEntry.setWorksheet(new Worksheet("Sheet1"));
tableEntry.setHeader(new Header(1));

// Specify columns in the table, start row, number of rows.
Data tableData = new Data();
tableData.setNumberOfRows(0);
// Start row index cannot overlap with header row.
tableData.setStartIndex(2);
// This table has only one column.
tableData.addColumn(new Column("A", "Column A"));

tableEntry.setData(tableData);
service.insert(tableFeedUrl, tableEntry);

Specifically, the part tableEntry.setHeader(new Header(1)) seems like it creates a header on the first row. Then, tableData.setStartIndex(2) seems to specify that data shouldn't go in the first row (since it's the header). Finally, tableData.addColumn(new Column("A", "Column A")) seems to add a column that would be labeled in the header.

like image 21
ColinD Avatar answered Sep 19 '22 11:09

ColinD