[英]Using for loop to write a paragraph inside .docx file using js with npm docx
主要問題:我想在 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() 會給你一個你想要的段落數組,你可以把它作為孩子的值放在你的 Table() 中,這也應該適用於你可能嘗試循環的其他事情......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.