[英]While loop won't stop, even though the condition has been met. It continues for another 9 times. Makes no logical sense
我正在努力解决这个问题,因为它没有意义。 while 循环的条件不满足,但它在停止之前继续执行另外 9 次。 我在 while 循环之前 console.log out 变量“donewithLoads”。 即使它是 True,while 循环也会继续。 没有意义,任何帮助表示赞赏。
function gatherLoads() {
var donewithLoads =
loadSheet
.getRange(currentRow, 1)
.getValue()
.toString().length == 0;
console.log('donewithLoads value: ' + donewithLoads);
console.log('isFound Value: ' + isFound);
console.log(
loadSheet
.getRange(currentRow, 1)
.getValue()
.toString().length
);
while (donewithLoads == false) {
//while there are still invoice numbers, do this function.
var isFound = false;
var currentLoad = loadSheet.getRange(currentRow, 1).getValue(); //record invoice number to
currentLoad;
for (var s in allDispatcherSpreadSheets) {
//search allDispatcherSheets //iterates through different dispatcher spreadsheets
var currentDispatchSpreadSheet = allDispatcherSpreadSheets[s];
var currentSheets = currentDispatchSpreadSheet.getSheets();
for (var o in currentSheets) {
//iterates through different sheets in spreadsheet
var currentSingleSheet = currentSheets[o];
if (currentSingleSheet.isSheetHidden() != true) {
// if sheet is visible and not hidden
//testerstuff
var testsheetname1 = currentSingleSheet.getName();
testsheetname1.toString();
var testSpreadSheetName = currentDispatchSpreadSheet.getName();
testSpreadSheetName.toString();
//currentRow2 is the row in the working single sheet that will be iterated to find matching invoice number
for (
var currentRow2 = 2;
currentRow2 <= numRowsDispatchSheet;
currentRow2++ //iterates through rows in column 14 (invoice)
) {
var currentLoad2 = currentSingleSheet
.getRange(currentRow2, 14)
.getValue();
if (currentLoad2 == currentLoad) {
//if found
var targetRange = currentSingleSheet
.getRange(currentRow2, 1, 1, 15)
.getValues();
foundLoads
.getRange(foundLoadscounter + 2, 1, 1, 15)
.setValues(targetRange);
foundLoadscounter++;
isFound = true;
if (isFound == true) {
loadSheet.getRange(currentRow, 2).setValue('found');
loadSheet.getRange(currentRow, 1).setBackground('green');
currentRow++;
processedLoadscounter++;
console.log(
'load found: ' +
'row ' +
currentRow +
'processed loads:' +
processedLoadscounter +
' found Loads: ' +
foundLoadscounter
);
console.log(loadSheet.getRange(currentRow, 1).getValue());
gatherLoads(); // start function again
break;
}
}
}
}
}
}
if (isFound == false) {
loadSheet.getRange(currentRow, 2).setValue('not found');
loadSheet.getRange(currentRow, 1).setBackground('red');
}
currentRow++;
processedLoadscounter++;
console.log(
'load found: ' +
'row ' +
currentRow +
'processed loads:' +
processedLoadscounter +
' found Loads: ' +
foundLoadscounter
);
console.log(loadSheet.getRange(currentRow, 1).getValue());
gatherLoads();
break;
}
}
该代码遍历了 9 行数据,然后遇到了空白,但仍继续执行。 该代码确实有效,它标记负载“找到”或“未找到”并更改颜色。
控制台 output:
10:02:12 AM Notice Execution started
10:02:12 AM Info donewithLoads value: false
10:02:12 AM Info isFound Value: undefined
10:02:12 AM Info 6
10:02:24 AM Info load found: row 3processed loads:1 found Loads: 1
10:02:24 AM Info 128361
10:02:24 AM Info donewithLoads value: false
10:02:24 AM Info isFound Value: undefined
10:02:24 AM Info 6
10:02:31 AM Info load found: row 4processed loads:2 found Loads: 2
10:02:31 AM Info 128362
10:02:31 AM Info donewithLoads value: false
10:02:31 AM Info isFound Value: undefined
10:02:31 AM Info 6
10:02:37 AM Info load found: row 5processed loads:3 found Loads: 3
10:02:37 AM Info 128363
10:02:37 AM Info donewithLoads value: false
10:02:37 AM Info isFound Value: undefined
10:02:37 AM Info 6
10:02:44 AM Info load found: row 6processed loads:4 found Loads: 4
10:02:44 AM Info 128365
10:02:44 AM Info donewithLoads value: false
10:02:44 AM Info isFound Value: undefined
10:02:44 AM Info 6
10:02:45 AM Info load found: row 7processed loads:5 found Loads: 5
10:02:45 AM Info 129488
10:02:45 AM Info donewithLoads value: false
10:02:45 AM Info isFound Value: undefined
10:02:45 AM Info 6
10:02:55 AM Info load found: row 8processed loads:6 found Loads: 5
10:02:55 AM Info 128369
10:02:55 AM Info donewithLoads value: false
10:02:55 AM Info isFound Value: undefined
10:02:55 AM Info 6
10:02:55 AM Info load found: row 9processed loads:7 found Loads: 6
10:02:56 AM Info 128371
10:02:56 AM Info donewithLoads value: false
10:02:56 AM Info isFound Value: undefined
10:02:56 AM Info 6
10:02:56 AM Info load found: row 10processed loads:8 found Loads: 7
10:02:56 AM Info 128372
10:02:56 AM Info donewithLoads value: false
10:02:56 AM Info isFound Value: undefined
10:02:56 AM Info 6
10:02:57 AM Info load found: row 11processed loads:9 found Loads: 8
10:02:57 AM Info
10:02:57 AM Info donewithLoads value: true
10:02:57 AM Info isFound Value: undefined
10:02:57 AM Info 0
10:03:05 AM Info load found: row 12processed loads:10 found Loads: 8
10:03:05 AM Info
10:03:05 AM Info donewithLoads value: true
10:03:05 AM Info isFound Value: undefined
10:03:05 AM Info 0
10:03:13 AM Info load found: row 13processed loads:11 found Loads: 8
10:03:13 AM Info
10:03:13 AM Info donewithLoads value: true
10:03:13 AM Info isFound Value: undefined
10:03:13 AM Info 0
10:03:20 AM Info load found: row 14processed loads:12 found Loads: 8
10:03:20 AM Info
10:03:20 AM Info donewithLoads value: true
10:03:20 AM Info isFound Value: undefined
10:03:20 AM Info 0
10:03:27 AM Info load found: row 15processed loads:13 found Loads: 8
10:03:27 AM Info
10:03:28 AM Info donewithLoads value: true
10:03:28 AM Info isFound Value: undefined
10:03:28 AM Info 0
10:03:29 AM Info load found: row 16processed loads:14 found Loads: 8
10:03:29 AM Info
10:03:29 AM Info donewithLoads value: true
10:03:29 AM Info isFound Value: undefined
10:03:29 AM Info 0
10:03:30 AM Info load found: row 17processed loads:15 found Loads: 8
10:03:30 AM Info
10:03:30 AM Info donewithLoads value: true
10:03:30 AM Info isFound Value: undefined
10:03:30 AM Info 0
10:03:32 AM Info load found: row 18processed loads:16 found Loads: 8
10:03:32 AM Info
10:03:32 AM Info donewithLoads value: true
10:03:32 AM Info isFound Value: undefined
10:03:32 AM Info 0
10:03:34 AM Info load found: row 19processed loads:17 found Loads: 8
10:03:34 AM Info
10:03:34 AM Info donewithLoads value: true
10:03:34 AM Info isFound Value: undefined
10:03:34 AM Info 0
10:03:35 AM Notice Execution completed
@VLAZ 提出了一个有效的观点。 这就是我的建议:
步骤 1 不使用变量 doneWithLoad,而是定义并使用 function numLoads 来计算加载次数:
function numLoads(ls) {
//ls is a loadSheet object
return ls.getRange(currentRow, 1).getValue().toString().length;
}
第 2 步在 while 循环控制中调用 numLoads 以在每次迭代时更新其值,如 @VLAZ 所建议的那样。
while(numLoads(loadSheet) > 0) {
//Your code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.