请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册

QQ登录

只需一步,快速开始

小仙

注册会员

12

主题

21

帖子

75

积分

注册会员

积分
75
小仙
注册会员   /  发表于:2025-4-11 10:41  /   查看:84  /  回复:2
20金币
  1. class StatementF extends GC.Spread.CalcEngine.Functions.AsyncFunction {
  2.   cellRequestMap: Map<string, CellRequest>;

  3.   constructor(name: string) {
  4.     super(name, 1, 255);
  5.     this.cellRequestMap = new Map<string, CellRequest>();
  6.   }

  7.   defaultValue(): any {
  8.     return "Loading";
  9.   }

  10.   evaluateAsync(context: GC.Spread.CalcEngine.AsyncEvaluateContext): any {
  11.     const args = arguments;
  12.     const p = arguments[0]
  13.     console.log(p)
  14.     const sheetName = p.ctx.source.getSheet().name()
  15.     if (args.length != 3) {
  16.       context.setAsyncResult("参数异常");
  17.       return;
  18.     }
  19.     console.log(sheetName, p.row, p.col)
  20.     let mapKey = sheetName + '-' + p.row + '-' + p.col
  21.     if (this.cellRequestMap.has(mapKey)) {
  22.       const cellMap = this.cellRequestMap.get(mapKey)
  23.       if (cellMap) {
  24.         cellMap.formula.push(args[1])
  25.         cellMap.argStr.push(args[2])
  26.         cellMap.context.push(context)
  27.       }
  28.     } else {
  29.       this.cellRequestMap.set(sheetName + '-' + p.row + '-' + p.col, {
  30.         formula: [args[1]],
  31.         argStr: [args[2]],
  32.         context: [context]
  33.       });
  34.     }
  35.     // 设置默认值,默认值是文本类型,和其他的一起的时候会报#NAME?
  36.     context.setAsyncResult("Loading...");
  37.   }
  38. }
复制代码
如代码,我定义了一个异步函数,然后我使用

  1. spread.addCustomFunction(StatementFun)
复制代码
添加后,在页面中不生效,显示为#NAME?,这个异步函数不也是继承自定义函数吗,为什么添加不生效

2 个回复

倒序浏览
Wilson.Zhang
超级版主   /  发表于:2025-4-11 13:34:38
沙发
您好!发现您在论坛发布的另一个帖子中的代码片段注册自定义公式的方式正确,如下图所示:
1744349580867.png543883361.png

由于帖子发布时间晚于本贴,且问题相关,所以请教下您本贴中所述问题是否已解决?

如未解决,如上图所示,定义自定义函数后,应该对Workbook或Worksheet或全局注册自定义函数实例,即StatementFun实例,需要new StatementFunc()。
回复 使用道具 举报
Lynn.Dou讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:4 天前
板凳
请问楼主问题解决了吗?如仍有疑问,请提供所需资料,以便问题的进一步调研
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部