XSSFDataValidation(CellRangeAddressList regions,CTDataValidation ctDataValidation) {
- super();
- this.validationConstraint = getConstraint(ctDataValidation);
- this.ctDdataValidation = ctDataValidation;
- this.regions = regions;
- this.ctDdataValidation.setErrorStyle(STDataValidationErrorStyle.STOP);
- this.ctDdataValidation.setAllowBlank(true);
+ this(getConstraint(ctDataValidation), regions, ctDataValidation);
}
public XSSFDataValidation(XSSFDataValidationConstraint constraint,CellRangeAddressList regions,CTDataValidation ctDataValidation) {
this.validationConstraint = constraint;
this.ctDdataValidation = ctDataValidation;
this.regions = regions;
- this.ctDdataValidation.setErrorStyle(STDataValidationErrorStyle.STOP);
- this.ctDdataValidation.setAllowBlank(true);
}
CTDataValidation getCtDdataValidation() {
return builder.toString();
}
- private XSSFDataValidationConstraint getConstraint(CTDataValidation ctDataValidation) {
+ private static XSSFDataValidationConstraint getConstraint(CTDataValidation ctDataValidation) {
XSSFDataValidationConstraint constraint = null;
String formula1 = ctDataValidation.getFormula1();
String formula2 = ctDataValidation.getFormula2();
--- /dev/null
+package org.apache.poi.xssf.streaming;\r
+\r
+import java.util.List;\r
+\r
+import org.apache.poi.ss.usermodel.BaseTestDataValidation;\r
+import org.apache.poi.ss.usermodel.DataValidation;\r
+import org.apache.poi.ss.usermodel.DataValidationConstraint;\r
+import org.apache.poi.ss.usermodel.DataValidationHelper;\r
+import org.apache.poi.ss.usermodel.Sheet;\r
+import org.apache.poi.ss.util.CellRangeAddressList;\r
+import org.apache.poi.xssf.SXSSFITestDataProvider;\r
+\r
+public class TestSXSSFDataValidation extends BaseTestDataValidation {\r
+\r
+ public TestSXSSFDataValidation(){\r
+ super(SXSSFITestDataProvider.instance);\r
+ }\r
+\r
+ public void test53965() throws Exception {\r
+ SXSSFWorkbook wb = new SXSSFWorkbook();\r
+ try {\r
+ Sheet sheet = wb.createSheet();\r
+ List<? extends DataValidation> lst = sheet.getDataValidations(); //<-- works\r
+ assertEquals(0, lst.size());\r
+ \r
+ //create the cell that will have the validation applied\r
+ sheet.createRow(0).createCell(0);\r
+ \r
+ DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();\r
+ DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("SUM($A$1:$A$1) <= 3500");\r
+ CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);\r
+ DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);\r
+ sheet.addValidationData(validation);\r
+ \r
+ // this line caused XmlValueOutOfRangeException , see Bugzilla 3965\r
+ lst = sheet.getDataValidations();\r
+ assertEquals(1, lst.size());\r
+ } finally {\r
+ wb.close();\r
+ }\r
+ }\r
+}\r
==================================================================== */
package org.apache.poi.xssf.usermodel;
-import java.io.FileOutputStream;
+import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
-import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.usermodel.BaseTestDataValidation;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.DataValidation;
+import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType;
import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType;
+import org.apache.poi.ss.usermodel.DataValidationHelper;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.ss.util.CellReference;
}
public void test53965() throws Exception {
+ XSSFWorkbook wb = new XSSFWorkbook();
+ try {
+ XSSFSheet sheet = wb.createSheet();
+ List<XSSFDataValidation> lst = sheet.getDataValidations(); //<-- works
+ assertEquals(0, lst.size());
+
+ //create the cell that will have the validation applied
+ sheet.createRow(0).createCell(0);
+
+ DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
+ DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("SUM($A$1:$A$1) <= 3500");
+ CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
+ DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
+ sheet.addValidationData(validation);
+
+ // this line caused XmlValueOutOfRangeException , see Bugzilla 3965
+ lst = sheet.getDataValidations();
+ assertEquals(1, lst.size());
+ } finally {
+ wb.close();
+ }
+ }
+
+ public void testDefaultAllowBlank() throws IOException {
+ XSSFWorkbook wb = new XSSFWorkbook();
+ try {
+ XSSFSheet sheet = wb.createSheet();
+
+ final XSSFDataValidation validation = createValidation(sheet);
+ sheet.addValidationData(validation);
+
+ final List<XSSFDataValidation> dataValidations = sheet.getDataValidations();
+ assertEquals(true, dataValidations.get(0).getCtDdataValidation().getAllowBlank());
+ } finally {
+ wb.close();
+ }
+ }
+ public void testSetAllowBlankToFalse() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
- XSSFSheet sheet = wb.createSheet();
- List<XSSFDataValidation> lst = sheet.getDataValidations(); //<-- works
- assertEquals(0, lst.size());
+ try {
+ XSSFSheet sheet = wb.createSheet();
+
+ final XSSFDataValidation validation = createValidation(sheet);
+ validation.getCtDdataValidation().setAllowBlank(false);
+
+ sheet.addValidationData(validation);
+
+ final List<XSSFDataValidation> dataValidations = sheet.getDataValidations();
+ assertEquals(false, dataValidations.get(0).getCtDdataValidation().getAllowBlank());
+ } finally {
+ wb.close();
+ }
+ }
+
+ public void testSetAllowBlankToTrue() throws IOException {
+ XSSFWorkbook wb = new XSSFWorkbook();
+ try {
+ XSSFSheet sheet = wb.createSheet();
+
+ final XSSFDataValidation validation = createValidation(sheet);
+ validation.getCtDdataValidation().setAllowBlank(true);
+
+ sheet.addValidationData(validation);
+
+ final List<XSSFDataValidation> dataValidations = sheet.getDataValidations();
+ assertEquals(true, dataValidations.get(0).getCtDdataValidation().getAllowBlank());
+ } finally {
+ wb.close();
+ }
+ }
+ private XSSFDataValidation createValidation(XSSFSheet sheet) {
//create the cell that will have the validation applied
- sheet.createRow(0).createCell(0);
+ final Row row = sheet.createRow(0);
+ row.createCell(0);
DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
- DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("SUM($A$1:$A$1) <= 3500");
- CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
- DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
- sheet.addValidationData(validation);
- // this line caused XmlValueOutOfRangeException , see Bugzilla 3965
- lst = sheet.getDataValidations();
- assertEquals(1, lst.size());
+ DataValidationConstraint constraint = dataValidationHelper.createCustomConstraint("true");
+ final XSSFDataValidation validation = (XSSFDataValidation) dataValidationHelper.createValidation(constraint, new CellRangeAddressList(0, 0, 0, 0));
+ return validation;
}
}