找回密码
 立即注册

QQ登录

只需一步,快速开始

KevinChen 讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2020-3-3 14:26  /   查看:2725  /  回复:0
背景:客户希望在自定义函数中实现让终端用户自行拼接参数来实现函数实时计算,
因此给出此方案来解决这一应用场景。

核心代码(完整示例见附件):

  1. function TestFunction() {
  2.             this.name = "TEST";
  3.             this.maxArgs = 100;
  4.             this.minArgs = 1;
  5.         }
  6.         TestFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
  7.         TestFunction.prototype.evaluate = function (arg1) {
  8.                         console.log(arguments);
  9.             var result = arg1;
  10.                         if(arguments.length > 1){
  11.                                 var args = getArgsArr(result);
  12.                                 if(args && args.length>0){
  13.                                        
  14.                                         for(let i=0; i<args.length; i++){
  15.                                                 if(arguments[i+1]){
  16.                                                         let _arg = arguments[i+1];
  17.                                                         // 如果参数是引用类型,获取引用区域
  18.                                                         if(_arg.getRow){
  19.                                                                 let row = _arg.getRow();
  20.                                                                 let col = _arg.getColumn();
  21.                                                                 let rowCount = _arg.getRowCount();
  22.                                                                 let colCount = _arg.getColumnCount();
  23.                                                                 let val = 1;
  24.                                                                 // 这里用累乘演示
  25.                                                                 for(let r = row; r < row+rowCount; r++){
  26.                                                                         for(let c = col; c < col+colCount; c++){
  27.                                                                                 val *= sheet.getValue(r,c);
  28.                                                                         }
  29.                                                                 }
  30.                                                                 result = result.replace(args[i],val);
  31.                                                         }else{
  32.                                                                 // 如果参数是表达式,直接拼接结果即可
  33.                                                                 result = result.replace(args[i],_arg);
  34.                                                         }
  35.                                                 }
  36.                                         }
  37.                                 }
  38.                         }
  39.                         return result;
  40.         };
  41.                 // 重写这个方法,返回true,就可以拿到参数的引用区域
  42.                 TestFunction.prototype.acceptsReference = function () {
  43.             return true;
  44.         };
  45.         var test = new TestFunction();
复制代码


自定义函数拼接引用区域.html

3.35 KB, 下载次数: 31

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部