fix(sheets): use -1 for unfrozen axis when opening frozen panes (#270)

Co-authored-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
This commit is contained in:
ANIRUDDHA ADAK
2026-09-09 16:15:00 +08:00
committed by GitHub
co-authored by Aniruddha Adak
parent 785d3f3705
commit fa58fff047
2 changed files with 82 additions and 10 deletions
+21 -10
View File
@@ -362,6 +362,26 @@ export function applyFormatPatchToRange(
}
}
/**
* File freeze state mapped onto Univer's `setFreeze` shape for workbook open.
* Univer uses -1 as the "no freeze on this axis" sentinel (see the
* freeze-top-row/freeze-first-col commands); a literal 0 start misaligns the
* frozen viewport or opens the grid blank, so an unfrozen axis maps to -1.
*/
export function toUniverFreeze(freeze: { frozenRows: number; frozenColumns: number } | null): {
freeze?: { xSplit: number; ySplit: number; startRow: number; startColumn: number }
} {
if (freeze === null) return {}
return {
freeze: {
xSplit: freeze.frozenColumns,
ySplit: freeze.frozenRows,
startRow: freeze.frozenRows === 0 ? -1 : freeze.frozenRows,
startColumn: freeze.frozenColumns === 0 ? -1 : freeze.frozenColumns,
},
}
}
export function loadWorkbookSkeleton(runtime: UniverRuntime | null, file: WorkbookFile): void {
if (!runtime) return
const activeWorkbook = runtime.univerAPI.getActiveWorkbook()
@@ -422,16 +442,7 @@ export function loadWorkbookSkeleton(runtime: UniverRuntime | null, file: Workbo
defaultColumnWidth: characterWidthToPixels(
sheet.defaultColumnWidth ?? paddedBaseColumnWidth(sheet.baseColumnWidth),
),
...(sheet.freeze === null
? {}
: {
freeze: {
xSplit: sheet.freeze.frozenColumns,
ySplit: sheet.freeze.frozenRows,
startRow: sheet.freeze.frozenRows,
startColumn: sheet.freeze.frozenColumns,
},
}),
...toUniverFreeze(sheet.freeze),
// Excel restores the saved normal-view zoom on open; without it
// a sheet authored at 70% opens cropped to the 100% viewport.
...(sheet.zoomScale === undefined ? {} : { zoomRatio: sheet.zoomScale / 100 }),
@@ -159,6 +159,67 @@ describe('loadWorkbookSkeleton', () => {
columnCount: 26,
})
})
it('maps an unfrozen axis to the -1 sentinel on open (issue #196)', () => {
const openWithFreeze = (freeze: { frozenRows: number; frozenColumns: number } | null) => {
const created: Array<{ sheets: Record<string, { freeze?: unknown }> }> = []
const runtime = {
univer: undoStub,
univerAPI: {
getActiveWorkbook: () => null,
disposeUnit: () => undefined,
createWorkbook: (config: (typeof created)[number]) => {
created.push(config)
return { getSheetBySheetId: () => null, setActiveSheet: () => undefined }
},
},
}
const file = {
sha256: 'frozen',
name: 'Frozen.xlsx',
visuals: [],
sheets: [
{
id: 'sheet-1',
name: 'Sheet1',
rowCount: 10,
columnCount: 5,
hidden: false,
showGridLines: true,
tabColor: null,
defaultRowHeight: null,
defaultColumnWidth: null,
freeze,
columnWidths: [],
},
],
}
loadWorkbookSkeleton(runtime as never, file as never)
return created[0]?.sheets['sheet-1'] as { freeze?: unknown }
}
// Rows-only and columns-only freezes keep -1 on the unfrozen axis, matching
// the in-app freeze commands; a literal 0 start misaligns the viewport.
expect(openWithFreeze({ frozenRows: 1, frozenColumns: 0 }).freeze).toEqual({
xSplit: 0,
ySplit: 1,
startRow: 1,
startColumn: -1,
})
expect(openWithFreeze({ frozenRows: 0, frozenColumns: 1 }).freeze).toEqual({
xSplit: 1,
ySplit: 0,
startRow: -1,
startColumn: 1,
})
expect(openWithFreeze({ frozenRows: 2, frozenColumns: 3 }).freeze).toEqual({
xSplit: 3,
ySplit: 2,
startRow: 2,
startColumn: 3,
})
expect(openWithFreeze(null)).not.toHaveProperty('freeze')
})
})
interface Range {