docx icon indicating copy to clipboard operation
docx copied to clipboard

Table layout fixed has no effect

Open Niccober opened this issue 2 years ago • 3 comments

I am trying to generate a table with a fixed width for each column. According to the Open XML docs (http://officeopenxml.com/WPtableLayout.php) setting the TableLayout parameter should cause the table in the word document to keep the inital column widths independent of the cell contents. So whenever a string becomes too long the cell should not auto-extend but keep its size - forcing the text to wrap (even if it does not look nice and the break happens within a single word).

In my understanding this should be achievable using the TableLayout property if it is set to FIXED. This approach was also proposed here in the past: https://github.com/dolanmiu/docx/issues/1139

Here's the code I use to generate the table:

new docx.Table({
  width: {
    size: 100,
    type: docx.WidthType.PERCENTAGE
  },
  layout: docx.TableLayoutType.FIXED,
  rows: [
    new docx.TableRow({ children: [
      new docx.TableCell({
        width:{
          size: 50,
          type: docx.WidthType.PERCENTAGE
        },
        children:[
          new docx.Paragraph({ children: [
            new docx.TextRun({ text: 'Lovely text for lovely paragraph'})
          ] })
        ]
      }),
      new docx.TableCell({
        width:{
          size: 50,
          type: docx.WidthType.PERCENTAGE
        },
        children:[
          new docx.Paragraph({ children: [
            new docx.TextRun({ text: 'longlonglonglonglonglonglonglonglonglonglonglongloonglonglonglonglonglonglonglonglong'})
          ] })
        ]
      })
    ]})
  ]
})

The result looks like this: image

So although in the code is specified that the table should have a width of 50 % for each cell and the layout should be fixed, the table still auto-adjusts the column widths. The behaviour is the same if I specify the cell widhts in DXA instead of percent. No matter whether I set the table layout to fixed or not, the rendered table in the word file always auto-adjusts the columns when the contents become too long. In the table properties this circumstance is also reflected: image

The highlighted property says "Allow automatic size adjustment" and should refer to the TableLayout property. When I manually deselect that property, the table is rendered as expected and forces the text in the second column to wrap: image

I am not sure if I am doing something wrong or if this is maybe a bug. Any help on that would be highly appreciated. Regarding that issue I am also wondering what is the difference between defining the columnWidths property directly at the table object vs. providing the widths in the table cells objects using the width object with size and type? Do they cause different behaviour or is it the same?

Niccober avatar Feb 16 '23 18:02 Niccober

hi @Niccober i'm not sure if my response would work to you, but when i use the width for a cell i define this like:

width: { size: '5%', type: WidthType.PERCENTAGE },

the size as string with the simbol of percentage (%) saludos!

damian-garrido avatar Mar 02 '23 19:03 damian-garrido

Hi @damian-garrido thanks your answer but it has no effect I am afraid. Interestingly, your approach does not throw an error but according to the docs, size should actually be a numeric and then be interpreted in combination with the type property.

@dolanmiu Do you have an idea regarding the inital issue (see above)?

Niccober avatar Mar 09 '23 18:03 Niccober

I got the same problem as @Niccober here. Has anyone found out a way for this to work? Appreciate the help!

QuangDuke avatar Jul 13 '24 02:07 QuangDuke