title: getRange() with specific criteria in Google Apps Sheets Script
body: <p>I'm very new to JavaScript, and have only recently started fiddling with coding. </p>
  
  <p>I had found a question that is similar to what I'm asking, so I've tried to modify the suggestions to fit my needs, but am drawing a blank pulling together other resources to understand the solution.
  That question and answer is here: <a href="https://stackoverflow.com/questions/49049335/range-getvalues-with-specific-date-in-specific-cell">range.getValues() With specific Date in Specific Cell</a></p>
  
  <p>I hope to ask my question by adding it to that thread, but I'm unable to make a comment - only an Answer, as I have zero votes.</p>
  
  <p>I want to know how I can use the range.setValues() function, but specify only rows of cells that fit a certain criteria (e.g. cell in column B = 1).</p>
  
  <p>Mostly I understand the answer, I think - push the full list of data into another array, then use a for loop to identify which rows to "keep".</p>
  
  <p>However, I don't understand a good chunk of the solution, specifically this portion:</p>
  
  <pre><code>data.forEach(function(row) {
    var colHvalue = row[7];
    var colKvalue = row[10];
    if( /* your desired test */) {
      // Add the name of the sheet as the row's 1st column value.
      row.unshift(sheetName);
      // Keep this row
      kept.push(row);
    }
  });
  </code></pre>
  
  <p>where I'm confused: </p>
  
  <ol>
  <li><p>What's <code>function(row)</code> refer to? It seems to me that "row" is not a defined variable, and is also not a method.</p></li>
  <li><p>What are the objects being specified in the line <code>var colHvalue = row[7];</code>? And same for the second row.</p></li>
  <li><p>How does the <code>push</code>ing and <code>unshift</code>ing work?</p></li>
  </ol>
  
  <p>Hope for some answers, thanks!</p>
  
  <p>Edit:</p>
  
  <p>So I still don't understand the detailed logic of the Answer to that other question, and wrote something else based off that:</p>
  
  <pre><code> function importtest() {
  
  
  
    var sheetX = SpreadsheetApp.openById("XXX-XXX-XXX"); // workbook containing orginal data
    var tabX = sheetX.getSheetByName("EXPORTER"); // tab containing original data
    var rangeX = tabX.getRange(6,2,sheetX.getLastRow(),sheetX.getLastColumn()).getValues(); //range of full original data
  
  
  
  var kept = [] ;
  var data = rangeX ; //storing full data in "data"
  
  // define function "latestrows", which fills up the "kept" array with selected rows
  function latestrows() {
    var datesent = data[i][2] ; // indicating dynamic cell position (using variable i)  that determines if should be kept or not
    var datarow = data[i] ; // specify the entire row of data
    // loop statement; starting with i=0 (or row 1), and increasing until the last row of "data"
    for ( i = 0; i &lt; data.length ; i++) {
      if (datesent == 1) { // if cell in the col indicating if it should be kept, is "1"
        kept.push(datarow); //move the chosen rows of data into "kept"
      }
    }
  }
  
    var sheetX3 = SpreadsheetApp.openById("XXX-XX-XX"); // workbook import destination
    var tabX3 = sheetX3.getSheetByName("IMPORTED"); // import destination tab
    var rangeX3 = tabX3.getRange(7,3,kept.length,kept[0].length) // imported array range
  
  
    rangeX3.setValues(kept); // set values
   }
  </code></pre>
  
  <p>I get the error message <code>TypeError: Cannot read property "length" from undefined. (line 31, file "Code")</code>, which together with testing other modifications indicates to me that <code>kept</code> is empty. What am I missing? </p>