我正在使用该docxtemplater
库来生成一个 word 文件。当我在同一行中使用多个标签时,它会提示以下错误:
"error":{"name":"TemplateError","message":"Multi error","stack":"Error: Multi error\n at new XTTemplateError (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\errors.js:24:16)\n
at throwMultiError (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\errors.js:76:13)\n at verifyErrors (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\docxtemplater.js:511:5)\n at Docxtemplater.compile (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\docxtemplater.js:345:7)\n at new Docxtemplater (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\docxtemplater.js:97:12)\n at PublicAPI.genMonthlyReport (E:\\Users\\cstsang\\workspace\\coss_sla\\server\\publicAPI.js:29:27)\n at Layer.handle [as handle_request] (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\express\\lib\\router\\layer.js:95:5)\n at next (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\express\\lib\\router\\route.js:137:13)\n at Route.dispatch (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\express\\lib\\router\\route.js:112:3)\n at Layer.handle [as handle_request] (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\express\\lib\\router\\layer.js:95:5)","properties":{"errors":[{"name":"TemplateError","message":"Unbalanced loop tag","stack":"Error: Unbalanced loop tag\n at new XTTemplateError (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\errors.js:24:16)\n at getUnbalancedLoopException (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\errors.js:234:13)\n at E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\modules\\expand-pair-trait.js:254:21\n at Array.map (<anonymous>)\n at Object.postparse (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\modules\\expand-pair-trait.js:213:31)\n at E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\parser.js:73:24\n at Array.reduce (<anonymous>)\n at _postparse (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\parser.js:72:22)\n at Object.postparse (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\parser.js:94:19)\n at XmlTemplater.parse (E:\\Users\\cstsang\\workspace\\coss_sla\\node_modules\\docxtemplater\\js\\xml-templater.js:172:38)","properties":{"id":"unbalanced_loop_tags","explanation":"Unbalanced loop tags {#actionTypeSummary}{/actionTypeSummary}{#abc}{/abc}","offset":[2575,2640],"lastPair":{"left":"actionTypeSummary","right":"actionTypeSummary"},"pair":{"left":"abc","right":"abc"},"file":"word/document.xml"}}],"id":"multi_error","explanation":"The template has multiple errors"}}}
下图为word文件模板情况供大家参考。
当我删除所有{abc}
标签时,它工作正常。
publicAPI.js 的代码片段如下:
let content = fs.readFileSync(path.resolve(__dirname, 'template.docx'), 'binary');
let zip = new PizZip(content);
let doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true });
声明let doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true });
是 publicAPI.js 的第 29 行