fix(sheets): shift whole-column spans on structural edits (#325)

This commit is contained in:
ANIRUDDHA ADAK
2026-09-13 17:34:37 +08:00
committed by GitHub
parent 0b404e1b85
commit 32e16d6a2c
2 changed files with 76 additions and 11 deletions
+36 -1
View File
@@ -187,7 +187,7 @@ export function shiftFormulaRefs(
const segments = formula.split(/("(?:[^"]|"")*")/)
const rewritten = segments.map((segment, index) => {
if (index % 2 === 1) return segment
return segment.replace(
let out = segment.replace(
REF_RE,
(match, quoted, bare, aAbsC, aCol, aAbsR, aRow, bAbsC, bCol, bAbsR, bRow) => {
const prefixSheet = (quoted ?? bare) as string | undefined
@@ -235,6 +235,41 @@ export function shiftFormulaRefs(
return next
},
)
// Whole-column spans (B:B): REF_RE needs a row component, so they need
// their own pass with the same structural semantics ($ does not pin,
// deleted endpoints become #REF!, partial overlap shrinks).
if (spec.axis === 'column') {
out = out.replace(COLUMN_SPAN_RE, (match, quoted, bare, aAbs, aCol, bAbs, bCol) => {
const prefixSheet = (quoted ?? bare) as string | undefined
const applies =
prefixSheet === undefined ? formulaSheetMatchesOp : prefixSheet === opSheetName
if (!applies) return match
const prefix =
prefixSheet === undefined ? '' : `${quoted !== undefined ? `'${quoted}'` : bare}!`
const part = (abs: string, letters: string): RefPart => ({
colAbs: abs,
col: columnIndex(letters),
rowAbs: '',
row: 0,
})
const formatCol = (p: RefPart): string => `${p.colAbs}${columnLabel(p.col)}`
let shiftedFirst = shiftRefPart(part(aAbs as string, aCol as string), spec)
let shiftedSecond = shiftRefPart(part(bAbs as string, bCol as string), spec)
if (!shiftedFirst && !shiftedSecond) {
changed = true
hasRefError = true
return `${prefix}#REF!`
}
if (!shiftedFirst)
shiftedFirst = clampRefPart(part(aAbs as string, aCol as string), spec, 'start')
if (!shiftedSecond)
shiftedSecond = clampRefPart(part(bAbs as string, bCol as string), spec, 'end')
const next = `${prefix}${formatCol(shiftedFirst)}:${formatCol(shiftedSecond)}`
if (next !== match) changed = true
return next
})
}
return out
})
return { formula: rewritten.join(''), changed, hasRefError }
+40 -10
View File
@@ -3,14 +3,30 @@ import { describe, expect, it } from 'vitest'
import { shiftFormulaRefs } from '../src/domain/formula-shift'
import type { StructuralOperation } from '../src/domain/workbook-dsl'
const insertRows = (row: number, count = 1): StructuralOperation =>
({ op: 'insert_rows', sheetId: 's1', row, count })
const deleteRows = (row: number, count = 1): StructuralOperation =>
({ op: 'delete_rows', sheetId: 's1', row, count })
const insertCols = (column: string, count = 1): StructuralOperation =>
({ op: 'insert_cols', sheetId: 's1', column, count })
const deleteCols = (column: string, count = 1): StructuralOperation =>
({ op: 'delete_cols', sheetId: 's1', column, count })
const insertRows = (row: number, count = 1): StructuralOperation => ({
op: 'insert_rows',
sheetId: 's1',
row,
count,
})
const deleteRows = (row: number, count = 1): StructuralOperation => ({
op: 'delete_rows',
sheetId: 's1',
row,
count,
})
const insertCols = (column: string, count = 1): StructuralOperation => ({
op: 'insert_cols',
sheetId: 's1',
column,
count,
})
const deleteCols = (column: string, count = 1): StructuralOperation => ({
op: 'delete_cols',
sheetId: 's1',
column,
count,
})
function shift(formula: string, op: StructuralOperation, sameSheet = true, opSheetName = 'Sheet1') {
return shiftFormulaRefs(formula, op, sameSheet, opSheetName)
@@ -64,6 +80,19 @@ describe('shiftFormulaRefs: column shifts', () => {
expect(shift('=D1', deleteCols('B')).formula).toBe('=C1')
expect(shift('=B1', deleteCols('B')).hasRefError).toBe(true)
})
it('shifts whole-column spans like ordinary ranges', () => {
expect(shift('=SUM(B:B)', insertCols('A')).formula).toBe('=SUM(C:C)')
// structural edits ignore $ anchors, like ordinary refs do
expect(shift('=SUM($B:B)', insertCols('A')).formula).toBe('=SUM($C:C)')
expect(shift('=SUM(B:B)', deleteCols('A')).formula).toBe('=SUM(A:A)')
})
it('errors and shrinks whole-column spans on deletes like ranges do', () => {
expect(shift('=SUM(B:B)', deleteCols('B')).formula).toBe('=SUM(#REF!)')
expect(shift('=SUM(B:B)', deleteCols('B')).hasRefError).toBe(true)
expect(shift('=SUM(A:C)', deleteCols('B')).formula).toBe('=SUM(A:B)')
})
})
describe('shiftFormulaRefs: what must NOT be rewritten', () => {
@@ -83,8 +112,9 @@ describe('shiftFormulaRefs: what must NOT be rewritten', () => {
describe('shiftFormulaRefs: sheet prefixes', () => {
it('rewrites prefixed refs that target the op sheet', () => {
expect(shift("=Sheet1!B5+'Sheet1'!C5", insertRows(3), false).formula)
.toBe("=Sheet1!B6+'Sheet1'!C6")
expect(shift("=Sheet1!B5+'Sheet1'!C5", insertRows(3), false).formula).toBe(
"=Sheet1!B6+'Sheet1'!C6",
)
})
it('leaves prefixed refs to other sheets alone', () => {