问题描述
我无法使 exceljs 库的数据验证正常工作。基本上,我需要验证 excel 文件中的特定列。 我正在使用以下命令从 HTTP 请求上传 excel 文件:
//Route file(index.js)
router.post('/uploadexcel',upload.array('excel',1),(req,res) => {
console.log("inside update excel route");
console.log("***** body",req.body);
console.log("Upload Details");
console.log(req.files);
var excelFile = req.files;
var path = req.files[0].path;
console.log("@@@@@@@@@",path);
console.log("##########",excelFile);
//Calling the function with parameters as arguments
sc.updateParam(excelFile,path)
.then(result => {
console.log(result,"*********");
res.send({"success":true,"result": result});
})
.catch(err => {
console.log("Sending response in excel update",err)
res.sendStatus(500);
})
});
然后我将上传的文件带到控制器异步函数,在那里我添加了数据验证部分,如下所示:
async function pUpdateParam(excelFile,path){
console.log("inside pUpdateParam func in student controller");
//Get the file name
let student_excelId = excelFile[0].originalname;
console.log("excel Id - Student",student_excelId);
var workbook = new Excel.Workbook();
workbook.xlsx.readFile(path)
.then(function(data){
var worksheet = workbook.getWorksheet("Sheet1");
for(var v=2;v<=worksheet.actualRowCount;v++)
{
worksheet.getCell(`A${v}`).dataValidation = {
type: 'whole',operator : 'greaterThan',showErrorMessage: true,allowBlank: false,formulae: [0],errorStyle: 'error',errorTitle: 'Whole number',error: 'id must be greater than 0'
};
if(worksheet.getCell(`A${v}`).dataValidation.error === false){
console.log("Data successfully validated");
} else {
console.log("Invalid Data");
}
var lN = worksheet.getCell("A"+v).value;
console.log("A",v+"------ Value :" +lN);
}
});
我能够读取文件并在控制台中打印“A”列的值。但是,如果我在 excel 中有无效值然后上传以测试它(例如其中一个值为 0),那么我希望它在控制台中显示为“无效数据”,并且如果这些值得到正确验证,我的期望是将消息显示为“数据已成功验证”。
我还附上了excel文件的快照供您参考。
我正在努力从代码中的 if-else 块中获取验证消息,但我不确定这是正确的方法。可以使用 nodejs 中的 exceljs 模块来完成。请帮助我是 nodejs 的初学者,尤其是 exceljs。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)