Search code examples
javagwtgoogle-sheetsgoogle-docs-apigoogle-sheets-api

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.


Solution

  • 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);