我正在迭代一個范圍,提取 2 個文本(要轉換為數字)并將它們相加,以便稍后將總數放回攪拌中。然而,雖然我看到數字記錄正常,但總和給了我NaN結果。這是代碼片段:
var totalPriceToPay = 0;
var totalCODAmount = 0;
if (ss.getActiveSheet().getName() == sheet.getName() && row > 1) {
for (var a = 0; a < dataRng.length; a ) {
if (dataRng[a][1] == true && dataRng[a][0] == 'SHIPPED' && dataRng[a][40] != 'Yes') {
//Isolate the Price to Pay Amounts to be summed and put the total back into the string.
const str = dataRng[a][35].toString();
const priceToPay = str.split(",").slice(8, 9)[0] //Extracts 8th element
totalPriceToPay = Number(priceToPay) //Converts it into a no and sums to the total
const codAmount = str.split(',').slice(9, 10)[0] //Extracts the 9th element
totalCODAmount = Number(codAmount) //Converts it into a no and sums to the total
Logger.log('Type Price To Pay: ' str.split(",").slice(8, 9)[0]);
Logger.log('Type codAmount: ' str.split(",").slice(9, 10)[0]);
Logger.log('Total Price to Pay: ' totalPriceToPay);
Logger.log('Total COD: ' totalCODAmount);
以下是日志:

謝謝。
uj5u.com熱心網友回復:
該Number()構造將給予NaN當值無法轉換為數字。當您添加NaN到一個數字時,您會得到NaN。為避免此問題,請使用以下模式:
totalPriceToPay = Number(priceToPay) || 0;
totalCODAmount = Number(codAmount) || 0;
uj5u.com熱心網友回復:
數字$前面有一個。您需要將其洗掉。使用String.slice:
const priceToPay = Number(str.split(",")[8].slice(1));
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/388614.html
上一篇:當按下Flutter中的按鈕時,是否可以從.js或.py檔案運行js或python腳本?(不是FlutterWeb)
