【问题标题】:input function using user prompt使用用户提示的输入功能
【发布时间】:2020-05-15 20:47:15
【问题描述】:

我之前发布过这个问题,但没有成功。

我有几行让一个用户菜单从 exceljs 获取数据。

1 - 如何在此函数中放置 while 条件,以获取所有行到菜单?看起来 switch(input) 不接受循环。

逻辑是: * 阅读customers.xlsx 以获取所有客户。 * 循环所有客户作为菜单选项。 * 选择后会打开另一个带有所选客户名称的 xlsx 文件 IE: 1 - 客户 1 2 - 客户 2 如果我选择 1,我会打开 coster1.xlsx

2 - 如何选择并作为字符串传递以打开 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;

【问题讨论】:

    标签: node.js switch-statement exceljs


    【解决方案1】:

    经过一番研究,我发现了一些关于提示(npm install prompt)的东西。

    现在我可以在 xlsx 文件中读取我的客户配置。

    在工作簿cosers.xlsx 上,我有两列:

    cell1 = id cell2 = 客户姓名

    在工作簿检查_(客户姓名).xlsx 我有我想放在某处的信息。

    这是我的代码。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-08
      • 2022-12-03
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 2011-05-11
      相关资源
      最近更新 更多