简体   繁体   English

While 循环不会停止,即使条件已经满足。 它又持续了 9 次。 没有逻辑意义

[英]While loop won't stop, even though the condition has been met. It continues for another 9 times. Makes no logical sense

I am banging my head against this problem because it doesn't make sense.我正在努力解决这个问题,因为它没有意义。 The conditions for the while loop are not met, yet it continues to execute another 9 times before it stops. while 循环的条件不满足,但它在停止之前继续执行另外 9 次。 I console.log out the variable 'donewithLoads' right before the while loop.我在 while 循环之前 console.log out 变量“donewithLoads”。 Even with it is True, the while loop continues.即使它是 True,while 循环也会继续。 Makes no sense, any help is appreciated.没有意义,任何帮助表示赞赏。

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;
  }
}

The code iterates through 9 rows of data, then it hits a blank but still continues to execute.该代码遍历了 9 行数据,然后遇到了空白,但仍继续执行。 The code does work, where it marks the loads 'found' or 'not found' and changes the color.该代码确实有效,它标记负载“找到”或“未找到”并更改颜色。

Console output:控制台 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 makes a valid point. @VLAZ 提出了一个有效的观点。 This is what I suggest:这就是我的建议:

STEP 1 Instead of using a variable doneWithLoad, define and use a function numLoads that counts the number of loads:步骤 1 不使用变量 doneWithLoad,而是定义并使用 function numLoads 来计算加载次数:

function numLoads(ls) {
//ls is a loadSheet object
return ls.getRange(currentRow, 1).getValue().toString().length;
}

STEP 2 Call numLoads in while loop control to update its value every iteration, as suggested by @VLAZ.第 2 步在 while 循环控制中调用 numLoads 以在每次迭代时更新其值,如 @VLAZ 所建议的那样。

while(numLoads(loadSheet) > 0) {
//Your code here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 循环JSON并在满足条件时停止。 然后计算物体 - Loop JSON and stop when condition met. Then count the objects 即使条件已满足,while循环也不会停止 - while loop doesnt stop even after condition has been fulfilled 为什么即使满足条件,我的if语句也不能正确执行? - Why isn't my if statement executing correctly even though condition has been met? jQuery验证程序继续,即使未满足所有规则 - jquery validator continues even though all rules have not been met 即使已发送响应,节点也会继续执行 - Node continues execution even though a response has been sent 函数正确返回,但即使条件最终变为假,也会在while循环中超时 - Function returns correctly, but times out in while loop even though condition eventually becomes false 在 if 条件下满足值后增量计数器不会停止 - increment counter won't stop after value is met in if condition 即使不满足条件,while循环的结果仍然会发生 - Result of a while loop still happens even when the condition isn't met 在满足特定条件后,我试图“启用”禁用的按钮。 遇到麻烦 - I am attempting to 'able' a disabled button after a certain criteria has been met. Having trouble 如果在下一个索引处不满足条件,则停止过滤数组,即使仍有数据可以满足下一个索引 - Stop filtering the array if the condition is not met at the next index even though there is still data that can satisfy the next index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM