/** * Prompts the user for a set of operations and submits them in a batch request. * * @param reader to read input from the keyboard. * @throws ServiceException when the request causes an error in the Google Spreadsheets service. * @throws IOException when an error occurs in communication with the Google Spreadsheets service. */ public void processBatchRequest(BufferedReader reader) throws IOException, ServiceException { final String BATCH_PROMPT = "Enter set operations one by one, " + "then enter submit to send the batch request:\n" + " set row# col# value [[add a set operation]]\n" + " submit [[submit the request]]"; CellFeed batchRequest = new CellFeed(); // Prompt user for operation System.out.println(BATCH_PROMPT); String operation = reader.readLine(); while (!operation.startsWith("submit")) { String[] s = operation.split(" ", 4); if (s.length != 4 || !s[0].equals("set")) { System.out.println("Invalid command: " + operation); operation = reader.readLine(); continue; } // Create a new cell entry and add it to the batch request. int row = Integer.parseInt(s[1]); int col = Integer.parseInt(s[2]); String value = s[3]; CellEntry batchOperation = createUpdateOperation(row, col, value); batchRequest.getEntries().add(batchOperation); // Display the current entries in the batch request. printBatchRequest(batchRequest); // Prompt for another operation. System.out.println(BATCH_PROMPT); operation = reader.readLine(); } // Get the batch feed URL and submit the batch request CellFeed feed = service.getFeed(cellFeedUrl, CellFeed.class); Link batchLink = feed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM); URL batchUrl = new URL(batchLink.getHref()); CellFeed batchResponse = service.batch(batchUrl, batchRequest); // Print any errors that may have happened. boolean isSuccess = true; for (CellEntry entry : batchResponse.getEntries()) { String batchId = BatchUtils.getBatchId(entry); if (!BatchUtils.isSuccess(entry)) { isSuccess = false; BatchStatus status = BatchUtils.getBatchStatus(entry); System.out.println( "\n" + batchId + " failed (" + status.getReason() + ") " + status.getContent()); } } if (isSuccess) { System.out.println("Batch operations successful."); } }
public void performBatchUpdate(CellFeed batchRequest, URL cellFeedUrl) throws IOException, ServiceException { logger.info("\n Begin Batch Update \n"); long startTime = System.currentTimeMillis(); // printBatchRequest(batchRequest); CellFeed cellFeed = spreadsheetService.getFeed(cellFeedUrl, CellFeed.class); // Submit the update // set header work around for http://code.google.com/p/gdata-java-client/issues/detail?id=103 spreadsheetService.setHeader("If-Match", "*"); Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM); // CellFeed batchResponse = spreadsheetService.batch(new URL(batchLink.getHref()), // batchRequest); spreadsheetService.batch(new URL(batchLink.getHref()), batchRequest); spreadsheetService.setHeader("If-Match", null); logger.info("\n ms elapsed for batch update: \n" + (System.currentTimeMillis() - startTime)); // Check the results // boolean isSuccess = true; // for (CellEntry entry : batchResponse.getEntries()) { // String batchId = BatchUtils.getBatchId(entry); // if (!BatchUtils.isSuccess(entry)) { // isSuccess = false; // BatchStatus status = BatchUtils.getBatchStatus(entry); // System.out.printf("%s failed (%s) %s", batchId, status.getReason(), // status.getContent()); // // } // // break; // } // // logger.info(isSuccess ? "\nBatch operations successful." : "\nBatch operations // failed"); // System.out.printf("\n%s ms elapsed\n", System.currentTimeMillis() - startTime); }
public static void gogogo( String username, String password, int itemsPerBatch, String spreadsheetName, String worksheetName, String data) throws Exception { System.out.println("# Initializing upload to Google Spreadsheets..."); System.out.print("# Logging in as: \"" + username + "\"... "); ImportClient client = new ImportClient(username, password, itemsPerBatch, spreadsheetName); System.out.println("Success!"); Pattern delim = Pattern.compile(DELIM); try { int row = 0; String[] allLines = data.split("\n"); int currentCell = 1; int allRow = allLines.length; System.out.println("# Preparing " + allRow + " rows to be updated... "); List<CellEntry> updatedCells = new LinkedList<CellEntry>(); Worksheet workSheet = client.getWorksheet(spreadsheetName, worksheetName); ProgressBar.updateProgress(0, allRow); for (String line : allLines) { // Break up the line by the delimiter and insert the cells String[] cells = delim.split(line, -1); for (int col = 0; col < cells.length; col++) { // old way - send the change // client.insertCellEntry(spreadsheet, worksheet, row + 1, col + 1, // cells[col]); // prepare change CellEntry cellEntry = workSheet.getCell(row + 1, col + 1); String value = cells[col]; cellEntry.changeInputValueLocal(value); updatedCells.add(cellEntry); } // Advance the loop ProgressBar.updateProgress(++row, allRow); } // send the batches int allBatches = updatedCells.size(); int currentBatch = 0; List<List<CellEntry>> batches = chunkList(updatedCells, ITEMS_PER_BATCH); System.out.println("\n\n# Uploading changes in " + batches.size() + " chunks, "); System.out.println("# containing a total of " + allBatches + " operations... "); for (List<CellEntry> batch : batches) { CellFeed batchFeed = new CellFeed(); for (CellEntry cellEntry : batch) { ProgressBar.updateProgress(++currentBatch, allBatches); Cell cell = cellEntry.getCell(); BatchUtils.setBatchId(cellEntry, "R" + cell.getRow() + "C" + cell.getCol()); BatchUtils.setBatchOperationType(cellEntry, BatchOperationType.UPDATE); batchFeed.getEntries().add(cellEntry); } Link batchLink = workSheet.getBatchUpdateLink(); CellFeed batchResultFeed = client.service.batch(new URL(batchLink.getHref()), batchFeed); // Make sure all the operations were successful. for (CellEntry entry : batchResultFeed.getEntries()) { if (!BatchUtils.isSuccess(entry)) { String batchId = BatchUtils.getBatchId(entry); BatchStatus status = BatchUtils.getBatchStatus(entry); System.err.println("Failed entry"); System.err.println("\t" + batchId + " failed (" + status.getReason() + ") "); return; } } } } catch (Exception e) { e.printStackTrace(); } }
private static void printUserEntryLinks(UserEntry ue) { System.out.println("--Links:"); for (Link link : ue.getLinks()) { System.out.println(" " + link.getRel() + " is " + link.getHref()); } }