簡體   English   中英

使用for循環在.docx文件中使用js與npm docx編寫段落

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM