简体   繁体   中英

docxtemplater using setData based on the value of another element

I'm using the wonderful docxtemplater for node, which is working perfectly.

However I want to be able to complete the template based on a value of a paragraph element on the page.

I've played around with various syntax styles but still can't seem to get the result I am after. Hopefully the code belows shows what I am trying to complete - apologies am fairly new with js.

<p id="text">Some text</p>

<script type="text/javascript">

    function myFunction() {
        var loadFile=function(url,callback){
            JSZipUtils.getBinaryContent(url,callback);
        }
        loadFile("Letter.docx",function(err,content){
            if (err) { throw e};
            doc=new Docxgen(content);

            doc.setData(
                document.getElementById("text").innerHTML;
            );

            doc.render()
            out=doc.getZip().generate({type:"blob"})
            saveAs(out,"Letter - Updated.docx")
        });
    }
</script>

If your template contained the text Hello {name}

You would have to write the following :

<p id="text">Some text</p>

<script type="text/javascript">

    function myFunction() {
        var loadFile=function(url,callback){
            JSZipUtils.getBinaryContent(url,callback);
        }
        loadFile("Letter.docx",function(err,content){
            if (err) { throw e};
            doc=new Docxgen(content);

            doc.setData({
                name: document.getElementById("text").innerHTML
            });

            doc.render()
            out=doc.getZip().generate({type:"blob"})
            saveAs(out,"Letter - Updated.docx")
        });
    }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM