【问题标题】:exception during adding a row to google spreadsheet向谷歌电子表格添加一行时出现异常
【发布时间】:2012-10-22 09:21:15
【问题描述】:

我已遵循文档列表(用于创建电子表格)和电子表格(用于创建工作表和添加行)的整个 API。 但是,我可以在创建后将工作表添加到电子表格中,但是当我尝试添加行时,我收到错误异常:执行请求失败:https://spreadsheets.google.com/feeds/list/tj0pIc6qpEB2LtZY9mwfT-A/od6/private/full

我已经提到了所有 OAuth 范围和所需的凭据,但无法解决此异常。 其余的工作都很好,比如创建谷歌电子表格和添加工作表。 我只是复制粘贴谷歌代码。

        // setUp the confirguration for OAuth 2.0
        string clientID = "********.apps.googleusercontent.com";
        string clientSecret = "*******************";
        string scope = "https://docs.google.com/feeds/ https://docs.googleusercontent.com/ https://spreadsheets.google.com/feeds/";
        string redirectURI = "urn:***:wg:oauth:2.0:oob";

        // setup the OAuth 2.0 object
        OAuth2Parameters parameters = new OAuth2Parameters();   // to hold all the parameters
        parameters.ClientId = clientID; // setup the clientID
        parameters.ClientSecret = clientSecret;  // setup the clientSecret
        parameters.RedirectUri=redirectURI; // setup the redirectURI

        //setup the authurization URL
        parameters.Scope = scope; // set the scope

        string authorizationURL = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
        Console.WriteLine(authorizationURL);
        Console.WriteLine("Please visit the URL above to authorize your OAuth " + "request token.  Once that is complete, type in your access code to "
    + "continue...");
        parameters.AccessCode = Console.ReadLine();

        // get the access token
        OAuthUtil.GetAccessToken(parameters);
        string accessToken = parameters.AccessToken;
        Console.WriteLine("Cosole Access token " + accessToken);

        //make Auth Request to Google
        GOAuth2RequestFactory factory = new GOAuth2RequestFactory(null, "SampleSpreadSheetApp-V1", parameters);
       // DocumentsService service = new DocumentsService("SampleSpreadSheetApp-V1");
        service.RequestFactory = factory;



        //---------------------------------------------------------------------
        GOAuth2RequestFactory requestFactory =
            new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
        SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
        service.RequestFactory = requestFactory;

        SpreadsheetQuery query = new SpreadsheetQuery();
        SpreadsheetFeed feed = service.Query(query);

        if (feed.Entries.Count == 0)
        {
            Console.WriteLine("no spreadsheets present here");
        }

        // TODO: Choose a spreadsheet more intelligently based on your
        // app's needs.
        SpreadsheetEntry spreadsheet = (SpreadsheetEntry)feed.Entries[0];
        Console.WriteLine(spreadsheet.Title.Text);

        // Get the first worksheet of the first spreadsheet.
        // TODO: Choose a worksheet more intelligently based on your
        // app's needs.
        WorksheetFeed wsFeed = spreadsheet.Worksheets;
        WorksheetEntry worksheet = (WorksheetEntry)wsFeed.Entries[0];

        if (wsFeed.Entries.Count == 0)
        {
            Console.WriteLine("no worksheets present here");
        }

        // Define the URL to request the list feed of the worksheet.
        AtomLink listFeedLink = worksheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);

        // Fetch the list feed of the worksheet.
        ListQuery listQuery = new ListQuery(listFeedLink.HRef.ToString());
        ListFeed listFeed = service.Query(listQuery);

        // Create a local representation of the new row.
        ListEntry row = new ListEntry();
        row.Elements.Add(new ListEntry.Custom() { LocalName = "firstname", Value = "Joe" });
        row.Elements.Add(new ListEntry.Custom() { LocalName = "lastname", Value = "Smith" });
        row.Elements.Add(new ListEntry.Custom() { LocalName = "age", Value = "26" });
        row.Elements.Add(new ListEntry.Custom() { LocalName = "height", Value = "176" });

        // Send the new row to the API for insertion.
        service.Insert(listFeed, row);

【问题讨论】:

  • 如果您深入了解异常详细信息,您会发现完整的错误消息,告诉您请求失败的原因。作为替代方案,尝试使用 Fiddler 捕获 HTTP 流量并检查响应的详细信息
  • 在内部异常下拉列表中,它显示 {"The remote server returned an error: (400) Bad Request."} 作为响应,StatusCode 作为 System.Net.HttpStatusCode.BadRequest。这是谷歌服务器错误还是我遗漏了什么?
  • 我什至在手动写入电子表格后尝试了行删除和行更新,但只有在电子表格 API 上添加行功能不起作用,我认为它们在我的代码中似乎没有任何问题,因为行更新,删除工作正常。响应字符串是:我们很抱歉,发生服务器错误。请稍等,然后尝试重新加载您的电子表格。 ——
  • 我可以从这个 API 的一部分的谷歌管理员那里得到任何支持,这个 API 是商业/测试版还是处于 alpha 状态?即使在 google 电子表格 api 论坛 link 这个问题是由其他用户发布的,但我们不支持这个 api 的问题。
  • 你有什么解决办法吗?

