简体   繁体   中英

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. I console.log out the variable 'donewithLoads' right before the while loop. Even with it is True, the while loop continues. 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. The code does work, where it marks the loads 'found' or 'not found' and changes the color.

Console 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. 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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM