0

I have made function that will insert new row and copy to the first and second column some data from another sheet. If there is N/A error in P and Q column together I want my function to copy data from column A and B on the same row. It's actually work, add row and stuff but it copying all data even if there is not N/A error row by row

var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MainShops");
var ssd = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ShopsData");



function AddShops() {



  for(j=2; j < 278; j++) { 

  var DoubleRowCheck = ssd.getRange(j, 16, 1, 2).getValues();

  if (DoubleRowCheck ==  "#N/A", "#N/A") {

  var ShopsToAdd = ssd.getRange(j, 1, 1, 2).getValues();
  var LastRow = ss.getLastRow();
  var LastRowPlusOne = LastRow + 1;                            
  ss.insertRowAfter(LastRow);
  ss.getRange(LastRowPlusOne, 1, 1, 2).setValues(ShopsToAdd);

      }

  else {

    continue;

       }
       }

  }

1 Answer 1

2

The problem in your code is with that statement:

 if (DoubleRowCheck ==  "#N/A", "#N/A")

This will always return "true". The getValues() method you use to init your value DoubleRowCheck returns a bi-dimensional array (rows, columns). You should check both columns values in first (and only) row individually in your "if" statement to verify if they equal "#N/A":

if (DoubleRowCheck[0][0] == "#N/A" && DoubleRowCheck[0][1] == "#N/A"){...}

Also, note that:

(1) the four last lines in your if statement could be replaced by the append(ShopsToAdd) method (see documentation)

(2) the else statement isn't necessary, your "for" loop will continue until it reaches the j < 278 limit or a break statement.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.