我正在尝试设置一个简单的测试项目来通过浏览器应用程序测试 ExcelJS,而在我的一生中,我无法让 Browserify 正常工作。
我使用适当的 require 调用将我的代码捆绑到一个包文件中,并且在加载页面时生成的包会引发以下错误:
Uncaught TypeError: Cannot read property 'prototype' of undefined
这个错误似乎是在 fs.ReadStream 方法上引发的?
这是我的代码:
索引.html
<!DOCTYPE html>
<head>
<title>Test Excel JS</title>
<meta charset="utf-8">
<meta name="description" content="">
<link rel="stylesheet" href="app.css">
</head>
<body>
<div>
<label>Test</label>
<button onclick="test()">Test this Stuff and Check your console log</button>
</div>
<script src="bundle.js"></script>
<script>
var test = function(){
var workbook = generateTestFile();
console.log(workbook);
};
</script>
</body>
</html>
app.js(bundle.js 的基础)
'use strict';
var Excel = require('exceljs');
var isBold = function(dataRow){
return dataRow.name === "Jeff";
};
var getRowColor = function(dataRow){
return dataRow.color;
};
var getCellColor = function(dataRow, cell){
return (dataRow.name === 'John' && cell.value === 0)? 'orange' : dataRow.color;
};
var getFont = function(isBold, color){
return {
name: 'Arial Black',
color: color,
family: 2,
size: 14,
bold: isBold
};
};
var getTestHeader = function(){
return [
{key: "id", header: "Id"},
{key: "name", header: "Name", width: 32},
{key: "color", header: "Color", width: 10}
];
};
var getTestData = function(){
return [
{
id: 0,
name: "John",
color: "green"
},
{
id: 1,
name: "Rehan",
color: "blue"
},
{
id: 2,
name: "Jeff",
color: "yellow"
}
];
};
var generateTestFile = function(){
var workbook = new Excel.Workbook();
var worksheet = workbook.addWorksheet('Sheet 1');
//Set Column Headers
worksheet.columns = getTestHeader();
//Add Rows
var testData = getTestData();
var length = testData.length;
for(var i = 0; i < length; i++){
worksheet.addRow(testData[i]);
}
//Format Rows
worksheet.eachRow(function(row, rowNumber){
console.log(row);
var isBold = isBold(row);
var rowColor = getRowcolor(row);
row.eachCell(function(cell, colNumber){
var cellColor = getCellColor(row, cell);
console.log(cell);
});
});
return workbook;
};
我怎样才能让它工作?我真正想做的就是测试excelJS。我不在乎解决方案是什么,只要我能验证该库是否有效,我就会很高兴。