dvconstraint.java

来自「EXCEL read and write」· Java 代码 · 共 482 行 · 第 1/2 页

JAVA
482
字号
		Double value2 = formula2 == null ? convertDate(expr2, df) : null;		return new DVConstraint(VT.DATE, comparisonOperator, formula1, formula2, value1, value2, null);	}		/**	 * Distinguishes formula expressions from simple value expressions.  This logic is only 	 * required by a few factory methods in this class that create data validation constraints	 * from more or less the same parameters that would have been entered in the Excel UI.  The	 * data validation dialog box uses the convention that formulas begin with '='.  Other methods	 * in this class follow the POI convention (formulas and values are distinct), so the '=' 	 * convention is not used there.	 *  	 * @param textExpr a formula or value expression	 * @return all text after '=' if textExpr begins with '='. Otherwise <code>null</code> if textExpr does not begin with '='	 */	private static String getFormulaFromTextExpression(String textExpr) {		if (textExpr == null) {			return null;		}		if (textExpr.length() < 1) {			throw new IllegalArgumentException("Empty string is not a valid formula/value expression");		}		if (textExpr.charAt(0) == '=') {			return textExpr.substring(1);		}		return null;	}	/**	 * @return <code>null</code> if numberStr is <code>null</code>	 */	private static Double convertNumber(String numberStr) {		if (numberStr == null) {			return null;		}		try {			return new Double(numberStr);		} catch (NumberFormatException e) {			throw new RuntimeException("The supplied text '" + numberStr 					+ "' could not be parsed as a number");		}	}	/**	 * @return <code>null</code> if timeStr is <code>null</code>	 */	private static Double convertTime(String timeStr) {		if (timeStr == null) {			return null;		}		return new Double(HSSFDateUtil.convertTime(timeStr));	}	/**	 * @param dateFormat pass <code>null</code> for default YYYYMMDD	 * @return <code>null</code> if timeStr is <code>null</code>	 */	private static Double convertDate(String dateStr, SimpleDateFormat dateFormat) {		if (dateStr == null) {			return null;		}		Date dateVal; 		if (dateFormat == null) {			dateVal = HSSFDateUtil.parseYYYYMMDDDate(dateStr);		} else {			try {				dateVal = dateFormat.parse(dateStr);			} catch (ParseException e) {				throw new RuntimeException("Failed to parse date '" + dateStr 						+ "' using specified format '" + dateFormat + "'", e);			}		}		return new Double(HSSFDateUtil.getExcelDate(dateVal));	}	public static DVConstraint createCustomFormulaConstraint(String formula) {		if (formula == null) {			throw new IllegalArgumentException("formula must be supplied");		}		return new DVConstraint(VT.FORMULA, OperatorType.IGNORED, formula, null, null, null, null);	}		/**	 * @return both parsed formulas (for expression 1 and 2). 	 */	/* package */ FormulaPair createFormulas(HSSFWorkbook workbook) {		Ptg[] formula1;		Ptg[] formula2;		if (isListValidationType()) {			formula1 = createListFormula(workbook);			formula2 = Ptg.EMPTY_PTG_ARRAY;		} else {			formula1 = convertDoubleFormula(_formula1, _value1, workbook);			formula2 = convertDoubleFormula(_formula2, _value2, workbook);		}		return new FormulaPair(formula1, formula2);	}	private Ptg[] createListFormula(HSSFWorkbook workbook) {		if (_explicitListValues == null) {			// formula is parsed with slightly different RVA rules: (root node type must be 'reference')			return HSSFFormulaParser.parse(_formula1, workbook, FormulaType.DATAVALIDATION_LIST);			// To do: Excel places restrictions on the available operations within a list formula.			// Some things like union and intersection are not allowed.		}		// explicit list was provided		StringBuffer sb = new StringBuffer(_explicitListValues.length * 16);		for (int i = 0; i < _explicitListValues.length; i++) {			if (i > 0) {				sb.append('\0'); // list delimiter is the nul char			}			sb.append(_explicitListValues[i]);				}		return new Ptg[] { new StringPtg(sb.toString()), };	}	/**	 * @return The parsed token array representing the formula or value specified. 	 * Empty array if both formula and value are <code>null</code>	 */	private static Ptg[] convertDoubleFormula(String formula, Double value, HSSFWorkbook workbook) {		if (formula == null) {			if (value == null) {				return Ptg.EMPTY_PTG_ARRAY;			}			return new Ptg[] { new NumberPtg(value.doubleValue()), };		}		if (value != null) {			throw new IllegalStateException("Both formula and value cannot be present");		}		return HSSFFormulaParser.parse(formula, workbook);	}			/**	 * @return data validation type of this constraint	 * @see ValidationType	 */	public int getValidationType() {		return _validationType;	}	/**	 * Convenience method	 * @return <code>true</code> if this constraint is a 'list' validation	 */	public boolean isListValidationType() {		return _validationType == VT.LIST;	}	/**	 * Convenience method	 * @return <code>true</code> if this constraint is a 'list' validation with explicit values	 */	public boolean isExplicitList() {		return _validationType == VT.LIST && _explicitListValues != null;	}	/**	 * @return the operator used for this constraint	 * @see OperatorType	 */	public int getOperator() {		return _operator;	}	/**	 * Sets the comparison operator for this constraint	 * @see OperatorType	 */	public void setOperator(int operator) {		_operator = operator;	}		public String[] getExplicitListValues() {		return _explicitListValues;	}	public void setExplicitListValues(String[] explicitListValues) {		if (_validationType != VT.LIST) {			throw new RuntimeException("Cannot setExplicitListValues on non-list constraint");		}		_formula1 = null;		_explicitListValues = explicitListValues;	}	/**	 * @return the formula for expression 1. May be <code>null</code>	 */	public String getFormula1() {		return _formula1;	}	/**	 * Sets a formula for expression 1.	 */	public void setFormula1(String formula1) {		_value1 = null;		_explicitListValues = null;		_formula1 = formula1;	}	/**	 * @return the formula for expression 2. May be <code>null</code>	 */	public String getFormula2() {		return _formula2;	}	/**	 * Sets a formula for expression 2.	 */	public void setFormula2(String formula2) {		_value2 = null;		_formula2 = formula2;	}	/**	 * @return the numeric value for expression 1. May be <code>null</code>	 */	public Double getValue1() {		return _value1;	}	/**	 * Sets a numeric value for expression 1.	 */	public void setValue1(double value1) {		_formula1 = null;		_value1 = new Double(value1);	}	/**	 * @return the numeric value for expression 2. May be <code>null</code>	 */	public Double getValue2() {		return _value2;	}	/**	 * Sets a numeric value for expression 2.	 */	public void setValue2(double value2) {		_formula2 = null;		_value2 = new Double(value2);	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?