Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(formula): fix sheets-filter hidden rows causing formula auto-fill… (#4840)

wpxp123456e7f98675a369
diff --git a/packages/sheets-formula-ui/src/controllers/formula-auto-fill.controller.ts b/packages/sheets-formula-ui/src/controllers/formula-auto-fill.controller.ts
index 11f4e903cf..e1a444367c 100644
--- a/packages/sheets-formula-ui/src/controllers/formula-auto-fill.controller.ts
+++ b/packages/sheets-formula-ui/src/controllers/formula-auto-fill.controller.ts
@@ -15,7 +15,7 @@
*/
import type { ICellData, Nullable } from '@univerjs/core';
-import type { AutoFillService, IAutoFillRule, ICopyDataInTypeIndexInfo, ICopyDataPiece } from '@univerjs/sheets-ui';
+import type { AutoFillService, IAutoFillLocation, IAutoFillRule, ICopyDataInTypeIndexInfo, ICopyDataPiece } from '@univerjs/sheets-ui';
import {
Direction,
Disposable,
@@ -49,9 +49,9 @@ export class FormulaAutoFillController extends Disposable {
return false;
},
applyFunctions: {
- [APPLY_TYPE.COPY]: (dataWithIndex, len, direction, copyDataPiece) => {
+ [APPLY_TYPE.COPY]: (dataWithIndex, len, direction, copyDataPiece, location) => {
const { data, index } = dataWithIndex;
- return this._fillCopyFormula(data, len, direction, index, copyDataPiece);
+ return this._fillCopyFormula(data, len, direction, index, copyDataPiece, location as IAutoFillLocation);
},
},
};
@@ -63,19 +63,21 @@ export class FormulaAutoFillController extends Disposable {
len: number,
direction: Direction,
index: ICopyDataInTypeIndexInfo,
- copyDataPiece: ICopyDataPiece
+ copyDataPiece: ICopyDataPiece,
+ location: IAutoFillLocation
) {
const step = getDataLength(copyDataPiece);
const applyData = [];
const formulaIdMap = new Map<number, string>();
for (let i = 1; i <= len; i++) {
- const index = (i - 1) % data.length;
- const d = Tools.deepClone(data[index]);
+ const dataIndex = (i - 1) % data.length;
+ const sourceIndex = index[dataIndex];
+ const d = Tools.deepClone(data[dataIndex]);
if (d) {
- const originalFormula = data[index]?.f || '';
- const originalFormulaId = data[index]?.si || '';
+ const originalFormula = data[dataIndex]?.f || '';
+ const originalFormulaId = data[dataIndex]?.si || '';
const checkFormula = isFormulaString(originalFormula);
const checkFormulaId = isFormulaId(originalFormulaId);
@@ -90,13 +92,13 @@ export class FormulaAutoFillController extends Disposable {
applyData.push(d);
} else if (checkFormula) {
// The first position setting formula and formulaId
- let formulaId = formulaIdMap.get(index);
+ let formulaId = formulaIdMap.get(dataIndex);
if (!formulaId) {
formulaId = Tools.generateRandomId(6);
- formulaIdMap.set(index, formulaId);
+ formulaIdMap.set(dataIndex, formulaId);
- const { offsetX, offsetY } = directionToOffset(step, len, direction);
+ const { offsetX, offsetY } = directionToOffset(step, len, direction, location, sourceIndex);
const shiftedFormula = this._lexerTreeBuilder.moveFormulaRefOffset(
originalFormula,
offsetX,
@@ -126,19 +128,28 @@ export class FormulaAutoFillController extends Disposable {
}
}
-function directionToOffset(step: number, len: number, direction: Direction) {
+function directionToOffset(step: number, len: number, direction: Direction, location: IAutoFillLocation, sourceIndex: number) {
+ const { source, target } = location;
+ const { rows: targetRows } = target;
+ const { rows: sourceRows } = source;
+
let offsetX = 0;
let offsetY = 0;
switch (direction) {
case Direction.UP:
- offsetY = -step * len; // The formula fills upwards, and the cell containing f and si must be at the top.
+ // The formula fills upwards, and the cell containing f and si must be at the top.
+ // This happens when the step row is hidden, find the relative row that is not hidden.
+ // For example, a hidden row caused by a sheets-filter.
+ offsetY = targetRows[sourceIndex] - sourceRows[sourceIndex];
break;
case Direction.RIGHT:
offsetX = step;
break;
case Direction.DOWN:
- offsetY = step;
+ // This happens when the step row is hidden, find the relative row that is not hidden.
+ // For example, a hidden row caused by a sheets-filter.
+ offsetY = targetRows[sourceIndex] - sourceRows[sourceIndex];
break;
case Direction.LEFT:
offsetX = -step * len; // The formula fills leftwards, and the cell containing f and si must be at the left.
diff --git a/packages/sheets-ui/src/controllers/auto-fill.controller.ts b/packages/sheets-ui/src/controllers/auto-fill.controller.ts
index 7411e041f8..cc2ae30c1b 100644
--- a/packages/sheets-ui/src/controllers/auto-fill.controller.ts
+++ b/packages/sheets-ui/src/controllers/auto-fill.controller.ts
@@ -318,7 +318,8 @@ export class AutoFillController extends Disposable {
asLen: number,
direction: Direction,
applyType: APPLY_TYPE,
- hasStyle: boolean = true
+ hasStyle: boolean = true,
+ location: IAutoFillLocation
) {
const applyData: Array<Nullable<ICellData>> = [];
const num = Math.floor(asLen / csLen);
@@ -356,7 +357,8 @@ export class AutoFillController extends Disposable {
direction,
applyType,
customApplyFunctions,
- copyDataPiece
+ copyDataPiece,
+ location
);
const arrIndex = getDataIndex(csLen, asLen, copySquad.index);
@@ -386,7 +388,8 @@ export class AutoFillController extends Disposable {
direction: Direction,
applyType: APPLY_TYPE,
customApplyFunctions: APPLY_FUNCTIONS,
- copyDataPiece: ICopyDataPiece
+ copyDataPiece: ICopyDataPiece,
+ location: IAutoFillLocation
) {
const { data } = copySquad;
const isReverse = direction === Direction.UP || direction === Direction.LEFT;
@@ -395,7 +398,7 @@ export class AutoFillController extends Disposable {
if (applyType === APPLY_TYPE.COPY) {
const custom = customApplyFunctions?.[APPLY_TYPE.COPY];
if (custom) {
- return custom(copySquad, len, direction, copyDataPiece);
+ return custom(copySquad, len, direction, copyDataPiece, location);
}
isReverse && data.reverse();
return fillCopy(data, len);
@@ -408,7 +411,7 @@ export class AutoFillController extends Disposable {
isReverse && data.reverse();
// special rules, if not provide custom SERIES apply functions, will be applied as copy
if (customApplyFunctions?.[APPLY_TYPE.COPY]) {
- return customApplyFunctions[APPLY_TYPE.COPY](copySquad, len, direction, copyDataPiece);
+ return customApplyFunctions[APPLY_TYPE.COPY](copySquad, len, direction, copyDataPiece, location);
}
return fillCopy(data, len);
}
@@ -646,7 +649,7 @@ export class AutoFillController extends Disposable {
targetCols.forEach((_, i) => {
const copyD = copyData[i];
- const applyData = this._getApplyData(copyD, csLen, asLen, direction, applyType, hasStyle);
+ const applyData = this._getApplyData(copyD, csLen, asLen, direction, applyType, hasStyle, location);
untransformedApplyDatas.push(applyData);
});
for (let i = 0; i < untransformedApplyDatas[0].length; i++) {
@@ -663,7 +666,7 @@ export class AutoFillController extends Disposable {
const asLen = targetCols.length;
targetRows.forEach((_, i) => {
const copyD = copyData[i];
- const applyData = this._getApplyData(copyD, csLen, asLen, direction, applyType, hasStyle);
+ const applyData = this._getApplyData(copyD, csLen, asLen, direction, applyType, hasStyle, location);
const row: Array<Nullable<ICellData>> = [];
for (let j = 0; j < applyData.length; j++) {
row.push({
diff --git a/packages/sheets-ui/src/services/auto-fill/type.ts b/packages/sheets-ui/src/services/auto-fill/type.ts
index 72a425f011..5aab8d8c06 100644
--- a/packages/sheets-ui/src/services/auto-fill/type.ts
+++ b/packages/sheets-ui/src/services/auto-fill/type.ts
@@ -94,7 +94,8 @@ export type APPLY_FUNCTIONS = {
dataWithIndex: ICopyDataInType,
len: number,
direction: Direction,
- copyDataPiece: ICopyDataPiece
+ copyDataPiece: ICopyDataPiece,
+ location?: IAutoFillLocation
) => Array<Nullable<ICellData>>;
};