0

I am using npm docx package to dynamically generate table based on the value of a heading in my data structure which is like data : { heading: value }, what I want is that if the value is not present or undefined the table row should be empty, however, if I try to make TableRow's children empty, the doc is being generated but word cannot open it.

Here's my snippet where I am dynamically generating the table.

const createTableCell = (heading: string, value: string): TableRow => {
    if (value) {
        return new TableRow({
            children: [
                new TableCell({
                    children: [
                        new Paragraph({
                            text: heading,
                            heading: HeadingLevel.HEADING_3,
                            indent: { start: 100, end: 100 },
                        }),
                    ],
                    width: {
                        size: 20,
                        type: WidthType.PERCENTAGE,
                    },
                }),
                new TableCell({
                    children: [
                        new Paragraph({
                            text: value,
                            heading: HeadingLevel.HEADING_4,
                            indent: { start: 100, end: 100 },
                        }),
                    ],
                }),
            ],
        });
    } else {
        return new TableRow({
            children: [
                new TableCell({
                    children: [],
                }),
            ],
        });
    }
};

If I make children [], word is not able to open the file and with the current code the table looks like this.

enter image description here

Kushal Jaiswal
  • 103
  • 1
  • 5

0 Answers0