在 Google 电子表格中插入一行:不能写入空白行;改用删除

Inserting a row in Google spreadsheet: Blank rows cannot be written; use delete instead

我无法向现有电子表格添加行。 我正在尝试从这里开始的步骤:https://developers.google.com/google-apps/spreadsheets/data 以下行引发以下异常:

row = service.insert(listFeedUrl, row);

异常:

Exception in thread "main" com.google.gdata.util.InvalidEntryException: Bad Request
Blank rows cannot be written; use delete instead.

at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:602)
at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)
at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
at com.google.gdata.client.Service.insert(Service.java:1409)
at com.google.gdata.client.GoogleService.insert(GoogleService.java:613)
at TestGoogle.main(TestGoogle.java:93)

完整的故事:上面的例子与我需要修复的应用程序中的代码非常相似,并且该应用程序在一段时间之前就可以运行。 我成功通过了OAuth2认证

您收到此错误消息的原因可能是因为您正尝试添加到空白电子表格,而 header 不存在。

如果我们先添加 headers,那么应该可以。

使用您链接的文档中的 "Add a list row" 示例;在添加列表行

之前添加这样的 header
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);

然后列表条目示例应正确添加到电子表格

// 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);