c# - 无法向 Google 电子表格添加行

标签 c# google-sheets

我可以打开工作表并从标题中读取单元格。 Google 电子表格中的第一行是标题,我已在 Google 电子表格中手动添加了“Name”、“my-val1”、“my-val2”、“my-val3”、“Other”。

以下是相关的 Google 文档:

https://developers.google.com/google-apps/spreadsheets/#adding_a_list_row

我试图向工作表添加一行,但收到​​相当一般的错误“远程服务器返回错误:(400) 错误请求。”知道我做错了什么吗?

这是代码:

AtomLink listFeedLink = worksheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);
ListQuery listQuery = new ListQuery(listFeedLink.HRef.ToString());
ListFeed listFeed = myService.Query(listQuery);

ListEntry row = new ListEntry();

row.Elements.Add(new ListEntry.Custom() { LocalName = "Name", Value = "Joe" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "my-val1", Value = "Smith" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "my-val2", Value = "26" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "my-val3", Value = "176" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "Other", Value = "176" });

myService.Insert(listFeed, row);

这是错误消息:

Google.GData.Client.GDataRequestException: Execution of request failed: https://spreadsheets.google.com/feeds/list/tlb-JYE8eZbWTRPCoqugCqw/od6/private/full ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
   at System.Net.HttpWebRequest.GetResponse()
   at Google.GData.Client.GDataRequest.Execute()
   --- End of inner exception stack trace ---
   at Google.GData.Client.GDataRequest.Execute()
   at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter)
   at Google.GData.Client.GDataGAuthRequest.Execute()
   at Google.GData.Client.Service.EntrySend(Uri feedUri, AtomBase baseEntry, GDataRequestType type, AsyncSendData data)
   at Google.GData.Client.Service.Insert(Uri feedUri, AtomEntry newEntry, AsyncSendData data)
   at Google.GData.Client.Service.Insert[TEntry](Uri feedUri, TEntry entry)
   at Google.GData.Client.Service.Google.GData.Client.IService.Insert(AtomFeed feed, AtomEntry entry)
   at Google.GData.Client.Service.Insert[TEntry](AtomFeed feed, TEntry entry)
   at DesktopControl.GoogleSpreadsheet.addRow() in C:\Users\mark\Documents\Visual Studio 2008\Projects\DSCON\DSCON\GoogleSpreadsheet.cs:line 248}

最佳答案

好的,根据this link ,列名称必须以小写字母表示,并且代码中不能有空格。更改代码后插入该行:

row.Elements.Add(new ListEntry.Custom() { LocalName = "name", Value = "Joe" }); // "Name" changed to "name"
row.Elements.Add(new ListEntry.Custom() { LocalName = "my-val1", Value = "Smith" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "my-val2", Value = "26" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "my-val3", Value = "176" });
row.Elements.Add(new ListEntry.Custom() { LocalName = "other", Value = "176" }); // "Other" changed to "other"

关于c# - 无法向 Google 电子表格添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11414422/

相关文章:

c# - 如何处理 http 500 错误?

c# - 等待事件处理程序

javascript - D3 Google Sheet Access-Control-Allow-Origin 错误

google-apps-script - 尝试了解 setFormula 与 setValue 行为

javascript - 以创建者用户身份单击按钮时运行函数 - Google 电子表格

c# - 对齐按钮背景图片

c# - 异步委托(delegate)调用 (ADI) 与任务并行库 (TPL)

c# - 我怎样才能保护字符串,例如C#

regex - 对仅包含字母 F-Z 且不包含 RR 的列中的单元格进行条件格式设置

google-apps-script - Google 脚本触发器 'from Spreadsheet' 不可用