private int operator = -1;
private String[] explicitListOfValues;
+ /**
+ * list literal constructor
+ */
public XSSFDataValidationConstraint(String[] explicitListOfValues) {
if( explicitListOfValues==null || explicitListOfValues.length==0) {
throw new IllegalArgumentException("List validation with explicit values must specify at least one value");
validate();
}
- public XSSFDataValidationConstraint(int validationType,String formula1) {
+ public XSSFDataValidationConstraint(int validationType, String formula1) {
super();
setFormula1(formula1);
this.validationType = validationType;
- public XSSFDataValidationConstraint(int validationType, int operator,String formula1) {
+ public XSSFDataValidationConstraint(int validationType, int operator, String formula1) {
super();
setFormula1(formula1);
this.validationType = validationType;
validate();
}
- public XSSFDataValidationConstraint(int validationType, int operator,String formula1, String formula2) {
+ /**
+ * This is the constructor called using the OOXML raw data. Excel overloads formula1 to also encode explicit value lists,
+ * so this constructor has to check for and parse that syntax.
+ * @param validationType
+ * @param operator
+ * @param formula1 Overloaded: formula1 or list of explicit values
+ * @param formula2 (formula1 is a list of explicit values, this is ignored: use <code>null</code>)
+ */
+ public XSSFDataValidationConstraint(int validationType, int operator, String formula1, String formula2) {
super();
setFormula1(formula1);
setFormula2(formula2);
*/
public void setExplicitListValues(String[] explicitListValues) {
this.explicitListOfValues = explicitListValues;
- if( explicitListOfValues!=null && explicitListOfValues.length > 0 ) {
- StringBuilder builder = new StringBuilder("\"");
+
+ // for OOXML we need to set formula1 to the quoted csv list of values (doesn't appear documented, but that's where Excel puts its lists)
+ // further, Excel has no escaping for commas in explicit lists, so we don't need to worry about that.
+ if ( explicitListOfValues!=null && explicitListOfValues.length > 0 ) {
+ StringBuilder builder = new StringBuilder(QUOTE);
for (int i = 0; i < explicitListValues.length; i++) {
String string = explicitListValues[i];
- if( builder.length() > 1) {
+ if (builder.length() > 1) {
builder.append(LIST_SEPARATOR);
}
builder.append(string);
}
- builder.append("\"");
+ builder.append(QUOTE);
setFormula1(builder.toString());
}
}
import org.junit.Test;
public class TestXSSFDataValidationConstraint {
+ static final int listType = ValidationType.LIST;
+ static final int ignoredType = OperatorType.IGNORED;
// See bug 59719
@Test
- public void listLiteralsQuotesAreStripped() {
- int listType = ValidationType.LIST;
- int ignoredType = OperatorType.IGNORED;
-
+ public void listLiteralsQuotesAreStripped_formulaConstructor() {
+ // literal list, using formula constructor
String literal = "\"one, two, three\"";
String[] expected = new String[] { "one", "two", "three" };
DataValidationConstraint constraint = new XSSFDataValidationConstraint(listType, ignoredType, literal, null);
assertArrayEquals(expected, constraint.getExplicitListValues());
-
+ // Excel and DataValidationConstraint parser ignore (strip) whitespace; quotes should still be intact
+ // FIXME: whitespace wasn't stripped
+ assertEquals(literal, constraint.getFormula1());
+ }
+
+ @Test
+ public void listLiteralsQuotesAreStripped_arrayConstructor() {
+ // literal list, using array constructor
+ String literal = "\"one, two, three\"";
+ String[] expected = new String[] { "one", "two", "three" };
+ DataValidationConstraint constraint = new XSSFDataValidationConstraint(expected);
+ assertArrayEquals(expected, constraint.getExplicitListValues());
+ // Excel and DataValidationConstraint parser ignore (strip) whitespace; quotes should still be intact
+ assertEquals(literal.replace(" ", ""), constraint.getFormula1());
+ }
+
+ @Test
+ public void rangeReference() {
+ // (unnamed range) reference list
String reference = "A1:A5";
- constraint = new XSSFDataValidationConstraint(listType, ignoredType, reference, null);
+ DataValidationConstraint constraint = new XSSFDataValidationConstraint(listType, ignoredType, reference, null);
assertNull(constraint.getExplicitListValues());
-
+ assertEquals("A1:A5", constraint.getFormula1());
+ }
+
+ @Test
+ public void namedRangeReference() {
+ // named range list
String namedRange = "MyNamedRange";
- constraint = new XSSFDataValidationConstraint(listType, ignoredType, namedRange, null);
+ DataValidationConstraint constraint = new XSSFDataValidationConstraint(listType, ignoredType, namedRange, null);
assertNull(constraint.getExplicitListValues());
+ assertEquals("MyNamedRange", constraint.getFormula1());
}
}