From 76550b5dd55b328f2345fdfb3966e4787a0a6511 Mon Sep 17 00:00:00 2001 From: baseballyama Date: Mon, 9 Nov 2026 07:02:58 +0900 Subject: [PATCH] refactor(streaming): write-only column widths reuse serializeCols The write-only writer had its own copy of the coalescing loop added in Build ColumnDimension entries and call the modelled writer's serializeCols so there is one coalescing implementation. Reword the #315 changeset to tell users that a reloaded file holds one columnDimensions entry per range. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MNFNHrh8yhz9PQ3bWTPBHo --- src/streaming/write-only.ts | 30 +++++------------------------- src/worksheet/writer.ts | 2 +- tests/streaming/write-only.test.ts | 2 +- 3 files changed, 7 insertions(+), 27 deletions(-) diff --git a/src/streaming/write-only.ts b/src/streaming/write-only.ts index 8eb6657b..38cd2ac8 100644 --- a/src/streaming/write-only.ts +++ b/src/streaming/write-only.ts @@ -21,8 +21,9 @@ import { OpenXmlIoError, OpenXmlSchemaError } from '../utils/exceptions.js'; import { utf8ByteLength } from '../utils/utf8.js'; import { makeSharedStrings } from '../workbook/shared-strings.js'; import { validateSheetTitle } from '../workbook/workbook.js'; +import type { ColumnDimension } from '../worksheet/dimensions.js'; import { isUsableDimensionSize } from '../worksheet/worksheet.js'; -import { serializeCell } from '../worksheet/writer.js'; +import { serializeCell, serializeCols } from '../worksheet/writer.js'; import { ARC_CONTENT_TYPES, ARC_ROOT_RELS, @@ -176,30 +177,9 @@ const makeWriteOnlyWorksheet = (state: WorkbookState, title: string, sheetId: nu let header = ''; header += ``; if (columnWidths.size > 0) { - header += ''; - const sorted = [...columnWidths.entries()].sort((a, b) => a[0] - b[0]); - // Contiguous columns of equal width merge into one range — the - // same coalescing the modelled writer's serializeCols applies. Widths - // are validated >= 0, so -1 marks "no run yet" without colliding with a - // real first run (column 1 at width 0). - let min = 0; - let max = 0; - let width = -1; - const emitRun = (): void => { - header += ``; - }; - for (const [col, w] of sorted) { - if (col === max + 1 && w === width) { - max = col; - continue; - } - if (min > 0) emitRun(); - min = col; - max = col; - width = w; - } - if (min > 0) emitRun(); - header += ''; + const cols = new Map(); + for (const [col, width] of columnWidths) cols.set(col, { min: col, max: col, width }); + header += serializeCols(cols); } header += ''; writeText(header); diff --git a/src/worksheet/writer.ts b/src/worksheet/writer.ts index 3d1d8c8f..f2064d28 100644 --- a/src/worksheet/writer.ts +++ b/src/worksheet/writer.ts @@ -608,7 +608,7 @@ const maxOutlineLevel = ( return max; }; -const serializeCols = (cols: ReadonlyMap): string => { +export const serializeCols = (cols: ReadonlyMap): string => { const sorted = [...cols.values()].sort((a, b) => a.min - b.min); const parts: string[] = ['']; // Contiguous entries that would emit identical attributes merge into one diff --git a/tests/streaming/write-only.test.ts b/tests/streaming/write-only.test.ts index b6ca984b..475421cf 100644 --- a/tests/streaming/write-only.test.ts +++ b/tests/streaming/write-only.test.ts @@ -239,7 +239,7 @@ describe('createWriteOnlyWorkbook — column widths', () => { expect(getColumnDimension(ws.sheet, 2)?.width).toBe(25.5); }); - it('keeps a zero-width column 1 out of the sentinel run', async () => { + it('keeps a lone zero-width column 1', async () => { const bytes = await writeWorkbook(async (wb) => { const ws = await wb.addWorksheet('Wide'); ws.setColumnWidth(1, 0);