leftshifters / excel-parser

nodejs wrapper for parsing spreadsheet.
MIT License
72 stars 70 forks source link

Does not parse all workbooks #18

Open lsborg opened 10 years ago

lsborg commented 10 years ago

I'm parsing the worksheets in a loop and the second parse is giving the result of the first.

The code:

var excelParser = require('excel-parser');

var loadWorksheet = function(id, path){
    excelParser.parse({
        inFile: path, 
        skipEmpty: true,
        worksheet: id
    }, function(err, result){
        if(err) {
            console.error(err);
            return;
        }

        console.log(result[0][0]);
    });
};

var loadExcel = function() {
    var worksheetpath = 'master.xls';

    excelParser.worksheets({
        inFile: worksheetpath, 
        skipEmpty: true
    }, function(err, worksheets){
        if(err) console.error(err);

        for(var i = 0; i < worksheets.length; i++){
            console.log(worksheets[i]);
            loadWorksheet(worksheets[i].id, worksheetpath);
        }
    });
};

loadExcel();

The output:

{ name: 'a1', id: 1 }
{ name: 'a2', id: 2 }
211.0
211.0

The workbook:

worksheet a1: [ [111;112], [121, 122] ]
worksheet a2: [ [211;212], [221, 222] ]
]``
lsborg commented 10 years ago

Found the bug, around util.js:18.

It is:

var _CRCsv = function(args, cb) {
  temp.mkdir('temp', function(err, dirPath) {
    if(err) return cb(err);
    csvFile = path.join(dirPath, 'convert.csv');
    args.push('-c', csvFile);
    utils.execute(args, function(err, stdout) {
      if(err) return cb(err);
      fs.readFile(csvFile, 'utf-8', function(err, csv_data) {
        if(err) return cb(err);
        cb(null, csv_data);
      });
    });
  });
};

Should be:

var _CRCsv = function(args, cb) {
  temp.mkdir('temp', function(err, dirPath) {
    if(err) return cb(err);
    csvFile = path.join(dirPath, 'convert.csv');
    args.push('-c', csvFile);
    !function(f){
      utils.execute(args, function(err, stdout) {
        if(err) return cb(err);
        fs.readFile(f, 'utf-8', function(err, csv_data) {
          if(err) return cb(err);
          cb(null, csv_data);
        });
      });
    }(csvFile);
  });
};
detj commented 10 years ago

@lsborg send a PR please

detj commented 10 years ago

Thanks for the PR :+1:

ryanm2000 commented 8 years ago

@detj / @leftshifters Any chance of getting the PR merged?