Skip to content

Commit

Permalink
Merge pull request #545 from ruilisi/feat/formula-optimization
Browse files Browse the repository at this point in the history
Formula calculation optimisations
  • Loading branch information
sanchit3008 committed May 20, 2024
2 parents 8b15a84 + 6087d22 commit 5b5c087
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
10 changes: 9 additions & 1 deletion packages/core/src/events/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,15 @@ function pasteHandlerOfCopyPaste(
func = `=${functionCopy(ctx, func, "left", Math.abs(offsetCol))}`;
}

const funcV = execfunction(ctx, func, h, c, undefined, true);
const funcV = execfunction(
ctx,
func,
h,
c,
undefined,
undefined,
true
);

if (!_.isNil(value.spl)) {
// value.f = funcV[2];
Expand Down
24 changes: 20 additions & 4 deletions packages/core/src/modules/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ export function updateCell(
if (_.isPlainObject(curv)) {
if (!isCurInline) {
if (_.isString(value) && value.slice(0, 1) === "=" && value.length > 1) {
const v = execfunction(ctx, value, r, c, undefined, true);
const v = execfunction(ctx, value, r, c, undefined, undefined, true);
isRunExecFunction = false;
curv = _.cloneDeep(d?.[r]?.[c] || {});
[, curv.v, curv.f] = v;
Expand Down Expand Up @@ -859,7 +859,15 @@ export function updateCell(
valueFunction.slice(0, 1) === "=" &&
valueFunction.length > 1
) {
const v = execfunction(ctx, valueFunction, r, c, undefined, true);
const v = execfunction(
ctx,
valueFunction,
r,
c,
undefined,
undefined,
true
);
isRunExecFunction = false;
// get v/m/ct

Expand Down Expand Up @@ -912,7 +920,7 @@ export function updateCell(
value = curv;
} else {
if (_.isString(value) && value.slice(0, 1) === "=" && value.length > 1) {
const v = execfunction(ctx, value, r, c, undefined, true);
const v = execfunction(ctx, value, r, c, undefined, undefined, true);
isRunExecFunction = false;
value = {
v: v[1],
Expand Down Expand Up @@ -941,7 +949,15 @@ export function updateCell(
valueFunction.slice(0, 1) === "=" &&
valueFunction.length > 1
) {
const v = execfunction(ctx, valueFunction, r, c, undefined, true);
const v = execfunction(
ctx,
valueFunction,
r,
c,
undefined,
undefined,
true
);
isRunExecFunction = false;
// value = {
// "v": v[1],
Expand Down
34 changes: 23 additions & 11 deletions packages/core/src/modules/formula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,8 @@ export function insertUpdateFunctionGroup(
ctx: Context,
r: number,
c: number,
id?: string
id?: string,
calcChainSet?: Set<string>
) {
if (_.isNil(id)) {
id = ctx.currentSheetId;
Expand All @@ -971,14 +972,18 @@ export function insertUpdateFunctionGroup(
calcChain = [];
}

for (let i = 0; i < calcChain.length; i += 1) {
const calc = calcChain[i];
if (calc.r === r && calc.c === c && calc.id === id) {
// server.saveParam("fc", index, calc, {
// op: "update",
// pos: i,
// });
return;
if (calcChainSet) {
if (calcChainSet.has(`${r}_${c}_${id}`)) return;
} else {
for (let i = 0; i < calcChain.length; i += 1) {
const calc = calcChain[i];
if (calc.r === r && calc.c === c && calc.id === id) {
// server.saveParam("fc", index, calc, {
// op: "update",
// pos: i,
// });
return;
}
}
}

Expand All @@ -1003,6 +1008,7 @@ export function execfunction(
r: number,
c: number,
id?: string,
calcChainSet?: Set<string>,
isrefresh?: boolean,
notInsertFunc?: boolean
) {
Expand Down Expand Up @@ -1162,7 +1168,7 @@ export function execfunction(
}

if (!notInsertFunc) {
insertUpdateFunctionGroup(ctx, r, c, id);
insertUpdateFunctionGroup(ctx, r, c, id, calcChainSet);
}
}

Expand Down Expand Up @@ -1640,6 +1646,11 @@ export function execFunctionGroup(

formulaRunList.reverse();

const calcChainSet = new Set<string>();
calcChains.forEach((item) => {
calcChainSet.add(`${item.r}_${item.c}_${item.id}`);
});

// console.log(formulaObjects, ii)
// console.timeEnd("3");

Expand All @@ -1657,7 +1668,8 @@ export function execFunctionGroup(
calc_funcStr,
formulaCell.r,
formulaCell.c,
formulaCell.id
formulaCell.id,
calcChainSet
);

ctx.groupValuesRefreshData.push({
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function sortDataRange(
} else if (moveOffset < 0) {
func = `=${functionCopy(ctx, func, "up", -moveOffset)}`;
}
const funcV = execfunction(ctx, func, r, c, undefined, true);
const funcV = execfunction(ctx, func, r, c, undefined, undefined, true);
[, cell!.v, cell!.f] = funcV;
cell.m = update(cell.ct?.fa || "General", cell.v);
}
Expand Down

0 comments on commit 5b5c087

Please sign in to comment.