6

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

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

有什么想法可以创建第一行吗?定义字段名称的那个。

4

2 回答 2

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);
于 2010-12-06T12:32:55.050 回答
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"))似乎添加了一个将在标题中标记的列。

于 2010-12-03T18:45:53.633 回答