总和范围值

时间:2014-09-29 21:16:58

标签: javascript google-docs

我试图对范围值求和,但由于某种原因它没有求和。知道为什么吗? 我在JavaScript方面不是很好,但我有一些知识...

function bag(input) {
    var sheet = SpreadsheetApp.getActiveSpreadsheet()
    var AllValues = sheet.getRange(input).getValues();
    var FValue = "";
    var TotalGold = 0;
    var TotalSilver = 0;
    var TotalBronze = 0;
    for(i = 0; i < AllValues.length; i++) {
        var total = AllValues[i][0].split(" ");

        for(i = 0; i < total.length; i++){
            if (total[i].indexOf("g") >= 0) {
                var value = total[i].replace("g","");
                var gold = value * 1;
                TotalGold = TotalGold + gold;
            }
            else if (total[i].indexOf("s") >= 0) {
                var value = total[i].replace("s","");
                var silver = value * 1;
                TotalSilver = TotalSilver + silver;
            }
            else if (total[i].indexOf("b") >= 0) {
                var value = total[i].replace("b","");
                var bronze = value * 1;
                TotalBronze = TotalBronze + bronze;
            }
            else {
                return null;
            }
        }
    }
    return TotalGold + "g " + TotalSilver + "s " + TotalBronze + "b";
}

1 个答案:

答案 0 :(得分:0)

您似乎在嵌套迭代中覆盖了i的值。选择其他标识符。例如:for (var j = 0; j < total.length; j++) { ... }