Hello,
I'm using the Spreadsheets Java API and
tried to add new rows into an existing spreadsheet (5 cols, 30 rows, header
values in first row set, rest of spreadsheet
empty) using the code below:
private void addRows(WorksheetEntry worksheet, String[][] values, int firstRow)
{
int row;
try {
// request feed of empty cells
CellQuery query = new CellQuery(worksheet.getCellFeedUrl());
query.setMinimumRow(firstRow);
query.setMaximumRow(firstRow + values.length);
query.setMinimumCol(1);
query.setMaximumCol(values[0].length);
query.setReturnEmpty(true);
CellFeed cellFeed = service.query(query, CellFeed.class);
CellFeed batchRequestFeed = new CellFeed();
// set values for each cell
int currentCellEntry=0;
for (int i=0; i < values.length; i++) {
for (int j=0; j < values[i].length; j++) {
CellEntry entry = new
CellEntry(cellFeed.getEntries().get(currentCellEntry));
entry.changeInputValueLocal(values[i][j]);
BatchUtils.setBatchId(entry, (new
Integer(currentCellEntry)).toString());
BatchUtils.setBatchOperationType(entry,
BatchOperationType.UPDATE);
batchRequestFeed.getEntries().add(entry);
currentCellEntry++;
}
}
// upload cells
Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH,
Link.Type.ATOM);
CellFeed batchResponse = service.batch(new
URL(batchLink.getHref()), batchRequestFeed);
for (CellEntry entry : batchResponse.getEntries()) {
if (!BatchUtils.isSuccess(entry)) {
System.out.println("Error uploading entry");
BatchStatus status =
BatchUtils.getBatchStatus(entry);
System.out.printf("%s failed (%s) %s\n",
BatchUtils.getBatchId(entry), status.getReason(), status.getContent());
}
}
} catch (IOException e) {
System.err.println("IOError: " +e);
} catch (ServiceException e) {
System.err.println("Google Service Error: " +e);
}
}
calling:
String[][] values = { {"1", "2", "3", "4", "5"},
{"a", "b", "c", "d", "e"},
{"dummy", "foo", "bar", "x", "y"}};
addRows(worksheet, values, 2);
with service being an authenticated SpreadsheetService and worksheet being an
existing worksheet (with properties as described above) gives me following
output on command line:
Error uploading entry
0 failed (If-Match or If-None-Match header or entry etag attribute required)
null
Error uploading entry
1 failed (Internal Error) null
Error uploading entry
2 failed (Internal Error) null
Error uploading entry
3 failed (Internal Error) null
Error uploading entry
4 failed (Internal Error) null
Error uploading entry
5 failed (Internal Error) null
Error uploading entry
6 failed (Internal Error) null
Error uploading entry
7 failed (Internal Error) null
Error uploading entry
8 failed (Internal Error) null
Error uploading entry
9 failed (Internal Error) null
Error uploading entry
10 failed (Internal Error) null
Error uploading entry
11 failed (Internal Error) null
Error uploading entry
12 failed (Internal Error) null
Error uploading entry
13 failed (Internal Error) null
Error uploading entry
14 failed (Internal Error) null
Is it that I'm doing something wrong or is this a bug in gdata-java-client?
Thanks in Advance