*
*/
public class XSSFDataValidation implements DataValidation {
- private static final int MAX_TEXT_LENGTH = 255;
+ private static final int MAX_TEXT_LENGTH = 255;
- private CTDataValidation ctDdataValidation;
- private XSSFDataValidationConstraint validationConstraint;
- private CellRangeAddressList regions;
+ private CTDataValidation ctDdataValidation;
+ private XSSFDataValidationConstraint validationConstraint;
+ private CellRangeAddressList regions;
static Map<Integer, STDataValidationOperator.Enum> operatorTypeMappings = new HashMap<>();
- static Map<STDataValidationOperator.Enum, Integer> operatorTypeReverseMappings = new HashMap<>();
- static Map<Integer, STDataValidationType.Enum> validationTypeMappings = new HashMap<>();
- static Map<STDataValidationType.Enum, Integer> validationTypeReverseMappings = new HashMap<>();
- static Map<Integer, STDataValidationErrorStyle.Enum> errorStyleMappings = new HashMap<>();
- static Map<STDataValidationErrorStyle.Enum, Integer> reverseErrorStyleMappings;
+ static Map<STDataValidationOperator.Enum, Integer> operatorTypeReverseMappings = new HashMap<>();
+ static Map<Integer, STDataValidationType.Enum> validationTypeMappings = new HashMap<>();
+ static Map<STDataValidationType.Enum, Integer> validationTypeReverseMappings = new HashMap<>();
+ static Map<Integer, STDataValidationErrorStyle.Enum> errorStyleMappings = new HashMap<>();
+ static Map<STDataValidationErrorStyle.Enum, Integer> reverseErrorStyleMappings;
static {
- errorStyleMappings.put(DataValidation.ErrorStyle.INFO, STDataValidationErrorStyle.INFORMATION);
- errorStyleMappings.put(DataValidation.ErrorStyle.STOP, STDataValidationErrorStyle.STOP);
- errorStyleMappings.put(DataValidation.ErrorStyle.WARNING, STDataValidationErrorStyle.WARNING);
-
- reverseErrorStyleMappings = MapUtils.invertMap(errorStyleMappings);
-
- operatorTypeMappings.put(DataValidationConstraint.OperatorType.BETWEEN,STDataValidationOperator.BETWEEN);
- operatorTypeMappings.put(DataValidationConstraint.OperatorType.NOT_BETWEEN,STDataValidationOperator.NOT_BETWEEN);
- operatorTypeMappings.put(DataValidationConstraint.OperatorType.EQUAL,STDataValidationOperator.EQUAL);
- operatorTypeMappings.put(DataValidationConstraint.OperatorType.NOT_EQUAL,STDataValidationOperator.NOT_EQUAL);
- operatorTypeMappings.put(DataValidationConstraint.OperatorType.GREATER_THAN,STDataValidationOperator.GREATER_THAN);
- operatorTypeMappings.put(DataValidationConstraint.OperatorType.GREATER_OR_EQUAL,STDataValidationOperator.GREATER_THAN_OR_EQUAL);
- operatorTypeMappings.put(DataValidationConstraint.OperatorType.LESS_THAN,STDataValidationOperator.LESS_THAN);
- operatorTypeMappings.put(DataValidationConstraint.OperatorType.LESS_OR_EQUAL,STDataValidationOperator.LESS_THAN_OR_EQUAL);
-
- for( Map.Entry<Integer,STDataValidationOperator.Enum> entry : operatorTypeMappings.entrySet() ) {
- operatorTypeReverseMappings.put(entry.getValue(),entry.getKey());
- }
-
- validationTypeMappings.put(DataValidationConstraint.ValidationType.FORMULA,STDataValidationType.CUSTOM);
- validationTypeMappings.put(DataValidationConstraint.ValidationType.DATE,STDataValidationType.DATE);
- validationTypeMappings.put(DataValidationConstraint.ValidationType.DECIMAL,STDataValidationType.DECIMAL);
- validationTypeMappings.put(DataValidationConstraint.ValidationType.LIST,STDataValidationType.LIST);
- validationTypeMappings.put(DataValidationConstraint.ValidationType.ANY,STDataValidationType.NONE);
- validationTypeMappings.put(DataValidationConstraint.ValidationType.TEXT_LENGTH,STDataValidationType.TEXT_LENGTH);
- validationTypeMappings.put(DataValidationConstraint.ValidationType.TIME,STDataValidationType.TIME);
- validationTypeMappings.put(DataValidationConstraint.ValidationType.INTEGER,STDataValidationType.WHOLE);
-
- for( Map.Entry<Integer,STDataValidationType.Enum> entry : validationTypeMappings.entrySet() ) {
- validationTypeReverseMappings.put(entry.getValue(),entry.getKey());
- }
- }
-
- XSSFDataValidation(CellRangeAddressList regions,CTDataValidation ctDataValidation) {
- this(getConstraint(ctDataValidation), regions, ctDataValidation);
- }
-
- public XSSFDataValidation(XSSFDataValidationConstraint constraint,CellRangeAddressList regions,CTDataValidation ctDataValidation) {
- super();
- this.validationConstraint = constraint;
- this.ctDdataValidation = ctDataValidation;
- this.regions = regions;
- }
+ errorStyleMappings.put(DataValidation.ErrorStyle.INFO, STDataValidationErrorStyle.INFORMATION);
+ errorStyleMappings.put(DataValidation.ErrorStyle.STOP, STDataValidationErrorStyle.STOP);
+ errorStyleMappings.put(DataValidation.ErrorStyle.WARNING, STDataValidationErrorStyle.WARNING);
+
+ reverseErrorStyleMappings = MapUtils.invertMap(errorStyleMappings);
+
+ operatorTypeMappings.put(DataValidationConstraint.OperatorType.BETWEEN,STDataValidationOperator.BETWEEN);
+ operatorTypeMappings.put(DataValidationConstraint.OperatorType.NOT_BETWEEN,STDataValidationOperator.NOT_BETWEEN);
+ operatorTypeMappings.put(DataValidationConstraint.OperatorType.EQUAL,STDataValidationOperator.EQUAL);
+ operatorTypeMappings.put(DataValidationConstraint.OperatorType.NOT_EQUAL,STDataValidationOperator.NOT_EQUAL);
+ operatorTypeMappings.put(DataValidationConstraint.OperatorType.GREATER_THAN,STDataValidationOperator.GREATER_THAN);
+ operatorTypeMappings.put(DataValidationConstraint.OperatorType.GREATER_OR_EQUAL,STDataValidationOperator.GREATER_THAN_OR_EQUAL);
+ operatorTypeMappings.put(DataValidationConstraint.OperatorType.LESS_THAN,STDataValidationOperator.LESS_THAN);
+ operatorTypeMappings.put(DataValidationConstraint.OperatorType.LESS_OR_EQUAL,STDataValidationOperator.LESS_THAN_OR_EQUAL);
+
+ for( Map.Entry<Integer,STDataValidationOperator.Enum> entry : operatorTypeMappings.entrySet() ) {
+ operatorTypeReverseMappings.put(entry.getValue(),entry.getKey());
+ }
+
+ validationTypeMappings.put(DataValidationConstraint.ValidationType.FORMULA,STDataValidationType.CUSTOM);
+ validationTypeMappings.put(DataValidationConstraint.ValidationType.DATE,STDataValidationType.DATE);
+ validationTypeMappings.put(DataValidationConstraint.ValidationType.DECIMAL,STDataValidationType.DECIMAL);
+ validationTypeMappings.put(DataValidationConstraint.ValidationType.LIST,STDataValidationType.LIST);
+ validationTypeMappings.put(DataValidationConstraint.ValidationType.ANY,STDataValidationType.NONE);
+ validationTypeMappings.put(DataValidationConstraint.ValidationType.TEXT_LENGTH,STDataValidationType.TEXT_LENGTH);
+ validationTypeMappings.put(DataValidationConstraint.ValidationType.TIME,STDataValidationType.TIME);
+ validationTypeMappings.put(DataValidationConstraint.ValidationType.INTEGER,STDataValidationType.WHOLE);
+
+ for( Map.Entry<Integer,STDataValidationType.Enum> entry : validationTypeMappings.entrySet() ) {
+ validationTypeReverseMappings.put(entry.getValue(),entry.getKey());
+ }
+ }
+
+ XSSFDataValidation(CellRangeAddressList regions,CTDataValidation ctDataValidation) {
+ this(getConstraint(ctDataValidation), regions, ctDataValidation);
+ }
+
+ public XSSFDataValidation(XSSFDataValidationConstraint constraint,CellRangeAddressList regions,CTDataValidation ctDataValidation) {
+ super();
+ this.validationConstraint = constraint;
+ this.ctDdataValidation = ctDataValidation;
+ this.regions = regions;
+ }
- CTDataValidation getCtDdataValidation() {
- return ctDdataValidation;
- }
-
-
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#createErrorBox(java.lang.String, java.lang.String)
- */
- public void createErrorBox(String title, String text) {
- // the spec does not specify a length-limit, however Excel reports files as "corrupt" if they exceed 255 bytes for these texts...
- if(title != null && title.length() > MAX_TEXT_LENGTH) {
- throw new IllegalStateException("Error-title cannot be longer than 32 characters, but had: " + title);
- }
- if(text != null && text.length() > MAX_TEXT_LENGTH) {
- throw new IllegalStateException("Error-text cannot be longer than 255 characters, but had: " + text);
- }
- ctDdataValidation.setErrorTitle(encodeUtf(title));
- ctDdataValidation.setError(encodeUtf(text));
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#createPromptBox(java.lang.String, java.lang.String)
- */
- public void createPromptBox(String title, String text) {
- // the spec does not specify a length-limit, however Excel reports files as "corrupt" if they exceed 255 bytes for these texts...
- if(title != null && title.length() > MAX_TEXT_LENGTH) {
- throw new IllegalStateException("Error-title cannot be longer than 32 characters, but had: " + title);
- }
- if(text != null && text.length() > MAX_TEXT_LENGTH) {
- throw new IllegalStateException("Error-text cannot be longer than 255 characters, but had: " + text);
- }
- ctDdataValidation.setPromptTitle(encodeUtf(title));
- ctDdataValidation.setPrompt(encodeUtf(text));
- }
-
- /**
- * For all characters which cannot be represented in XML as defined by the XML 1.0 specification,
- * the characters are escaped using the Unicode numerical character representation escape character
- * format _xHHHH_, where H represents a hexadecimal character in the character's value.
- * <p>
- * Example: The Unicode character 0D is invalid in an XML 1.0 document,
- * so it shall be escaped as <code>_x000D_</code>.
- * </p>
- * See section 3.18.9 in the OOXML spec.
- *
- * @param text the string to encode
- * @return the encoded string
- */
- private String encodeUtf(String text) {
- if(text == null) {
- return null;
- }
-
- StringBuilder builder = new StringBuilder();
- for(char c : text.toCharArray()) {
- // for now only encode characters below 32, we can add more here if needed
- if(c < 32) {
- builder.append("_x").append(c < 16 ? "000" : "00").append(Integer.toHexString(c)).append("_");
- } else {
- builder.append(c);
- }
- }
-
- return builder.toString();
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#getEmptyCellAllowed()
- */
- public boolean getEmptyCellAllowed() {
- return ctDdataValidation.getAllowBlank();
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#getErrorBoxText()
- */
- public String getErrorBoxText() {
- return ctDdataValidation.getError();
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#getErrorBoxTitle()
- */
- public String getErrorBoxTitle() {
- return ctDdataValidation.getErrorTitle();
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#getErrorStyle()
- */
- public int getErrorStyle() {
- return reverseErrorStyleMappings.get(ctDdataValidation.getErrorStyle());
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#getPromptBoxText()
- */
- public String getPromptBoxText() {
- return ctDdataValidation.getPrompt();
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#getPromptBoxTitle()
- */
- public String getPromptBoxTitle() {
- return ctDdataValidation.getPromptTitle();
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#getShowErrorBox()
- */
- public boolean getShowErrorBox() {
- return ctDdataValidation.getShowErrorMessage();
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#getShowPromptBox()
- */
- public boolean getShowPromptBox() {
- return ctDdataValidation.getShowInputMessage();
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#getSuppressDropDownArrow()
- */
- public boolean getSuppressDropDownArrow() {
- return !ctDdataValidation.getShowDropDown();
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#getValidationConstraint()
- */
- public DataValidationConstraint getValidationConstraint() {
- return validationConstraint;
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#setEmptyCellAllowed(boolean)
- */
- public void setEmptyCellAllowed(boolean allowed) {
- ctDdataValidation.setAllowBlank(allowed);
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#setErrorStyle(int)
- */
- public void setErrorStyle(int errorStyle) {
- ctDdataValidation.setErrorStyle(errorStyleMappings.get(errorStyle));
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#setShowErrorBox(boolean)
- */
- public void setShowErrorBox(boolean show) {
- ctDdataValidation.setShowErrorMessage(show);
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#setShowPromptBox(boolean)
- */
- public void setShowPromptBox(boolean show) {
- ctDdataValidation.setShowInputMessage(show);
- }
-
- /* (non-Javadoc)
- * @see org.apache.poi.ss.usermodel.DataValidation#setSuppressDropDownArrow(boolean)
- */
- public void setSuppressDropDownArrow(boolean suppress) {
- if (validationConstraint.getValidationType()==ValidationType.LIST) {
- ctDdataValidation.setShowDropDown(!suppress);
- }
- }
-
- public CellRangeAddressList getRegions() {
- return regions;
- }
-
- public String prettyPrint() {
- StringBuilder builder = new StringBuilder();
- for(CellRangeAddress address : regions.getCellRangeAddresses()) {
- builder.append(address.formatAsString());
- }
- builder.append(" => ");
- builder.append(this.validationConstraint.prettyPrint());
- return builder.toString();
- }
-
+ CTDataValidation getCtDdataValidation() {
+ return ctDdataValidation;
+ }
+
+
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#createErrorBox(java.lang.String, java.lang.String)
+ */
+ public void createErrorBox(String title, String text) {
+ // the spec does not specify a length-limit, however Excel reports files as "corrupt" if they exceed 255 bytes for these texts...
+ if(title != null && title.length() > MAX_TEXT_LENGTH) {
+ throw new IllegalStateException("Error-title cannot be longer than 32 characters, but had: " + title);
+ }
+ if(text != null && text.length() > MAX_TEXT_LENGTH) {
+ throw new IllegalStateException("Error-text cannot be longer than 255 characters, but had: " + text);
+ }
+ ctDdataValidation.setErrorTitle(encodeUtf(title));
+ ctDdataValidation.setError(encodeUtf(text));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#createPromptBox(java.lang.String, java.lang.String)
+ */
+ public void createPromptBox(String title, String text) {
+ // the spec does not specify a length-limit, however Excel reports files as "corrupt" if they exceed 255 bytes for these texts...
+ if(title != null && title.length() > MAX_TEXT_LENGTH) {
+ throw new IllegalStateException("Error-title cannot be longer than 32 characters, but had: " + title);
+ }
+ if(text != null && text.length() > MAX_TEXT_LENGTH) {
+ throw new IllegalStateException("Error-text cannot be longer than 255 characters, but had: " + text);
+ }
+ ctDdataValidation.setPromptTitle(encodeUtf(title));
+ ctDdataValidation.setPrompt(encodeUtf(text));
+ }
+
+ /**
+ * For all characters which cannot be represented in XML as defined by the XML 1.0 specification,
+ * the characters are escaped using the Unicode numerical character representation escape character
+ * format _xHHHH_, where H represents a hexadecimal character in the character's value.
+ * <p>
+ * Example: The Unicode character 0D is invalid in an XML 1.0 document,
+ * so it shall be escaped as <code>_x000D_</code>.
+ * </p>
+ * See section 3.18.9 in the OOXML spec.
+ *
+ * @param text the string to encode
+ * @return the encoded string
+ */
+ private String encodeUtf(String text) {
+ if(text == null) {
+ return null;
+ }
+
+ StringBuilder builder = new StringBuilder();
+ for(char c : text.toCharArray()) {
+ // for now only encode characters below 32, we can add more here if needed
+ if(c < 32) {
+ builder.append("_x").append(c < 16 ? "000" : "00").append(Integer.toHexString(c)).append("_");
+ } else {
+ builder.append(c);
+ }
+ }
+
+ return builder.toString();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#getEmptyCellAllowed()
+ */
+ public boolean getEmptyCellAllowed() {
+ return ctDdataValidation.getAllowBlank();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#getErrorBoxText()
+ */
+ public String getErrorBoxText() {
+ return ctDdataValidation.getError();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#getErrorBoxTitle()
+ */
+ public String getErrorBoxTitle() {
+ return ctDdataValidation.getErrorTitle();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#getErrorStyle()
+ */
+ public int getErrorStyle() {
+ return reverseErrorStyleMappings.get(ctDdataValidation.getErrorStyle());
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#getPromptBoxText()
+ */
+ public String getPromptBoxText() {
+ return ctDdataValidation.getPrompt();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#getPromptBoxTitle()
+ */
+ public String getPromptBoxTitle() {
+ return ctDdataValidation.getPromptTitle();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#getShowErrorBox()
+ */
+ public boolean getShowErrorBox() {
+ return ctDdataValidation.getShowErrorMessage();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#getShowPromptBox()
+ */
+ public boolean getShowPromptBox() {
+ return ctDdataValidation.getShowInputMessage();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#getSuppressDropDownArrow()
+ */
+ public boolean getSuppressDropDownArrow() {
+ return !ctDdataValidation.getShowDropDown();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#getValidationConstraint()
+ */
+ public DataValidationConstraint getValidationConstraint() {
+ return validationConstraint;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#setEmptyCellAllowed(boolean)
+ */
+ public void setEmptyCellAllowed(boolean allowed) {
+ ctDdataValidation.setAllowBlank(allowed);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#setErrorStyle(int)
+ */
+ public void setErrorStyle(int errorStyle) {
+ ctDdataValidation.setErrorStyle(errorStyleMappings.get(errorStyle));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#setShowErrorBox(boolean)
+ */
+ public void setShowErrorBox(boolean show) {
+ ctDdataValidation.setShowErrorMessage(show);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#setShowPromptBox(boolean)
+ */
+ public void setShowPromptBox(boolean show) {
+ ctDdataValidation.setShowInputMessage(show);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.poi.ss.usermodel.DataValidation#setSuppressDropDownArrow(boolean)
+ */
+ public void setSuppressDropDownArrow(boolean suppress) {
+ if (validationConstraint.getValidationType()==ValidationType.LIST) {
+ ctDdataValidation.setShowDropDown(!suppress);
+ }
+ }
+
+ public CellRangeAddressList getRegions() {
+ return regions;
+ }
+
+ public String prettyPrint() {
+ StringBuilder builder = new StringBuilder();
+ for(CellRangeAddress address : regions.getCellRangeAddresses()) {
+ builder.append(address.formatAsString());
+ }
+ builder.append(" => ");
+ builder.append(this.validationConstraint.prettyPrint());
+ return builder.toString();
+ }
+
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);
+ 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);
}
}
}
@Test
- public void testAddValidations() throws Exception {
- XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("DataValidations-49244.xlsx");
- Sheet sheet = wb1.getSheetAt(0);
- List<XSSFDataValidation> dataValidations = ((XSSFSheet)sheet).getDataValidations();
-
+ public void testAddValidations() throws Exception {
+ XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("DataValidations-49244.xlsx");
+ Sheet sheet = wb1.getSheetAt(0);
+ List<XSSFDataValidation> dataValidations = ((XSSFSheet)sheet).getDataValidations();
+
/**
* For each validation type, there are two cells with the same validation. This tests
* application of a single validation definition to multiple cells.
*
* = 52 ( Total )
*/
- assertEquals(52,dataValidations.size());
-
- DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
- int[] validationTypes = new int[]{ValidationType.INTEGER,ValidationType.DECIMAL,ValidationType.TEXT_LENGTH};
-
- int[] singleOperandOperatorTypes = new int[]{
- OperatorType.LESS_THAN,OperatorType.LESS_OR_EQUAL,
- OperatorType.GREATER_THAN,OperatorType.GREATER_OR_EQUAL,
- OperatorType.EQUAL,OperatorType.NOT_EQUAL
- } ;
- int[] doubleOperandOperatorTypes = new int[]{
- OperatorType.BETWEEN,OperatorType.NOT_BETWEEN
- };
-
- BigDecimal value = new BigDecimal("10"),value2 = new BigDecimal("20");
- BigDecimal dvalue = new BigDecimal("10.001"),dvalue2 = new BigDecimal("19.999");
- final int lastRow = sheet.getLastRowNum();
- int offset = lastRow + 3;
-
- int lastKnownNumValidations = dataValidations.size();
-
- Row row = sheet.createRow(offset++);
- Cell cell = row.createCell(0);
- DataValidationConstraint explicitListValidation = dataValidationHelper.createExplicitListConstraint(new String[]{"MA","MI","CA"});
- CellRangeAddressList cellRangeAddressList = new CellRangeAddressList();
- cellRangeAddressList.addCellRangeAddress(cell.getRowIndex(), cell.getColumnIndex(), cell.getRowIndex(), cell.getColumnIndex());
- DataValidation dataValidation = dataValidationHelper.createValidation(explicitListValidation, cellRangeAddressList);
- setOtherValidationParameters(dataValidation);
- sheet.addValidationData(dataValidation);
- lastKnownNumValidations++;
-
- row = sheet.createRow(offset++);
- cell = row.createCell(0);
-
- cellRangeAddressList = new CellRangeAddressList();
- cellRangeAddressList.addCellRangeAddress(cell.getRowIndex(), cell.getColumnIndex(), cell.getRowIndex(), cell.getColumnIndex());
-
- Cell firstCell = row.createCell(1);firstCell.setCellValue("UT");
- Cell secondCell = row.createCell(2);secondCell.setCellValue("MN");
- Cell thirdCell = row.createCell(3);thirdCell.setCellValue("IL");
-
- int rowNum = row.getRowNum() + 1;
- String listFormula = new StringBuilder("$B$").append(rowNum).append(":").append("$D$").append(rowNum).toString();
- DataValidationConstraint formulaListValidation = dataValidationHelper.createFormulaListConstraint(listFormula);
-
- dataValidation = dataValidationHelper.createValidation(formulaListValidation, cellRangeAddressList);
- setOtherValidationParameters(dataValidation);
- sheet.addValidationData(dataValidation);
- lastKnownNumValidations++;
-
- offset++;
- offset++;
-
- for (int i = 0; i < validationTypes.length; i++) {
- int validationType = validationTypes[i];
- offset = offset + 2;
- final Row row0 = sheet.createRow(offset++);
- Cell cell_10 = row0.createCell(0);
- cell_10.setCellValue(validationType==ValidationType.DECIMAL ? "Decimal " : validationType==ValidationType.INTEGER ? "Integer" : "Text Length");
- offset++;
- for (int j = 0; j < singleOperandOperatorTypes.length; j++) {
- int operatorType = singleOperandOperatorTypes[j];
- final Row row1 = sheet.createRow(offset++);
-
- //For Integer (> and >=) we add 1 extra cell for validations whose formulae reference other cells.
- final Row row2 = i==0 && j < 2 ? sheet.createRow(offset++) : null;
-
- cell_10 = row1.createCell(0);
- cell_10.setCellValue(XSSFDataValidation.operatorTypeMappings.get(operatorType).toString());
- Cell cell_11 = row1.createCell(1);
- Cell cell_21 = row1.createCell(2);
- Cell cell_22 = i==0 && j < 2 ? row2.createCell(2) : null;
-
- Cell cell_13 = row1.createCell(3);
-
-
- cell_13.setCellType(CellType.NUMERIC);
- cell_13.setCellValue(validationType==ValidationType.DECIMAL ? dvalue.doubleValue() : value.intValue());
-
-
- //First create value based validation;
- DataValidationConstraint constraint = dataValidationHelper.createNumericConstraint(validationType,operatorType, value.toString(), null);
- cellRangeAddressList = new CellRangeAddressList();
- cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_11.getRowIndex(),cell_11.getRowIndex(),cell_11.getColumnIndex(),cell_11.getColumnIndex()));
- DataValidation validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
- setOtherValidationParameters(validation);
- sheet.addValidationData(validation);
- assertEquals(++lastKnownNumValidations,((XSSFSheet)sheet).getDataValidations().size());
-
- //Now create real formula based validation.
- String formula1 = new CellReference(cell_13.getRowIndex(),cell_13.getColumnIndex()).formatAsString();
- constraint = dataValidationHelper.createNumericConstraint(validationType,operatorType, formula1, null);
- if (i==0 && j==0) {
- cellRangeAddressList = new CellRangeAddressList();
- cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_21.getRowIndex(), cell_21.getRowIndex(), cell_21.getColumnIndex(), cell_21.getColumnIndex()));
- validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
- setOtherValidationParameters(validation);
- sheet.addValidationData(validation);
- assertEquals(++lastKnownNumValidations, ((XSSFSheet) sheet).getDataValidations().size());
-
- cellRangeAddressList = new CellRangeAddressList();
- cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_22.getRowIndex(), cell_22.getRowIndex(), cell_22.getColumnIndex(), cell_22.getColumnIndex()));
- validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
- setOtherValidationParameters( validation);
- sheet.addValidationData(validation);
- assertEquals(++lastKnownNumValidations, ((XSSFSheet) sheet).getDataValidations().size());
- } else if(i==0 && j==1 ){
- cellRangeAddressList = new CellRangeAddressList();
- cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_21.getRowIndex(), cell_21.getRowIndex(), cell_21.getColumnIndex(), cell_21.getColumnIndex()));
- cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_22.getRowIndex(), cell_22.getRowIndex(), cell_22.getColumnIndex(), cell_22.getColumnIndex()));
- validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
- setOtherValidationParameters( validation);
- sheet.addValidationData(validation);
- assertEquals(++lastKnownNumValidations, ((XSSFSheet) sheet).getDataValidations().size());
- } else {
- cellRangeAddressList = new CellRangeAddressList();
- cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_21.getRowIndex(), cell_21.getRowIndex(), cell_21.getColumnIndex(), cell_21.getColumnIndex()));
- validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
- setOtherValidationParameters(validation);
- sheet.addValidationData(validation);
- assertEquals(++lastKnownNumValidations, ((XSSFSheet) sheet).getDataValidations().size());
- }
- }
-
- for (int operatorType : doubleOperandOperatorTypes) {
- final Row row1 = sheet.createRow(offset++);
-
- cell_10 = row1.createCell(0);
- cell_10.setCellValue(XSSFDataValidation.operatorTypeMappings.get(operatorType).toString());
-
- Cell cell_11 = row1.createCell(1);
- Cell cell_21 = row1.createCell(2);
-
- Cell cell_13 = row1.createCell(3);
- Cell cell_14 = row1.createCell(4);
-
-
- String value1String = validationType==ValidationType.DECIMAL ? dvalue.toString() : value.toString();
- cell_13.setCellType(CellType.NUMERIC);
- cell_13.setCellValue(validationType==ValidationType.DECIMAL ? dvalue.doubleValue() : value.intValue());
-
- String value2String = validationType==ValidationType.DECIMAL ? dvalue2.toString() : value2.toString();
- cell_14.setCellType(CellType.NUMERIC);
- cell_14.setCellValue(validationType==ValidationType.DECIMAL ? dvalue2.doubleValue() : value2.intValue());
-
-
- //First create value based validation;
- DataValidationConstraint constraint = dataValidationHelper.createNumericConstraint(validationType,operatorType, value1String, value2String);
- cellRangeAddressList = new CellRangeAddressList();
- cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_11.getRowIndex(),cell_11.getRowIndex(),cell_11.getColumnIndex(),cell_11.getColumnIndex()));
- DataValidation validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
- setOtherValidationParameters(validation);
- sheet.addValidationData(validation);
- assertEquals(++lastKnownNumValidations,((XSSFSheet)sheet).getDataValidations().size());
-
-
- //Now create real formula based validation.
- String formula1 = new CellReference(cell_13.getRowIndex(),cell_13.getColumnIndex()).formatAsString();
- String formula2 = new CellReference(cell_14.getRowIndex(),cell_14.getColumnIndex()).formatAsString();
- constraint = dataValidationHelper.createNumericConstraint(validationType,operatorType, formula1, formula2);
- cellRangeAddressList = new CellRangeAddressList();
- cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_21.getRowIndex(),cell_21.getRowIndex(),cell_21.getColumnIndex(),cell_21.getColumnIndex()));
- validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
-
- setOtherValidationParameters(validation);
- sheet.addValidationData(validation);
- assertEquals(++lastKnownNumValidations,((XSSFSheet)sheet).getDataValidations().size());
- }
- }
-
- XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
- wb1.close();
- Sheet sheetAt = wb2.getSheetAt(0);
- assertEquals(lastKnownNumValidations,((XSSFSheet)sheetAt).getDataValidations().size());
- wb2.close();
- }
-
- protected void setOtherValidationParameters(DataValidation validation) {
- boolean yesNo = true;
- validation.setEmptyCellAllowed(yesNo);
- validation.setShowErrorBox(yesNo);
- validation.setShowPromptBox(yesNo);
- validation.createErrorBox("Error Message Title", "Error Message");
- validation.createPromptBox("Prompt", "Enter some value");
- validation.setSuppressDropDownArrow(yesNo);
- }
-
- @Test
+ assertEquals(52,dataValidations.size());
+
+ DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
+ int[] validationTypes = new int[]{ValidationType.INTEGER,ValidationType.DECIMAL,ValidationType.TEXT_LENGTH};
+
+ int[] singleOperandOperatorTypes = new int[]{
+ OperatorType.LESS_THAN,OperatorType.LESS_OR_EQUAL,
+ OperatorType.GREATER_THAN,OperatorType.GREATER_OR_EQUAL,
+ OperatorType.EQUAL,OperatorType.NOT_EQUAL
+ } ;
+ int[] doubleOperandOperatorTypes = new int[]{
+ OperatorType.BETWEEN,OperatorType.NOT_BETWEEN
+ };
+
+ BigDecimal value = new BigDecimal("10"),value2 = new BigDecimal("20");
+ BigDecimal dvalue = new BigDecimal("10.001"),dvalue2 = new BigDecimal("19.999");
+ final int lastRow = sheet.getLastRowNum();
+ int offset = lastRow + 3;
+
+ int lastKnownNumValidations = dataValidations.size();
+
+ Row row = sheet.createRow(offset++);
+ Cell cell = row.createCell(0);
+ DataValidationConstraint explicitListValidation = dataValidationHelper.createExplicitListConstraint(new String[]{"MA","MI","CA"});
+ CellRangeAddressList cellRangeAddressList = new CellRangeAddressList();
+ cellRangeAddressList.addCellRangeAddress(cell.getRowIndex(), cell.getColumnIndex(), cell.getRowIndex(), cell.getColumnIndex());
+ DataValidation dataValidation = dataValidationHelper.createValidation(explicitListValidation, cellRangeAddressList);
+ setOtherValidationParameters(dataValidation);
+ sheet.addValidationData(dataValidation);
+ lastKnownNumValidations++;
+
+ row = sheet.createRow(offset++);
+ cell = row.createCell(0);
+
+ cellRangeAddressList = new CellRangeAddressList();
+ cellRangeAddressList.addCellRangeAddress(cell.getRowIndex(), cell.getColumnIndex(), cell.getRowIndex(), cell.getColumnIndex());
+
+ Cell firstCell = row.createCell(1);firstCell.setCellValue("UT");
+ Cell secondCell = row.createCell(2);secondCell.setCellValue("MN");
+ Cell thirdCell = row.createCell(3);thirdCell.setCellValue("IL");
+
+ int rowNum = row.getRowNum() + 1;
+ String listFormula = new StringBuilder("$B$").append(rowNum).append(":").append("$D$").append(rowNum).toString();
+ DataValidationConstraint formulaListValidation = dataValidationHelper.createFormulaListConstraint(listFormula);
+
+ dataValidation = dataValidationHelper.createValidation(formulaListValidation, cellRangeAddressList);
+ setOtherValidationParameters(dataValidation);
+ sheet.addValidationData(dataValidation);
+ lastKnownNumValidations++;
+
+ offset++;
+ offset++;
+
+ for (int i = 0; i < validationTypes.length; i++) {
+ int validationType = validationTypes[i];
+ offset = offset + 2;
+ final Row row0 = sheet.createRow(offset++);
+ Cell cell_10 = row0.createCell(0);
+ cell_10.setCellValue(validationType==ValidationType.DECIMAL ? "Decimal " : validationType==ValidationType.INTEGER ? "Integer" : "Text Length");
+ offset++;
+ for (int j = 0; j < singleOperandOperatorTypes.length; j++) {
+ int operatorType = singleOperandOperatorTypes[j];
+ final Row row1 = sheet.createRow(offset++);
+
+ //For Integer (> and >=) we add 1 extra cell for validations whose formulae reference other cells.
+ final Row row2 = i==0 && j < 2 ? sheet.createRow(offset++) : null;
+
+ cell_10 = row1.createCell(0);
+ cell_10.setCellValue(XSSFDataValidation.operatorTypeMappings.get(operatorType).toString());
+ Cell cell_11 = row1.createCell(1);
+ Cell cell_21 = row1.createCell(2);
+ Cell cell_22 = i==0 && j < 2 ? row2.createCell(2) : null;
+
+ Cell cell_13 = row1.createCell(3);
+
+
+ cell_13.setCellType(CellType.NUMERIC);
+ cell_13.setCellValue(validationType==ValidationType.DECIMAL ? dvalue.doubleValue() : value.intValue());
+
+
+ //First create value based validation;
+ DataValidationConstraint constraint = dataValidationHelper.createNumericConstraint(validationType,operatorType, value.toString(), null);
+ cellRangeAddressList = new CellRangeAddressList();
+ cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_11.getRowIndex(),cell_11.getRowIndex(),cell_11.getColumnIndex(),cell_11.getColumnIndex()));
+ DataValidation validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
+ setOtherValidationParameters(validation);
+ sheet.addValidationData(validation);
+ assertEquals(++lastKnownNumValidations,((XSSFSheet)sheet).getDataValidations().size());
+
+ //Now create real formula based validation.
+ String formula1 = new CellReference(cell_13.getRowIndex(),cell_13.getColumnIndex()).formatAsString();
+ constraint = dataValidationHelper.createNumericConstraint(validationType,operatorType, formula1, null);
+ if (i==0 && j==0) {
+ cellRangeAddressList = new CellRangeAddressList();
+ cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_21.getRowIndex(), cell_21.getRowIndex(), cell_21.getColumnIndex(), cell_21.getColumnIndex()));
+ validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
+ setOtherValidationParameters(validation);
+ sheet.addValidationData(validation);
+ assertEquals(++lastKnownNumValidations, ((XSSFSheet) sheet).getDataValidations().size());
+
+ cellRangeAddressList = new CellRangeAddressList();
+ cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_22.getRowIndex(), cell_22.getRowIndex(), cell_22.getColumnIndex(), cell_22.getColumnIndex()));
+ validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
+ setOtherValidationParameters( validation);
+ sheet.addValidationData(validation);
+ assertEquals(++lastKnownNumValidations, ((XSSFSheet) sheet).getDataValidations().size());
+ } else if(i==0 && j==1 ){
+ cellRangeAddressList = new CellRangeAddressList();
+ cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_21.getRowIndex(), cell_21.getRowIndex(), cell_21.getColumnIndex(), cell_21.getColumnIndex()));
+ cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_22.getRowIndex(), cell_22.getRowIndex(), cell_22.getColumnIndex(), cell_22.getColumnIndex()));
+ validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
+ setOtherValidationParameters( validation);
+ sheet.addValidationData(validation);
+ assertEquals(++lastKnownNumValidations, ((XSSFSheet) sheet).getDataValidations().size());
+ } else {
+ cellRangeAddressList = new CellRangeAddressList();
+ cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_21.getRowIndex(), cell_21.getRowIndex(), cell_21.getColumnIndex(), cell_21.getColumnIndex()));
+ validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
+ setOtherValidationParameters(validation);
+ sheet.addValidationData(validation);
+ assertEquals(++lastKnownNumValidations, ((XSSFSheet) sheet).getDataValidations().size());
+ }
+ }
+
+ for (int operatorType : doubleOperandOperatorTypes) {
+ final Row row1 = sheet.createRow(offset++);
+
+ cell_10 = row1.createCell(0);
+ cell_10.setCellValue(XSSFDataValidation.operatorTypeMappings.get(operatorType).toString());
+
+ Cell cell_11 = row1.createCell(1);
+ Cell cell_21 = row1.createCell(2);
+
+ Cell cell_13 = row1.createCell(3);
+ Cell cell_14 = row1.createCell(4);
+
+
+ String value1String = validationType==ValidationType.DECIMAL ? dvalue.toString() : value.toString();
+ cell_13.setCellType(CellType.NUMERIC);
+ cell_13.setCellValue(validationType==ValidationType.DECIMAL ? dvalue.doubleValue() : value.intValue());
+
+ String value2String = validationType==ValidationType.DECIMAL ? dvalue2.toString() : value2.toString();
+ cell_14.setCellType(CellType.NUMERIC);
+ cell_14.setCellValue(validationType==ValidationType.DECIMAL ? dvalue2.doubleValue() : value2.intValue());
+
+
+ //First create value based validation;
+ DataValidationConstraint constraint = dataValidationHelper.createNumericConstraint(validationType,operatorType, value1String, value2String);
+ cellRangeAddressList = new CellRangeAddressList();
+ cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_11.getRowIndex(),cell_11.getRowIndex(),cell_11.getColumnIndex(),cell_11.getColumnIndex()));
+ DataValidation validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
+ setOtherValidationParameters(validation);
+ sheet.addValidationData(validation);
+ assertEquals(++lastKnownNumValidations,((XSSFSheet)sheet).getDataValidations().size());
+
+
+ //Now create real formula based validation.
+ String formula1 = new CellReference(cell_13.getRowIndex(),cell_13.getColumnIndex()).formatAsString();
+ String formula2 = new CellReference(cell_14.getRowIndex(),cell_14.getColumnIndex()).formatAsString();
+ constraint = dataValidationHelper.createNumericConstraint(validationType,operatorType, formula1, formula2);
+ cellRangeAddressList = new CellRangeAddressList();
+ cellRangeAddressList.addCellRangeAddress(new CellRangeAddress(cell_21.getRowIndex(),cell_21.getRowIndex(),cell_21.getColumnIndex(),cell_21.getColumnIndex()));
+ validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
+
+ setOtherValidationParameters(validation);
+ sheet.addValidationData(validation);
+ assertEquals(++lastKnownNumValidations,((XSSFSheet)sheet).getDataValidations().size());
+ }
+ }
+
+ XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
+ wb1.close();
+ Sheet sheetAt = wb2.getSheetAt(0);
+ assertEquals(lastKnownNumValidations,((XSSFSheet)sheetAt).getDataValidations().size());
+ wb2.close();
+ }
+
+ protected void setOtherValidationParameters(DataValidation validation) {
+ boolean yesNo = true;
+ validation.setEmptyCellAllowed(yesNo);
+ validation.setShowErrorBox(yesNo);
+ validation.setShowPromptBox(yesNo);
+ validation.createErrorBox("Error Message Title", "Error Message");
+ validation.createPromptBox("Prompt", "Enter some value");
+ validation.setSuppressDropDownArrow(yesNo);
+ }
+
+ @Test
public void test53965() throws Exception {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
assertEquals(1, lst.size());
}
}
-
- @Test
+
+ @Test
public void testDefaultErrorStyle() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
assertEquals(DataValidation.ErrorStyle.STOP, dataValidations.get(0).getErrorStyle());
}
}
-
- @Test
+
+ @Test
public void testSetErrorStyles() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
}
}
- @Test
+ @Test
public void testDefaultAllowBlank() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
}
}
- @Test
+ @Test
public void testSetAllowBlankToFalse() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
}
}
- @Test
+ @Test
public void testSetAllowBlankToTrue() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();