|
| 1 | +/** |
| 2 | + * 复制源位置记录测试 |
| 3 | + * 验证复制时记录的源位置是否正确用于粘贴时的公式调整 |
| 4 | + */ |
| 5 | + |
| 6 | +describe('复制源位置记录测试', () => { |
| 7 | + it('应该正确记录复制时的源位置', () => { |
| 8 | + // 模拟EventManager的行为 |
| 9 | + const mockEventManager = { |
| 10 | + copySourceRange: null as { startCol: number; startRow: number } | null, |
| 11 | + |
| 12 | + // 模拟handleCopy中记录源位置的逻辑 |
| 13 | + recordCopySourceRange(ranges: any[]) { |
| 14 | + if (ranges && ranges.length > 0) { |
| 15 | + const sourceRange = ranges[0]; |
| 16 | + this.copySourceRange = { |
| 17 | + startCol: Math.min(sourceRange.start.col, sourceRange.end.col), |
| 18 | + startRow: Math.min(sourceRange.start.row, sourceRange.end.row) |
| 19 | + }; |
| 20 | + } |
| 21 | + }, |
| 22 | + |
| 23 | + // 模拟粘贴时获取源位置的逻辑 |
| 24 | + getCopySourcePosition() { |
| 25 | + return this.copySourceRange; |
| 26 | + }, |
| 27 | + |
| 28 | + // 模拟清除复制源位置的逻辑 |
| 29 | + clearCopySourceRange() { |
| 30 | + this.copySourceRange = null; |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + // 测试1:复制C5时的位置记录 |
| 35 | + const copyRanges1 = [ |
| 36 | + { |
| 37 | + start: { col: 2, row: 4 }, // C5 |
| 38 | + end: { col: 2, row: 4 } |
| 39 | + } |
| 40 | + ]; |
| 41 | + |
| 42 | + mockEventManager.recordCopySourceRange(copyRanges1); |
| 43 | + |
| 44 | + console.log('复制C5后的源位置:', mockEventManager.getCopySourcePosition()); |
| 45 | + expect(mockEventManager.getCopySourcePosition()).toEqual({ |
| 46 | + startCol: 2, // C列 |
| 47 | + startRow: 4 // 第5行 |
| 48 | + }); |
| 49 | + |
| 50 | + // 测试2:复制A1:B2区域时的位置记录 |
| 51 | + const copyRanges2 = [ |
| 52 | + { |
| 53 | + start: { col: 0, row: 0 }, // A1 |
| 54 | + end: { col: 1, row: 1 } // B2 |
| 55 | + } |
| 56 | + ]; |
| 57 | + |
| 58 | + mockEventManager.recordCopySourceRange(copyRanges2); |
| 59 | + |
| 60 | + console.log('复制A1:B2后的源位置:', mockEventManager.getCopySourcePosition()); |
| 61 | + expect(mockEventManager.getCopySourcePosition()).toEqual({ |
| 62 | + startCol: 0, // A列 |
| 63 | + startRow: 0 // 第1行 |
| 64 | + }); |
| 65 | + |
| 66 | + // 测试3:清除源位置 |
| 67 | + mockEventManager.clearCopySourceRange(); |
| 68 | + console.log('清除后的源位置:', mockEventManager.getCopySourcePosition()); |
| 69 | + expect(mockEventManager.getCopySourcePosition()).toBeNull(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('应该正确处理相对位置计算', () => { |
| 73 | + // 模拟processFormulaPaste的逻辑 |
| 74 | + const calculateRelativeOffset = ( |
| 75 | + sourceStartCol: number, |
| 76 | + sourceStartRow: number, |
| 77 | + targetStartCol: number, |
| 78 | + targetStartRow: number |
| 79 | + ) => { |
| 80 | + return { |
| 81 | + colOffset: targetStartCol - sourceStartCol, |
| 82 | + rowOffset: targetStartRow - sourceStartRow |
| 83 | + }; |
| 84 | + }; |
| 85 | + |
| 86 | + // 测试场景:C5复制到D5 |
| 87 | + const sourcePos = { startCol: 2, startRow: 4 }; // C5 |
| 88 | + const targetPos = { startCol: 3, startRow: 4 }; // D5 |
| 89 | + |
| 90 | + const offset = calculateRelativeOffset( |
| 91 | + sourcePos.startCol, |
| 92 | + sourcePos.startRow, |
| 93 | + targetPos.startCol, |
| 94 | + targetPos.startRow |
| 95 | + ); |
| 96 | + |
| 97 | + console.log('C5->D5的相对偏移:', offset); |
| 98 | + expect(offset).toEqual({ |
| 99 | + colOffset: 1, // 右移1列 |
| 100 | + rowOffset: 0 // 行不变 |
| 101 | + }); |
| 102 | + |
| 103 | + // 测试场景:A1复制到C3 |
| 104 | + const offset2 = calculateRelativeOffset(0, 0, 2, 2); |
| 105 | + |
| 106 | + console.log('A1->C3的相对偏移:', offset2); |
| 107 | + expect(offset2).toEqual({ |
| 108 | + colOffset: 2, // 右移2列 |
| 109 | + rowOffset: 2 // 下移2行 |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + it('应该验证完整的复制粘贴流程', () => { |
| 114 | + // 完整的流程测试 |
| 115 | + const mockWorkSheet = { |
| 116 | + copySourceRange: null as { startCol: number; startRow: number } | null, |
| 117 | + |
| 118 | + // 复制时记录源位置 |
| 119 | + handleCopy(ranges: any[]) { |
| 120 | + if (ranges && ranges.length > 0) { |
| 121 | + const sourceRange = ranges[0]; |
| 122 | + this.copySourceRange = { |
| 123 | + startCol: Math.min(sourceRange.start.col, sourceRange.end.col), |
| 124 | + startRow: Math.min(sourceRange.start.row, sourceRange.end.row) |
| 125 | + }; |
| 126 | + } |
| 127 | + }, |
| 128 | + |
| 129 | + // 粘贴时使用记录的源位置 |
| 130 | + processFormulaPaste(formulas: string[][], targetStartCol: number, targetStartRow: number): string[][] { |
| 131 | + if (!this.copySourceRange) { |
| 132 | + return formulas; // 没有源位置,不处理 |
| 133 | + } |
| 134 | + |
| 135 | + const colOffset = targetStartCol - this.copySourceRange.startCol; |
| 136 | + const rowOffset = targetStartRow - this.copySourceRange.startRow; |
| 137 | + |
| 138 | + return formulas.map(row => |
| 139 | + row.map(cell => { |
| 140 | + if (cell.startsWith('=')) { |
| 141 | + return cell.replace(/([A-Z]+)(\d+)/g, (match, col, row) => { |
| 142 | + const colNum = col.charCodeAt(0) - 'A'.charCodeAt(0) + colOffset; |
| 143 | + const rowNum = parseInt(row, 10) + rowOffset; |
| 144 | + return String.fromCharCode('A'.charCodeAt(0) + colNum) + rowNum; |
| 145 | + }); |
| 146 | + } |
| 147 | + return cell; |
| 148 | + }) |
| 149 | + ); |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + // 步骤1:复制C5(公式=A2) |
| 154 | + mockWorkSheet.handleCopy([ |
| 155 | + { |
| 156 | + start: { col: 2, row: 4 }, // C5 |
| 157 | + end: { col: 2, row: 4 } |
| 158 | + } |
| 159 | + ]); |
| 160 | + |
| 161 | + // 步骤2:粘贴到D5 |
| 162 | + const sourceFormula = [['=A2']]; |
| 163 | + const result = mockWorkSheet.processFormulaPaste(sourceFormula, 3, 4); // D5 |
| 164 | + |
| 165 | + console.log('完整流程结果:', result); |
| 166 | + expect(result).toEqual([['=B2']]); // C5的=A2粘贴到D5应该变成=B2 |
| 167 | + }); |
| 168 | +}); |
0 commit comments