3

如何通过 Google 电子表格 API 在以编程方式创建的工作表上添加简单的标题行?很难理解他们的文档,它只提到了如何在已经创建的标题行上添加一行。如果第一次使用基于单元格的提要添加标题行,

// Fetch the cell feed of the worksheet.
  CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink);
  CellFeed cellFeed = service.Query(cellQuery);

  // Iterate through each cell, updating its value if necessary.
  foreach (CellEntry cell in cellFeed.Entries)
  {
    if (cell.Title.Text == "A1")
    {
      cell.InputValue = "name";
      cell.Update();
    }
    else if (cell.Title.Text == "B1")
    {
      cell.InputValue = "age";
      cell.Update();
    }
  }

此代码不起作用,因为 cellFeed.Entries 为 0,因此控制永远不会进入 for 循环。我已经添加了一个包含 (4) 行和 (10) 列的工作表。但是如何先输入一个简单的标题行(作为一列),然后再输入其他行(作为这些列中的值)?我知道已经问了几个问题,但没有一个问题有答案,这太令人沮丧和难以理解这个 API 实现起来如此复杂是什么?

4

2 回答 2

5

答案是 :

        CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink);
        CellFeed cellFeed = _SpService.Query(cellQuery);

        CellEntry cellEntry = new CellEntry(1, 1, "name");
        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 2, "age");
        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 3, "address");
        cellFeed.Insert(cellEntry);

然后可以根据与上述标题行匹配的文档在下面添加行。

于 2012-10-30T12:58:31.873 回答
1

默认情况下,单元格查询不返回空单元格。您需要覆盖此设置。

像这样编辑您的代码:

CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink);
cellQuery.ReturnEmpty = ReturnEmptyCells.yes;
CellFeed cellFeed = _SpService.Query(cellQuery);
于 2012-12-24T12:54:07.373 回答