Docx: Set table cell width

Created on 17 Jul 2018  路  8Comments  路  Source: dolanmiu/docx

Hi,

I'm struggling to change the cell width. My code below doesn't have any affect on the table (the cells are both evenly spaced) What am I doing wrong?

var table = new docx.Table(1, 2); table.getCell(0, 1).cellProperties.setWidth(80, "pct"); table.getCell(0, 0).cellProperties.setWidth(20, "pct"); doc.addTable(table);

bug feature request

Most helpful comment

Hi @dwenders and @dolanmiu ,

Yes, it will only work on table cell level.
When using PERCENTAGE, always send string including the % sign (or it will not work in Word)-> tableCell.cellProperties.setWidth('20%', WidthType.PERCENTAGE)

More background (if someone is interested):

Because Microsoft Word and Libre Office work little different, the only solution that works in both cases is to define the width of each cell in the table. Defining on table level (meaning using GridCol) is not going to work.
Because of that limitation, we have API that only works on cell level. It's a manual work (adding on every cell) but we follow a different concept here. Because on each cell you can have vertical merge or horizontal span - you need to define for each cell, and having more global API (on table level) will be confusing from where to get the width.
Columns on the tables can have different sizes than the actual cells in the rows (because of merging and span of each individual cell).

tableCell.cellProperties.setWidth(width: string | number, type: WidthType);

export enum WidthType {
    /** Auto. */
    AUTO = "auto",
    /** Value is in twentieths of a point */
    DXA = "dxa",
    /** No (empty) value. */
    NIL = "nil",
    /** Value is in percentage. */
    PERCENTAGE = "pct",
}

All 8 comments

Hi @ee0jmt,

Can you try to add % also. Something like:
.setWidth('80%', pct')

This works for me

const table = doc.createTable(3, 2);
table.setWidth(docx.WidthType.PERCENTAGE, '98%');
table.getCell(1, 1).addContent(new docx.Paragraph('Hello'));

Hi,

I want to have all columns in the same size. How is that possible? Tried to change the tableCell width.

tableCell.CellProperties.setWidth(widthPercentage, docx.WidthType.PERCENTAGE);

I have used '20%' and 20 for widthPercentage.

Nothing helped... .
Is there a way to set the width static?
But nice and clean API!

AFAIK, this is not a feature yet

tableCell is just an individual cell, you are looking for a column or GridCol, but I can't see any methods on it

Can't see any methods on table too

Correct me if i am wrong @h4buli

Hi @dwenders and @dolanmiu ,

Yes, it will only work on table cell level.
When using PERCENTAGE, always send string including the % sign (or it will not work in Word)-> tableCell.cellProperties.setWidth('20%', WidthType.PERCENTAGE)

More background (if someone is interested):

Because Microsoft Word and Libre Office work little different, the only solution that works in both cases is to define the width of each cell in the table. Defining on table level (meaning using GridCol) is not going to work.
Because of that limitation, we have API that only works on cell level. It's a manual work (adding on every cell) but we follow a different concept here. Because on each cell you can have vertical merge or horizontal span - you need to define for each cell, and having more global API (on table level) will be confusing from where to get the width.
Columns on the tables can have different sizes than the actual cells in the rows (because of merging and span of each individual cell).

tableCell.cellProperties.setWidth(width: string | number, type: WidthType);

export enum WidthType {
    /** Auto. */
    AUTO = "auto",
    /** Value is in twentieths of a point */
    DXA = "dxa",
    /** No (empty) value. */
    NIL = "nil",
    /** Value is in percentage. */
    PERCENTAGE = "pct",
}

Hi, @AlexanderLukin , i tried your solved ways , but it's not work for me. i tried all method in the source code. so sad :( .
i'm working on mac
the version of docx.js : the latest

Cell widths only seem to work in version 4.3.0, version 4.7.1 and above the cell widths are all zero

Was this page helpful?
0 / 5 - 0 ratings