0

Right now I have a page that opens up and reads an excel sheet and does stuff with that data based on the index.jade file I have. I want to be able to re-use the exact same code for other router pages and only have the excel sheet change for each page so that the data is displayed based on the different excel sheets.

Therefore I would probably just need to turn this code into a function that can be used in each router.get function, and not just the homepage one.

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

So I want more pages like the one above that uses the same code, where only the location of the excel sheet will change among the pages. How can I turn the above code into a function that can be re-used for the next few pages?:

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

etc.... Therefore I need the excel workbook variable to change from homepage.xlsx to page1.xlsx, page2.xlsx, page3.xlsx, etc.. in the function. I want this all to be done in the same index.js file.

2 Answers 2

1

How about if you move all that repetitive logic into a function:

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

for then you can reuse it in your route handlers:

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

Hope this helps you with your doubt.

Sign up to request clarification or add additional context in comments.

Comments

0

If you can allow urls like '/page/2', '/page/3' etc... I would recommend doing:

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

1 Comment

I was using page1, 2, 3 to simplify the example, in reality the .xlsx files may be called an array of different things. I will probably just make each excel file a variable and call that variable based on what router page I'm on. I just don't know how to turn the code I have now into a re-usuble function that takes different workbook names as a parameter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.