|
| 1 | +import {DetailedCellError, ErrorType, HyperFormula} from '../src' |
| 2 | +import {SimpleCellAddress, simpleCellAddress} from '../src/Cell' |
| 3 | + |
| 4 | +const adr = (stringAddress: string, sheet: number = 0): SimpleCellAddress => { |
| 5 | + const result = /^(\$([A-Za-z0-9_]+)\.)?(\$?)([A-Za-z]+)(\$?)([0-9]+)$/.exec(stringAddress)! |
| 6 | + const row = Number(result[6]) - 1 |
| 7 | + return simpleCellAddress(sheet, colNumber(result[4]), row) |
| 8 | +} |
| 9 | + |
| 10 | +const colNumber = (input: string): number => { |
| 11 | + if (input.length === 1) { |
| 12 | + return input.toUpperCase().charCodeAt(0) - 65 |
| 13 | + } else { |
| 14 | + return input.split('').reduce((currentColumn, nextLetter) => { |
| 15 | + return currentColumn * 26 + (nextLetter.toUpperCase().charCodeAt(0) - 64) |
| 16 | + }, 0) - 1 |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +describe('HF-305 arrayFunctionResultOverwritesData', () => { |
| 21 | + it('OFF (flag omitted, default): array spilling onto an occupied cell yields #SPILL! and leaves the occupant intact', () => { |
| 22 | + const data = [ |
| 23 | + ['=TRANSPOSE(C1:E1)', null, 1, 2, 3], |
| 24 | + ['x'], |
| 25 | + ] |
| 26 | + |
| 27 | + const hf = HyperFormula.buildFromArray(data, {licenseKey: 'gpl-v3'}) |
| 28 | + |
| 29 | + const a1 = hf.getCellValue(adr('A1')) |
| 30 | + expect(a1 instanceof DetailedCellError).toBe(true) |
| 31 | + expect((a1 as DetailedCellError).type).toBe(ErrorType.SPILL) |
| 32 | + expect(hf.getCellValue(adr('A2'))).toBe('x') |
| 33 | + |
| 34 | + hf.destroy() |
| 35 | + }) |
| 36 | + |
| 37 | + it('OFF (explicit false): array spilling onto an occupied cell yields #SPILL! and leaves the occupant intact', () => { |
| 38 | + const data = [ |
| 39 | + ['=TRANSPOSE(C1:E1)', null, 1, 2, 3], |
| 40 | + ['x'], |
| 41 | + ] |
| 42 | + |
| 43 | + const hf = HyperFormula.buildFromArray(data, {licenseKey: 'gpl-v3', arrayFunctionResultOverwritesData: false}) |
| 44 | + |
| 45 | + const a1 = hf.getCellValue(adr('A1')) |
| 46 | + expect((a1 as DetailedCellError).type).toBe(ErrorType.SPILL) |
| 47 | + expect(hf.getCellValue(adr('A2'))).toBe('x') |
| 48 | + |
| 49 | + hf.destroy() |
| 50 | + }) |
| 51 | + |
| 52 | + it('OFF: setting an array formula that collides with an occupied cell yields #SPILL! (recalc parity)', () => { |
| 53 | + const data = [ |
| 54 | + [null, null, 1, 2, 3], |
| 55 | + ['x'], |
| 56 | + ] |
| 57 | + |
| 58 | + const hf = HyperFormula.buildFromArray(data, {licenseKey: 'gpl-v3', arrayFunctionResultOverwritesData: false}) |
| 59 | + |
| 60 | + hf.setCellContents(adr('A1'), [['=TRANSPOSE(C1:E1)']]) |
| 61 | + |
| 62 | + const a1 = hf.getCellValue(adr('A1')) |
| 63 | + expect((a1 as DetailedCellError).type).toBe(ErrorType.SPILL) |
| 64 | + expect(hf.getCellValue(adr('A2'))).toBe('x') |
| 65 | + |
| 66 | + hf.destroy() |
| 67 | + }) |
| 68 | + |
| 69 | + it('ON: setting an array formula that collides with an occupied cell overwrites it and the spill lands', () => { |
| 70 | + const data = [ |
| 71 | + [null, null, 1, 2, 3], |
| 72 | + ['x'], |
| 73 | + ] |
| 74 | + |
| 75 | + const hf = HyperFormula.buildFromArray(data, {licenseKey: 'gpl-v3', arrayFunctionResultOverwritesData: true}) |
| 76 | + |
| 77 | + hf.setCellContents(adr('A1'), [['=TRANSPOSE(C1:E1)']]) |
| 78 | + |
| 79 | + expect(hf.getCellValue(adr('A1'))).toBe(1) |
| 80 | + expect(hf.getCellValue(adr('A2'))).toBe(2) |
| 81 | + expect(hf.getCellValue(adr('A3'))).toBe(3) |
| 82 | + |
| 83 | + hf.destroy() |
| 84 | + }) |
| 85 | + |
| 86 | + it('ON: a cell referencing the overwritten occupant reflects the new spilled value (dependent reroute)', () => { |
| 87 | + const data = [ |
| 88 | + [null, '=A2', 1, 2, 3], |
| 89 | + ['x'], |
| 90 | + ] |
| 91 | + |
| 92 | + const hf = HyperFormula.buildFromArray(data, {licenseKey: 'gpl-v3', arrayFunctionResultOverwritesData: true}) |
| 93 | + |
| 94 | + hf.setCellContents(adr('A1'), [['=TRANSPOSE(C1:E1)']]) |
| 95 | + |
| 96 | + expect(hf.getCellValue(adr('A2'))).toBe(2) |
| 97 | + expect(hf.getCellValue(adr('B1'))).toBe(2) |
| 98 | + |
| 99 | + hf.destroy() |
| 100 | + }) |
| 101 | + |
| 102 | + it('OFF: free spill into empty cells still works (no regression)', () => { |
| 103 | + const data = [ |
| 104 | + ['=TRANSPOSE(C1:E1)', null, 1, 2, 3], |
| 105 | + ] |
| 106 | + |
| 107 | + const hf = HyperFormula.buildFromArray(data, {licenseKey: 'gpl-v3', arrayFunctionResultOverwritesData: false}) |
| 108 | + |
| 109 | + expect(hf.getCellValue(adr('A1'))).toBe(1) |
| 110 | + expect(hf.getCellValue(adr('A2'))).toBe(2) |
| 111 | + expect(hf.getCellValue(adr('A3'))).toBe(3) |
| 112 | + |
| 113 | + hf.destroy() |
| 114 | + }) |
| 115 | + |
| 116 | + it('ON: free spill into empty cells still works (no regression when the flag is on but there is no collision)', () => { |
| 117 | + const data = [ |
| 118 | + ['=TRANSPOSE(C1:E1)', null, 1, 2, 3], |
| 119 | + ] |
| 120 | + |
| 121 | + const hf = HyperFormula.buildFromArray(data, {licenseKey: 'gpl-v3', arrayFunctionResultOverwritesData: true}) |
| 122 | + |
| 123 | + expect(hf.getCellValue(adr('A1'))).toBe(1) |
| 124 | + expect(hf.getCellValue(adr('A2'))).toBe(2) |
| 125 | + expect(hf.getCellValue(adr('A3'))).toBe(3) |
| 126 | + |
| 127 | + hf.destroy() |
| 128 | + }) |
| 129 | + |
| 130 | + // OUT OF SCOPE (documented current behavior): the overwrite primitive lives on the |
| 131 | + // setFormulaToCell / setCellContents path (`exchangeOrAddFormulaVertex`). When an array |
| 132 | + // formula AND a conflicting occupant are declared *inline in the same buildFromArray call*, |
| 133 | + // the occupant is processed after the array and GraphBuilder.shrinkArrayIfNeeded shrinks the |
| 134 | + // array back to its corner, so the inline occupant wins at build time even with the flag ON. |
| 135 | + // Asserting current behavior so a future change to this edge is a conscious decision. |
| 136 | + it('ON (out of scope): an occupant declared inline in the same buildFromArray still wins at build time', () => { |
| 137 | + const data = [ |
| 138 | + ['=TRANSPOSE(C1:E1)', null, 1, 2, 3], |
| 139 | + ['x'], |
| 140 | + ] |
| 141 | + |
| 142 | + const hf = HyperFormula.buildFromArray(data, {licenseKey: 'gpl-v3', arrayFunctionResultOverwritesData: true}) |
| 143 | + |
| 144 | + expect(hf.getCellValue(adr('A2'))).toBe('x') |
| 145 | + expect(hf.getCellValue(adr('A3'))).toBe(null) |
| 146 | + |
| 147 | + hf.destroy() |
| 148 | + }) |
| 149 | + |
| 150 | + // Array-vs-array collisions ALWAYS keep #SPILL!, even in overwrite mode: the flag clears |
| 151 | + // static occupants but must never clobber another array (matches Excel, and avoids corrupting |
| 152 | + // the pre-existing array). Guarded by DependencyGraph.canOverwriteArrayResult / overwriteWouldHitArray. |
| 153 | + it('ON: array-vs-array collision stays #SPILL! and leaves the pre-existing array intact', () => { |
| 154 | + // First array at B2 spills B2:B4 = 1,2,3 into free space. |
| 155 | + const hf = HyperFormula.buildFromArray([ |
| 156 | + [null, null, 1, 2, 3], |
| 157 | + [null, '=TRANSPOSE(C1:E1)'], |
| 158 | + ], {licenseKey: 'gpl-v3', arrayFunctionResultOverwritesData: true}) |
| 159 | + expect(hf.getCellValue(adr('B2'))).toBe(1) |
| 160 | + |
| 161 | + // A second array at B1 would spill B1:B2, hitting the first array's anchor at B2. |
| 162 | + hf.setCellContents(adr('B1'), [['=TRANSPOSE(C1:D1)']]) |
| 163 | + |
| 164 | + const b1 = hf.getCellValue(adr('B1')) |
| 165 | + expect(b1 instanceof DetailedCellError).toBe(true) |
| 166 | + expect((b1 as DetailedCellError).type).toBe(ErrorType.SPILL) |
| 167 | + // the pre-existing array is left untouched (no corruption) |
| 168 | + expect(hf.getCellValue(adr('B2'))).toBe(1) |
| 169 | + expect(hf.getCellValue(adr('B3'))).toBe(2) |
| 170 | + expect(hf.getCellValue(adr('B4'))).toBe(3) |
| 171 | + |
| 172 | + hf.destroy() |
| 173 | + }) |
| 174 | + |
| 175 | + it('ON: overwrites a formula occupant (recalc path)', () => { |
| 176 | + const hf = HyperFormula.buildFromArray([ |
| 177 | + [null, null, 1, 2, 3], |
| 178 | + ['=C1+100'], |
| 179 | + ], {licenseKey: 'gpl-v3', arrayFunctionResultOverwritesData: true}) |
| 180 | + expect(hf.getCellValue(adr('A2'))).toBe(101) |
| 181 | + |
| 182 | + hf.setCellContents(adr('A1'), [['=TRANSPOSE(C1:E1)']]) |
| 183 | + |
| 184 | + expect(hf.getCellValue(adr('A1'))).toBe(1) |
| 185 | + expect(hf.getCellValue(adr('A2'))).toBe(2) // formula occupant overwritten |
| 186 | + expect(hf.getCellValue(adr('A3'))).toBe(3) |
| 187 | + |
| 188 | + hf.destroy() |
| 189 | + }) |
| 190 | + |
| 191 | + it('ON: overwrites a 2-D block of occupants (MMULT 2x2)', () => { |
| 192 | + const hf = HyperFormula.buildFromArray([ |
| 193 | + [null, null, null, 1, 0, null, 1, 0], |
| 194 | + ['x', 'y', null, 0, 1, null, 0, 1], |
| 195 | + ], {licenseKey: 'gpl-v3', arrayFunctionResultOverwritesData: true}) |
| 196 | + |
| 197 | + hf.setCellContents(adr('A1'), [['=MMULT(D1:E2,G1:H2)']]) // 2x2 spill A1:B2 over A2='x', B2='y' |
| 198 | + |
| 199 | + expect(hf.getCellValue(adr('A1'))).toBe(1) |
| 200 | + expect(hf.getCellValue(adr('B1'))).toBe(0) |
| 201 | + expect(hf.getCellValue(adr('A2'))).toBe(0) // occupant 'x' overwritten |
| 202 | + expect(hf.getCellValue(adr('B2'))).toBe(1) // occupant 'y' overwritten |
| 203 | + |
| 204 | + hf.destroy() |
| 205 | + }) |
| 206 | + |
| 207 | + // KNOWN LIMITATION (documented in ConfigParams JSDoc + PR): overwrite is destructive and the |
| 208 | + // cleared occupants are NOT recorded on the undo stack, so undo does not restore them. Asserted |
| 209 | + // here so the gap is explicit and a future fix has a red anchor. |
| 210 | + it('ON: undo of an overwrite does NOT restore the overwritten occupant (known limitation)', () => { |
| 211 | + const hf = HyperFormula.buildFromArray([ |
| 212 | + [null, null, 1, 2, 3], |
| 213 | + ['x'], |
| 214 | + ], {licenseKey: 'gpl-v3', arrayFunctionResultOverwritesData: true}) |
| 215 | + hf.setCellContents(adr('A1'), [['=TRANSPOSE(C1:E1)']]) // overwrites A2='x' |
| 216 | + expect(hf.getCellValue(adr('A2'))).toBe(2) |
| 217 | + |
| 218 | + hf.undo() |
| 219 | + |
| 220 | + expect(hf.getCellValue(adr('A1'))).toBe(null) // formula removed (correct) |
| 221 | + // KNOWN GAP: the overwritten 'x' is lost rather than restored. |
| 222 | + expect(hf.getCellValue(adr('A2'))).toBe(null) |
| 223 | + |
| 224 | + hf.destroy() |
| 225 | + }) |
| 226 | +}) |
0 commit comments