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

QQ登录

只需一步,快速开始

小仙

注册会员

12

主题

21

帖子

75

积分

注册会员

积分
75
小仙
注册会员   /  发表于:2025-4-11 11:24  /   查看:133  /  回复:5
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.ctx.source)
  14.     const workbookName = p.ctx.source.getSheet().getParent().name || 'xxxx-when-workbook-new'
  15.     const sheetName = p.ctx.source.getSheet().name()
  16.     if (args.length != 3) {
  17.       context.setAsyncResult("参数异常");
  18.       return;
  19.     }
  20.     console.log(workbookName, sheetName, p.row, p.col)
  21.     let mapKey = workbookName + '-' + sheetName + '-' + p.row + '-' + p.col
  22.     if (this.cellRequestMap.has(mapKey)) {
  23.       const cellMap = this.cellRequestMap.get(mapKey)
  24.       if (cellMap) {
  25.         cellMap.formula.push(args[1])
  26.         cellMap.argStr.push(args[2])
  27.         cellMap.context.push(context)
  28.       }
  29.     } else {
  30.       this.cellRequestMap.set(sheetName + '-' + p.row + '-' + p.col, {
  31.         formula: [args[1]],
  32.         argStr: [args[2]],
  33.         context: [context]
  34.       });
  35.     }
  36.     // 设置默认值,默认值是文本类型,和其他的一起的时候会报#NAME?
  37.     context.setAsyncResult("Loading...");
  38.   }
  39. }
复制代码
  1. GC.Spread.CalcEngine.Functions.defineGlobalCustomFunction("STATEMENTF", new StatementF('STATEMENTF'))
复制代码
执行公式请求如下

  1. function handleGetFormula() {
  2.   loading.value = true
  3.   const statementF: StatementF = GC.Spread.CalcEngine.Functions.findGlobalFunction('STATEMENTF')
  4.   let reqList = Array.from(statementF.cellRequestMap.values());
  5.   console.log(reqList)
  6.   reqList.reduce((previousPromise, request) => {
  7.     return previousPromise.then(() => {
  8.       request.formula.forEach((item, index) => {
  9.         apiGetFormulaStatementResult(item, request.argStr[index]).then((res) => {
  10.           console.log(request.context[index])
  11.           request.context[index].setAsyncResult(res)
  12.         })
  13.       })
  14.     });
  15.   }, Promise.resolve()).then(() => {
  16.     loading.value = false;
  17.     // statementF.cellRequestMap.clear();
  18.   }).catch(error => {
  19.     loading.value = false;
  20.     console.error("Error during request sequence:", error);
  21.   });
  22. }
复制代码
现在遇到两个问题:
清除单元格内容和使用删除键清空单元格的时候,怎么触发cellRequestMap中请求个数的变化
编辑单元格公式中某个参数,如何更新cellRequestMap中对应请求的变化
如果是多条异步函数相加的场景下呢

5 个回复

倒序浏览
Matthew.Xue
超级版主   /  发表于:2025-4-11 14:40:51
沙发
请问您讲的“触发CellRequestMap中请求个数的变化”具体是指什么?
编辑公式后,会自动触发evaluateAsync方法,您可以在该方法中动态更新CellRequestMap。
多个异步函数相加是没有问题的,请参考我在另一个问题中给您的回复。
回复 使用道具 举报
小仙
注册会员   /  发表于:2025-4-11 14:59:58
板凳
Matthew.Xue 发表于 2025-4-11 14:40
请问您讲的“触发CellRequestMap中请求个数的变化”具体是指什么?
编辑公式后,会自动触发evaluateAsync ...

就是我这个单元格中设置了公式,然后会把请求收集起来,现在我清除了单元格内容,但是map中的请求没被删除
回复 使用道具 举报
Matthew.Xue
超级版主   /  发表于:2025-4-11 15:44:47
地板
小仙 发表于 2025-4-11 14:59
就是我这个单元格中设置了公式,然后会把请求收集起来,现在我清除了单元格内容,但是map中的请求没被删 ...

您好,看到您最近提了比较多关于自定义异步函数的问题,论坛沟通的效果不是很好,咱们可以约一个会议讨论一下,您看下周什么时间方便,我来定会议?
回复 使用道具 举报
小仙
注册会员   /  发表于:2025-4-14 09:04:45
5#
Matthew.Xue 发表于 2025-4-11 15:44
您好,看到您最近提了比较多关于自定义异步函数的问题,论坛沟通的效果不是很好,咱们可以约一个会议讨论 ...

不用了哈,公司这两周应该会跟贵司讨论授权的事,我等等吧
回复 使用道具 举报
Matthew.Xue
超级版主   /  发表于:2025-4-14 11:48:25
6#
小仙 发表于 2025-4-14 09:04
不用了哈,公司这两周应该会跟贵司讨论授权的事,我等等吧

好的,是否方便问一下您是属于哪家公司呢?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部