예제 #1
0
  /**
   * Shows a particular range of cells, limited by minimum/maximum rows and columns.
   *
   * @param minRow the minimum row, inclusive, 1-based
   * @param maxRow the maximum row, inclusive, 1-based
   * @param minCol the minimum column, inclusive, 1-based
   * @param maxCol the maximum column, inclusive, 1-based
   * @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 showRange(int minRow, int maxRow, int minCol, int maxCol)
      throws IOException, ServiceException {
    CellQuery query = new CellQuery(cellFeedUrl);
    query.setMinimumRow(minRow);
    query.setMaximumRow(maxRow);
    query.setMinimumCol(minCol);
    query.setMaximumCol(maxCol);
    CellFeed feed = service.query(query, CellFeed.class);

    for (CellEntry entry : feed.getEntries()) {
      printCell(entry);
    }
  }
예제 #2
0
    List<List<Object>> getRowsOfCells(
        SpreadsheetService service,
        WorksheetEntry worksheet,
        int startRow, // 1-based
        int rowCount)
        throws IOException, ServiceException {
      URL cellFeedUrl = worksheet.getCellFeedUrl();

      int minRow = startRow;
      int maxRow = Math.min(worksheet.getRowCount(), startRow + rowCount - 1);
      int cols = worksheet.getColCount();
      int rows = worksheet.getRowCount();

      CellQuery cellQuery = new CellQuery(cellFeedUrl);
      cellQuery.setMinimumRow(minRow);
      cellQuery.setMaximumRow(maxRow);
      cellQuery.setMaximumCol(cols);
      cellQuery.setMaxResults(rows * cols);
      cellQuery.setReturnEmpty(false);

      CellFeed cellFeed = service.query(cellQuery, CellFeed.class);
      List<CellEntry> cellEntries = cellFeed.getEntries();

      List<List<Object>> rowsOfCells = new ArrayList<List<Object>>(rowCount);
      for (CellEntry cellEntry : cellEntries) {
        Cell cell = cellEntry.getCell();
        if (cell != null) {
          int row = cell.getRow() - startRow;
          int col = cell.getCol() - 1;

          while (row >= rowsOfCells.size()) {
            rowsOfCells.add(new ArrayList<Object>());
          }
          List<Object> rowOfCells = rowsOfCells.get(row);

          while (col >= rowOfCells.size()) {
            rowOfCells.add(null);
          }
          rowOfCells.set(col, cell.getValue());
        }
      }
      return rowsOfCells;
    }