在c#中写入Google电子表格中特定列的值时出错

时间:2015-11-11 21:56:23

标签: c# google-api google-sheets google-spreadsheet-api

我已在电子表格中定义了一个名为“

”的列
  • 新建家园?
  • 日期和时间
  • M / F

如何为这些列写入值。我试过了

  row.Elements.Add(new ListEntry.Custom() { LocalName = "newlybuildhome?", Value = "Joe"  });
  row.Elements.Add(new ListEntry.Custom() { LocalName = "m/f", Value = "f" });
  row.Elements.Add(new ListEntry.Custom() { LocalName = "dateandtime", Value = "26" });
  service.Insert(listFeed, row);

我得到例外无法写入空白行;改为使用删除。

我试过了:

row.Elements.Add(new ListEntry.Custom() { LocalName = "Newly build home?", Value = "Joe"  });
row.Elements.Add(new ListEntry.Custom() { LocalName = "M/F", Value = "Smith" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "Date and Time", Value = "26" });
service.Insert(listFeed, row);

我的响应字符串出现异常:与元素类型“gsx:Newly”关联的属性名称“build”必须后跟“=”字符。

1 个答案:

答案 0 :(得分:0)

我使用其他程序来批量更改单元格值。

API支持在单个请求中更新整个列,行或其他一组单元格。这是为了提高性能而不是制作大量的单个请求。此过程称为"批处理请求"

许多批处理操作可以合并为一个请求。支持的两种批处理操作是查询和更新。不支持插入和删除,因为单元格Feed不能用于插入或删除单元格。请记住,必须使用工作表提要来执行此操作。

查询和更新可以组合在任何大小的集合中,并在单个请求中发送给API。 API响应批次中每个单独操作的状态信息。对于查询请求,结果是请求的单元格。对于更新请求,结果包括具有新值的受控单元格。

class BatchCellUpdater
  {
    // The number of rows to fill in the destination workbook
    const int MAX_ROWS = 75;

    // The number of columns to fill in the destination workbook
    const int MAX_COLS = 5;

    /**
     * A basic struct to store cell row/column information and the associated RnCn
     * identifier.
     */
    private class CellAddress
    {
      public uint Row;
      public uint Col;
      public string IdString;

      /**
       * Constructs a CellAddress representing the specified {@code row} and
       * {@code col}. The IdString will be set in 'RnCn' notation.
       */
      public CellAddress(uint row, uint col)
      {
        this.Row = row;
        this.Col = col;
        this.IdString = string.Format("R{0}C{1}", row, col);
      }
    }

    static void Main(string[] args)
    {
      string key;

      // Command line parsing
      if (args.Length != 1)
      {
        Console.Error.WriteLine("Syntax: BatchCellUpdater <key>");
        return;
      }
      else
      {
        key = args[0];
      }

      // Prepare Spreadsheet Service
      SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");

      // TODO: Authorize the service object for a specific user (see other sections)

      CellQuery cellQuery = new CellQuery(key, "od6", "private", "full");
      CellFeed cellFeed = service.Query(cellQuery);

      // Build list of cell addresses to be filled in
      List<CellAddress> cellAddrs = new List<CellAddress>();
      for (uint row = 1; row <= MAX_ROWS; ++row)
      {
        for (uint col = 1; col <= MAX_COLS; ++col)
        {
          cellAddrs.Add(new CellAddress(row, col));
        }
      }

      // Prepare the update
      // GetCellEntryMap is what makes the update fast.
      Dictionary<String, CellEntry> cellEntries = GetCellEntryMap(service, cellFeed, cellAddrs);

      CellFeed batchRequest = new CellFeed(cellQuery.Uri, service);
      foreach (CellAddress cellAddr in cellAddrs)
      {
        CellEntry batchEntry = cellEntries[cellAddr.IdString];
        batchEntry.InputValue = cellAddr.IdString;
        batchEntry.BatchData = new GDataBatchEntryData(cellAddr.IdString, GDataBatchOperationType.update);
        batchRequest.Entries.Add(batchEntry);
      }

      // Submit the update
      CellFeed batchResponse = (CellFeed)service.Batch(batchRequest, new Uri(cellFeed.Batch));

      // Check the results
      bool isSuccess = true;
      foreach (CellEntry entry in batchResponse.Entries)
      {
        string batchId = entry.BatchData.Id;
        if (entry.BatchData.Status.Code != 200)
        {
          isSuccess = false;
          GDataBatchStatus status = entry.BatchData.Status;
          Console.WriteLine("{0} failed ({1})", batchId, status.Reason);
        }
      }

      Console.WriteLine(isSuccess ? "Batch operations successful." : "Batch operations failed");
    }

    /**
     * Connects to the specified {@link SpreadsheetsService} and uses a batch
     * request to retrieve a {@link CellEntry} for each cell enumerated in {@code
     * cellAddrs}. Each cell entry is placed into a map keyed by its RnCn
     * identifier.
     *
     * @param service the spreadsheet service to use.
     * @param cellFeed the cell feed to use.
     * @param cellAddrs list of cell addresses to be retrieved.
     * @return a dictionary consisting of one {@link CellEntry} for each address in {@code
     *         cellAddrs}
     */
    private static Dictionary<String, CellEntry> GetCellEntryMap(
        SpreadsheetsService service, CellFeed cellFeed, List<CellAddress> cellAddrs)
    {
      CellFeed batchRequest = new CellFeed(new Uri(cellFeed.Self), service);
      foreach (CellAddress cellId in cellAddrs)
      {
        CellEntry batchEntry = new CellEntry(cellId.Row, cellId.Col, cellId.IdString);
        batchEntry.Id = new AtomId(string.Format("{0}/{1}", cellFeed.Self, cellId.IdString));
        batchEntry.BatchData = new GDataBatchEntryData(cellId.IdString, GDataBatchOperationType.query);
        batchRequest.Entries.Add(batchEntry);
      }

      CellFeed queryBatchResponse = (CellFeed)service.Batch(batchRequest, new Uri(cellFeed.Batch));

      Dictionary<String, CellEntry> cellEntryMap = new Dictionary<String, CellEntry>();
      foreach (CellEntry entry in queryBatchResponse.Entries)
      {
        cellEntryMap.Add(entry.BatchData.Id, entry);
        Console.WriteLine("batch {0} (CellEntry: id={1} editLink={2} inputValue={3})",
            entry.BatchData.Id, entry.Id, entry.EditUri,
            entry.InputValue);
      }

      return cellEntryMap;
    }
  }
相关问题