首页 > 解决方案 > 数组未分配给第一次出现,但完成了所有其他行的任务。“异常”错误

问题描述

我正在尝试完成的特定任务出现以下错误:

例外:参数 (null,number) 与 SpreadsheetApp.Sheet.getRange 的方法签名不匹配。在 printAppleCount(代码:84:19) 在 assignNumbers(代码:24:9)

function onInstall(e) {
    onOpen(e);
}

function onOpen() {
    var ui = SpreadsheetApp.getUi();

    ui.createMenu('Invite Automation')
        .addItem('Assign Numbers to Zeroes (Max. 50)', 'assignNumbers')
        .addToUi();
}

function assignNumbers() {
    const sheet = SpreadsheetApp.getActiveSheet();
    var colC = sheet.getRange("C:C");
    var colCValues = colC.getValues();

    var appleCount = 0; // to store the apple count
    var appleLineAddress = []; // to store the row-no of Apple 0's

    // get the total count and and its row-no

    for (let i = 0; i < colCValues.length; i++) {
        if (colCValues[i][0] == "") continue;
        if (colCValues[i][0] == 'Apple 0') { // Target only "Apple 0" -- not "Apple 00" or "Apple 01" etc.
            appleCount++; //++
            appleLineAddress.push(i+1);
        }
    }

    // Check apple count and divide 

    if (appleCount < 50) {
        printAppleCount(1, appleCount, appleLineAddress);

    } else if (appleCount > 50 && appleCount <= 100) {
        printAppleCount(2, appleCount, appleLineAddress);

    } else if (appleCount > 100 && appleCount <= 150) {
        printAppleCount(3, appleCount, appleLineAddress);

    } else if (appleCount > 150 && appleCount <= 200) {
        printAppleCount(4, appleCount, appleLineAddress);

    } else if (appleCount > 200 && appleCount <= 250) {
        printAppleCount(5, appleCount, appleLineAddress);

    } else if (appleCount > 250 && appleCount <= 300) {
        printAppleCount(6, appleCount, appleLineAddress);

    } else if (appleCount > 300 && appleCount <= 350) {
        printAppleCount(7, appleCount, appleLineAddress);
    }

}



function printAppleCount(caseNo, appleCount, appleLineAddress) {

    const sheet = SpreadsheetApp.getActiveSheet();
    var splitInteger = function numParts(num, parts) {
    var val;
    var mod = num % parts;
    if (mod == 0) {
        val = num / parts;
        retData = Array(parts).fill(val);
    } else {
        val = (num - mod) / parts;
        retData = Array(parts).fill(val);
        for (i = 0; i < mod; i++) {
            retData[i] = retData[i] + 1;
        }
        retData.reverse()
        //Comment the above line to unreverse the result.
    }
    return retData;
}

    console.log("Case No: " + caseNo);
    console.log("AppleCount : " + appleCount);
    var equalSplits = splitInteger(appleCount, caseNo);

    console.log(equalSplits);

    // for the applecount: 113(suppose), the var equalSplits will log [37,38,38].
    // You can print the data now with the equalSplits and appleLineAddress

    var k = 0;
    for (var i = 0; i < equalSplits.length; i++) {

        for (var j = 0; j < equalSplits[i]; j++) {
            console.log('Print Apple ' + (i + 1) + ' at ' + appleLineAddress[k++]);
            sheet.getRange(appleLineAddress[k], 3).setValue('Apple ' + (i + 1));

        }
    }

}

这样做的目的是在定位列表时为每个数字分配最多 50 个“1”“2”“3”等,并仅替换“Apple 0”(将 0 替换为 1、2 或 3 等)

更具体地说,如果有超过 50 个(如果有 75 个“Apple 0”则为两个批次),它会平均分配它们,分配 37 个“Apple 1”和 38 个“Apple 2”。

运行脚本时出现问题。它会执行正确的操作,但会跳过“Apple 0”的第一次出现,并且不会考虑在循环中为其分配值。

示例表:

价值观
1 橙子
2 橙子
3 苹果 0
4 橙子
5 苹果 0
6 苹果 0
7 苹果 0
8 苹果 0
9 苹果 0
10 苹果 0
11 苹果 0
12 苹果 0
13 苹果 0
14 苹果 0
15 橙子
16 苹果 0
17 苹果 0
18 苹果 0
19 苹果 0
20 苹果 0

它最终仅替换“Apple 0”第二次出现的“Apple 0”(第 5 行)及以后。但是第一个(第 3 行)不会替换,即使在等分计算中也会被忽略。

另请注意,控制台日志正在记录“Print Apple 1 at 3” - 所以它正在考虑它。在分配/设置值过程中,它没有这样做。

当我通过在此处添加 +1 来修复另一个错误时,这特别开始发生:

appleLineAddress.push(i+1);

谢谢!

标签: javascriptgoogle-apps-scriptgoogle-sheets

解决方案


问题:

k++在设置值之前使用增量运算符,因此第一次出现 ( k=0) 被跳过,并且在最后一次迭代appleLineAddress[k]中不存在,数组没有那么大。

解决方案:

k设置值后递增。

替换这个:

console.log('Print Apple ' + (i + 1) + ' at ' + appleLineAddress[k++]);
sheet.getRange(appleLineAddress[k], 3).setValue('Apple ' + (i + 1));

有了这个:

sheet.getRange(appleLineAddress[k], 3).setValue('Apple ' + (i + 1));
console.log('Print Apple ' + (i + 1) + ' at ' + appleLineAddress[k++]);

或者这个(我不会在 中使用增量运算符console.log):

sheet.getRange(appleLineAddress[k], 3).setValue('Apple ' + (i + 1));
k++;
console.log('Print Apple ' + (i + 1) + ' at ' + appleLineAddress[k]);

参考:


推荐阅读