比较Google Apps脚本中的数组元素

时间:2017-07-08 19:45:14

标签: arrays google-apps-script google-sheets

Basic Example data

我无法弄清楚为什么if语句被标记为"如果有问题的语句",永远不会评估为true并且它的命令永远不会执行。

  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;
         }
       }
  }

以下是确认某些元素相同的日志:

[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

1 个答案:

答案 0 :(得分:0)

我明白了。

.getValues()创建了一个二维数组,尽管其范围内只有一列。

所以我改变了:

var from = FromValues[i];

var from = FromValues[i][0];

var from = ToValues[j];

var from = ToValues[j][0];