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

时间:2017-05-07 13:04:37

标签: javascript node.js excel protractor

我正在使用xlsx-template在我创建的自定义模板的基础上生成Excel文件。但是我在从模板

创建新的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:38)       在Workbook.module.exports.Workbook.substitute(E:\ latest-code \ latest \ test automation \ node_modules \ xlsx-template \ lib \ index.js:123:26)       在E:\ latest-code \ latest \ test automation \ config \ index.js:117:16       在FSReqWrap.readFileAfterClose [as oncomplete](fs.js:416:3)

代码段:

 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');
            });
          })

我错过了xlsx-template的任何其他配置。请告知如何摆脱这个错误,因为我无法解决它。

提前致谢。

2 个答案:

答案 0 :(得分:0)

我认为您的问题来自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);
});

希望它会对你有所帮助!

ý。

答案 1 :(得分:0)

您确定fs.readFile找到了该文件吗? 当您使用template.substitute时,第一个参数是表格编号 如果找不到工作表,则错误TypeError: Cannot read property 'length' of undefined at Workbook.module.exports.Workbook.loadSheet会附加...

你在err的回调中对fs.readFile有什么看法?
收率

相关问题