Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

You can create validators to validate the user data.
You can display a list of valid values for the user and display an invalid data image if the user types invalid data. Set the highlightInvalidData property to use the red ellipse as an invalid data image. The following image displays a red ellipse since the cell value is invalid.
Image Removed
You can also show an input tip for the user as in the following image.
Image Removed
Select the drop-down button to display the list of valid values for a list type validator.
Image Removed
You can use any of several types of validator methods to create the validation criteria.你可以创建数据验证用来验证用户的数据。


你可以显示用户可以输入的值的下拉列表,并且当用户输入的值不正确时,显示一个图片。

设置 highlightInvalidData 属性来显示一个椭圆来作为数据不可用时的提示图片,如下图所示:

Image Added
你也可以显示一个提示信息,如下图所示
Image Added
你可以使用组合框和下拉列表来显示允许填入的值。
Image Added

你可以使用以下类型的校验器来创建数据验证条件:

The formula validator is valid if the formula condition returns true. The formula list validator uses a range of cells to create the list of valid values.
The ValidationError event occurs when the applied cell value is invalid.
Using Code
This example creates a list of valid values, displays an input tip, and displays an invalid data image if the incorrect value is entered.公式验证器如果返回为 true,则该校验器就是通过的。公式校验器使用一个单元格区域来创建可用值的列表

如果单元格验证不通过,则会触发 ValidationError 事件。


示例代码

以下代码创建一个可用值的列表,显示一个提示信息。当输入错误时,会显示一个错误图片。

JavaScript

Copy Code

spread.options.highlightInvalidData = true;var dv = GC.Spread.Sheets.DataValidation.createListValidator("1,2,3");dv.showInputMessage(true);dv.inputMessage("Value must be 1,2 or 3");dv.inputTitle("tip");activeSheet.setDataValidator(1,1,dv); alert(activeSheet.getDataValidator(1,1).getValidList(activeSheet,1,1));

 

Using Code
This example uses a number validator, displays an input tip, and displays an invalid data image if the incorrect value is entered.示例代码

以下代码使用了数字数据校验,显示了一个输入提示信息,当输入错误时,会显示一个错误图片。

JavaScript

Copy Code

spread.options.highlightInvalidData = true;var dv = GC.Spread.Sheets.DataValidation.createNumberValidator(GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.Between, "5", "20", true);dv.showInputMessage(true);dv.inputMessage("Value must be between 5 and 20.");dv.inputTitle("tip");activeSheet.setDataValidator(1, 1, dv);

 

Using Code
This example uses a text validator, displays an input tip, and displays an invalid data image if the incorrect value is entered.示例代码
以下代码使用了文本长度数据校验,显示了一个输入提示信息,当输入错误时,会显示一个错误图片。

JavaScript

Copy Code

spread.options.highlightInvalidData = true;var dv = GC.Spread.Sheets.DataValidation.createTextLengthValidator(GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.GreaterThan, "4", "20");dv.showInputMessage(true);dv.inputMessage("Number of characters must be greater than 4.");dv.inputTitle("tip");activeSheet.setDataValidator(1, 1, dv);

 

Using Code
This example creates a list of valid values based on a range of cells specified by a formula.示例代码

以下例子通过公式校验器使用一个单元格区域来创建了可能值的列表用于数据校验。

JavaScript

Copy Code

activeSheet.setValue(0, 2, 5);activeSheet.setValue(1, 2, 4);activeSheet.setValue(2, 2, 5); spread.options.highlightInvalidData = true;var dv = GC.Spread.Sheets.DataValidation.createFormulaListValidator("$C$1:$C$3");dv.showInputMessage(true);dv.inputMessage("Pick a value from the list.");dv.inputTitle("tip");activeSheet.setDataValidator(1, 1, dv);var validList = activeSheet.getDataValidator(1, 1).getValidList(sheet, 1, 1);

 

Using Code
This example creates a date validator.示例代码
以下代码使用了日期的数据校验

JavaScript

Copy Code

spread.options.highlightInvalidData = true;var dv = GC.Spread.Sheets.DataValidation.createDateValidator(GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.Between, new Date(2012, 11, 31), new Date(2013, 11, 31));dv.showInputMessage(true);dv.inputMessage("Enter a date between 12/31/2012 and 12/31/2013.");dv.inputTitle("Tip");activeSheet.setDataValidator(1, 1, dv);

 

Using Code
This example creates a formula validator.示例代码
以下代码使用了公式的数据校验。

JavaScript

Copy Code

spread.options.highlightInvalidData = true;//The formula validator is valid if the formula condition returns true.var dv = GC.Spread.Sheets.DataValidation.createFormulaValidator("A1>0");dv.showInputMessage(true); dv.inputMessage("Enter a value greater than 0 in A1.");dv.inputTitle("Tip");activeSheet.setDataValidator(0, 0, dv);

 

See Also另见
setDataValidator Method
Workbook Method