Skip to content

Commit f72ee7e

Browse files
committed
fix: add del record should update formula
1 parent 333fdc8 commit f72ee7e

22 files changed

Lines changed: 1126 additions & 203 deletions

packages/vtable-plugins/src/add-row-column.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,14 @@ export class AddRowColumnPlugin implements pluginsDefinition.IVTablePlugin {
235235
// width: 100
236236
// });
237237
// this.table.updateColumns(columns);
238-
this.table.addColumn(
239-
{
240-
field: addColIndex,
241-
title: `New Column ${col}`,
242-
width: 100
243-
},
238+
this.table.addColumns(
239+
[
240+
{
241+
field: addColIndex,
242+
title: `New Column ${col}`,
243+
width: 100
244+
}
245+
],
244246
addColIndex,
245247
true
246248
);

packages/vtable-plugins/src/column-series.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class ColumnSeriesPlugin implements pluginsDefinition.IVTablePlugin {
4343
const e = eventArgs.event;
4444
if (this.pluginOptions.autoExtendColumnTriggerKeys?.includes(e.key)) {
4545
if (this.table.stateManager.select.cellPos.col === this.table.colCount - 1) {
46-
this.table.addColumn(this.generateColumn(this.table.colCount - 1) as ColumnDefine);
46+
this.table.addColumns([this.generateColumn(this.table.colCount - 1) as ColumnDefine]);
4747
}
4848
}
4949
}

packages/vtable-plugins/src/paste-add-row-column.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ export class PasteAddRowColumnPlugin implements pluginsDefinition.IVTablePlugin
5656
if (this.pluginOptions?.addColumnCallback) {
5757
this.pluginOptions.addColumnCallback(newColIndex, this.table);
5858
} else {
59-
this.table.addColumn({
60-
field: `field_${newColIndex}`,
61-
title: `New Column ${newColIndex}`,
62-
width: 100
63-
});
59+
this.table.addColumns([
60+
{
61+
field: `field_${newColIndex}`,
62+
title: `New Column ${newColIndex}`,
63+
width: 100
64+
}
65+
]);
6466
}
6567
}
6668
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// @ts-nocheck
2+
import { FormulaManager } from '../src/managers/formula-manager';
3+
import type VTableSheet from '../src/components/vtable-sheet';
4+
5+
// Mock VTableSheet for testing
6+
const mockVTableSheet = {
7+
getSheetManager: () => ({
8+
getSheet: (sheetKey: string) => ({
9+
sheetTitle: 'Test Sheet',
10+
sheetKey: sheetKey,
11+
showHeader: true,
12+
columnCount: 10,
13+
rowCount: 10,
14+
columns: [] as any[]
15+
})
16+
}),
17+
getActiveSheet: (): any => ({
18+
tableInstance: {
19+
changeCellValue: () => {
20+
/* Mock implementation */
21+
}
22+
}
23+
}),
24+
getSheet: (sheetKey: string) => ({
25+
columnCount: 10,
26+
rowCount: 10
27+
}),
28+
formulaManager: null // 这会在创建FormulaManager时自动设置
29+
} as unknown as VTableSheet;
30+
31+
describe('Column Debug Test', () => {
32+
let formulaManager: FormulaManager;
33+
34+
beforeEach(() => {
35+
formulaManager = new FormulaManager(mockVTableSheet);
36+
mockVTableSheet.formulaManager = formulaManager;
37+
});
38+
39+
afterEach(() => {
40+
formulaManager.release();
41+
});
42+
43+
test('debug column deletion logic', () => {
44+
const sheetKey = 'Sheet1';
45+
46+
// 添加包含数据的工作表
47+
formulaManager.addSheet(sheetKey, [
48+
['A', 'B', 'C'],
49+
['10', '20', '30'],
50+
['', '', '']
51+
]);
52+
53+
// 在C3中创建引用A2:B2的求和公式
54+
formulaManager.setCellContent({ sheet: sheetKey, row: 2, col: 2 }, '=SUM(A2:B2)');
55+
56+
// 检查公式和值
57+
const formula_before = formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 2 });
58+
const value_before = formulaManager.getCellValue({ sheet: sheetKey, row: 2, col: 2 });
59+
60+
expect(formula_before).toBe('=SUM(A2:B2)');
61+
expect(value_before.value).toBe(30); // 10+20=30
62+
63+
// 模拟删除B列(索引1)
64+
const result = formulaManager.formulaEngine.adjustFormulaReferences(sheetKey, 'delete', 'column', 1, 1, 3, 3);
65+
66+
// 检查公式和值 after deletion
67+
const formula_after = formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 1 });
68+
const value_after = formulaManager.getCellValue({ sheet: sheetKey, row: 2, col: 1 });
69+
70+
console.log('Formula after column deletion:', formula_after);
71+
console.log('Value after column deletion:', value_after);
72+
73+
// 期望:公式应该变成 =SUM(A2),值应该是10
74+
expect(formula_after).toBe('=SUM(A2)');
75+
expect(value_after.value).toBe(10); // 现在只有A2的值
76+
});
77+
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// @ts-nocheck
2+
import { FormulaManager } from '../src/managers/formula-manager';
3+
import type VTableSheet from '../src/components/vtable-sheet';
4+
5+
// Mock VTableSheet for testing
6+
const mockVTableSheet = {
7+
getSheetManager: () => ({
8+
getSheet: (sheetKey: string) => ({
9+
sheetTitle: 'Test Sheet',
10+
sheetKey: sheetKey,
11+
showHeader: true,
12+
columnCount: 10,
13+
rowCount: 10,
14+
columns: [] as any[]
15+
})
16+
}),
17+
getActiveSheet: (): any => ({
18+
tableInstance: {
19+
changeCellValue: () => {
20+
/* Mock implementation */
21+
}
22+
}
23+
}),
24+
getSheet: (sheetKey: string) => ({
25+
columnCount: 10,
26+
rowCount: 10
27+
}),
28+
formulaManager: null // 这会在创建FormulaManager时自动设置
29+
} as unknown as VTableSheet;
30+
31+
describe('Column Logic Debug Test', () => {
32+
let formulaManager: FormulaManager;
33+
34+
beforeEach(() => {
35+
formulaManager = new FormulaManager(mockVTableSheet);
36+
mockVTableSheet.formulaManager = formulaManager;
37+
});
38+
39+
afterEach(() => {
40+
formulaManager.release();
41+
});
42+
43+
test('understand column deletion logic step by step', () => {
44+
const sheetKey = 'Sheet1';
45+
46+
// 添加包含数据的工作表
47+
formulaManager.addSheet(sheetKey, [
48+
['A', 'B', 'C'],
49+
['10', '20', '30'],
50+
['', '', '']
51+
]);
52+
53+
// 在C3中创建引用A2:B2的求和公式
54+
formulaManager.setCellContent({ sheet: sheetKey, row: 2, col: 2 }, '=SUM(A2:B2)');
55+
56+
// 检查原始状态
57+
console.log('Before deletion:');
58+
console.log('Formula:', formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 2 }));
59+
console.log('Value:', formulaManager.getCellValue({ sheet: sheetKey, row: 2, col: 2 }));
60+
console.log('A2:', formulaManager.getCellValue({ sheet: sheetKey, row: 1, col: 0 }));
61+
console.log('B2:', formulaManager.getCellValue({ sheet: sheetKey, row: 1, col: 1 }));
62+
63+
// 模拟删除B列(索引1)
64+
const result = formulaManager.formulaEngine.adjustFormulaReferences(sheetKey, 'delete', 'column', 1, 1, 3, 3);
65+
66+
console.log('Deletion result:', result);
67+
68+
// 检查最终状态
69+
console.log('After deletion:');
70+
console.log('Formula:', formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 1 }));
71+
console.log('Value:', formulaManager.getCellValue({ sheet: sheetKey, row: 2, col: 1 }));
72+
console.log('A2:', formulaManager.getCellValue({ sheet: sheetKey, row: 1, col: 0 }));
73+
74+
// 期望:公式应该变成 =SUM(A2),值应该是10
75+
expect(formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 1 })).toBe('=SUM(A2)');
76+
expect(formulaManager.getCellValue({ sheet: sheetKey, row: 2, col: 1 }).value).toBe(10);
77+
});
78+
});

