20金币
- class StatementF extends GC.Spread.CalcEngine.Functions.AsyncFunction {
- cellRequestMap: Map<string, CellRequest>;
- constructor(name: string) {
- super(name, 1, 255);
- this.cellRequestMap = new Map<string, CellRequest>();
- }
- defaultValue(): any {
- return "Loading";
- }
- evaluateAsync(context: GC.Spread.CalcEngine.AsyncEvaluateContext): any {
- const args = arguments;
- const p = arguments[0]
- console.log(p.ctx.source)
- const workbookName = p.ctx.source.getSheet().getParent().name || 'xxxx-when-workbook-new'
- const sheetName = p.ctx.source.getSheet().name()
- if (args.length != 3) {
- context.setAsyncResult("参数异常");
- return;
- }
- console.log(workbookName, sheetName, p.row, p.col)
- let mapKey = workbookName + '-' + sheetName + '-' + p.row + '-' + p.col
- if (this.cellRequestMap.has(mapKey)) {
- const cellMap = this.cellRequestMap.get(mapKey)
- if (cellMap) {
- cellMap.formula.push(args[1])
- cellMap.argStr.push(args[2])
- cellMap.context.push(context)
- }
- } else {
- this.cellRequestMap.set(sheetName + '-' + p.row + '-' + p.col, {
- formula: [args[1]],
- argStr: [args[2]],
- context: [context]
- });
- }
- // 设置默认值,默认值是文本类型,和其他的一起的时候会报#NAME?
- context.setAsyncResult("Loading...");
- }
- }
复制代码- GC.Spread.CalcEngine.Functions.defineGlobalCustomFunction("STATEMENTF", new StatementF('STATEMENTF'))
复制代码 执行公式请求如下
- function handleGetFormula() {
- loading.value = true
- const statementF: StatementF = GC.Spread.CalcEngine.Functions.findGlobalFunction('STATEMENTF')
- let reqList = Array.from(statementF.cellRequestMap.values());
- console.log(reqList)
- reqList.reduce((previousPromise, request) => {
- return previousPromise.then(() => {
- request.formula.forEach((item, index) => {
- apiGetFormulaStatementResult(item, request.argStr[index]).then((res) => {
- console.log(request.context[index])
- request.context[index].setAsyncResult(res)
- })
- })
- });
- }, Promise.resolve()).then(() => {
- loading.value = false;
- // statementF.cellRequestMap.clear();
- }).catch(error => {
- loading.value = false;
- console.error("Error during request sequence:", error);
- });
- }
复制代码 现在遇到两个问题:
清除单元格内容和使用删除键清空单元格的时候,怎么触发cellRequestMap中请求个数的变化
编辑单元格公式中某个参数,如何更新cellRequestMap中对应请求的变化
如果是多条异步函数相加的场景下呢
|
|