示例#1
0
    /** Presents the given cell feed as a map from row, column pair to CellEntry. */
    private void refreshCachedData() throws IOException, ServiceException {

      CellQuery cellQuery = new CellQuery(backingEntry.getCellFeedUrl());
      cellQuery.setReturnEmpty(true);
      this.cellFeed = spreadsheetService.getFeed(cellQuery, CellFeed.class);

      //            A subtlety: Spreadsheets row,col numbers are 1-based whereas the
      //            cellEntries array is 0-based. Rather than wasting an extra row and
      //            column worth of cells in memory, we adjust accesses by subtracting
      //            1 from each row or column number.
      cellEntries = new CellEntry[rows][columns];
      for (CellEntry cellEntry : cellFeed.getEntries()) {
        Cell cell = cellEntry.getCell();
        cellEntries[cell.getRow() - 1][cell.getCol() - 1] = cellEntry;
      }
    }
示例#2
0
  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();
    }
  }