【发布时间】:2017-06-09 11:12:36
【问题描述】:
在这段代码中,我想通过这一行计算我从 excel 文件中获取的特定数据的总和:
" sumofKoli += JSON.stringify(row.values[4]);".
我的问题是,即使是这行代码:
console.log(JSON.stringify(row.values[3])+" Sold number of :"+ JSON.stringify(row.values[4]));
给出我想要的“47”,当我尝试对所有数据求和时,我得到的结果类似于“94559129995555.5.2556149.9”。它将它添加为一个字符串值,我希望它作为整数求和。你能帮帮我吗?
这里是完整的代码:
Reader.prototype.readExcel = function(chartType,callback){
// read from a file
var workbook = new Excel.Workbook();
var sumofKoli=0;
var sumPrice =0;
workbook.xlsx.readFile(filename)
.then(function(err,results) {
var worksheet = workbook.getWorksheet('Sayfa1');
worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) {
console.log("----------");
console.log(JSON.stringify(row.values[3])+" Sold number of :"+ JSON.stringify(row.values[4]));
console.log("----------");
sumofKoli += JSON.stringify(row.values[4]);
console.log(JSON.stringify(row.values[3])+" Sold in price of :"+ JSON.stringify(row.values[5]));
sumPrice += JSON.stringify(row.values[5]);
console.log("----------");
});
results = { sumofKoli : sumofKoli, sumTutar : sumPrice};
if (err){callback(err,null);}
callback(null,results);
});
【问题讨论】:
-
您正在尝试添加类型为
string的变量和类型为integer的变量。
标签: javascript json node.js string integer