// Zero-dependency .xlsx workbook writer with bold frozen headers, typed cells & auto widths. import { Buffer } from "node:buffer"; import { escapeXml as esc, zip } from "./zip.js"; export type Cell = string | number | boolean | null | undefined; export interface Sheet { name: string; header: string[]; rows: Cell[][]; } const NS = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"; const R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"; const REL = "http://schemas.openxmlformats.org/package/2006/relationships"; const XML_HEAD = '\n'; function columnName(index: number): string { let name = ""; let n = index; do { name = String.fromCharCode(65 + (n % 26)) + name; n = Math.floor(n / 26) - 1; } while (n >= 0); return name; } function sheetName(name: string): string { return name.replace(/[[\]:*?/\\]/g, " ").trim().slice(0, 31) || "Sheet"; } function cellXml(ref: string, value: Cell, bold: boolean): string { const s = bold ? ' s="1"' : ""; if (value === null || value === undefined) return ""; if (typeof value === "number") { return Number.isFinite(value) ? `${value}` : ""; } if (typeof value === "boolean") return `${value ? 1 : 0}`; return `${esc(value)}`; } function sheetXml(sheet: Sheet): string { const colCount = Math.max(sheet.header.length, ...sheet.rows.map((r) => r.length), 1); const widths = Array.from({ length: colCount }, (_, c) => Math.min( 60, Math.max( 10, (sheet.header[c] ?? "").length + 2, ...sheet.rows.slice(0, 200).map((r) => String(r[c] ?? "").length + 2), ), ), ); const cols = `${widths.map((w, c) => ``).join("")}`; const allRows: Cell[][] = sheet.header.length > 0 ? [sheet.header, ...sheet.rows] : sheet.rows; const rowsXml = allRows .map( (cells, r) => `${cells.map((v, c) => cellXml(`${columnName(c)}${r + 1}`, v, r === 0 && sheet.header.length > 0)).join("")}`, ) .join(""); const freeze = sheet.header.length > 0 ? '' : ""; return `${XML_HEAD}${freeze}${cols}${rowsXml}`; } export function renderXlsx(sheets: Sheet[]): Buffer { if (sheets.length === 0) throw new Error("A workbook needs at least one sheet."); const names = new Set(); const unique = sheets.map((s) => { let name = sheetName(s.name); let n = 2; while (names.has(name)) name = `${sheetName(s.name).slice(0, 28)} ${n++}`; names.add(name); return name; }); const workbook = `${XML_HEAD}${unique.map((name, i) => ``).join("")}`; const workbookRels = `${XML_HEAD}${sheets.map((_, i) => ``).join("")}`; const styles = `${XML_HEAD}`; const rootRels = `${XML_HEAD}`; const contentTypes = `${XML_HEAD}${sheets.map((_, i) => ``).join("")}`; return zip([ { name: "[Content_Types].xml", data: Buffer.from(contentTypes) }, { name: "_rels/.rels", data: Buffer.from(rootRels) }, { name: "xl/workbook.xml", data: Buffer.from(workbook) }, { name: "xl/_rels/workbook.xml.rels", data: Buffer.from(workbookRels) }, { name: "xl/styles.xml", data: Buffer.from(styles) }, ...sheets.map((s, i) => ({ name: `xl/worksheets/sheet${i + 1}.xml`, data: Buffer.from(sheetXml(s)), })), ]); }