Search in sources :

Example 1 with CTDataValidation

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataValidation in project poi by apache.

the class XSSFDataValidationHelper method createValidation.

/* (non-Javadoc)
	 * @see org.apache.poi.ss.usermodel.DataValidationHelper#createValidation(org.apache.poi.ss.usermodel.DataValidationConstraint, org.apache.poi.ss.util.CellRangeAddressList)
	 */
public DataValidation createValidation(DataValidationConstraint constraint, CellRangeAddressList cellRangeAddressList) {
    XSSFDataValidationConstraint dataValidationConstraint = (XSSFDataValidationConstraint) constraint;
    CTDataValidation newDataValidation = CTDataValidation.Factory.newInstance();
    int validationType = constraint.getValidationType();
    switch(validationType) {
        case DataValidationConstraint.ValidationType.LIST:
            newDataValidation.setType(STDataValidationType.LIST);
            newDataValidation.setFormula1(constraint.getFormula1());
            break;
        case DataValidationConstraint.ValidationType.ANY:
            newDataValidation.setType(STDataValidationType.NONE);
            break;
        case DataValidationConstraint.ValidationType.TEXT_LENGTH:
            newDataValidation.setType(STDataValidationType.TEXT_LENGTH);
            break;
        case DataValidationConstraint.ValidationType.DATE:
            newDataValidation.setType(STDataValidationType.DATE);
            break;
        case DataValidationConstraint.ValidationType.INTEGER:
            newDataValidation.setType(STDataValidationType.WHOLE);
            break;
        case DataValidationConstraint.ValidationType.DECIMAL:
            newDataValidation.setType(STDataValidationType.DECIMAL);
            break;
        case DataValidationConstraint.ValidationType.TIME:
            newDataValidation.setType(STDataValidationType.TIME);
            break;
        case DataValidationConstraint.ValidationType.FORMULA:
            newDataValidation.setType(STDataValidationType.CUSTOM);
            break;
        default:
            newDataValidation.setType(STDataValidationType.NONE);
    }
    if (validationType != ValidationType.ANY && validationType != ValidationType.LIST) {
        STDataValidationOperator.Enum op = XSSFDataValidation.operatorTypeMappings.get(constraint.getOperator());
        if (op != null) {
            newDataValidation.setOperator(op);
        }
        if (constraint.getFormula1() != null) {
            newDataValidation.setFormula1(constraint.getFormula1());
        }
        if (constraint.getFormula2() != null) {
            newDataValidation.setFormula2(constraint.getFormula2());
        }
    }
    CellRangeAddress[] cellRangeAddresses = cellRangeAddressList.getCellRangeAddresses();
    List<String> sqref = new ArrayList<String>();
    for (int i = 0; i < cellRangeAddresses.length; i++) {
        CellRangeAddress cellRangeAddress = cellRangeAddresses[i];
        sqref.add(cellRangeAddress.formatAsString());
    }
    newDataValidation.setSqref(sqref);
    newDataValidation.setAllowBlank(true);
    newDataValidation.setErrorStyle(STDataValidationErrorStyle.STOP);
    return new XSSFDataValidation(dataValidationConstraint, cellRangeAddressList, newDataValidation);
}
Also used : ArrayList(java.util.ArrayList) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) CTDataValidation(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataValidation) DataValidationConstraint(org.apache.poi.ss.usermodel.DataValidationConstraint) STDataValidationOperator(org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationOperator)

Example 2 with CTDataValidation

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataValidation in project poi by apache.

the class XSSFDataValidation method getConstraint.

private static XSSFDataValidationConstraint getConstraint(CTDataValidation ctDataValidation) {
    String formula1 = ctDataValidation.getFormula1();
    String formula2 = ctDataValidation.getFormula2();
    Enum operator = ctDataValidation.getOperator();
    org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationType.Enum type = ctDataValidation.getType();
    Integer validationType = XSSFDataValidation.validationTypeReverseMappings.get(type);
    Integer operatorType = XSSFDataValidation.operatorTypeReverseMappings.get(operator);
    return new XSSFDataValidationConstraint(validationType, operatorType, formula1, formula2);
}
Also used : Enum(org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationOperator.Enum) STDataValidationType(org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationType)

Aggregations

ArrayList (java.util.ArrayList)1 DataValidationConstraint (org.apache.poi.ss.usermodel.DataValidationConstraint)1 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1 CTDataValidation (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataValidation)1 STDataValidationOperator (org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationOperator)1 Enum (org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationOperator.Enum)1 STDataValidationType (org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationType)1