您好,SpreadJS单元格校验原生并不支持多条件校验,不过您的这个校验规则,实际上可以作为非空校验+规则校验。请参考一下学习指南:
https://demo.grapecity.com.cn/Sp ... customDataValidator
其中有一个ignoreBlank的参数,可以解决非空验证的问题。
当然,多条件校验也可以实现,但实际上可以通过一些思路来实现,参考以下代码:
- $(document).ready(function() {
- var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount : 1 });
- var spreadNS = GC.Spread.Sheets;
- var validators = [{
- validator : "A2<=A1",
- error_message: "error_1: A2<=A1"
- },{
- validator : "A3<=A1",
- error_message: "error_2: A3<=A1"
- },{
- validator : "A1=A4+A5+A6",
- error_message: "error_3: A1=A4+A5+A6"
- },{
- validator : "A1=B1+C1-D1",
- error_message: "error_4: A1=B1+C1-D1"
- },{
- validator : "A1<>0",
- error_message: "error_5: A1<>0"
- }];
- validators = [{
- validator: "A1>B1",
- error_message: "error_1: 得分应小于标准分!"
- }, {
- validator: "A1>0",
- error_message: "error_1: 得分不能为负数!"
- }];
- var current_sheet = spread.getActiveSheet();
- current_sheet.setValue(0, 0, 15);
- current_sheet.setValue(1, 0, 1);
- current_sheet.setValue(2, 0, 15);
- current_sheet.setValue(3, 0, 4);
- current_sheet.setValue(4, 0, 5);
- current_sheet.setValue(5, 0, 6);
- current_sheet.setValue(0, 1, 9);
- current_sheet.setValue(0, 2, 8);
- current_sheet.setValue(0, 3, 2);
- var rowIndex = 1;
- var colIndex = 1;
- var cell = current_sheet.getCell(rowIndex, colIndex);
- $("#run").click(function () {
- validators.some(function (obj) {
- let tmpValidator = new GC.Spread.Sheets.DataValidation.createFormulaValidator(obj.validator);
- tmpValidator.errorTitle("error");
- tmpValidator.errorMessage(obj.error_message);
- tmpValidator.ignoreBlank(false);
- current_sheet.setDataValidator(rowIndex, colIndex, tmpValidator);
- let vad = current_sheet.getDataValidator(rowIndex, colIndex);
- if( vad.isValid( current_sheet, rowIndex, colIndex, parseInt(cell.value())) ){
- cell.backColor("green");
- }else{
- cell.backColor("red");
- alert("error:" + obj.error_message);
- return true;
- }
- current_sheet.setDataValidator(rowIndex, colIndex, null);
- });
- });
- });
复制代码 |