您好!
第一张贴图中显示的问题是自定义函数的作用域不对,代码显示自定义函数只添加到spread实例上,没有给spread2添加,所以第二张表格无法识别自定义函数。
下边您给出的代码截图片段没有上下文,我这边实现了一个简单的四舍五入的自定义函数Demo,您参考一下。
- $(document).ready(function () {
- var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 1 });
- initSpread(spread);
- });
- function initSpread(spread) {
- var sheet = spread.getSheet(0);
- sheet.setValue(1, 1, 'Press \'Add a Custom Function\' button');
- sheet.setColumnWidth(1, 225);
- sheet.setColumnWidth(2, 100);
- function FactorialFunction() {
- this.name = "ROUND";
- this.maxArgs = 1;
- this.minArgs = 1;
- }
- FactorialFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
- FactorialFunction.prototype.evaluate = function (arg) {
- var result = 1;
- if (arguments.length === 1 && !isNaN(parseInt(arg))) {
- var res = arg%10;
- if(res < 5){
- return arg - res;
- }else{
- return arg + 10 - res;
- }
- return result;
- }
- return "#VALUE!";
- };
- var factorial = new FactorialFunction();
- $("#addCustomFunction").click(function() {
- sheet.setValue(3, 1, 'Formula');
- sheet.setValue(3, 2, '=ROUND(5)');
- sheet.setValue(4, 1, 'Result');
- sheet.addCustomFunction(factorial);
- sheet.setFormula(4, 2, "=ROUND(5)");
- });
- $("#removeCustomFunction").click(function() {
- sheet.removeCustomFunction("ROUND");
- });
- };
复制代码 |