From dc4b12a466f5fdcdef02032ba04deaa307af7f81 Mon Sep 17 00:00:00 2001 From: Shashank Agarwal <53386582+Corbe30@users.noreply.github.com> Date: Thu, 9 Jan 2025 10:10:54 +0000 Subject: [PATCH] added: batch API, calculateFormula API improvements --- packages/core/src/api/sheet.ts | 35 ++++++++++++++++--- packages/react/src/components/Workbook/api.ts | 19 +++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/packages/core/src/api/sheet.ts b/packages/core/src/api/sheet.ts index 0e779c31..65356949 100644 --- a/packages/core/src/api/sheet.ts +++ b/packages/core/src/api/sheet.ts @@ -2,7 +2,7 @@ import _ from "lodash"; import { v4 as uuidv4 } from "uuid"; import { dataToCelldata, getSheet } from "./common"; import { Context } from "../context"; -import { CellMatrix, CellWithRowAndCol, Sheet } from "../types"; +import { CellMatrix, CellWithRowAndCol, Sheet, SingleRange } from "../types"; import { getSheetIndex } from "../utils"; import { api, execfunction, insertUpdateFunctionGroup, locale } from ".."; @@ -137,11 +137,24 @@ export function copySheet(ctx: Context, sheetId: string) { api.setSheetOrder(ctx, sheetOrderList); } -export function calculateSheetFromula(ctx: Context, id: string) { +function calculateSheetFromula(ctx: Context, id: string, range?: SingleRange) { const index = getSheetIndex(ctx, id) as number; if (!ctx.luckysheetfile[index].data) return; - for (let r = 0; r < ctx.luckysheetfile[index].data!.length; r += 1) { - for (let c = 0; c < ctx.luckysheetfile[index].data![r].length; c += 1) { + + if (!range) { + range = { + row: [0, ctx.luckysheetfile[index].data!.length - 1], + column: [0, ctx.luckysheetfile[index].data![0].length - 1], + }; + } + const rowCount = range.row[1] - range.row[0] + 1; + const columnCount = range.column[1] - range.column[0] + 1; + + for (let _r = 0; _r < rowCount; _r += 1) { + for (let _c = 0; _c < columnCount; _c += 1) { + const r = range.row[0] + _r; + const c = range.column[0] + _c; + if (!ctx.luckysheetfile[index].data![r][c]?.f) { continue; } @@ -157,3 +170,17 @@ export function calculateSheetFromula(ctx: Context, id: string) { } } } + +export function calculateFormula( + ctx: Context, + id?: string, + range?: SingleRange +) { + if (id) { + calculateSheetFromula(ctx, id, range); + return; + } + _.forEach(ctx.luckysheetfile, (sheet_obj) => { + calculateSheetFromula(ctx, sheet_obj.id as string, range); + }); +} diff --git a/packages/react/src/components/Workbook/api.ts b/packages/react/src/components/Workbook/api.ts index 1526c2de..0c78790b 100644 --- a/packages/react/src/components/Workbook/api.ts +++ b/packages/react/src/components/Workbook/api.ts @@ -36,6 +36,10 @@ export function generateAPIs( scrollbarX: HTMLDivElement | null, scrollbarY: HTMLDivElement | null ) { + type ApiCall = { + name: string; + args: any[]; + }; return { applyOp: (ops: Op[]) => { setContext( @@ -311,7 +315,7 @@ export function generateAPIs( calculateFormula: () => { setContext((draftCtx) => { _.forEach(draftCtx.luckysheetfile, (sheet_obj) => { - api.calculateSheetFromula(draftCtx, sheet_obj.id as string); + api.calculateFormula(draftCtx, sheet_obj.id as string); }); }); }, @@ -327,5 +331,18 @@ export function generateAPIs( ) => { return api.celldataToData(celldata, rowCount, colCount); }, + + batchCallApis: (apiCalls: ApiCall[]) => { + setContext((draftCtx) => { + apiCalls.forEach((apiCall) => { + const { name, args } = apiCall; + if (typeof (api as any)[name] === "function") { + (api as any)[name](draftCtx, ...args); + } else { + console.warn(`API ${name} does not exist`); + } + }); + }); + }, }; }