标签: c# google-drive-api google-docs-api google-spreadsheet-api google-cloud-storage


【解决方案1】:

我也为此苦苦挣扎,发现以下问题:

每一列都有一个在第一行中指定的名称。 在 Google API 示例中,第一列是“名字”,它在现有工作表的单元格 A1 中指示。

当您添加行时(如您在上面粘贴的示例代码中),“LocalName”属性必须完全匹配。 此外,API 小写并从列名中删除空格(上帝知道为什么??),因此如果您将列名定义为“名称”,API 会将其小写为“名称”并尝试与之匹配。 因此,当您添加一行时,您需要在代码中以小写形式定义列名,不要有空格。

为了安全起见,创建一个包含一列的工作表并将第一行单元格设置为“名称”。 现在使用您的身份验证运行示例代码并尝试添加只有一个条目的行,如下所示:

ListEntry row = new ListEntry();
row.Elements.Add(new ListEntry.Custom() { LocalName = "name", Value = "John" });

这行不通

row.Elements.Add(new ListEntry.Custom() { LocalName = "Name", Value = "John" });

【讨论】:

  • 另外,数字(例如“1”)似乎不能用作列名。
  • 并且下划线将与空格一起从列标题中删除。
  • Java 版本也已确认。 newRow.getCustomElements().setValueLocal("columnname", value);注意“列名:”似乎也不起作用,因此没有特殊字符的小写名称是最佳选择。
【解决方案2】:

您需要先创建/确保工作表的标题行:

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

        CellEntry cellEntry = new CellEntry(1, 1, "firstname");
        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 2, "lastname");
        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 3, "age");
        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 4, "height");
        cellFeed.Insert(cellEntry);

【讨论】:

  • 使用 CellEntry 是可以的,但是使用 ListEntry 对我不起作用
【解决方案3】:

如文档中所述,列表提要对数据在电子表格中的布局方式做出了一些假设。特别是,列表提要将工作表的第一行视为标题行:

https://developers.google.com/google-apps/spreadsheets/#working_with_list-based_feeds

在您的代码中,您尝试添加具有四个值的行:名字、姓氏、年龄和身高。如果工作表的第一行没有定义这些标题,您的请求将无效并且您的代码会引发异常。

请确保您的代码与电子表格的结构相匹配,或者如果您需要动态确定标题行的内容,请使用单元格提要。

【讨论】:

  • 您好,根据您的帖子,我在创建电子表格工作表时修改了它的结构。但是,我仍然遇到同样的错误。也许,如果我手动添加标题行然后添加行(如文档中所示),然后它成功添加了行。但这完全是错误的,因为我在创建工作表后无法手动插入标题行。在编程术语中,流程应该是 1. 创建电子表格 2. 添加工作表 3. 在工作表中添加一行。我无法在两者之间手动添加标题列!如果我做错了,或者这就是 API 的构建方式,请说明?
  • 即使我尝试实现单元格提要,我也没有发现像在单元格中添加值之类的东西,它只会更改单元格的内容,其中单元格值在找到匹配项时发生更改。您能否指定如何添加单元格(对于第一个标题行)。提前感谢您的及时回复。
  • 使用单元格提要时,不会“添加”单元格,因为它们已经存在于电子表格中。使用该提要,您可以更改任何单元格的值,包括第一行
  • 我不确定这个 api 是否不起作用(严重),我正在尝试使用 google 代码添加一个新行:link。我有一个空白工作表,正好有 4 列 n 20 行,从而匹配结构,那么为什么不添加行? justpaste.it/1gwg 并且错误显示“GDataRequestException spreadsheets.google.com/feeds/list/tcNuf-lsZ12j1_acLx3Lceg/od7/…; 。我应该在这里做什么?
【解决方案4】:

在这几个小时里头疼之后,我发现 Google API 似乎有一个错误。如果工作表底部有空白行,则添加一行可以正常工作,并且 API 会返回适当的响应。但是,如果工作表底部没有空白行,则您的数据已正确正确地添加到工作表底部的新行中,但 Google 仍会返回 HTTP 400 错误,其中包含文本“不能使用空白行写的。”
您可以通过捕获错误、检查错误是否属于“空白行”种类来解决此错误,如果是,则使用 listfeed 查询来检索您刚刚添加的行。如果该查询成功,则新行已正确添加,可以忽略 400 错误。
我会给你我的代码,但我正在使用 PHP,所以可能没有帮助。

【讨论】:

  • 我在这上面浪费了好几天。然后我找到了这个答案,谢谢
猜你喜欢
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多