【问题标题】:Google spreadsheets API C# missing resource version id on batch updateGoogle 电子表格 API C# 在批量更新时缺少资源版本 ID
【发布时间】:2023-03-11 17:14:02
【问题描述】:

我正在尝试以这种方式使用电子表格 api C# v2.2.0.0 批量更新空单元格:

foreach (var csvline in csv)
{
          var csvlineParsed = csvline.Split(';');
          for (uint index = 0; index < csvlineParsed.Length; index++)
          {
                var cellEntry = new CellEntry(i, index + 1, csvlineParsed[index]);
                cellFeed.Insert(cellEntry);

                var cell = new CellEntry(i, index + 1);
                cell.Id = new AtomId(string.Format("{0}/{1}", cellFeed.Self, "R" + i + "C" + index + 1));
                cell.InputValue = csvlineParsed[index];
                cell.BatchData = new GDataBatchEntryData("R" + cell.Row + "C" + cell.Column, GDataBatchOperationType.update);
                batchRequest.Entries.Add(cell);*/

          }
          i++;
}

var batchResponse = (CellFeed) _service.Batch(batchRequest, new Uri(cellFeed.Batch));

但我有“缺少资源版本 id”错误

【问题讨论】:

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


    【解决方案1】:

    我解决此问题的另一种方法是添加额外的 HTTP 标头

    If-Match: *
    

    这说覆盖任何东西。

    【讨论】:

    • 我正在使用普通协议,而 Etag="*" 没有用,这个是一种魅力。
    【解决方案2】:

    我们在我工作的地方遇到了类似的问题。我们最终将 CellEntry 上的 Etag 属性设置为 *,这允许更新通过。我确信它基本上是让它覆盖单元格中的值中的任何值,但这只是在发生大量并发进程/更新时才会出现的问题。

    我们抓取了 CellFeed 并直接从中获取了 CellEntry,并进行了修改,包括设置 Etag。之后我们调用了Publish(),它对所有标记为脏的条目进行批量请求,当您更改它们时会自动发生这种情况,或者您可以手动设置entry.Dirty = True

    一些粗略的代码来解释我们所做的事情,这只是将第一行中的单元格设置为values 中的字符串,cellQuery 是一个 CellQuery,用于获取您想要使用的单元格。

    CellFeed cellFeed = spreadsheetService.Query(cellQuery);
    int i = 1;
    foreach (string value in values)
    {
       CellEntry entry = cellFeed[1, i++];
       entry.InputValue = value;
       entry.Etag = "*";
    }
    
    cellFeed.Publish();
    

    您可以自己发出批处理请求并将实体Etag 设置为* 并获得相同的结果,但我发现Publish() 方法更易于使用。

    【讨论】:

    • +1 这对我有用,但在我的情况下,我同时使用 ListFeed 和 ListEntry,所以我必须同时使用 ListEntry.Update() 和 ListFeed.Publish();非常感谢:)
    【解决方案3】:

    因为我个人在弄清楚如何应用之前建议的答案时遇到了一些麻烦。我去了互联网的尽头,试图找到如何设置额外的标题(If-Match:*)

    所以我把这个留给你。

    在我的 Java 应用程序中,这是这样做的:

        service.setHeader("If-Match", "*")
    

    在我的 .NET 应用程序中它已经完成了:

        ((GDataRequestFactory)service.RequestFactory).CustomHeaders.Add("If-Match: *");
    

    我希望这可以为你们中的一些人节省一些时间。

    • AydinE

    【讨论】:

      【解决方案4】:

      A 也有同样的问题,根据文档 (https://developers.google.com/google-apps/spreadsheets/?hl=fr-FR&csw=1#updating_multiple_cells_with_a_batch_request),

      首先你必须得到实体,不能像我想的那样“简单”。

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

      【讨论】:

        猜你喜欢
        • 2021-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-27
        • 1970-01-01
        • 2014-07-04
        • 1970-01-01
        相关资源
        最近更新 更多