Search in sources :

Example 6 with DataValidationConstraint

use of org.apache.poi.ss.usermodel.DataValidationConstraint in project poi by apache.

the class TestDataValidation method testGetDataValidationsDecimal.

@Test
public void testGetDataValidationsDecimal() throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    List<HSSFDataValidation> list = sheet.getDataValidations();
    assertEquals(0, list.size());
    DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
    DataValidationConstraint constraint = dataValidationHelper.createDecimalConstraint(OperatorType.BETWEEN, "=A2", "200");
    CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
    DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
    sheet.addValidationData(validation);
    // <-- works
    list = sheet.getDataValidations();
    assertEquals(1, list.size());
    HSSFDataValidation dv = list.get(0);
    DVConstraint c = dv.getConstraint();
    assertEquals(ValidationType.DECIMAL, c.getValidationType());
    assertEquals(OperatorType.BETWEEN, c.getOperator());
    assertEquals("A2", c.getFormula1());
    assertEquals(null, c.getFormula2());
    assertEquals(null, c.getValue1());
    assertEquals(new Double("200"), c.getValue2());
    wb.close();
}
Also used : DataValidationConstraint(org.apache.poi.ss.usermodel.DataValidationConstraint) CellRangeAddressList(org.apache.poi.ss.util.CellRangeAddressList) DataValidationHelper(org.apache.poi.ss.usermodel.DataValidationHelper) DataValidation(org.apache.poi.ss.usermodel.DataValidation) BaseTestDataValidation(org.apache.poi.ss.usermodel.BaseTestDataValidation) Test(org.junit.Test)

Example 7 with DataValidationConstraint

use of org.apache.poi.ss.usermodel.DataValidationConstraint in project poi by apache.

the class TestDataValidation method testGetDataValidationsListFormula.

@Test
public void testGetDataValidationsListFormula() throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    List<HSSFDataValidation> list = sheet.getDataValidations();
    assertEquals(0, list.size());
    DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
    DataValidationConstraint constraint = dataValidationHelper.createFormulaListConstraint("A2");
    CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
    DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
    validation.setSuppressDropDownArrow(true);
    sheet.addValidationData(validation);
    // <-- works
    list = sheet.getDataValidations();
    assertEquals(1, list.size());
    HSSFDataValidation dv = list.get(0);
    assertEquals(true, dv.getSuppressDropDownArrow());
    DVConstraint c = dv.getConstraint();
    assertEquals(ValidationType.LIST, c.getValidationType());
    assertEquals("A2", c.getFormula1());
    assertEquals(null, c.getFormula2());
    assertEquals(null, c.getValue1());
    assertEquals(null, c.getValue2());
    wb.close();
}
Also used : DataValidationConstraint(org.apache.poi.ss.usermodel.DataValidationConstraint) CellRangeAddressList(org.apache.poi.ss.util.CellRangeAddressList) DataValidationHelper(org.apache.poi.ss.usermodel.DataValidationHelper) DataValidation(org.apache.poi.ss.usermodel.DataValidation) BaseTestDataValidation(org.apache.poi.ss.usermodel.BaseTestDataValidation) Test(org.junit.Test)

Example 8 with DataValidationConstraint

use of org.apache.poi.ss.usermodel.DataValidationConstraint in project poi by apache.

the class TestDataValidation method testGetDataValidationsFormula.

@Test
public void testGetDataValidationsFormula() throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    List<HSSFDataValidation> list = sheet.getDataValidations();
    assertEquals(0, list.size());
    DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
    DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("A2:A3");
    CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
    DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
    sheet.addValidationData(validation);
    // <-- works
    list = sheet.getDataValidations();
    assertEquals(1, list.size());
    HSSFDataValidation dv = list.get(0);
    DVConstraint c = dv.getConstraint();
    assertEquals(ValidationType.FORMULA, c.getValidationType());
    assertEquals("A2:A3", c.getFormula1());
    assertEquals(null, c.getFormula2());
    assertEquals(null, c.getValue1());
    assertEquals(null, c.getValue2());
    wb.close();
}
Also used : DataValidationConstraint(org.apache.poi.ss.usermodel.DataValidationConstraint) CellRangeAddressList(org.apache.poi.ss.util.CellRangeAddressList) DataValidationHelper(org.apache.poi.ss.usermodel.DataValidationHelper) DataValidation(org.apache.poi.ss.usermodel.DataValidation) BaseTestDataValidation(org.apache.poi.ss.usermodel.BaseTestDataValidation) Test(org.junit.Test)

Example 9 with DataValidationConstraint

use of org.apache.poi.ss.usermodel.DataValidationConstraint in project poi by apache.

the class TestDataValidation method testGetDataValidationsAny.

