Skip to content

Commit

Permalink
added: batch API, calculateFormula API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Corbe30 committed Jan 9, 2025
1 parent 4acc19c commit dc4b12a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
35 changes: 31 additions & 4 deletions packages/core/src/api/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "..";

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
});
}
19 changes: 18 additions & 1 deletion packages/react/src/components/Workbook/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export function generateAPIs(
scrollbarX: HTMLDivElement | null,
scrollbarY: HTMLDivElement | null
) {
type ApiCall = {
name: string;
args: any[];
};
return {
applyOp: (ops: Op[]) => {
setContext(
Expand Down Expand Up @@ -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);
});
});
},
Expand All @@ -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`);
}
});
});
},
};
}

0 comments on commit dc4b12a

Please sign in to comment.