本帖最后由 Fiooona 于 2020-5-23 22:38 编辑
用正则实现了一个自定义函数,给单元格添加这个自定义的公式验证器,请查看附件:
- function RegExFunction() {
- this.name = "REGEX";
- this.maxArgs = 2;
- this.minArgs = 2;
- this.typeName = "RegExFunction";
- }
- RegExFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
- RegExFunction.prototype.isContextSensitive = function () {
- return true;
- }
- RegExFunction.prototype.evaluate = function () {
- debugger
- var context = arguments[0], value = context.source.getValue(context.row, context.column);
- var reg = new RegExp(arguments[1]);
- if (value) {
- return reg.test(value.toString());
- }
- return false;
- }
- sheet.addCustomFunction(new RegExFunction());
- sheet.setValue(19, 0, "自定义公式数据验证");
- sheet.setValue(19, 1, "最多四位小数的数字");
- 1
- var dv8 = GC.Spread.Sheets.DataValidation.createFormulaValidator('REGEX("^([1-9]\\d{0,15}|0)(\\.\\d{1,4})?$")');
复制代码
|