2017-05-07 9 views
2

私が作成したカスタムテンプレートのベースにExcelファイルを生成するのにxlsx-templateを使用しています。分度器:xlsx-templateエラーが未定義のプロパティ 'length'を読み取ることができません

E/launcher - Cannot read property 'length' of undefined

E/launcher - TypeError: Cannot read property 'length' of undefined at Workbook.module.exports.Workbook.loadSheet (E:\latest-code\latest\test automation\node_modules\xlsx-template\lib\index.js:373:38) at Workbook.module.exports.Workbook.substitute (E:\latest-code\latest\test automation\node_modules\xlsx-template\lib\index.js:123:26) at E:\latest-code\latest\test automation\config\index.js:117:16 at 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'); 
      }); 
      }) 

私は、他の構成が足りない新しいExcelは、テンプレートの外に

以下

ファイル作成はエラーになりながら、しかし、私はエラーを取得していますxlsx-templateの場合私はそれを解決することができないので、このエラーを取り除く方法をアドバイスしてください。

ありがとうございます。

+0

こんにちは。事前に感謝:) – prpa1234

答えて

0

は、私がインストールされているのxlsx、テンプレートの問題を発見しました。実際にはxlsx-template index.jsファイルのloadSheetメソッドで、私はi < self.sheets.lengthでなければならないforループでi < self.sheet.lengthを持っていました。 xlsx-templateをもう一度インストールしましたが、今はうまく動作しています。

大変ありがとうございます。

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

は、それはあなたを助けることを願っています!

Y.

+0

返信いただきありがとうございます。実際には 'template.substitute(1、values)'にこのエラーが出ます。 また、私たちはエラーからそれを私に与えることができます 'Workbook.module.exports.Workbook.substitute(E:\最新コード\最新\テストオートメーション\ node_modules \ xlsxテンプレート\ lib \ index.js:123 :26) ' – prpa1234

0

あなたはfs.readFileがファイルを見つけましたか?
template.substituteを使用すると、最初のパラメータはシート番号です。何枚が見つからない場合
、エラーTypeError: Cannot read property 'length' of undefined at Workbook.module.exports.Workbook.loadSheetはあなたがfs.readFileのコールバックでerrに何を持っていない...

を追加しますか?

Y.

関連する問題