packages/vtable-sheet/__tests__/column-operations.test.ts

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ const mockVTableSheet = {
1414
columns: [] as any[]
1515
})
1616
}),
17-
getActiveSheet: (): any => null,
17+
getActiveSheet: (): any => ({
18+
tableInstance: {
19+
changeCellValue: () => {
20+
/* Mock implementation */
21+
}
22+
}
23+
}),
1824
getSheet: (sheetKey: string) => ({
1925
columnCount: 10,
2026
rowCount: 10
21-
})
27+
}),
28+
formulaManager: null // 这会在创建FormulaManager时自动设置
2229
} as unknown as VTableSheet;
2330

2431
// 测试用的基本标准化函数
@@ -58,6 +65,8 @@ describe('Column Operations Formula References', () => {
5865

5966
beforeEach(() => {
6067
formulaManager = new FormulaManager(mockVTableSheet);
68+
// 设置mock对象的formulaManager属性,以便在测试中使用
69+
mockVTableSheet.formulaManager = formulaManager;
6170
});
6271

6372
afterEach(() => {
@@ -84,7 +93,7 @@ describe('Column Operations Formula References', () => {
8493
expect(formulaManager.getCellValue({ sheet: sheetKey, row: 2, col: 4 }).value).toBe(80); // 40*2=80
8594

8695
// 模拟删除B列(索引1)
87-
const { adjustedCells } = formulaManager.formulaEngine.adjustFormulaReferences(
96+
const { adjustedCells, movedCells } = formulaManager.formulaEngine.adjustFormulaReferences(
8897
sheetKey,
8998
'delete',
9099
'column',
@@ -96,15 +105,16 @@ describe('Column Operations Formula References', () => {
96105

97106
// 验证公式引用是否被正确调整
98107
// C3的公式应该变成 =A2+A2 (原来是 =A2+B2,但B2已经变成A2了)
99-
const originalFormula = formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 2 });
100-
expect(originalFormula).toContain('A2+B2');
108+
const originalFormula = formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 1 });
109+
expect(originalFormula).toContain('#REF!');
101110

102111
// 验证引用调整后的单元格列表
103-
expect(adjustedCells.length).toBeGreaterThan(0);
112+
expect(adjustedCells.length).toEqual(0);
113+
expect(movedCells.length).toBeGreaterThan(0);
104114

105115
// E3的公式应该变成 =C2*2 (原来是 =D2*2,但D2已经变成C2了)
106-
const eFormula = formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 4 });
107-
expect(eFormula).toContain('D2');
116+
const eFormula = formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 3 });
117+
expect(eFormula).toContain('C2');
108118
});
109119

