-1

I'm trying to extend this working script to include a check for matching columns prior to copying.

function copyInfo() // https://stackoverflow.com/questions/44967086/copy-data-from-one-sheet-to-another-in-google-app-script-and-append-a-row-one-s
{
  var copySheet = ssA.getSheetByName("export");
  var pasteSheet = ssA.getSheetByName("paste");
  var compareFrom = copySheet.getRange('B2:B99');
  var compareTo = pasteSheet.getRange('C2:C99');
  // if(compareFrom == compareTo){
    var source = copySheet.getRange('N1:O99'); // get source range
    var destination = ss.getRange('O1:P99'); // get destination range
    source.copyTo(destination); // copy values to destination range
    source.clearContent(); // clear source values
  // } else{SpreadsheetApp.getUi().alert("rows don't match!");}
}

Basically, I want to check that the values in column B on the source sheet are in the same order of the values of column C of the paste sheet.

Rubén
  • 34,714
  • 9
  • 70
  • 166
testing123
  • 761
  • 6
  • 13
  • 37

1 Answers1

0

I think this should help. You were missing a few steps and I tried to clarify in the comments.

function copyInfo() // https://stackoverflow.com/questions/44967086/copy-data-from-one-sheet-to-another-in-google-app-script-and-append-a-row-one-s
{
  //you need to get the SpreadsheetApp service
  var copySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("export");
  var pasteSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("paste");

  //get the values from the ranges and flatten with reduce
  var compareFrom = copySheet.getRange('B2:B99').getValues().reduce(function(a, b) {return a.concat(b);});
  var compareTo = pasteSheet.getRange('C2:C99').getValues().reduce(function(a, b) {return a.concat(b);});

  //you need to loop over the compare from array and indexOf the compareTo array
  for(var i = 0; i < compareFrom.length; i++) {
    var inCompareTo = compareTo.indexOf(compareFrom[i]);

    if(inCompareTo > -1) {
      var source = copySheet.getRange('N1:O99'); // get source range
      var destination = ss.getRange('O1:P99'); // get destination range
      source.copyTo(destination); // copy values to destination range
      source.clearContent(); // clear source values
    }
  }
}
Jordan Rhea
  • 1,186
  • 15
  • 34