1

Basic Example data

I cannot figure out why the if statement I have marked as "If statement in question", never evaluates to true and its commands never execute.

  function setDuplicatesArray () {
  var sheet = SpreadsheetApp.getActive().getActiveSheet();

  var compareFromCol = "A";
  var compareToCol = "E";
  var compareFromIndex = sheet.getRange(compareFromCol+"1").getColumn();
  var compareToIndex = sheet.getRange(compareToCol+"1").getColumn();
  var maxRows = sheet.getMaxRows();


  var FromValues = sheet.getRange(4,compareFromIndex,maxRows,1).getValues();
  var ToValues   = sheet.getRange(4,compareToIndex,maxRows,1).getValues();

  var count = 0
  for (i=0; i <= maxRows; i++) {
    var from = FromValues[i];
    if (from == "") { break; }
       for ( j=0; j <= maxRows; j++) {
         var to = ToValues[j];

         if (to == "") {break; }

         Logger.log("\n"+from+"\n"+to+"\n\n");

         if (from === to) { //IF STATEMENT IN QUESTION
           count = count++;
           sheet.getRange(i+4,compareFromIndex,1,4).setBackgroundRGB(255,255,0);
           break;
         }
       }
  }

Here is the log to confirm that some elements are the same:

[17-07-08 12:27:57:899 PDT] 
Desk
Cats


[17-07-08 12:27:57:900 PDT] 
Desk
Dogs


[17-07-08 12:27:57:900 PDT] 
Desk
Pigs


[17-07-08 12:27:57:900 PDT] 
Desk
Fish


[17-07-08 12:27:57:900 PDT] 
Cats
Cats


[17-07-08 12:27:57:901 PDT] 
Cats
Dogs


[17-07-08 12:27:57:901 PDT] 
Cats
Pigs


[17-07-08 12:27:57:901 PDT] 
Cats
Fish


[17-07-08 12:27:57:902 PDT] 
Fish
Cats


[17-07-08 12:27:57:902 PDT] 
Fish
Dogs


[17-07-08 12:27:57:902 PDT] 
Fish
Pigs


[17-07-08 12:27:57:903 PDT] 
Fish
Fish


[17-07-08 12:27:57:903 PDT] 
Pencils
Cats


[17-07-08 12:27:57:903 PDT] 
Pencils
Dogs


[17-07-08 12:27:57:904 PDT] 
Pencils
Pigs


[17-07-08 12:27:57:904 PDT] 
Pencils
Fish


[17-07-08 12:27:57:904 PDT] 0.0
2
  • Welcome to Stack Overflow! It looks like you need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please feel free to come back with a Minimal, Complete, and Verifiable example that demonstrates your problem. Commented Jul 8, 2017 at 19:52
  • Please note that JavaScript is not Java, and Google Apps Script is not Google App Engine; these are totally different things. While [javascript] is relevant, the tag usage guidance specifically excludes Google Apps Script. The most important tag to have here is [google-apps-script], which I now added. Commented Jul 8, 2017 at 21:55

1 Answer 1

2

I figured it out.

.getValues() creates a two dimensional array despite there being only one column in its range.

So I changed:

var from = FromValues[i];

to

var from = FromValues[i][0];

and

var from = ToValues[j];

to

var from = ToValues[j][0];
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.