title: Google Script - Opting out of a loop before it's finished, returns later to finish the loop
body: <p>I have a bit of a weird problem. I built a script that will essentially take variable information from Spreadsheet 1, it will then create a copy of EmptySheet, fill out all the information row by row (let's refer to it as FilledInSheet after).</p>
  <p>After that it takes FilledInSheet and deletes the requested tabs. This all works perfectly, except that the last row to be filled in, that row is skipped, the copyfile for 4 is created, and AFTER that the FilledInSheet is updated with the right information.</p>
  <p>I have spent hours and hours trying to fix this, any feedback is much appreciated. Again, everything works except the code seems to skip one row loop, and then comes back to it later?</p>
  <p>I have tried to boil the code down as much as possible, but please let me know if anyone has additional questions.</p>
  <pre><code>function ingestProduct() {
    var ss = SpreadsheetApp.getActive();
    var rows = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
    var selectionRange = ss.getRange(&quot;Overview!A:A&quot;).getValues();
    var destinationId = id_of_the_folder
    var variable2 = [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;];
  
    //cut off 2 headers
    var headers = rows.shift();
    var headers2 = rows.shift();
    //loop through each row and check if it's been selected. If so, continue
    for (var r = 0; r &lt; rows.length; r++) {
      var row = rows[r];
      if (row[0] === true) {
        //overview of all information fields available
        var unimportantvariable = row[1];
  
        //toggle between the different variable2's and execute the fillDistroOut code
        for (var x = 0; x &lt; variable2.length; x++) {
          fillDistroOut(variable2[x]);
        }
      }
    } //this should close the loop
  
    for (var e = 0; e &lt; variable2.length; e++) {
      var destFile = ss.openById(&quot;1234&quot;);
      if (destFile.hasNext()) {
        //check if file already exists
        Logger.log(&quot;File exists&quot;);
      } else {
        //splitting up files
        var newFolder = DriveApp.getFolderById(destinationId);
        var ingestionFile = DriveApp.getFileById(fileid).makeCopy(
          newName,
          newFolder
        );
        var ingestionFileID = ingestionFile.getId();
        var ingestionFile2 = DriveApp.getFileById(ingestionFileID);
        ingestionFile2.moveTo(newFolder);
        var fileOpenned = SpreadsheetApp.openById(ingestionFileID);
  
        switch (variable2[e]) {
          case &quot;1&quot;:
            fileOpenned.deleteSheet(
              SpreadsheetApp.openById(ingestionFileID).getSheetByName(&quot;2&quot;)
            );
            fileOpenned.deleteSheet(
              SpreadsheetApp.openById(ingestionFileID).getSheetByName(&quot;3&quot;)
            );
            fileOpenned.deleteSheet(
              SpreadsheetApp.openById(ingestionFileID).getSheetByName(&quot;4&quot;)
            );
            break;
          case &quot;2&quot;:
            fileOpenned.deleteSheet(
              SpreadsheetApp.openById(ingestionFileID).getSheetByName(&quot;1&quot;)
            );
            fileOpenned.deleteSheet(
              SpreadsheetApp.openById(ingestionFileID).getSheetByName(&quot;3&quot;)
            );
            fileOpenned.deleteSheet(
              SpreadsheetApp.openById(ingestionFileID).getSheetByName(&quot;4&quot;)
            );
            break;
          case &quot;3&quot;:
            fileOpenned.deleteSheet(
              SpreadsheetApp.openById(ingestionFileID).getSheetByName(&quot;1&quot;)
            );
            fileOpenned.deleteSheet(
              SpreadsheetApp.openById(ingestionFileID).getSheetByName(&quot;2&quot;)
            );
            fileOpenned.deleteSheet(
              SpreadsheetApp.openById(ingestionFileID).getSheetByName(&quot;4&quot;)
            );
            break;
          case &quot;4&quot;:
            fileOpenned.deleteSheet(
              SpreadsheetApp.openById(ingestionFileID).getSheetByName(&quot;1&quot;)
            );
            fileOpenned.deleteSheet(
              SpreadsheetApp.openById(ingestionFileID).getSheetByName(&quot;2&quot;)
            );
            fileOpenned.deleteSheet(
              SpreadsheetApp.openById(ingestionFileID).getSheetByName(&quot;3&quot;)
            );
            break;
          default:
            break;
        }
      }
    }
  }
  
  function fillDistroOut(variable2){
    var destRange = SpreadsheetApp.openById(copyID).getSheetByName(name);
    var Avals = destRange.getRange(&quot;A1:A&quot;).getValues();
    var Alast = Avals.filter(String).length+1;
    var headerRange = destRange.getRange(1,1,2,30).getValues();
      for(var x=1; x &lt; headerRange[0].length; x++){
      destRange.getRange(Alast,x).setValue(variabel4[x]);}
      }
  </code></pre>