无法修剪Excel.js单元格值

时间:2019-07-25 16:49:10

标签: node.js express exceljs

我想从Excel文件中获取值:

var Excel = require("exceljs");
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname, '../config/uploads/'));
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname);
    }
});
var upload = multer({ storage: storage });
router.post("/affecterConducteur", upload.single('fichier'), function(req, res) {
    var filename = req.file.destination + req.file.filename;
    var workbook = new Excel.Workbook();
    workbook.xlsx.readFile(filename).then(function() {
        var sheet = workbook.getWorksheet(1);
        for(var r=2; r<=sheet.rowCount; r++) {
            var row = sheet.getRow(r);
            var msisdn = row.getCell(1).value, immatriculation = row.getCell(2).value;
            if (msisdn != null) {
                msisdn = msisdn.trim();
                msisdn = (msisdn.substr(0,3) == "034" ? msisdn : "0".concat(msisdn));
                immatriculation = immatriculation.trim();
                immatriculation = immatriculation.replace(/ /g, "");
                var sql = "insert into "+db.getPrefixNomTables()+"conducteur(type_conducteur_id, msisdn) "+
                          "values((select type_conducteur_id from "+db.getPrefixNomTables()+"type_conducteur where lower(type_conducteur_lib) = 'cdz'), '"+msisdn+"')";
                connexion.query(sql, function(err, rows) {
                    if (err)
                        throw err;
                    var maj = "update "+db.getPrefixNomTables()+"vehicule "+
                              "set conducteur_id = "+rows.insertId+" where replace(immatriculation, ' ', '') = '"+immatriculation+"'";
                    connexion.query(maj, function(err2, rows2) {
                        if (err2)
                            throw err2;
                    });
                });
            }
        }
        res.send("");
    });
});

在运行时,我得到TypeError: msisdn.trim is not a function。那怎么了?

1 个答案:

答案 0 :(得分:1)

您可能要检查msisdn是一个字符串,而不是检查它是否不为空。

if(typeof msisdn === 'string')

有人抱怨修剪不是函数,因为您试图在不是字符串的东西上执行修剪。