Package org.zkoss.poi.ss.util

Examples of org.zkoss.poi.ss.util.CellRangeAddress


  //TODO:
  private static final String ALL_BLANK_MSG = "Cannot find the range. Please select a cell within the range and try again!";
  @Override
  public AutoFilter autoFilter() {
    synchronized (_sheet) {
      CellRangeAddress affectedArea;
      if(_sheet.isAutoFilterMode()){
        affectedArea = _sheet.removeAutoFilter();
        Range unhideArea = Ranges.range(_sheet,
            affectedArea.getFirstRow(),affectedArea.getFirstColumn(),
            affectedArea.getLastRow(),affectedArea.getLastColumn());
        unhideArea.getRows().setHidden(false);
      } else {
        //The logic to decide the actual affected range to implement autofilter:
        //If it's a multi cell range, it's the range intersect with largest range of the sheet.
        //If it's a single cell range, it has to be extend to a continuous range by looking up the near 8 cells of the single cell.
        affectedArea = new CellRangeAddress(getRow(), getLastRow(), getColumn(), getLastColumn());
        final Ref ref = getRefs().iterator().next();
        //ZSS-199
        if (ref.isWholeRow()) {
          //extend to a continuous range from the top row
          CellRangeAddress maxRange = getRowCurrentRegion(_sheet, ref.getTopRow(), ref.getBottomRow());
          if (maxRange == null) {
            throw new RuntimeException(ALL_BLANK_MSG);
          }
          affectedArea = maxRange;
        } else if (BookHelper.isOneCell(_sheet, affectedArea)) { //only one cell selected(include merged one), try to look the max range surround by blank cells
          CellRangeAddress maxRange = getCurrentRegion(_sheet, getRow(), getColumn());
          if (maxRange == null) {
            throw new RuntimeException(ALL_BLANK_MSG);
          }
          affectedArea = maxRange;
        } else {
          CellRangeAddress largeRange = getLargestRange(_sheet); //get the largest range that contains non-blank cells
          if (largeRange == null) {
            throw new RuntimeException(ALL_BLANK_MSG);
          }
          int left = largeRange.getFirstColumn();
          int top = largeRange.getFirstRow();
          int right = largeRange.getLastColumn();
          int bottom = largeRange.getLastRow();
          if (left < getColumn()) {
            left = getColumn();
          }
          if (right > getLastColumn()) {
            right = getLastColumn();
          }
          if (top < getRow()) {
            top = getRow();
          }
          if (bottom > getLastRow()) {
            bottom = getLastRow();
          }
          if (top > bottom || left > right) {
            throw new RuntimeException(ALL_BLANK_MSG);
          }
          affectedArea = new CellRangeAddress(top, bottom, left, right);
        }
        _sheet.setAutoFilter(affectedArea);
      }
      
      //I have to know the top row area to show/remove the combo button
View Full Code Here


    synchronized (_sheet) {
      AutoFilter af = _sheet.getAutoFilter();
      if (af == null) { //no AutoFilter to apply
        return;
      }
      final CellRangeAddress afrng = af.getRangeAddress();
      final List<FilterColumn> fcs = af.getFilterColumns();
      if (fcs == null)
        return;
      for(FilterColumn fc : fcs) {
        BookHelper.setProperties(fc, null, AutoFilter.FILTEROP_VALUES, null, null); //clear all filter
      }
      final int row1 = afrng.getFirstRow();
      final int row = row1 + 1;
      final int row2 = afrng.getLastRow();
      final int col1 = afrng.getFirstColumn();
      final int col2 = afrng.getLastColumn();
      final Set<Ref> all = new HashSet<Ref>();
      for (int r = row; r <= row2; ++r) {
        final Row rowobj = _sheet.getRow(r);
        if (rowobj != null && rowobj.getZeroHeight()) { //a hidden row
          final int left = rowobj.getFirstCellNum();
View Full Code Here

    synchronized (_sheet) {
      AutoFilter af = _sheet.getAutoFilter();
      if (af == null) { //no AutoFilter to apply
        return;
      }
      final CellRangeAddress affectedArea = af.getRangeAddress();
      final int row1 = affectedArea.getFirstRow();
      final int col1 = affectedArea.getFirstColumn();
      final int row = row1 + 1;
      final int row2 = affectedArea.getLastRow();
      final int col2 = affectedArea.getLastColumn();
     
      final Set<Ref> all = new HashSet<Ref>();
      for (int r = row; r <= row2; ++r) {
        boolean hidden = false;
        final List<FilterColumn> fcs = af.getFilterColumns();
View Full Code Here

      }
      final FilterColumn fc = BookHelper.getOrCreateFilterColumn(af, field-1);
      BookHelper.setProperties(fc, criteria1, filterOp, criteria2, visibleDropDown);
     
      //update rows
      final CellRangeAddress affectedArea = af.getRangeAddress();
      final int row1 = affectedArea.getFirstRow();
      final int col1 = affectedArea.getFirstColumn();
      final int col =  col1 + field - 1;
      final int row = row1 + 1;
      final int row2 = affectedArea.getLastRow();
      final Set cr1 = fc.getCriteria1();
 
      final Set<Ref> all = new HashSet<Ref>();
      for (int r = row; r <= row2; ++r) {
        final Cell cell = BookHelper.getCell(_sheet, r, col);
View Full Code Here

  @Override
  public CellRangeAddress getRepeatingRowsAndColumns(int sheetIndex) {
    final int nameIndex  = findExistingBuiltinNameRecordIdx(sheetIndex, NameRecord.BUILTIN_PRINT_TITLE);
    if (nameIndex == -1) {
      return new CellRangeAddress(-1, -1, -1, -1);
    }
    final NameRecord r = getNameRecord(nameIndex);
    Ptg[] ptgs = r.getNameDefinition();
    return BookHelper.getRepeatRowsAndColumns(ptgs);
  }
View Full Code Here

  @Override
  public void initMerged() {
      final int num = _sheet.getNumMergedRegions();
      _mergedRegions = new HashMap<String, CellRangeAddress>(num);
      for (int j = 0; j < num; ++j) {
        final CellRangeAddress addr = _sheet.getMergedRegion(j);
        final int col = addr.getFirstColumn();
        final int row = addr.getFirstRow();
        _mergedRegions.put(mergeId(row, col), addr);
      }
  }
View Full Code Here

        return super.addMergedRegion(region);
    }

    @Override
    public void removeMergedRegion(int index) {
      final CellRangeAddress region = getMergedRegion(index);
      if (region != null) {
          deleteMerged(region);
      }
      super.removeMergedRegion(index);
    }
View Full Code Here

        return super.addMergedRegion(region);
    }

    @Override
    public void removeMergedRegion(int index) {
      final CellRangeAddress region = getMergedRegion(index);
      if (region != null) {
          deleteMerged(region);
      }
      super.removeMergedRegion(index);
    }
View Full Code Here

    testToFormulaString(cellC1, "SUM(B8:B10)");
   
    //6 merge area
    assertEquals(6, sheet1.getNumMergedRegions());
    for (int j = 0; j < 6; ++j) {
      CellRangeAddress rng = sheet1.getMergedRegion(j);
      switch(j) {
      case 0:
        assertEquals("E6:F8", rng.formatAsString());
        break;
      case 1:
        assertEquals("E10:F12", rng.formatAsString());
        break;
      case 2:
        assertEquals("G5:H5", rng.formatAsString());
        break;
      case 3:
        assertEquals("E3:F4", rng.formatAsString());
        break;
      case 4:
        assertEquals("I3:J7", rng.formatAsString());
        break;
      case 5:
        assertEquals("E14:F15", rng.formatAsString());
        break;
      }
    }
 
    //remove rows 4 ~ 6
    BookHelper.deleteRows(sheet1, 3, 3); //remove rows 4 ~ 6
    _evaluator.notifySetFormula(cellA1);
    _evaluator.notifySetFormula(cellB1);
    _evaluator.notifySetFormula(cellC1);
   
    //B4: 5, B5:7, C5: empty, B6: 9, B7: 11, row 8 ~ row 10 empty
    Row row4 = sheet1.getRow(3);
    row5 = sheet1.getRow(4);
    row6 = sheet1.getRow(5);
    row7 = sheet1.getRow(6);
   
    row8 = sheet1.getRow(7);
    row9 = sheet1.getRow(8);
    row10 = sheet1.getRow(9);
   
    assertNull(row5.getCell(2)); //C5 not exist
    assertNull(row8.getCell(1));
    assertNull(row9.getCell(1));
    assertNull(row10);
   
    assertEquals(5, row4.getCell(1).getNumericCellValue(), 0.0000000000000001);
    assertEquals(7, row5.getCell(1).getNumericCellValue(), 0.0000000000000001);
    assertEquals(9, row6.getCell(1).getNumericCellValue(), 0.0000000000000001);
    assertEquals(11, row7.getCell(1).getNumericCellValue(), 0.0000000000000001);
   
    //C1: =SUM(B5:B7)
    valueC1 = _evaluator.evaluate(cellC1);
    assertEquals(27, valueC1.getNumberValue(), 0.0000000000000001);
    assertEquals(Cell.CELL_TYPE_NUMERIC, valueC1.getCellType());
    testToFormulaString(cellC1, "SUM(B5:B7)");
   
    //B1: =SUM(B4:B7)
    valueB1 = _evaluator.evaluate(cellB1);
    assertEquals(32, valueB1.getNumberValue(), 0.0000000000000001);
    assertEquals(Cell.CELL_TYPE_NUMERIC, valueB1.getCellType());
    testToFormulaString(cellB1, "SUM(B4:B7)");
   
    //A1: =SUM(#REF!)
    valueA1 = _evaluator.evaluate(cellA1);
    assertEquals(Cell.CELL_TYPE_ERROR, valueA1.getCellType());
    testToFormulaString(cellA1, "SUM(#REF!)");
   
    //5 merge area
    assertEquals(5, sheet1.getNumMergedRegions()); //G5:H5 unmerged
    for (int j = 0; j < 5; ++j) {
      CellRangeAddress rng = sheet1.getMergedRegion(j);
      switch(j) {
      case 0:
        assertEquals("E4:F5", rng.formatAsString()); //E6:F8 -> E4:F5
        break;
      case 1:
        assertEquals("E7:F9", rng.formatAsString()); //E10:F12 -> E7:F9
        break;
      case 2:
        assertEquals("E3:F3", rng.formatAsString()); //E3:F4 -> E3:F3
        break;
      case 3:
        assertEquals("I3:J4", rng.formatAsString()); //I3:J7 -> I3:J4
        break;
      case 4:
        assertEquals("E11:F12", rng.formatAsString()); //E14:F15 -> E11:F12
      }
    }
  }
View Full Code Here

    testToFormulaString(cellC1, "SUM(B8:B10)");
   
    //6 merge area
    assertEquals(6, sheet1.getNumMergedRegions());
    for (int j = 0; j < 6; ++j) {
      CellRangeAddress rng = sheet1.getMergedRegion(j);
      switch(j) {
      case 0:
        assertEquals("E6:F8", rng.formatAsString());
        break;
      case 1:
        assertEquals("E10:F12", rng.formatAsString());
        break;
      case 2:
        assertEquals("G5:H5", rng.formatAsString());
        break;
      case 3:
        assertEquals("E3:F4", rng.formatAsString());
        break;
      case 4:
        assertEquals("I3:J7", rng.formatAsString());
        break;
      case 5:
        assertEquals("E14:F15", rng.formatAsString());
        break;
      }
    }
 
    //remove A4:J6
    BookHelper.deleteRange(sheet1, 3, 0, 5, 9, false);
    _evaluator.notifySetFormula(cellA1);
    _evaluator.notifySetFormula(cellB1);
    _evaluator.notifySetFormula(cellC1);
   
    //B4: 5, B5:7, C5: empty, B6: 9, B7: 11, row 8 ~ row 10 empty
    Row row4 = sheet1.getRow(3);
    row5 = sheet1.getRow(4);
    row6 = sheet1.getRow(5);
    row7 = sheet1.getRow(6);
   
    row8 = sheet1.getRow(7);
    row9 = sheet1.getRow(8);
    row10 = sheet1.getRow(9);
   
    assertNull(row5.getCell(2)); //C5 not exist
    assertNull(row8.getCell(1));
    assertNull(row9.getCell(1));
    assertNull(row10.getCell(1));
   
    assertEquals(5, row4.getCell(1).getNumericCellValue(), 0.0000000000000001);
    assertEquals(7, row5.getCell(1).getNumericCellValue(), 0.0000000000000001);
    assertEquals(9, row6.getCell(1).getNumericCellValue(), 0.0000000000000001);
    assertEquals(11, row7.getCell(1).getNumericCellValue(), 0.0000000000000001);
   
    //C1: =SUM(B5:B7)
    valueC1 = _evaluator.evaluate(cellC1);
    assertEquals(27, valueC1.getNumberValue(), 0.0000000000000001);
    assertEquals(Cell.CELL_TYPE_NUMERIC, valueC1.getCellType());
    testToFormulaString(cellC1, "SUM(B5:B7)");
   
    //B1: =SUM(B4:B7)
    valueB1 = _evaluator.evaluate(cellB1);
    assertEquals(32, valueB1.getNumberValue(), 0.0000000000000001);
    assertEquals(Cell.CELL_TYPE_NUMERIC, valueB1.getCellType());
    testToFormulaString(cellB1, "SUM(B4:B7)");
   
    //A1: =SUM(#REF!)
    valueA1 = _evaluator.evaluate(cellA1);
    assertEquals(Cell.CELL_TYPE_ERROR, valueA1.getCellType());
    testToFormulaString(cellA1, "SUM(#REF!)");
   
    //5 merge area
    assertEquals(5, sheet1.getNumMergedRegions()); //G5:H5 unmerged
    for (int j = 0; j < 5; ++j) {
      CellRangeAddress rng = sheet1.getMergedRegion(j);
      switch(j) {
      case 0:
        assertEquals("E4:F5", rng.formatAsString()); //E6:F8 -> E4:F5
        break;
      case 1:
        assertEquals("E7:F9", rng.formatAsString()); //E10:F12 -> E7:F9
        break;
      case 2:
        assertEquals("E3:F3", rng.formatAsString()); //E3:F4 -> E3:F3
        break;
      case 3:
        assertEquals("I3:J4", rng.formatAsString()); //I3:J7 -> I3:J4
        break;
      case 4:
        assertEquals("E11:F12", rng.formatAsString()); //E14:F15 -> E11:F12
      }
    }
  }
View Full Code Here

TOP

Related Classes of org.zkoss.poi.ss.util.CellRangeAddress

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.