1

I know this has been asked before because I have spent the last 5 hours trying them all from here and even the closed google product forum. I can't get any of the loops to stop. The conditions are getting met, but the loop keep running and running. I have been copying from other examples online and inserting my own variables, but in the end, I'll have 5 rows go to the new sheet several times until the sheet gets tired or something.

I've tried getDataRange...I've tried getRange...I've tried var in loops and other loops, I've tried them all, so please take a look and let me know how this code keeps resulting in rows getting added to the new sheet multiple times over:

function runReportAllMemCos1() {
  var sheet1 = sskey.getSheetByName('Businesses');
  var sheet2 = sskey.getSheetByName('tempsheet');

  var data = sheet1.getRange(1,1, sheet1.getLastRow(), sheet1.getLastColumn()).getValues();

  var dest = [];
  for (var i = 0; i < data.length; i++ ) {
    if (data[i][12] == "Associate") { 
      dest.push(data[i]); 
    } Logger.log(data)
    if (dest.length > 0 ) {
    sheet2.getRange(sheet2.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
    }
  }
}

Thanks for any help!

2 Answers 2

6

I don't really agree with user1795832's answer as it doesn't make use of batch writing but simply writes data to sheet 2 each time the condition is true... so why using an array to do that ?

Try simply to batch write to sheet 2 after the loop is completed and it should work as expected.

function runReportAllMemCos1() {
  var sheet1 = sskey.getSheetByName('Businesses');
  var sheet2 = sskey.getSheetByName('tempsheet');

  var data = sheet1.getRange(1,1, sheet1.getLastRow(), sheet1.getLastColumn()).getValues();

  var dest = [];
  for (var i = 0; i < data.length; i++ ) {
    Logger.log(data[i][12]);// just to check if the condition is true sometimes ;-)
    if (data[i][12] == "Associate") { 
      dest.push(data[i]); 
    }
  } // here is the end of the for loop

  Logger.log(dest) ; // log the dest array instead

  if (dest.length > 0 ) { // if something has been written in your array then batch write it to the dest. sheet
    sheet2.getRange(sheet2.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
  }
}

EDIT : other possibility following your comment to choose the columns you copy

function runReportAllMemCos1() {
  var sheet1 = sskey.getSheetByName('Businesses');
  var sheet2 = sskey.getSheetByName('tempsheet');

  var data = sheet1.getRange(1,1, sheet1.getLastRow(), sheet1.getLastColumn()).getValues();

  var dest = [];
  for (var i = 0; i < data.length; i++ ) {
    Logger.log(data[i][12]);// just to check if the condition is true sometimes ;-)
    if (data[i][12] == "Associate") { 
      var destRow = []; // initialise intermediate array
      destRow.push(data[i][1],data[i][2],data[i][12]);// choose here the columns you want to add (here col2, 3 & 13)
      dest.push(destRow); 
    }
  } // here is the end of the for loop
  Logger.log(dest) ; // log the dest array instead
  if (dest.length > 0 ) { // if something has been written in your array then batch write it to the dest. sheet
    sheet2.getRange(sheet2.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
  }
}
5
  • oh geez, that was it. i missed watching football all day to work on this because I had the setValues within the loop. thanks again for the great help @Serge! Nov 12, 2012 at 15:07
  • quick question - using above, can I select a specific column from sheet1 to send to sheet2? I've tried putting column numbers in the dest.push(data[i][2]); for example but I keep getting an error. Nov 12, 2012 at 15:54
  • Not sure I understand exactly what you want... do you want only the specific column to be copied when the condition is true an leave the other columns not copied on sheet 2? Nov 12, 2012 at 16:22
  • yes - say I've got the sheet1 range, then filter those rows based on the search term, but then only want one or maybe two columns from sheet1 to move over to sheet2. the problem seems to be the different range sizes, but I am struggling with how to manipulate them Nov 12, 2012 at 16:34
  • You have to use an intermediate array, I'll update my answer :-) Nov 12, 2012 at 16:40
1

Yep, the problem is your dest variable. You aren't resetting it after each loop, so each row just keeps getting compounded in there and duplicates each time (so when looping through the third row, rows 1 and 2 are still in the dest variable). Put "dest = [];" just inside your for loop and it'll work.

function runReportAllMemCos1() {
  var sheet1 = sskey.getSheetByName('Businesses');
  var sheet2 = sskey.getSheetByName('tempsheet');

  var data = sheet1.getRange(1,1, sheet1.getLastRow(), sheet1.getLastColumn()).getValues();

  var dest = [];
  for (var i = 0; i < data.length; i++ ) {
    dest = [];
    if (data[i][12] == "Associate") { 
      dest.push(data[i]); 
    } Logger.log(data)
    if (dest.length > 0 ) {
      sheet2.getRange(sheet2.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
    }
  }
}
1
  • thanks - the solution in the other answer helped too - I had the setValues within the loop, so that's why it kept re-writing the rows over and over. Nov 12, 2012 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.