Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 5 additions & 25 deletions src/streaming/write-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -176,30 +177,9 @@ const makeWriteOnlyWorksheet = (state: WorkbookState, title: string, sheetId: nu
let header = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
header += `<worksheet xmlns="${SHEET_MAIN_NS}" xmlns:r="${REL_NS}">`;
if (columnWidths.size > 0) {
header += '<cols>';
const sorted = [...columnWidths.entries()].sort((a, b) => a[0] - b[0]);
// Contiguous columns of equal width merge into one <col> 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 += `<col min="${min}" max="${max}" width="${width}" customWidth="1"/>`;
};
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 += '</cols>';
const cols = new Map<number, ColumnDimension>();
for (const [col, width] of columnWidths) cols.set(col, { min: col, max: col, width });
header += serializeCols(cols);
}
header += '<sheetData>';
writeText(header);
Expand Down
2 changes: 1 addition & 1 deletion src/worksheet/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ const maxOutlineLevel = (
return max;
};

const serializeCols = (cols: ReadonlyMap<number, ColumnDimension>): string => {
export const serializeCols = (cols: ReadonlyMap<number, ColumnDimension>): string => {
const sorted = [...cols.values()].sort((a, b) => a.min - b.min);
const parts: string[] = ['<cols>'];
// Contiguous entries that would emit identical attributes merge into one
Expand Down
2 changes: 1 addition & 1 deletion tests/streaming/write-only.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading