EPPlus 的验证方法如何工作?
How does Validate method of EPPlus work?
我正在使用 EPPlus 作为计算服务器。这是我的代码:
using (var xlp = new ExcelPackage(stream))
{
OfficeOpenXml.ExcelWorksheet Sheet = xlp.Workbook.Worksheets["sheet1"];
//Some code for feeding user data to excel sheet
//...
//We first invoke calculate method to let contraints of data validation get updated.
xlp.Workbook.Calculate();
var v = Sheet.DataValidations["A1"];
if (v != null)
{
switch (v.ValidationType.Type)
{
case OfficeOpenXml.DataValidation.eDataValidationType.DateTime:
OfficeOpenXml.DataValidation.ExcelDataValidationDateTime V1 = (OfficeOpenXml.DataValidation.ExcelDataValidationDateTime)v;
try
{
//this line doesn't do any thing
V1.Validate();
}
catch
{
}
break;
case ...
}
}
}
我在某处读到 Validate() 方法会为无效数据抛出异常。它没有。
我的问题:如何使用 Validate() 方法?
这取决于单元格的内容和验证器操作员的设置:
http://epplus.codeplex.com/SourceControl/latest#EPPlus/DataValidation/ExcelDataValidationOperator.cs
/// <summary>
/// Operator for comparison between Formula and Formula2 in a validation.
/// </summary>
public enum ExcelDataValidationOperator
{
any,
equal,
notEqual,
lessThan,
lessThanOrEqual,
greaterThan,
greaterThanOrEqual,
between,
notBetween
}
ExcelDataValidationDateTime
(最终)派生自 ExcelDataValidationWithFormula<IExcelDataValidationFormulaDateTime>
,其中包含 Validate()
的实现:
public override void Validate()
{
base.Validate();
if (Operator == ExcelDataValidationOperator.between || Operator == ExcelDataValidationOperator.notBetween)
{
if (string.IsNullOrEmpty(Formula2Internal))
{
throw new InvalidOperationException("Validation of " + Address.Address + " failed: Formula2 must be set if operator is 'between' or 'notBetween'");
}
}
}
因此当验证操作为ExcelDataValidationOperator.between
或ExcelDataValidationOperator.notBetween
且未设置Forumla2
时,它将抛出异常(无效)(不要与主要的[=混淆) 18=]).换句话说,当您使用需要两个 values/formulas 进行比较但只设置了一个的操作时,它认为验证器无效。
我正在使用 EPPlus 作为计算服务器。这是我的代码:
using (var xlp = new ExcelPackage(stream))
{
OfficeOpenXml.ExcelWorksheet Sheet = xlp.Workbook.Worksheets["sheet1"];
//Some code for feeding user data to excel sheet
//...
//We first invoke calculate method to let contraints of data validation get updated.
xlp.Workbook.Calculate();
var v = Sheet.DataValidations["A1"];
if (v != null)
{
switch (v.ValidationType.Type)
{
case OfficeOpenXml.DataValidation.eDataValidationType.DateTime:
OfficeOpenXml.DataValidation.ExcelDataValidationDateTime V1 = (OfficeOpenXml.DataValidation.ExcelDataValidationDateTime)v;
try
{
//this line doesn't do any thing
V1.Validate();
}
catch
{
}
break;
case ...
}
}
}
我在某处读到 Validate() 方法会为无效数据抛出异常。它没有。 我的问题:如何使用 Validate() 方法?
这取决于单元格的内容和验证器操作员的设置:
http://epplus.codeplex.com/SourceControl/latest#EPPlus/DataValidation/ExcelDataValidationOperator.cs
/// <summary>
/// Operator for comparison between Formula and Formula2 in a validation.
/// </summary>
public enum ExcelDataValidationOperator
{
any,
equal,
notEqual,
lessThan,
lessThanOrEqual,
greaterThan,
greaterThanOrEqual,
between,
notBetween
}
ExcelDataValidationDateTime
(最终)派生自 ExcelDataValidationWithFormula<IExcelDataValidationFormulaDateTime>
,其中包含 Validate()
的实现:
public override void Validate()
{
base.Validate();
if (Operator == ExcelDataValidationOperator.between || Operator == ExcelDataValidationOperator.notBetween)
{
if (string.IsNullOrEmpty(Formula2Internal))
{
throw new InvalidOperationException("Validation of " + Address.Address + " failed: Formula2 must be set if operator is 'between' or 'notBetween'");
}
}
}
因此当验证操作为ExcelDataValidationOperator.between
或ExcelDataValidationOperator.notBetween
且未设置Forumla2
时,它将抛出异常(无效)(不要与主要的[=混淆) 18=]).换句话说,当您使用需要两个 values/formulas 进行比较但只设置了一个的操作时,它认为验证器无效。