Search code examples
node.jsswitch-statementexceljs

input function using user prompt


I've posted this question before without success.

I have some lines making one user menu getting data from exceljs.

1 - How to put while condition inside this function, to get all rows to menu? Looks like switch(input) doesn't accept loop.

The logic is: * Read costumers.xlsx to get all costumers. * Loop all costumers as menu choices. * After the choice it'll open another xlsx file with the name of chosen costumer ie: 1 - Costumer1 2 - Costumer2 If I chose 1 I'll open costumer1.xlsx

2 - How to take that choice and pass as string to open the xlsx?

wb_costumers.xlsx.readFile('costumers.xlsx').then(function(){
  sh_costumers = wb_costumers.getWorksheet("Sheet1");
  var ic = 2;

  while (ic <= sh_costumers.rowCount){
      console.log("Row " + sh_costumers.getRow(ic).getCell(1) + " - " + sh_costumers.getRow(ic).getCell(2)); 
      ic++; 
}
 });
function processChoice(input){
return new Promise(function(resolve, reject) {
    var error = undefined;
    var func;

    switch(input){

        case sh_costumers.getRow(2).getCell(1).text :
            func = addPreset;               
        break;

Solution

  • After some researches, I've found something about prompt (npm install prompt).

    Now I can read my costumers configuration inside the xlsx file.

    on workbook costumers.xlsx I have two columns:

    cell1 = id cell2 = costumer's name

    on workbook check_(costumer's name).xlsx I have the informations what I want to place somewhere.

    That's my code.

    const prompt = require('prompt');
    var Excel = require('exceljs');
    
    
    var wb = new Excel.Workbook();
    var wbc = new Excel.Workbook();
    
    
    
    prompt.start();
    var ic = 1;
    wbc.xlsx.readFile('costumers.xlsx').then(function(){
      shc = wbc.getWorksheet("Sheet1");
      while (ic <= shc.rowCount){  
        console.log(shc.getRow(ic).getCell(1).value +" - "+ shc.getRow(ic).getCell(2).value);
        ic++;
      }
    
    });
    
    
    prompt.get(['costumer'], function (err, result) {
        if (err) { return onErr(err); }
        var costumer = shc.getRow(result.costumer).getCell(2).value;
    
    
        wb.xlsx.readFile('check_'+costumer+'.xlsx').then(function(){
          sh = wb.getWorksheet("Sheet1");
          console.log(sh.getRow(2).getCell(3).value);
    
        });
    
    });
    
    function onErr(err) {
        console.log(err);
        return 1;
    }