Why does my Word table exported from docx.js still have equal-width columns? Set the widths with Table.columnWidths

May 30, 2025

While preparing SQL query materials for a hospital's level-five assessment, I wrote a Node.js script to put rows from several CSV files into a Word table. The first time I opened it in LibreOffice, all the data was there, but the table was squeezed into a tiny space. I had to drag it wider to read it. Once I did, I ran into a second problem: an ID only a few characters long got a column as wide as an entire SQL statement. I wanted SQL in the second-to-last column, taking up half the table, and the other five columns to split the other half evenly.

The table already fills the line. Why is the SQL column still so narrow?

My first script set the whole table to 100% width, then set each cell's width to a percentage calculated as 100 / number of fields. The first value tells the layout program how much space the whole table would like. The second gives all six columns exactly the same value. I later tried widening the SQL cells individually, but when I opened the document, the columns were still the same width.

What's easy to overlook is the column grid shared by the entire table. Picture a set of vertical lines extending from the header through the last row. In the version I used, docx@9.5.0, new Table({ columnWidths }) writes the array into the Word document's w:tblGrid, with one w:gridCol per column. If you omit the array, this version gives each column the same initial value, 100.1

TableCell.width, meanwhile, is written to the cell's own w:tcW. The specification says, “This element specifies the preferred width for this table cell.” The word preferred matters. The layout program still needs to keep column boundaries consistent across rows and can adjust widths to fit unbreakable text. I wanted the fifth column in every row to take up half the table. Writing that proportion directly into the column grid expresses that intent.2

I also came across an issue with a seemingly perfect title, Unable to set cell's width. It concerned the TableCell constructor arguments in 5.0.0-rc6, and its fix was merged back in 2019. My project used 9.5.0 from the start. I ultimately changed the code that generated the table.34

How do I make the fifth column of a six-column table take up half the width?

I chose a total width of 9010 and assigned 4505 to SQL and 901 to each of the other five columns. The unit is DXA: 20 units equal one point, making 9010 about 15.9 centimeters. If you change the paper size or margins, calculate a new total based on the available page width, then distribute it across the columns.5

This minimal example is adapted from my original six-column script. The original calculated column and cell widths separately, and the two sets of numbers eventually drifted apart. Here the table width, column grid, and cells all draw from one array. In a Node.js project with docx@9.5.0 installed, save the code as example.cjs, run node example.cjs, and open the resulting example.docx in LibreOffice.

const { writeFileSync } = require("node:fs");
const { Document, Packer, Paragraph, Table, TableCell, TableRow, WidthType } = require("docx");

const columnWidths = [901, 901, 901, 901, 4505, 901];
const records = [
  ["1", "门诊", "统计", "就诊数", "SELECT COUNT(*) FROM visits", "42"],
];

const rows = records.map(values => new TableRow({
  children: values.map((value, index) => new TableCell({
    width: { size: columnWidths[index], type: WidthType.DXA },
    children: [new Paragraph(value)],
  })),
}));

const table = new Table({
  width: { size: columnWidths.reduce((sum, width) => sum + width, 0), type: WidthType.DXA },
  columnWidths,
  rows,
});
const doc = new Document({ sections: [{ children: [table] }] });
Packer.toBuffer(doc).then(buffer => writeFileSync("example.docx", buffer));

What did the final exported table look like?

On May 30 I first committed the version where SQL took up half the table. As I assembled the full materials, I found the execution results in the last column also contained long text. Later that day I changed the grid to [350, 550, 750, 550, 3355, 3355], giving most of the space to SQL and results side by side. I checked the change in two steps:

  1. Inspect what the script wrote. You can open a DOCX as a ZIP archive. After extracting it, find <w:tblGrid> in word/document.xml and read the w:w values of its six <w:gridCol> elements in order. The original exported file I kept contained exactly the six numbers above.
  2. Inspect what readers actually saw. Later I reopened two saved documents in LibreOffice and exported each as a PDF. Both ran to 81 pages. Measuring the vertical lines in the first page's table showed that the first four columns took up about 4%, 6%, 8%, and 6%, respectively. SQL and results each took up roughly 38%. The table I'd originally had to drag wider now allocated space according to the importance of its contents.

Widening the SQL column reduces wrapping, but doesn't solve the problem of arbitrarily long execution results. I had hoped to show only a few lines in the cell while still letting readers copy the whole text. The final script truncated long fields, and I later raised the cap to 5,000 characters. To copy a complete result beyond that cap, I still have to go back to the original CSV.

Footnotes

  1. docx contributors. (n.d.). Table constructor (docx 9.5.0). GitHub. Default column widths and construction of TableGrid. ↩

  2. Microsoft. (n.d.). TableCellWidth class. Microsoft Learn. Remarks, “tcW (Preferred Table Cell Width)”, first paragraph and the list that follows. ↩

  3. exsesx. (2019, September 30). Unable to set cell's width (Issue #411). GitHub. Problem description and affected version. ↩

  4. seiya-git. (2019, October 11). Fix set table cell width (Pull request #424). GitHub. Fix description and merge record. ↩

  5. docx contributors. (n.d.). Tables. docx documentation. Set width of a cell. ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-05-30). Why does my Word table exported from docx.js still have equal-width columns? Set the widths with Table.columnWidths. https://caiborui.com/blog/2025-05-30-zh-docx-table-column-widths-entrans

@misc{borui2025-1ab82848,
  author = {borui},
  title = {{Why does my Word table exported from docx.js still have equal-width columns? Set the widths with Table.columnWidths}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-05-30-zh-docx-table-column-widths-entrans}
}