NodeJS Docxtemplater angular-表达式不适用于嵌套对象

wljmcqd8  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(124)

我试图在nodejs中使用docxtemplater生成一个.docx文件。我也按照文档中的说明使用了angular-expressions库。问题是,每次我试图访问嵌套对象的字段时,生成的文档中都没有定义。我似乎无法找出我做错了什么.在模板文件中,我使用{field.property}访问字段。

const PizZip = require("pizzip");
const Docxtemplater = require("docxtemplater");
const angularParser = require("docxtemplater/expressions");

const fs = require("fs");
const { getTemplateFilePath, getGeneratedFilePath } = require("./files.utils");

function generateAnnex1(data, outputName) {
  const templatePath = getTemplateFilePath("annex_1");
  const ouputPath = getGeneratedFilePath("annex_1", outputName);

  generateDoc(templatePath, ouputPath, data);
}

function generateDoc(templatePath, outputPath, data) {
  const content = fs.readFileSync(templatePath, "binary");
  const zip = new PizZip(content);

  const doc = new Docxtemplater(zip, {
    parser: angularParser,
  });

  doc.render(data);

  const buf = doc.getZip().generate({
    type: "nodebuffer",
    compression: "DEFLATE",
  });

  fs.writeFileSync(outputPath, buf);
}

字符串

r8xiu3jd

r8xiu3jd1#

把这部分改了,

const doc = new Docxtemplater(zip, {
    parser: angularParser,
  });

字符串
喜欢这个,

const expressionParser = require('docxtemplater/expressions.js')
  const doc = new Docxtemplater(zip, {
    parser: (tagString) => {
            const parser = expressionParser(tagString);
              return {
                get(scope, context) {
                  const module =
                  context &&
                  context.meta &&
                  context.meta.part &&
                  context.meta.part.module;
                  const isIncludeTag =
                    [
                      "pro-xml-templating/subsection-module",
                      "pro-xml-templating/replacesection-module",
                    ].indexOf(module) !== -1;
                    const value = parser.get(scope, context);
                    if (isIncludeTag && value) {
                      value.render(scope);
                    }
                    return value;
            },
         };
      },
  });

相关问题