在Google Spreadsheets中更新单元格会返回错误"缺少资源版本ID" /"远程服务器返回错误:(400)错误请求。"

时间:2014-03-28 17:10:45

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

我想更新Google Spreadsheets中的单元格值,但不幸的是收到错误:

Google.GData.Client.GDataRequestException was unhandled
  HResult=-2146233088
  Message=Execution of request failed: https://spreadsheets.google.com/feeds/cells/1nW8nxoS2l9pbj6dctreEfKHNXmsfbbsCAvOd7TIj4Bo/od6/private/full/R1C1
  Source=Google.GData.Client
  ResponseString=Missing resource version ID
  StackTrace:
   at Google.GData.Client.GDataRequest.Execute()
   ...
   at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Net.WebException
   HResult=-2146233079
   Message=The remote server returned an error: (400) Bad Request.
   Source=System
   StackTrace:
        at System.Net.HttpWebRequest.GetResponse()
        at Google.GData.Client.GDataRequest.Execute()

我的代码非常简单,基于从https://developers.google.com/google-apps/spreadsheets/?csw=1#changing_contents_of_a_cell下载的示例:

        SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");

        // TODO: Authorize the service object for a specific user (see other sections)
        service.setUserCredentials("...", "...");            

        // Instantiate a SpreadsheetQuery object to retrieve spreadsheets.
        SpreadsheetQuery query = new SpreadsheetQuery();

        // Make a request to the API and get all spreadsheets.
        SpreadsheetFeed feed = service.Query(query);

        foreach (SpreadsheetEntry spreadsheet in feed.Entries)
        {
            if (spreadsheet.Title.Text == "Test01")
            {
                // Get the first worksheet of the first spreadsheet.
                WorksheetFeed wsFeed = spreadsheet.Worksheets;
                WorksheetEntry worksheet = (WorksheetEntry)wsFeed.Entries[0];

                // Fetch the cell feed of the worksheet.
                CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink);
                cellQuery.MinimumRow = 1;
                cellQuery.MaximumRow = 10;
                cellQuery.MinimumColumn = cellQuery.MaximumColumn = 1;
                cellQuery.ReturnEmpty = ReturnEmptyCells.yes;
                CellFeed cellFeed = service.Query(cellQuery);

                // Iterate through each cell, updating its value if necessary.
                foreach (CellEntry cell in cellFeed.Entries)
                {
                    cell.InputValue = "Foooooo!";
                    cell.Update();
                }

            }
        }

在以下行引发错误:

                    cell.Update();

我使用Google.GData版本2.2.0.0(http://code.google.com/p/google-gdata/)。 你知道什么可能导致这个问题吗?

[编辑]此问题也在gdata python客户端中报告过。希望很快得到修复。 http://code.google.com/p/gdata-python-client/issues/detail?id=692&sort=-opened&colspec=Opened%20Stars%20ID%20Type%20Status%20Priority%20Component%20Summary

谢谢!

2 个答案:

答案 0 :(得分:6)

我们在一周前遇到了同样的问题,当时Google似乎将所有电子表格翻转为“新”格式。
这适用于通过GData api创建的新的。到处都是400个错误。

我深入研究了GData变体库(Python,Java,.Net等)的报告,并最终找到了这个小块: https://stackoverflow.com/a/23438381/1685090

将Etag属性设置为“*”就是答案:)

要清楚,当我们想要在工作表上运行Update()时,我们正在设置WorksheetEntry.Etag,并且我们还在通过SpreadsheetsService.Batch()进行批量更新时设置CellEntry.Etag。

到目前为止,它似乎与Google的“新”电子表格完美配合。

这种方法的一个优点是任何并发/合并操作都将被放弃 - 实质上,您告诉Google您的更新必须清除单元格中的任何其他并发先前值。

答案 1 :(得分:1)

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

If-Match: *

哪个说覆盖任何东西。