主要问题:我想在Microsoft word文档中插入40多个文本。 通过 java 脚本代码:
// requiring modules
const fs = require('fs'),
docx = require('docx');
// create a new doc
const doc = new docx.Document();
// get paragraph text
const getData = new docx.Paragraph({
children:[
new docx.TextRun({
text: 'Welcome from getData',
size: 72
}),
],
})
//add a section to document to render the paragraph
doc.addSection({
children:[
getData
]
});
//write the buffer to the file.docx
docx.Packer.toBuffer(doc).then((Buffer)=>{
fs.writeFileSync("text.docx" , Buffer);
})
我尝试了所有方法,但没有任何效果。
最佳答案
我遇到了同样的问题...但这里有一个解决方法。 Docx.J 的库不允许在 TableCell 或 TableRow 中循环,因此最好在 Table() 之外循环。假设您有一个字符串列表...
let myStrings = ['firstString', 'secondString', 'thirdString'];
const buildParagraph = async(arr)=>{
let paragraphArray = []
arr.map((cur , index)=> {
paragraphArray.push(new Paragraph(cur))
});
return paragraphArray;
};
所以 buildParagrahp() 会给你一个段落数组,就像你想要的那样,你可以把它作为 child 的值放在你的 Table() 中,这也应该适用于你可能尝试循环的其他事情......
关于javascript - 使用for循环在.docx文件中使用js和npm docx写一段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62757767/