title: Google Apps Script Copy Range If Condition Met
body: <p>I have two spreadsheets, source and destination. In source I have data sheet and in destination I have archive sheet. In data sheet J column contains percentages. I am trying to workout a script which automatically copies the range (A:I) of the rows that has greater than 10% in the J cell to append in the archive sheet.</p>
  <p>I'm stuck at this point:</p>
  <pre><code>function CopyRange() {
    var sourcespread = SpreadsheetApp.openById('aaa'); //replace with source ID
    var sourcesheet = sourcespread.getSheetByName('bbb'); //replace with source Sheet tab name
    var destspread = SpreadsheetApp.openById('ccc'); //replace with destination ID
    var destsheet = destspread.getSheetByName('ddd'); //replace with destination Sheet tab name
    var testrange = sourcesheet.getRange('J:J');
    var testvalue = (testrange.setNumberFormat(&quot;0.00&quot;).getValues());
    var data = [];
    var j =[];
    //Condition to check in J:J, if true, copy the same row to data array 
    for (i=0;i&lt;testvalue.length;i++) {
      if (testvalue[i] &gt;= 10) {
      data.push.apply(data,sourcesheet.getRange(i+1,1,1,3).getValues());
    //Copy matched ROW numbers to j
      j.push(i);
  }  
  }
    //Copy data array to destination sheet
    destsheet.getRange(destsheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);
  }
  </code></pre>
  <p>I'm getting error:</p>
  <blockquote>
  <p>TypeError: Cannot read property 'length' of undefined (line 19, file &quot;cp3&quot;)</p>
  </blockquote>
  <p><a href="https://i.stack.imgur.com/erqEc.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/erqEc.jpg" alt="screenshot" /></a></p>