简体   繁体   中英

Nested For Loop Inside While Loop

I am creating a script to copy a number of google sheets from one google drive location to another and then loop over those sheets and apply permissions / editors. I have to also track which sheets from the source location have been previously copied.

My problem is the for loop inside of the while loop. For some reason after a single iteration of the while loop the variable running my for loop appears to taking the value of the filename.

while (files.hasNext()) {

    var file = files.next();

    for (var k in check){
      Logger.log(check[k]);
      if(check[k] = file.getName()){

        Logger.log("this file has been copied already");
        break;
      }
      else{
        Logger.log("this file was not found on the list, copying");
        file.makeCopy(file.getName(),copyFolder);
        ss = SpreadsheetApp.open(copyFolder.getFilesByName(file.getName()));
        ss.addEditors(admins);
        ss.addEditors(editors);

        var sheets = ss.getSheets();

        for (var y in sheets) {
             Logger.log(sheets[k].getSheetName());
             var protection = sheets[y].protect();
             protection.addEditors(admins);
             protection.removeEditors(editors);
           }


        checkSheet.appendRow(file.getName());
        Logger.log("file copied, file name appended to check list");

      }



    }

  }

This is the output log. As you can see in the first iteration of the for loop the proper value in the array is logged ie. "LISTED FILE". Starting with the second iteration the value of check[k] is somehow being assigned the filename ie. "batch permissions", "CP-007791".

[19-07-08 11:06:49:423 MDT] LISTED FILE
[19-07-08 11:06:49:424 MDT] this file has been copied already
[19-07-08 11:06:49:425 MDT] batch permissions
[19-07-08 11:06:49:425 MDT] this file has been copied already
[19-07-08 11:06:49:426 MDT] CP-007791
[19-07-08 11:06:49:427 MDT] this file has been copied already
[19-07-08 11:06:49:428 MDT] batch permissions 2
[19-07-08 11:06:49:429 MDT] this file has been copied already
[19-07-08 11:06:49:430 MDT] CP-008504
[19-07-08 11:06:49:431 MDT] this file has been copied already
[19-07-08 11:06:49:432 MDT] CP-007796
[19-07-08 11:06:49:433 MDT] this file has been copied already
[19-07-08 11:06:49:434 MDT] CP-007802
[19-07-08 11:06:49:435 MDT] this file has been copied already
[19-07-08 11:06:49:436 MDT] CP-003675
[19-07-08 11:06:49:437 MDT] this file has been copied already
[19-07-08 11:06:49:438 MDT] CP-007317
[19-07-08 11:06:49:439 MDT] this file has been copied already
[19-07-08 11:06:49:440 MDT] WO 81382901

I feel like I'm just missing something real simple here. Let me know if you can help. Thanks

Problem and Solution

You are right, it is simply that instead of a comparison operator === you used an assignment = in your if statement condition check[k] = file.getName() . Your issue should be amended by changing this to check[k]===file.getName() .

Improvement points

Also, to make the answer more informative, you could optimize the code to access filename only once, before the if...else statement and outside the for loop by writing file.getName() into a variable, eg var name = file.getName() . This will save you three calls to getName() .

Useful links

  1. Assignment operators reference ;
  2. Comparison operators reference ;

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