2金币
本帖最后由 Wilson.Zhang 于 2024-9-26 11:11 编辑
产品:SpreadJS
版本:17.1.5
调研编号:SJS-26550
Last Review:2024-09-26
当前进展:非问题,需要将自定义函数定义挂载在window全局。
如图,使用自定义函数,自定义函数引用decimal(这只是简单精度计算的例子,实际使用会更复杂)
import { Decimal } from "decimal.js";
/**
* 数组求和
* @param arr {Array}
*/
function Compute_SUM(arr) {
let sum = new Decimal(0);
arr.forEach((v) => {
sum = sum.add(v);
});
if (isValidNumber(sum.toString())) {
return sum;
} else {
return null;
}
}/**
* 判断args是否为有效的数值(非NaN非无穷)
* @param {...any} args
* @returns
*/
function isValidNumber(...args) {
return args.every(
(v) =>
isFinite(v) &&
((typeof v === "string" && v.trim().length) ||
typeof v === "number" ||
v instanceof Decimal)
);
}
function FactorialFunction() {
this.name = "Compute_SUM";
this.maxArgs = 1000;
this.minArgs = 1;
}
FactorialFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
FactorialFunction.prototype.evaluate = function (...arg) {
var result = 1;
var res= Compute_SUM(arg)
if(!res){
return "#VALUE!";
}
return res;
};
this.factorial = new FactorialFunction();
this.spread.getActiveSheet().addCustomFunction(this.factorial)
}
|
|