开发者

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

开发者 https://www.devze.com 2023-01-28 14:24 出处:网络
This is how I create the spreadsheet: DocsService client= new DocsService (\"idea\"); client.useSsl ();

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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消