Skip to content

Commit

Permalink
fix(formula): formula update after hide row column (#4238)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Actions <[email protected]>
  • Loading branch information
Dushusir and actions-user authored Mar 7, 2025
1 parent 2ac6883 commit 4113714
Show file tree
Hide file tree
Showing 27 changed files with 557 additions and 55 deletions.
177 changes: 177 additions & 0 deletions e2e/visual-comparison/sheets/sheets-formula-related.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/**
* Copyright 2023-present DreamNum Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { chromium, expect, test } from '@playwright/test';
import { generateSnapshotName } from '../const';

const SHEET_MAIN_CANVAS_ID = '[id^="univer-sheet-main-canvas_"]';
const isCI = !!process.env.CI;

test('diff formula related', async () => {
const browser = await chromium.launch({
headless: !!isCI, // Set to false to see the browser window
});
const context = await browser.newContext({
viewport: { width: 1280, height: 1280 },
deviceScaleFactor: 2, // Set your desired DPR
});
const page = await context.newPage();
await page.goto('http://localhost:3000/sheets/');
await page.waitForTimeout(2000);
await page.evaluate(() => window.E2EControllerAPI.loadDefaultSheet());
await page.waitForTimeout(5000);

await page.evaluate(async () => {
const worksheet = window.univerAPI.getActiveWorkbook().create('formula', 50, 20);
worksheet.getRange('A1:C6').setValues({
0: {
0: {
v: 1,
t: 2,
s: 'cYigQ9',
},
1: {
v: 1,
t: 2,
s: 'cYigQ9',
},
},
1: {
0: {
v: 2,
t: 2,
s: 'cYigQ9',
},
1: {
v: 2,
t: 2,
s: 'cYigQ9',
},
},
2: {
0: {
v: 3,
t: 2,
s: 'cYigQ9',
},
1: {
v: 3,
t: 2,
s: 'cYigQ9',
},
},
3: {
0: {
v: 4,
t: 2,
s: 'cYigQ9',
},
1: {
v: 4,
t: 2,
s: 'cYigQ9',
},
},
4: {
0: {
v: 5,
t: 2,
s: 'cYigQ9',
},
1: {
v: 5,
t: 2,
s: 'cYigQ9',
},
},
5: {
0: {
f: '=SUBTOTAL(109,A1:A5)',
v: 15,
t: 2,
},
1: {
f: '=SUBTOTAL(109,B1:B5)',
v: 15,
t: 2,
},
2: {
f: '=SUM(B1:B5)',
v: 15,
t: 2,
},
},
});

const worksheet2 = window.univerAPI.getActiveWorkbook().create('formula2', 50, 20);
worksheet2.getRange('A1').setValue({ f: '=SUBTOTAL(109,formula!B1:B5)' });
});

// The impact of hide row on the formula of the current worksheet
await page.evaluate(async () => {
const worksheet = window.univerAPI.getActiveWorkbook().getSheetByName('formula');
window.univerAPI.getActiveWorkbook().setActiveSheet(worksheet);
await worksheet.hideRows(3, 2);
});

await page.waitForTimeout(100);

const filename1 = generateSnapshotName('formula-hide-row-current-worksheet');
const screenshot1 = await page.locator(SHEET_MAIN_CANVAS_ID).screenshot();
await expect(screenshot1).toMatchSnapshot(filename1, { maxDiffPixels: 7 });

// restore the hidden row
await page.evaluate(async () => {
const worksheet = window.univerAPI.getActiveWorkbook().getActiveSheet();
await worksheet.showRows(3, 2);
});

// The impact of filtering on the formula of the current worksheet
await page.evaluate(async () => {
const worksheet = window.univerAPI.getActiveWorkbook().getActiveSheet();
const filter = await worksheet.getRange('A1:A5').createFilter();
await filter.setColumnFilterCriteria(0, {
colId: 0,
filters: {
filters: [
'5',
'4',
'2',
],
},
});
});

await page.waitForTimeout(100);

const filename2 = generateSnapshotName('formula-filter-row-current-worksheet');
const screenshot2 = await page.locator(SHEET_MAIN_CANVAS_ID).screenshot();
await expect(screenshot2).toMatchSnapshot(filename2, { maxDiffPixels: 7 });

await page.evaluate(async () => {
const worksheet2 = window.univerAPI.getActiveWorkbook().getSheetByName('formula2');
window.univerAPI.getActiveWorkbook().setActiveSheet(worksheet2);
});

await page.waitForTimeout(100);

const filename3 = generateSnapshotName('formula-filter-row-other-worksheet');
const screenshot3 = await page.locator(SHEET_MAIN_CANVAS_ID).screenshot();
await expect(screenshot3).toMatchSnapshot(filename3, { maxDiffPixels: 11 });

await page.waitForTimeout(1000);
await browser.close();
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class E2EController extends Disposable {
loadDemoSheet: () => this._loadDemoSheet(),
loadMergeCellSheet: () => this._loadMergeCellSheet(2000),
loadDefaultStyleSheet: (loadTimeout) => this._loadDefaultStyleSheet(loadTimeout),
disposeCurrSheetUnit: (disposeTimeout?: number) => this._diposeDefaultSheetUnit(disposeTimeout),
disposeCurrSheetUnit: (disposeTimeout?: number) => this._disposeDefaultSheetUnit(disposeTimeout),
loadDefaultDoc: (loadTimeout) => this._loadDefaultDoc(loadTimeout),
disposeUniver: () => this._disposeUniver(),
};
Expand Down Expand Up @@ -121,7 +121,7 @@ export class E2EController extends Disposable {
await this._commandService.executeCommand(DisposeUniverCommand.id);
}

private async _diposeDefaultSheetUnit(disposingTimeout: number = AWAIT_DISPOSING_TIMEOUT): Promise<void> {
private async _disposeDefaultSheetUnit(disposingTimeout: number = AWAIT_DISPOSING_TIMEOUT): Promise<void> {
const unit = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SHEET);
const unitId = unit?.getUnitId();
await this._univerInstanceService.disposeUnit(unitId || '');
Expand Down
5 changes: 5 additions & 0 deletions packages/engine-formula/src/basics/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ export interface IUnitExcludedCell {
[unitId: string]: Nullable<{ [sheetId: string]: ObjectMatrix<boolean> }>;
}

export interface IUnitRowData {
[unitId: string]: Nullable<{ [sheetId: string]: IObjectArrayPrimitiveType<Partial<IRowData>> }>;
}

export interface IFormulaDatasetConfig {
formulaData: IFormulaData;
arrayFormulaCellData: IArrayFormulaUnitCellType;
Expand All @@ -191,6 +195,7 @@ export interface IFormulaDatasetConfig {
unitStylesData?: IUnitStylesData;
unitSheetNameMap?: IUnitSheetNameMap;
maxIteration?: number;
rowData?: IUnitRowData; // Include rows hidden by filters
}

export enum ConcatenateType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class CalculateController extends Disposable {
private async _calculate(
formulaDirtyData: Partial<IFormulaDirtyData>
) {
const { forceCalculation: forceCalculate = false, dirtyRanges = [], dirtyNameMap = {}, dirtyDefinedNameMap = {}, dirtyUnitFeatureMap = {}, dirtyUnitOtherFormulaMap = {}, clearDependencyTreeCache = {}, maxIteration = DEFAULT_CYCLE_REFERENCE_COUNT } = formulaDirtyData;
const { forceCalculation: forceCalculate = false, dirtyRanges = [], dirtyNameMap = {}, dirtyDefinedNameMap = {}, dirtyUnitFeatureMap = {}, dirtyUnitOtherFormulaMap = {}, clearDependencyTreeCache = {}, maxIteration = DEFAULT_CYCLE_REFERENCE_COUNT, rowData } = formulaDirtyData;

const formulaData = this._formulaDataModel.getFormulaData();

Expand All @@ -99,6 +99,7 @@ export class CalculateController extends Disposable {
dirtyUnitOtherFormulaMap,
clearDependencyTreeCache,
maxIteration,
rowData,
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/engine-formula/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,4 @@ export { ENGINE_FORMULA_CYCLE_REFERENCE_COUNT, ENGINE_FORMULA_PLUGIN_CONFIG_KEY,
export { generateRandomDependencyTreeId } from './engine/dependency/formula-dependency';
export { DependencyManagerBaseService } from './services/dependency-manager.service';
export { LambdaValueObjectObject } from './engine/value-object/lambda-value-object';
export { type IUnitRowData } from './basics/common';
48 changes: 46 additions & 2 deletions packages/engine-formula/src/models/formula-data.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { ICellData, IObjectMatrixPrimitiveType, IRange, IUnitRange, Nullable, Workbook } from '@univerjs/core';
import type { ICellData, IObjectArrayPrimitiveType, IObjectMatrixPrimitiveType, IRange, IRowData, IUnitRange, Nullable, Workbook } from '@univerjs/core';
import type {
IArrayFormulaRangeType,
IArrayFormulaUnitCellType,
Expand All @@ -24,10 +24,12 @@ import type {
IRuntimeUnitDataType,
ISheetData,
IUnitData,
IUnitRowData,
IUnitSheetNameMap,
IUnitStylesData,
} from '../basics/common';

import { Disposable, Inject, isFormulaId, isFormulaString, IUniverInstanceService, ObjectMatrix, RANGE_TYPE, UniverInstanceType } from '@univerjs/core';
import { BooleanNumber, Disposable, Inject, isFormulaId, isFormulaString, IUniverInstanceService, ObjectMatrix, RANGE_TYPE, UniverInstanceType } from '@univerjs/core';
import { LexerTreeBuilder } from '../engine/analysis/lexer-tree-builder';
import { clearArrayFormulaCellDataByCell, updateFormulaDataByCellValue } from './utils/formula-data-util';

Expand Down Expand Up @@ -258,6 +260,8 @@ export class FormulaDataModel extends Disposable {

const allUnitData: IUnitData = {};

const unitStylesData: IUnitStylesData = {};

const unitSheetNameMap: IUnitSheetNameMap = {};

for (const workbook of unitAllSheet) {
Expand Down Expand Up @@ -287,15 +291,55 @@ export class FormulaDataModel extends Disposable {

allUnitData[unitId] = sheetData;

unitStylesData[unitId] = workbook.getStyles();

unitSheetNameMap[unitId] = sheetNameMap;
}

return {
allUnitData,
unitStylesData,
unitSheetNameMap,
};
}

/**
* Get the hidden rows that are filtered or manually hidden.
*
* For formulas that are sensitive to hidden rows.
*/
getHiddenRowsFiltered() {
const unitAllSheet = this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
const rowData: IUnitRowData = {};

for (const workbook of unitAllSheet) {
const unitId = workbook.getUnitId();
const sheets = workbook.getSheets();
rowData[unitId] = {};

for (const sheet of sheets) {
const sheetId = sheet.getSheetId();
rowData[unitId][sheetId] = {};

const startRow = 0;
const endRow = sheet.getRowCount() - 1;
const sheetRowData: IObjectArrayPrimitiveType<Partial<IRowData>> = {};

for (let i = startRow; i <= endRow; i++) {
if (!sheet.getRowVisible(i)) {
sheetRowData[i] = {
hd: BooleanNumber.TRUE,
};
}
}

rowData[unitId][sheetId] = sheetRowData;
}
}

return rowData;
}

updateFormulaData(unitId: string, sheetId: string, cellValue: IObjectMatrixPrimitiveType<Nullable<ICellData>>) {
const cellMatrix = new ObjectMatrix(cellValue);

Expand Down
Loading

0 comments on commit 4113714

Please sign in to comment.