@@ -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} ) ;
0 commit comments