量angular器:xlsx-template错误无法读取未定义的属性“长度”

我正在使用xlsx-template在我创build的自定义模板的基础上生成Excel文件。 但是我用模板创build一个新的excel文件时出现错误

下面是错误:

E / launcher – 无法读取未定义的属性“长度”

E / launcher – TypeError:无法读取在Workbook.module.exports.Workbook.loadSheet(E:\ latest-code \ latest \ test automation \ node_modules \ xlsx-template \ lib \ index.js:373)未定义的属性'length' E:\ latest-code \ 38)在Workbook.module.exports.Workbook.substitute(E:\ latest-code \ latest \ test automation \ node_modules \ xlsx-template \ lib \ index.js:123:26)在FSReqWrap.readFileAfterClose [ascomplete](fs.js:416:3)上testing自动化\ config \ index.js:117:16

代码片段:

var XlsxTemplate = require('xlsx-template'), fs = require('fs'), path = require('path'); describe("excel", function() { it("excel file", function() { console.log('inside after each...'); console.log(__dirname); fs.readFile(path.join(__dirname, 'Template.xlsx'), function(err, data) { var template = new XlsxTemplate(data); var values = { testcase: [{ id: 1, name: "login", status: "pass" }, { id: 2, name: "logout", status: "fail" }, { id: 3, name: "searchbox", status: "pass" }] } console.log(values); template.substitute(1, values); console.log('excel data'); var newData = template.generate(); fs.writeFileSync('test1.xlsx', newData, 'binary'); }); }) 

我是否缺lessxlsx-template的其他configuration。 请build议如何摆脱这个错误,因为我无法解决它。

提前致谢。

我认为你的问题来自于newData

Template.generate不返回二进制数据。

NodeJs你可以使用:

 var tplt = template.generate(); var binaryTplt = new Buffer(tplt, 'binary'); fs.writeFile(fileName, binaryTplt, function (err) { if (err) return console.log(err); console.log('XLS file saved : ' + fileName); }); 

希望它会帮助你!

Y.

你确定fs.readFilefind这个文件吗?
当你使用template.substitute第一个参数是工作表编号。
如果没有find工作表,则会TypeError: Cannot read property 'length' of undefined at Workbook.module.exports.Workbook.loadSheet附加错误TypeError: Cannot read property 'length' of undefined at Workbook.module.exports.Workbook.loadSheet

你在fs.readFile的callback中有什么err

Y.

发现与我已经安装的xlsx模板的问题。 实际上,在xlsx-template index.js文件的loadSheet方法中, i < self.sheet.length在for循环中使用了i < self.sheet.length ,它应该是i < self.sheets.length 。 我再次安装了xlsx-template,现在工作正常。

谢谢你的帮助:)