4

我已经按照文档列表(用于创建spreadhseet)和电子表格(用于创建工作表和添加行)的整个API。 但是,我能够在创建后到工作表添加到等试算表,但是当我尝试添加该行,我得到的错误错误时抛出:执行的请求失败:https://spreadsheets.google.com/feeds/list/tj0pIc6qpEB2LtZY9mwfT-A/od6/private/full在向Google电子表格添加行时出现异常

我所提到的所有的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); 
+0

如果在异常细节深入,你会发现完整的错误信息,告诉您请求失败的原因。作为替代方法,尝试使用Fiddler捕获HTTP流量并检查响应的详细信息 –

+0

在内部异常下拉菜单中,它表示{“远程服务器返回了一个错误:(400)错误请求。”}作为响应,StatusCode作为System .Net.HttpStatusCode.BadRequest。这是谷歌服务器错误,或者我错过了什么? – Vacca

+0

我甚至尝试过在电子表格上手动编写行后删除和行更新,但只在电子表格API上添加行功能不工作,我不认为他们似乎是我的代码中的任何错误,因为行更新,删除工作正常。回应字符串是:我们'对不起,发生服务器错误。请稍等,然后尝试重新加载电子表格。 - – Vacca

回答

1

正如文档中所报告的那样,列表提要对数据在电子表格中的布局做了一些假设。特别是,该列表饲料对待工作表的第一行作为标题行:

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

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

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

+0

你好, 根据你的文章,我修改了我的电子表格工作表的结构,而它的创建。但是,我仍然得到相同的错误。或许,如果我手动添加标题行,然后添加行(如在文档中) ,那么它会成功添加该行。但这是完全错误的,因为我在创建工作表后无法手动插入标题行。在编程方面,流程应为 1.创建电子表格2.添加工作表3.添加一行到工作表。我无法手动添加标题列! 请指定我是否做错了或者是API的构建方式? – Vacca

+0

即使我试图实现细胞饲料,我没有发现像在细胞中添加值的任何内容,它只改变发现匹配时细胞值发生变化的细胞内容。你能指定如何添加一个单元格(首先是标题行)。 Thankx提前作出及时回应。 – Vacca

+0

当使用单元格源时,单元格不会“添加”,因为它们已经存在于电子表格中。使用该饲料,你可以改变任何单元格的值,包括第一行 –

9

我也挣扎着这一点,我已经找到了以下内容:

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

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

为了安全起见,创建一个带有一列的工作表并将第一行单元格设置为'name'。 现在用您的身份验证运行示例代码,并尝试添加一排只有一个入口,像这样:

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

此外,它似乎数字(例如,“1”)不能用作列名称 –

+0

并且该下划线将从列标题和空格中除去 –

+0

也以Java版本进行验证。注意“columnname:”似乎也没有工作,所以小写没有特殊字符的名称是最佳的 –

1

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

+0

我浪费了这个日子,然后我找到了这个答案,谢谢 – AlwaysTraining

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); 
+0

使用CellEntry是好的,但后来使用ListEntry不工作我 –

相关问题