猿问

在单个请求中使用数据清除和填充多个工作表

我需要定期清除多个工作表中的数据并用数据重新填充它们(通过 Google Sheets API v4)。为此,我对每个工作表执行 2 个单独的请求(1 个清除和 1 个更新)。当用户坐在那里等待它时,这是一种缓慢的过程。在我看来,每个新请求都会显着增加完成时间。如果我可以将所有这些包装成一个批处理命令请求,它可能会有很大帮助。


我目前正在为每张纸做这个......


service.spreadsheets()

  .values()

  .clear(idSpreadsheet, sheetTitle + "!$A$1:$Y", new ClearValuesRequest())

  .execute();


service.spreadsheets()

  .values()

  .update(idSpreadsheet, range, new ValueRange().setValues(values))

  .setValueInputOption("USER_ENTERED")

  .execute();

我没有看到将一堆通用命令包装到单个批处理请求中的方法。我看到了DeleteDimensionRequest,AppendCellsRequest可以打包成一个批次,但我真的找不到一个很好的AppendCellsRequest例子(而且似乎人们values().update()仍然推荐我目前的方法)。


任何人都可以推荐一种简化此操作的好方法吗?还是我已经在做最好的方式?


蝴蝶不菲
浏览 113回答 1
1回答

郎朗坤

仍然不知道我是否以最佳方式执行此操作,但是我能够实现在单个批处理请求中使用数据清除和填充多个工作表的目标。诀窍是不使用该clear()方法,而是使用RepeatCellRequest. 此外,现在使用AppendCellsRequest代替update(). 这两个请求可以包装在一个批处理请求中。我对 3 张纸的早期测试显示,性能提高了 25%。不壮观,但它有帮助。List<Request> requests = new ArrayList<Request>();for (SheetData mySheet : sheetDatas){&nbsp; &nbsp; List<List<Object>> values = mySheet.getValues();&nbsp; &nbsp; Request clearSheetRequest = new Request()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setRepeatCell(new RepeatCellRequest()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setRange(new GridRange()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setSheetId(mySheet.getSheetId())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setFields("*")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setCell(new CellData())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; List<RowData> preppedRows = new ArrayList<RowData>();&nbsp; &nbsp; for (List<Object> row : values)&nbsp; &nbsp; {&nbsp; &nbsp; RowData preppedRow = new RowData();&nbsp; &nbsp; List<CellData> cells = new ArrayList<CellData>();&nbsp; &nbsp; for (Object value : row)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; CellData cell = new CellData();&nbsp; &nbsp; &nbsp; &nbsp; ExtendedValue userEnteredValue = new ExtendedValue();&nbsp; &nbsp; &nbsp; &nbsp; if (value instanceof String)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userEnteredValue.setStringValue((String) value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (value instanceof Double)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userEnteredValue.setNumberValue((Double) value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (value instanceof Integer)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userEnteredValue.setNumberValue(Double.valueOf((Integer) value).doubleValue());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (value instanceof Boolean)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userEnteredValue.setBoolValue((Boolean) value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; cell.setUserEnteredValue(userEnteredValue);&nbsp; &nbsp; &nbsp; &nbsp; cells.add(cell);&nbsp; &nbsp; }&nbsp; &nbsp; preppedRow.setValues(cells);&nbsp; &nbsp; preppedRows.add(preppedRow);&nbsp; &nbsp; }&nbsp; &nbsp; Request appendCellsRequest = new Request().setAppendCells(&nbsp; &nbsp; new AppendCellsRequest()&nbsp; &nbsp; &nbsp; &nbsp; .setSheetId(mySheet.getSheetId())&nbsp; &nbsp; &nbsp; &nbsp; .setRows(preppedRows)&nbsp; &nbsp; &nbsp; &nbsp; .setFields("*")&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; requests.add(clearSheetRequest);&nbsp; &nbsp; requests.add(appendCellsRequest);}BatchUpdateSpreadsheetRequest batch = new BatchUpdateSpreadsheetRequest().setRequests(requests);BatchUpdateSpreadsheetResponse batchResponse = service.spreadsheets().batchUpdate(idSpreadsheet, batch).execute();
随时随地看视频慕课网APP

相关分类

Java
我要回答