0
votes

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.

I'm stuck at this point:

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("0.00").getValues());
  var data = [];
  var j =[];
  //Condition to check in J:J, if true, copy the same row to data array 
  for (i=0;i<testvalue.length;i++) {
    if (testvalue[i] >= 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);
}

I'm getting error:

TypeError: Cannot read property 'length' of undefined (line 19, file "cp3")

screenshot

2
@Marios attached the screenshot, kindly take a lookNix
then 1.8>10 will evaluate to false. This is why you are getting the error.soMario
@Marios that's the first row... there are many rows that are above 10Nix
true.. Is this the source sheet ? (bbb) or the destination sheet (ddd) ?soMario

2 Answers

3
votes

Issue:

if (testvalue[i] >= 10)
  • This condition is never satisfied, therefore data is a 1D array (=[]) and not a 2D array. Therefore,

    • data=[]
    • data[0] =undefined(has no value in index 0)
    • data[0].length => Throws:

TypeError: Cannot read property 'length' of undefined (line 19, file "cp3")

The reason the condition is never satisfied is because

In data sheet J column contains percentages

The value of 10% is 0.1(10/100) and NOT 10.

Solution:

  • Use 0.1 instead of 10

  • In addition, As the previous answer mentions , testValue is a 2D array. Although implicit conversion to number will take place, it is preferable to use proper index:

    if (testvalue[i][0] >= 0.1)
    

To read:

1
votes

Issue:

  • testvalue is a 2D array of values, therefore in the if condition you are comparing a row with a value, instead of a value vs value. Therefore, you should flat the array: var testvalue = testrange.setNumberFormat("0.00").getValues().flat();

Solution:

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.getValues().flat();
  var data = [];
  var j =[];
  //Condition to check in J:J, if true, copy the same row to data array 
  for (i=0;i<testvalue.length;i++) {
    if (testvalue[i] >= 10) { 
    data.push.apply(data,sh.getRange(i+1,1,1,3).getValues());  
  //Copy matched ROW numbers to j
    j.push(i);
}  
}
  //Copy data array to destination sheet
if(data.length!=0){destsheet.getRange(destsheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);}
}