110120
test('should update formula references when adding columns', () => {
@@ -127,7 +137,7 @@ describe('Column Operations Formula References', () => {
127137
expect(formulaManager.getCellValue({ sheet: sheetKey, row: 2, col: 3 }).value).toBe(60); // 30*2=60
128138

129139
// 模拟在B列(索引1)前插入一列
130-
const { adjustedCells } = formulaManager.formulaEngine.adjustFormulaReferences(
140+
const { adjustedCells, movedCells } = formulaManager.formulaEngine.adjustFormulaReferences(
131141
sheetKey,
132142
'insert',
133143
'column',
@@ -139,15 +149,15 @@ describe('Column Operations Formula References', () => {
139149

140150
// 验证公式引用是否被正确调整
141151
// C3的公式应该变成 =A2+C2 (原来是 =A2+B2,但B2已经变成C2了)
142-
const originalFormula = formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 2 });
143-
expect(originalFormula).toContain('A2+B2');
152+
const originalFormula = formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 3 });
153+
expect(originalFormula).toContain('A2+C2');
144154

145155
// 验证引用调整后的单元格列表
146-
expect(adjustedCells.length).toBeGreaterThan(0);
147-
156+
expect(adjustedCells.length).toEqual(0);
157+
expect(movedCells.length).toBeGreaterThan(0);
148158
// D3的公式应该变成 =D2*2 (原来是 =C2*2,但C2已经变成D2了)
149-
const dFormula = formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 3 });
150-
expect(dFormula).toContain('C2');
159+
const dFormula = formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 4 });
160+
expect(dFormula).toContain('D2');
151161
});
152162

153163
test('should handle edge cases when manipulating columns', () => {
@@ -211,4 +221,35 @@ describe('Column Operations Formula References', () => {
211221
console.error(`删除包含公式的列应该不会抛出错误: ${error}`);
212222
}
213223
});
224+
// 测试情况 C3=SUM(A2:B2),删除B列,B3(原C3)应该变成 =SUM(A2)
225+
test('should handle SUM formula when deleting columns', () => {
226+
const sheetKey = 'Sheet1';
227+
formulaManager.addSheet(sheetKey, [
228+
['A', 'B', 'C'],
229+
['10', '20', '30'],
230+
['', '', '']
231+
]);
232+
233+
// 在C3中创建引用A2:B2的求和公式
234+
formulaManager.setCellContent({ sheet: sheetKey, row: 2, col: 2 }, '=SUM(A2:B2)');
235+
expect(formulaManager.getCellValue({ sheet: sheetKey, row: 2, col: 2 }).value).toBe(30); // 10+20=30
236+
237+
// 模拟删除B列(索引1)
238+
const { adjustedCells, movedCells } = formulaManager.formulaEngine.adjustFormulaReferences(
239+
sheetKey,
240+
'delete',
241+
'column',
242+
1,
243+
1,
244+
3,
245+
3
246+
);
247+
248+
// 验证公式已被修复
249+
const formula = formulaManager.getCellFormula({ sheet: sheetKey, row: 2, col: 1 });
250+
expect(formula).toEqual('=SUM(A2)');
251+
252+
// 确认值仍然正确
253+
expect(formulaManager.getCellValue({ sheet: sheetKey, row: 2, col: 1 }).value).toBe(10); // 现在只有A2的值
254+
});
214255
});

packages/vtable-sheet/src/formula/__tests__/debug-adjustment.js renamed to packages/vtable-sheet/__tests__/formula-engine/debug-adjustment.js

File renamed without changes.

0 commit comments

Comments
 (0)