[]
CalcEngine.Functions.Function
Function
• new Function(name, minArgs?, maxArgs?, functionDescription?)
表示用于定义函数的抽象基类。
example
class FactorialFunction extends GC.Spread.CalcEngine.Functions.Function {
constructor () {
super('FACTORIAL', 1, 1, {
description: "计算斐波那契数的函数。",
parameters: [{ name: 'n' }]
});
}
evaluate (n) {
var fib = [0, 1];
for (var i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}
}
spread.addCustomFunction(new FactorialFunction());
spread.getActiveSheet().setFormula(0, 0, '=FACTORIAL(10)');
| Name | Type | Description |
|---|---|---|
name |
string |
函数名称。 |
minArgs? |
number |
- |
maxArgs? |
number |
- |
functionDescription? |
IFunctionDescription |
- |
• maxArgs: number
表示函数的最大参数数量。
• minArgs: number
表示函数的最小参数数量。
• name: string
表示函数的名称。
• typeName: string
表示用于支持序列化的类型名称字符串。
▸ acceptsArray(argIndex): boolean
确定函数是否接受指定参数的数组值。
function
| Name | Type | Description |
|---|---|---|
argIndex |
number |
参数的索引。 |
boolean
如果函数接受指定参数的数组值,则为true;否则为false。
▸ acceptsError(argIndex): boolean
指示函数是否可以处理错误值。
function
| Name | Type | Description |
|---|---|---|
argIndex |
number |
参数的索引。 |
boolean
如果函数可以处理指定参数的错误值,则为true;否则为false。
▸ acceptsMissingArgument(argIndex): boolean
指示Evaluate方法是否可以处理缺失的参数。
| Name | Type | Description |
|---|---|---|
argIndex |
number |
参数的索引 |
boolean
如果Evaluate方法可以处理缺失的参数,则为true;否则为false。
▸ acceptsReference(argIndex): boolean
确定函数是否接受指定参数的引用值。
function
| Name | Type | Description |
|---|---|---|
argIndex |
number |
参数的索引。 |
boolean
如果函数接受指定参数的引用值,则为true;否则为false。
▸ description(): IFunctionDescription
返回函数的描述。
function
函数的描述。
▸ evaluate(...args): any
返回函数应用于参数的结果。
| Name | Type | Description |
|---|---|---|
...args |
any |
函数求值的参数 |
any
函数应用于参数的结果。
▸ findBranchArgument(test): number
查找分支参数。
example
function EqualsFunction() {
this.name = 'Equals';
this.maxArgs = 3;
this.minArgs = 3;
}
EqualsFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
EqualsFunction.prototype.evaluate = function(logicalTest, valueIfTrue, valueIfFalse) {
return logicalTest ? valueIfTrue : valueIfFalse;
}
EqualsFunction.prototype.isBranch = function() {
return true;
}
EqualsFunction.prototype.findTestArgument = function() {
return 0;
}
EqualsFunction.prototype.findBranchArgument = function(logicalTestResult) {
if (logicalTestResult === true) {
return 1;
}
return 2;
}
var equalsFunction = new EqualsFunction();
var spread = GC.Spread.Sheets.findControl("ss") || GC.Spread.Sheets.findControl("sampleDiv");
spread.addCustomFunction(equalsFunction);
| Name | Type | Description |
|---|---|---|
test |
any |
测试条件。 |
number
表示将被视为分支条件的参数的索引。
▸ findTestArgument(): number
当此函数有分支时查找测试参数。
example
function EqualsFunction() {
this.name = 'Equals';
this.maxArgs = 3;
this.minArgs = 3;
}
EqualsFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
EqualsFunction.prototype.evaluate = function(logicalTest, valueIfTrue, valueIfFalse) {
return logicalTest ? valueIfTrue : valueIfFalse;
}
EqualsFunction.prototype.isBranch = function() {
return true;
}
EqualsFunction.prototype.findTestArgument = function() {
return 0;
}
EqualsFunction.prototype.findBranchArgument = function(logicalTestResult) {
if (logicalTestResult === true) {
return 1;
}
return 2;
}
var equalsFunction = new EqualsFunction();
var spread = GC.Spread.Sheets.findControl("ss") || GC.Spread.Sheets.findControl("sampleDiv");
spread.addCustomFunction(equalsFunction);
number
表示将被视为测试条件的参数的索引。
▸ isBranch(): boolean
获取一个值,该值指示此函数是否按条件参数进行分支。 与findBranchArgument和findTestArgument配合使用。
example
function EqualsFunction() {
this.name = 'Equals';
this.maxArgs = 3;
this.minArgs = 3;
}
EqualsFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
EqualsFunction.prototype.evaluate = function(logicalTest, valueIfTrue, valueIfFalse) {
return logicalTest ? valueIfTrue : valueIfFalse;
}
EqualsFunction.prototype.isBranch = function() {
return true;
}
EqualsFunction.prototype.findTestArgument = function() {
return 0;
}
EqualsFunction.prototype.findBranchArgument = function(logicalTestResult) {
if (logicalTestResult === true) {
return 1;
}
return 2;
}
var equalsFunction = new EqualsFunction();
var spread = GC.Spread.Sheets.findControl("ss") || GC.Spread.Sheets.findControl("sampleDiv");
spread.addCustomFunction(equalsFunction);
boolean
如果此实例有分支,则为true;否则为false。
▸ isContextSensitive(): boolean
确定函数的求值是否依赖于求值发生的上下文。
example
class ContextFunction extends GC.Spread.CalcEngine.Functions.Function {
constructor () {
super('CONTEXT', 0, 0);
}
isContextSensitive () {
return true;
}
evaluate (context) {
return context.row + "/" + context.column;
}
}
spread.addCustomFunction(new ContextFunction());
spread.getActiveSheet().setFormula(0, 0, '=CONTEXT()');
boolean
如果函数的求值依赖于上下文,则为true;否则为false。
▸ isVolatile(): boolean
确定函数在求值过程中是否易变。 易变性函数会在工作簿中的任何单元格发生更改时重新计算。
example
class RandColorFunction extends GC.Spread.CalcEngine.Functions.Function {
constructor () {
super('RANDCOLOR', 0, 0);
}
isVolatile () {
return true;
}
evaluate () {
return "#" + Math.floor(Math.random() * 16777215).toString(16);
}
}
spread.addCustomFunction(new RandColorFunction());
spread.getActiveSheet().setFormula(0, 0, '=RANDCOLOR()');
boolean
如果函数易变,则为true;否则为false。