Docx: table width property not working for google and microsoft doc

Created on 7 Jul 2019  路  4Comments  路  Source: dolanmiu/docx

Hi, all I have trying around to create a table with width 100% having two columns with width 50% each, but the table is not taking full doc width in Microsoft doc and google doc. Here is the code that i am using

````
let rowmatch = Object.keys(state.match_keys).length;
const tablematch = doc.createTable(rowmatch, 2).setWidth('pct', '100%');

Object.keys(state.match_keys).map((key, i) => {
const parakey = new docx.Paragraph().spacing({
before: 20,
after: 20
});

parakey.addRun(
  new docx.TextRun(`${key}`)
    .font("Verdana")
    .size(20)
    .color("#2b2b2b")
);

parakey.addRun(new docx.TextRun("").break());
const paraval = new docx.Paragraph().spacing({
  before: 20,
  after: 20
});

paraval.addRun(
  new docx.TextRun(`${state.match_keys[key]}`)
    .font("Verdana")
    .size(20)
    .color("#2b2b2b")
);

paraval.addRun(new docx.TextRun("").break());

tablematch.getCell(i, 0).addContent(parakey).CellProperties.setWidth('50%', 'pct');
tablematch.getCell(i, 1).addContent(paraval).CellProperties.setWidth('50%', 'pct');

});
````

I am using docx version 4.7.1
I have attached the screenshot for reference.

Screenshot 2019-07-07 at 2 18 32 PM

Most helpful comment

@dolanmiu it looks like it's a unit problem, Google Docs does not like percents.
MS Word generates xml like this (which is GDocs compatible) :

<w:tblW w:w="0" w:type="auto"/>
...
<w:gridCol w:w="1795"/>

And docx generates this :

<w:tblW w:type="pct" w:w="80%"/>
...
<w:gridCol w:w="100"/>

It looks like the global width in % is ignored, and the gridCol value is interpreted as DXA.
And the MS way is to set explicitly each column in DXA.
Workaround with current version:

new docx.Table({
    rows: 5,
    columns: 3,
    width: 0, // AUTO
    columnWidths: [3213, 3213, 3212] // total page width is 9638 DXA for A4 portrait
});

All 4 comments

Tables are only supported in Word. I use Tab Stops to work around this, they work perfectly in both Pages and Google Docs.

@carlbolduc Is there any way to make it supported. And how do you use Tab Stops to work around that?
Actually if you open it in microsoft word and save it it will work fine for pages and google docs.

Table on google docs aren't supported at this moment

Would appreciate it if anyone can figure out why it has this weird behaviour

@dolanmiu it looks like it's a unit problem, Google Docs does not like percents.
MS Word generates xml like this (which is GDocs compatible) :

<w:tblW w:w="0" w:type="auto"/>
...
<w:gridCol w:w="1795"/>

And docx generates this :

<w:tblW w:type="pct" w:w="80%"/>
...
<w:gridCol w:w="100"/>

It looks like the global width in % is ignored, and the gridCol value is interpreted as DXA.
And the MS way is to set explicitly each column in DXA.
Workaround with current version:

new docx.Table({
    rows: 5,
    columns: 3,
    width: 0, // AUTO
    columnWidths: [3213, 3213, 3212] // total page width is 9638 DXA for A4 portrait
});
Was this page helpful?
0 / 5 - 0 ratings