question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

table width property not working for google and microsoft doc

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
fpirschcommented, Aug 8, 2019

@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
});
0reactions
ZeroJsuscommented, May 27, 2022

@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
});

I it work on Google Docs. width doesn’t have to be set.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Column width won't adjust - Google Groups
1. Disable auto resizing (Options... on the Table tab). · 2. Disable any exact width for the entire table. · 3. Clear any...
Read more >
fixing table width - Microsoft Community
Here's how you should follow: Select the table, then right click on it. ... Go back to Table Properties > Under Column Tab,...
Read more >
How to fix tables that run off the page in Google Docs
The solution. The solution is to go to Table>Table Properties and then uncheck the column width button. This allows the table to resize....
Read more >
5 Common table problems & its best solution - YouTube
Following are 5 most common problems encountered while working with tables in Ms Word. This video shows how to solve these problems in...
Read more >
Google Docs: Fixing Tables You Copied and Pasted
To fix this, right-click on the column and choose “Table properties.” right click on the column and choose table properties. Adjust Column Width....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found