现在我有一个页面可以打开并读取excel表格并根据我拥有的index.jade文件处理这些数据。我希望能够为其他路由器页面重复使用完全相同的代码,并且只对每个页面更改Excel工作表,以便根据不同的Excel工作表显示数据。
因此我可能只需将此代码转换为可在每个router.get函数中使用的函数,而不仅仅是主页。
router.get('/homepage', function(req, res) {
var workbook= xlsx.readFile('C:\Users\user\Desktop\homepage.xlsx');
var listStuff = [];
results1 = [];
results2 = [];
results3 = [];
_.each(workbook.SheetNames, function(value, key, collection) {
listStuff.push(value);
var address_of_cell = 'O3'
var worksheet = workbook.Sheets[value];
var desired_cell = worksheet[address_of_cell];
var percent_1 = desired_cell.v;
results1.push(percent_1)
var address_of_cell = 'P3'
var worksheet = workbook.Sheets[value];
var desired_cell = worksheet[address_of_cell];
var percent_2 = desired_cell.v;
results2.push(percent_2)
var address_of_cell = 'Q3'
var worksheet = workbook.Sheets[value];
var desired_cell = worksheet[address_of_cell];
var percent_3 = desired_cell.v;
results3.push(percent_3)
})
res.render('index', { title: 'Blah Blah', listStuff: listStuff, workbook: workbook, percentofData: results1, percentofData2: results2, percentofData3: results3});
});
所以我想要更多像上面那样使用相同代码的页面,其中只有excel表的位置会在页面之间发生变化。如何将上述代码转换为可以在接下来的几页中重复使用的函数?:
router.get('/page2', function(req, res) {
var workbook= xlsx.readFile('C:\Users\user\Desktop\page1.xlsx');
res.send('HelloWorld')
})
router.get('/page3', function(req, res) {
var workbook= xlsx.readFile('C:\Users\user\Desktop\page2.xlsx');
res.send('HelloWorld')
})
router.get('/page4', function(req, res) {
var workbook= xlsx.readFile('C:\Users\user\Desktop\page3.xlsx');
res.send('HelloWorld')
})
等.... 因此我需要excel工作簿变量从homepage.xlsx更改为page1.xlsx,page2.xlsx,page3.xlsx等。在函数中。我希望这一切都在同一个index.js文件中完成。
答案 0 :(得分:1)
如果将所有重复逻辑移动到函数中怎么样:
function processXLSXFile(filePath) {
var workbook= xlsx.readFile(filePath);
var listStuff = [];
results1 = [];
results2 = [];
results3 = [];
_.each(workbook.SheetNames, function(value, key, collection) {
listStuff.push(value);
var address_of_cell = 'O3'
var worksheet = workbook.Sheets[value];
var desired_cell = worksheet[address_of_cell];
var percent_1 = desired_cell.v;
results1.push(percent_1)
var address_of_cell = 'P3'
var worksheet = workbook.Sheets[value];
var desired_cell = worksheet[address_of_cell];
var percent_2 = desired_cell.v;
results2.push(percent_2)
var address_of_cell = 'Q3'
var worksheet = workbook.Sheets[value];
var desired_cell = worksheet[address_of_cell];
var percent_3 = desired_cell.v;
results3.push(percent_3)
})
return {
listStuff: listStuff,
results1: results1,
results2: results2,
results3: results3
};
}
然后您可以在路由处理程序中重复使用它:
router.get('/homepage', function(req, res) {
var result = processXLSXFile('C:\Users\user\Desktop\homepage.xlsx');
var listStuff = result.listStuff;
var workbook = result.workbook;
var results1 = result.results1;
var results2 = result.results2;
var results3 = result.results3;
res.render('index', { title: 'Blah Blah', listStuff: listStuff, workbook: workbook, percentofData: results1, percentofData2: results2, percentofData3: results3});
});
router.get('/page2', function(req, res) {
var result = processXLSXFile('C:\Users\user\Desktop\page1.xlsx');
var listStuff = result.listStuff;
var workbook = result.workbook;
var results1 = result.results1;
var results2 = result.results2;
var results3 = result.results3;
res.send('HelloWorld')
})
router.get('/page3', function(req, res) {
var result = processXLSXFile('C:\Users\user\Desktop\page2.xlsx');
var listStuff = result.listStuff;
var workbook = result.workbook;
var results1 = result.results1;
var results2 = result.results2;
var results3 = result.results3;
res.send('HelloWorld')
})
router.get('/page4', function(req, res) {
var result = processXLSXFile('C:\Users\user\Desktop\page3.xlsx');
var listStuff = result.listStuff;
var workbook = result.workbook;
var results1 = result.results1;
var results2 = result.results2;
var results3 = result.results3;
res.send('HelloWorld')
})
希望这可以帮助您解决疑问。
答案 1 :(得分:0)
如果您允许使用' / page / 2',' / page / 3'等网址。等等... 我建议你这样做:
router.get('/page/:id', function(req, res) {
// do whatever validation test you need with req.params.id
// then
var workbook= xlsx.readFile('C:\Users\user\Desktop\page' + req.params.id + '.xlsx');
res.send('HelloWorld')
})