DOCX Javascript Library Appending Table Rows

Viewed 964

I'm currently trying to use the docx javascript library to create a table based on data I have stored in a list. I need to be able to dynamically add more rows to the table depending on the amount of data I have to populate the table but I'm not sure if this is possible.

The way I understand the library is that you need to define the entire table in one line meaning that trying to insert a for-loop to create the number of rows I need would be impossible but I have to imagine that there's a work-around otherwise this library really hasn't been developed with what I imagine is a pretty common use case.

If anyone who has experience using this library has a solution or advice I'd really appreciate some guidance.

Below is the section of code that this post concerns.

this.getPersonnelByLocation('Onsite GTA').forEach(record => {
              new TableRow({
                height: {height: 500, rule: HeightRule.ATLEAST},
                children: [
                  new TableCell({children: []}),
                  new TableCell({
                    new Paragraph({
                      children: [
                        new TextRun({
                          text: record['resourceRole'],
                          size: 24,
                          color: 'black',
                          bold: true
                        })
                      ]
                    })
                  }),
                  new TableCell({children: []}),
                  new TableCell({children: []})
                ]
              })
            }),
2 Answers

This is building on the answer from @SWAT with a slightly adjusted syntax. I have also added a section. Tested and works.

// Building on SWAT's answer above and doc.js documentation       
const tableToAddToDocument = new docx.Table({
            rows: [
                new docx.TableRow({
                    children: [
                        new docx.TableCell({
                            children: [new docx.Paragraph("Row 1 Cell 1 ")],
                        }),
                        new docx.TableCell({
                            children: [ new docx.Paragraph("Row 1 Cell 2 ")],
                        }),
                    ],
                }),
                new docx.TableRow({
                    children: [
                        new docx.TableCell({
                            children: [new docx.Paragraph("Row 2 Cell 1 ")],
                        }),
                        new docx.TableCell({
                            children: [new docx.Paragraph("Row 2 Cell 2 ")],
                        }),
                    ],
                }),
            ],
        });
    
        var tableRow= new docx.TableRow({
            children: [
                new docx.TableCell({
                    children: [new docx.Paragraph("Added Row 3 Cell 1 ")],
                }),
                new docx.TableCell({
                    children: [new docx.Paragraph("Added Row 3 Cell 2 ")],
                }),
            ],
        });
        tableToAddToDocument.root.push(tableRow);
    
        
    
        
    
        const doc = new docx.Document({
            sections: [
                {
                    children: [
                       
                        new docx.Paragraph({
                            children: [
                                new docx.Paragraph({ text: "Table with third row added to it" }),
                                tableToAddToDocument,
                            ],
                        }),
                    ],
                },
            ],
        });
    
        
    
        doc.addSection({
            children: [
                new docx.TableRow({
                    children: [
                        new docx.TableCell({
                            children: [],
                        }),
                        new docx.TableCell({
                            children: [new docx.Paragraph("New Section also added")],
                        }),
                    ],
                }),
            ],
        });
    
        docx.Packer.toBlob(doc).then((blob) => {
            console.log(blob);
            saveAs(blob, "exampleOfAddingRowToTable.docx");
            console.log("Docx generated");
        });
    }

What you need to do is:

function generateRows(){
    let tableRow=new TableRow({
        new TableCell({
            children:[new Paragraph("tableInfo")]         
        })
    })
    table.root.push(tableRow)

}

You need to create the function outside of the declaration of the document. I didt in the beginning. Remember to declare the function after you declare the table in the first place.

Related