SpreadJS は関数参照 (「eta 縮小ラムダ」とも呼ばれます) をサポートしています。この機能は、不要な抽象化を排除し、可能な場合は省略された構文を使用することで関数を簡素化します。以下のデモは、この機能の使用方法の例を示しています。
関数をカスタム名で定義できます。その後、数式で使用できます。
spread.addCustomName("op","SUM");
sheet.setFormula(0,0,"op(B1:B5)");
関数を LAMBDA 関数のパラメータとして使用できます。
sheet.setFormula(0,0,"=LAMBDA(op,op(123))(SUM)");
sheet.setFormula(1,0,"=MAP(B1:C3,ABS)");
sheet.setFormula(2,0,"=BYCOL(B1:C3,SUM)");
sheet.setFormula(3,0,"=BYROW(B1:C3,SUM)");
sheet.setFormula(4,0,"=MAKEARRAY(3, 3, SUM)");
sheet.setFormula(5,0,"=REDUCE(True, B1:C2, AND)");
sheet.setFormula(6,0,"=SCAN(1, B1:C2, COUNT)");
sheet.setFormula(7,0,"=GROUPBY(B2:B34,D2:D34,SUM)");
名前付きの変数 を関数参照として定義し、それを LET 関数で使用できます。
sheet.setFormula(0,0,"=LET(op,SUM,op(B1:B5))");
カスタム関数を定義して、上記のユースケースに使用できます。
function IsNegativeFunction() {
this.name = "ISNEGATIVE";
this.maxArgs = 1;
this.minArgs = 1;
}
IsNegativeFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
IsNegativeFunction.prototype.evaluate = function (arg) {
if (parseInt(arg)<0) {
return true;
}
return false;
};
spread.addCustomFunction(new IsNegativeFunction());
sheet.setFormula(4, 2, "=MAP(A1:C3,ISNEGATIVE)");