SpreadJS Documentation > Developer's Guide > Managing the User Interface > Using Formulas > Using Asynchronous Functions |
You can evaluate a function on the server side with the AsyncFunction and AsyncEvaluateContext classes.
This example defines a custom function that extends the AsyncFunction class and creates a timeout to simulate the server side evaluate.
JavaScript |
Copy Code
|
---|---|
var asum = function () {} //Define a class "ASUM" that extends AsyncFunction asum.prototype = new GcSpread.Sheets.Calc.Functions.AsyncFunction("ASUM", 1, 255); //Set default value to "Loading..." asum.prototype.defaultValue = function () { return "Loading..."; }; //Override the evaluateAsync function asum.prototype.evaluateAsync = function (args, context) { //Use a timeout to simulate the server side evaluate or use an ajax post setTimeout(function () { //Evaluation logic var result = 0; for (var i = 0; i < args.length; i++) { result += args[i]; } result *= 2; //Set the async evaluate result to CalcEngine context.SetAsyncResult(result); }, 2000); } //Or use Ajax post /$.ajax({ //url: '@Url.Action("ASUM", "Home")', //type: "POST", //data: JSON.stringify(args), //contentType: "application/json,charset=UTF-8", //success: function (data) { //context.SetAsyncResult(data.result); //} //}); //Add the ASUM function to spread and set the formula activeSheet.addCustomFunction(new asum()); activeSheet.setValue(0, 0, 5); activeSheet.setValue(0, 1, 15); activeSheet.setFormula(1, 1, "ASUM(A1,B1)"); |