您好,请参考以下代码来实现自定义函数自行比较的逻辑:
function performOperation(a, b, operator) {
switch (operator) {
case "<":
return a < b;
case "<=":
return a <= b;
case ">":
return a > b;
case ">=":
return a >= b;
case "==":
return a === b;
case "!=":
return a !== b;
default:
return false;
}
}
function MyCustomFunction() {
this.name = "COMPARE";
this.maxArgs = 3;
this.minArgs = 3;
}
MyCustomFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
MyCustomFunction.prototype.isContextSensitive = function () {
return true;
};
MyCustomFunction.prototype.evaluate = function (arg, address, number, op) {
let sheet = arg.source.getSheet();
let value = sheet.getRange(address).value();
return performOperation(value, number, op);
};
var customFunction = new MyCustomFunction();
sheet.addCustomFunction(customFunction);
sheet.setValue(0, 0, 321);
sheet.setFormula(0, 1, 'COMPARE("A1", 132, ">=")');
|