C# 在c语言中为google电子表格中的特定列写入值时出错#

C# 在c语言中为google电子表格中的特定列写入值时出错#,c#,google-api,google-sheets,google-spreadsheet-api,C#,Google Api,Google Sheets,Google Spreadsheet Api,我在电子表格中手动定义了一列,名为: 新建住宅 日期和时间 男/女 如何为这些列写入值。我试过了 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 Lis

我在电子表格中手动定义了一列,名为:

  • 新建住宅
  • 日期和时间
  • 男/女
如何为这些列写入值。我试过了

  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:newy”关联的属性名“build”必须后跟“=”字符。我使用其他过程更改批处理中的单元格值。

API支持在单个请求中更新整个列、行或其他单元格集。这是为了提高性能,而不是生成大量的单个请求。此过程称为“批处理请求”

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

查询和更新可以组合在任何大小的集合中,并在单个请求中发送到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;
    }
  }
类BatchCellUpdater { //要在目标工作簿中填充的行数 const int MAX_ROWS=75; //要在目标工作簿中填充的列数 常数int MAX_COLS=5; /** *用于存储单元格行/列信息和关联RnCn的基本结构 *标识符。 */ 私有类蜂窝地址 { 公共交通路权; 公共部门; 公共字符串IdString; /** *构造表示指定的{@code row}和 *{@code col}。IdString将以“RnCn”表示法设置。 */ 公共蜂窝地址(uint行、uint列) { this.Row=行; this.Col=Col; this.IdString=string.Format(“R{0}C{1}”,行,列); } } 静态void Main(字符串[]参数) { 字符串键; //命令行解析 如果(args.Length!=1) { Console.Error.WriteLine(“语法:BatchCellUpdater”); 返回; } 其他的 { key=args[0]; } //准备电子表格服务 电子表格服务=新的电子表格服务(“MySpreadsheetIntegration-v1”); //TODO:为特定用户授权服务对象(请参阅其他部分) CellQuery CellQuery=新的CellQuery(键,“od6”、“专用”、“完整”); CellFeed CellFeed=service.Query(cellQuery); //生成要填写的单元格地址列表 List cellAddrs=新列表(); 对于(uint行=1;行