2015-10-19 72 views
0

現在我有一個頁面打開並讀取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文件中完成。

回答

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

希望這幫助你懷疑。

0

如果你能允許的網址,如「/頁/ 2」,「/頁/ 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') 
}) 
+0

我正在使用page1,2,3來簡化示例,實際上.xlsx文件可能被稱爲不同事物的數組。我可能會讓每個excel文件變成一個變量,並根據我所在的路由器頁面調用該變量。我只是不知道如何將我現在的代碼變成一個可重用的函數,它將不同的工作簿名稱作爲參數 –

相關問題