@Test
public void testGetDataValidationsAny() throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    List<HSSFDataValidation> list = sheet.getDataValidations();
    assertEquals(0, list.size());
    DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
    DataValidationConstraint constraint = dataValidationHelper.createNumericConstraint(ValidationType.ANY, OperatorType.IGNORED, null, null);
    CellRangeAddressList addressList = new CellRangeAddressList(1, 2, 3, 4);
    DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
    validation.setEmptyCellAllowed(true);
    validation.createErrorBox("error-title", "error-text");
    validation.createPromptBox("prompt-title", "prompt-text");
    sheet.addValidationData(validation);
    // <-- works
    list = sheet.getDataValidations();
    assertEquals(1, list.size());
    HSSFDataValidation dv = list.get(0);
    {
        CellRangeAddressList regions = dv.getRegions();
        assertEquals(1, regions.countRanges());
        CellRangeAddress address = regions.getCellRangeAddress(0);
        assertEquals(1, address.getFirstRow());
        assertEquals(2, address.getLastRow());
        assertEquals(3, address.getFirstColumn());
        assertEquals(4, address.getLastColumn());
    }
    assertEquals(true, dv.getEmptyCellAllowed());
    assertEquals(false, dv.getSuppressDropDownArrow());
    assertEquals(true, dv.getShowErrorBox());
    assertEquals("error-title", dv.getErrorBoxTitle());
    assertEquals("error-text", dv.getErrorBoxText());
    assertEquals(true, dv.getShowPromptBox());
    assertEquals("prompt-title", dv.getPromptBoxTitle());
    assertEquals("prompt-text", dv.getPromptBoxText());
    DataValidationConstraint c = dv.getValidationConstraint();
    assertEquals(ValidationType.ANY, c.getValidationType());
    assertEquals(OperatorType.IGNORED, c.getOperator());
    wb.close();
}
Also used : DataValidationConstraint(org.apache.poi.ss.usermodel.DataValidationConstraint) CellRangeAddressList(org.apache.poi.ss.util.CellRangeAddressList) DataValidationHelper(org.apache.poi.ss.usermodel.DataValidationHelper) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) DataValidation(org.apache.poi.ss.usermodel.DataValidation) BaseTestDataValidation(org.apache.poi.ss.usermodel.BaseTestDataValidation) Test(org.junit.Test)

Example 10 with DataValidationConstraint

use of org.apache.poi.ss.usermodel.DataValidationConstraint in project poi by apache.

the class TestDataValidation method testGetDataValidationsDate.

@Test
public void testGetDataValidationsDate() throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    List<HSSFDataValidation> list = sheet.getDataValidations();
    assertEquals(0, list.size());
    DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
    DataValidationConstraint constraint = dataValidationHelper.createDateConstraint(OperatorType.EQUAL, "2014/10/25", null, null);
    CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
    DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
    sheet.addValidationData(validation);
    // <-- works
    list = sheet.getDataValidations();
    assertEquals(1, list.size());
    HSSFDataValidation dv = list.get(0);
    DVConstraint c = dv.getConstraint();
    assertEquals(ValidationType.DATE, c.getValidationType());
    assertEquals(OperatorType.EQUAL, c.getOperator());
    assertEquals(null, c.getFormula1());
    assertEquals(null, c.getFormula2());
    assertEquals(DateUtil.getExcelDate(DateUtil.parseYYYYMMDDDate("2014/10/25")), c.getValue1(), 0);
    assertEquals(null, c.getValue2());
    wb.close();
}
Also used : DataValidationConstraint(org.apache.poi.ss.usermodel.DataValidationConstraint) CellRangeAddressList(org.apache.poi.ss.util.CellRangeAddressList) DataValidationHelper(org.apache.poi.ss.usermodel.DataValidationHelper) DataValidation(org.apache.poi.ss.usermodel.DataValidation) BaseTestDataValidation(org.apache.poi.ss.usermodel.BaseTestDataValidation) Test(org.junit.Test)

Aggregations

DataValidationConstraint (org.apache.poi.ss.usermodel.DataValidationConstraint)20 Test (org.junit.Test)16 CellRangeAddressList (org.apache.poi.ss.util.CellRangeAddressList)14 DataValidationHelper (org.apache.poi.ss.usermodel.DataValidationHelper)13 BaseTestDataValidation (org.apache.poi.ss.usermodel.BaseTestDataValidation)12 DataValidation (org.apache.poi.ss.usermodel.DataValidation)12 Sheet (org.apache.poi.ss.usermodel.Sheet)3 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)3 ArrayList (java.util.ArrayList)2 Row (org.apache.poi.ss.usermodel.Row)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 BigDecimal (java.math.BigDecimal)1 AssertionFailedError (junit.framework.AssertionFailedError)1 HSSFDataValidation (org.apache.poi.hssf.usermodel.HSSFDataValidation)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 StringEval (org.apache.poi.ss.formula.eval.StringEval)1 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)1 Cell (org.apache.poi.ss.usermodel.Cell)1 CellReference (org.apache.poi.ss.util.CellReference)1