From 3d98e41d2b75662c1cb017f44c58ad763a29a45a Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Thu, 5 Feb 2026 01:54:56 -0700 Subject: [PATCH] Add column width calculation --- scripts/createOdsFile.js | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/scripts/createOdsFile.js b/scripts/createOdsFile.js index ae3e1cb..8141360 100644 --- a/scripts/createOdsFile.js +++ b/scripts/createOdsFile.js @@ -79,6 +79,17 @@ function generateContentFileXMLString(sheetsData) { root.setAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0'); root.setAttribute('office:version', '1.2'); + const styleNode = doc.createElement("office:automatic-styles"); + const boldCellStyleNode = doc.createElement("style:style"); + boldCellStyleNode.setAttribute("style:name", "boldcell"); + boldCellStyleNode.setAttribute("style:family", "table-cell"); + const boldCellTextPropsNode = doc.createElement("style:text-properties"); + boldCellTextPropsNode.setAttribute("fo:font-weight", "bold"); + boldCellStyleNode.appendChild(boldCellTextPropsNode); + styleNode.appendChild(boldCellStyleNode); + + root.appendChild(styleNode); + const bodyNode = doc.createElement('office:body'); root.appendChild(bodyNode); @@ -91,8 +102,29 @@ function generateContentFileXMLString(sheetsData) { tableNode.setAttribute('table:name', sheetName); spreadsheetNode.appendChild(tableNode); - const columnNode = doc.createElement('table:table-column'); - tableNode.appendChild(columnNode); + var columnsWidthChars = {}; + for (let r = 0; r < sheetData.length; r++) { + for (let c = 0; c < sheetData[r].length; c++) { + if (typeof columnsWidthChars[c] == "undefined") { + columnsWidthChars[c] = sheetData[r][c].value.length; + } + columnsWidthChars[c] = Math.max(columnsWidthChars[c], sheetData[r][c].value.length); + } + } + + for (var prop in columnsWidthChars) { + var columnNode = doc.createElement('table:table-column'); + columnNode.setAttribute("table:style-name", "colwidth" + columnsWidthChars[prop]); + tableNode.appendChild(columnNode); + + var columnWidthNode = doc.createElement("style:style"); + columnWidthNode.setAttribute("style:name", "colwidth" + columnsWidthChars[prop]); + columnWidthNode.setAttribute("style:family", "table-column"); + const columnWidthPropsNode = doc.createElement("style:table-column-properties"); + columnWidthPropsNode.setAttribute("style:column-width", `${columnsWidthChars[prop] * 0.26}cm`); + columnWidthNode.appendChild(columnWidthPropsNode); + styleNode.appendChild(columnWidthNode); + } // Iterate through rows sheetData.forEach((row) => {