如果数字字段为空,则会发生错误
An error occurs if the number field is empty
当数字字段为空值时出现错误。
Value sent to Number function of cells ["F3"] was not a number, it has
type of object and value of null
这能以某种方式解决吗?
我如何用数值填充 table 的示例:
var styleNumber = wb.createStyle({
font: {
color: '#000000',
size: 14,
name: 'Times New Roman',
},
alignment: {
horizontal: 'center',
vertical: 'top'
}
});
reports.forEach((report, index) => ws.cell(index + 3, 6).number(report._previousDataValues.code).style(styleNumber));
javascript 对 ||
运算符有一个巧妙的技巧,您可以使用
它的作用是return第一个值是真值,第二个值不是真值
这将导致
reports.forEach((report, index) => ws.cell(index + 3, 6).number(report._previousDataValues.code || 0).style(styleNumber));
只是写出这段代码会发生什么:
previousDataValues.code = 1234
(_previousDataValues.code || 0) //returns 1234
previousDataValues.code = null
(_previousDataValues.code || 0) //returns 0
previousDataValues.code = 0
(_previousDataValues.code || 0) //returns 0
previousDataValues.code = -1234
(_previousDataValues.code || 0) //returns -1234
看到这个答案:
当数字字段为空值时出现错误。
Value sent to Number function of cells ["F3"] was not a number, it has type of object and value of null
这能以某种方式解决吗?
我如何用数值填充 table 的示例:
var styleNumber = wb.createStyle({
font: {
color: '#000000',
size: 14,
name: 'Times New Roman',
},
alignment: {
horizontal: 'center',
vertical: 'top'
}
});
reports.forEach((report, index) => ws.cell(index + 3, 6).number(report._previousDataValues.code).style(styleNumber));
javascript 对 ||
运算符有一个巧妙的技巧,您可以使用
它的作用是return第一个值是真值,第二个值不是真值
这将导致
reports.forEach((report, index) => ws.cell(index + 3, 6).number(report._previousDataValues.code || 0).style(styleNumber));
只是写出这段代码会发生什么:
previousDataValues.code = 1234
(_previousDataValues.code || 0) //returns 1234
previousDataValues.code = null
(_previousDataValues.code || 0) //returns 0
previousDataValues.code = 0
(_previousDataValues.code || 0) //returns 0
previousDataValues.code = -1234
(_previousDataValues.code || 0) //returns -1234
看到这个答案: