[]
CalcEngine.Functions.Function
Function
• new Function(name, minArgs?, maxArgs?, functionDescription?)
Represents an abstract base class for defining functions.
example
class FactorialFunction extends GC.Spread.CalcEngine.Functions.Function {
constructor () {
super('FACTORIAL', 1, 1, {
description: "Function to calculate the Fibonacci number.",
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 |
The name of the function. |
minArgs? |
number |
- |
maxArgs? |
number |
- |
functionDescription? |
IFunctionDescription |
- |
• maxArgs: number
Represents the maximum number of arguments for the function.
• minArgs: number
Represents the minimum number of arguments for the function.
• name: string
Represents the name of the function.
• typeName: string
Represents the type name string used for supporting serialization.
▸ acceptsArray(argIndex): boolean
Determines whether the function accepts array values for the specified argument.
function
| Name | Type | Description |
|---|---|---|
argIndex |
number |
Index of the argument. |
boolean
true if the function accepts array values for the specified argument; otherwise, false.
▸ acceptsError(argIndex): boolean
Indicates whether the function can process Error values.
function
| Name | Type | Description |
|---|---|---|
argIndex |
number |
Index of the argument. |
boolean
true if the function can process Error values for the specified argument; otherwise, false.
▸ acceptsMissingArgument(argIndex): boolean
Indicates whether the Evaluate method can process missing arguments.
| Name | Type | Description |
|---|---|---|
argIndex |
number |
Index of the argument |
boolean
true if the Evaluate method can process missing arguments; otherwise, false.
▸ acceptsReference(argIndex): boolean
Determines whether the function accepts Reference values for the specified argument.
function
| Name | Type | Description |
|---|---|---|
argIndex |
number |
Index of the argument. |
boolean
true if the function accepts Reference values for the specified argument; otherwise, false.
▸ description(): IFunctionDescription
Returns the description of the function.
function
The description of the function.
▸ evaluate(...args): any
Returns the result of the function applied to the arguments.
| Name | Type | Description |
|---|---|---|
...args |
any |
Arguments for the function evaluation |
any
The result of the function applied to the arguments.
▸ findBranchArgument(test): number
Finds the branch argument.
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 |
The test. |
number
Indicates the index of the argument that would be treated as the branch condition.
▸ findTestArgument(): number
Finds the test argument when this function is branched.
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
Indicates the index of the argument that would be treated as the test condition.
▸ isBranch(): boolean
Gets a value that indicates whether this function is branched by arguments as conditional. Used in conjunction with the findBranchArgument and 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 if this instance is branched; otherwise, false.
▸ isContextSensitive(): boolean
Determines whether the evaluation of the function is dependent on the context in which the evaluation occurs.
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 if the evaluation of the function is dependent on the context; otherwise, false.
▸ isVolatile(): boolean
Determines whether the function is volatile while it is being evaluated. The volatile function is re-evaluated when any cell in the workbook is changed.
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 if the function is volatile; otherwise, false.