如何使用API​​在新的Google电子表格中创建第一行?

时间:2010-12-03 18:26:06

标签: java gwt google-sheets google-docs-api google-spreadsheet-api

这是我创建电子表格的方式:

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

现在我想在其中写下第一行。

但不幸的是,所有的API调用都基于这样的事实,即已经存在第一行,因为您插入了名称 - 值对(其中名称是我想要创建的标题)。 http://code.google.com/apis/spreadsheets/data/3.0/developers_guide.html#CreatingTableRecords

我是如何创建第一行的?定义字段名称的那个。

2 个答案:

答案 0 :(得分:14)

终于找到了它。你必须逐个细胞地做到这一点:

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

答案 1 :(得分:1)

我没有尝试过,但它在我看来就像“创建表格”一节中描述的那样:

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

具体来说,部分tableEntry.setHeader(new Header(1))似乎在第一行创建了标题。然后,tableData.setStartIndex(2)似乎指定数据不应该进入第一行(因为它是标题)。最后,tableData.addColumn(new Column("A", "Column A"))似乎添加了一个将在标题中标记的列。