在Google表格中的前x行的列中突出显示重复的值

时间:2019-03-06 11:11:15

标签: google-sheets google-sheets-formula

所以任务是我需要在列BUT中突出显示或扫描重复的值,例如最后5行,例如是否有数据

1. 1
2. 5
3. 7
4. 2
5. 2
6. 3
7. 4
8. 2
9. 3

因此,最后只应突出显示第5、6、8和9行。我可以在这里使用脚本轻松做到这一点

function myFunction() {
  // List the columns you want to check by number (A = 1)
  var CHECK_COLUMNS = [1];

  // Get the active sheet and info about it
  var sourceSheet = SpreadsheetApp.getActiveSheet();
  var numRows = sourceSheet.getLastRow();
  var numCols = sourceSheet.getLastColumn();

  // Create the temporary working sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var newSheet = ss.insertSheet("FindDupess");

  // Copy the desired rows to the FindDupes sheet
  for (var i = 0; i < CHECK_COLUMNS.length; i++) {
    var sourceRange = sourceSheet.getRange(1,CHECK_COLUMNS[i],numRows);
    var nextCol = newSheet.getLastColumn() + 1;
    sourceRange.copyTo(newSheet.getRange(1,nextCol,numRows));
  }

  // Find duplicates in the FindDupes sheet and color them in the main sheet
  var dupes = false;
  var data = newSheet.getDataRange().getValues();
  data.length;
  for (i = data.length-1; i > data.length-4; i--) {
    for (j = i-1; j > data.length-6; j--) {
      if  (data[i].join() == data[j].join()) {
        i;
        j;
        dupes = true;
        sourceSheet.getRange(i+1,1,1,numCols).setBackground("red");
        sourceSheet.getRange(j+1,1,1,numCols).setBackground("red");
      }
    }
  }

  // Remove the FindDupes temporary sheet
  ss.deleteSheet(newSheet);

  // Alert the user with the results
  if (dupes) {
    Browser.msgBox("Possible duplicate(s) found and colored red.");
  } else {
    Browser.msgBox("No duplicates found.");
  }
};

现在,我要做的是,当我添加新行时,我希望代码/公式自动再次运行并选择“现在的前5行”。就像我添加另一行

1. 1
2. 5
3. 7
4. 2
5. 2
6. 3
7. 4
8. 2
9. 3
10. 3

现在应突出显示第6、9和10行。希望我已经说清楚了。

P.S我也使用了这个公式

=UNIQUE(FILTER(A:A,ARRAYFORMULA(ROW(A:A)>COUNT(A:A)-5+1)))

但是首先,它不突出显示行,其次,它不返回重复值,而是所有唯一值

1 个答案:

答案 0 :(得分:1)

您只需两个CF规则即可在不使用脚本的情况下解决该问题:

  • 白色背景:=SUBTOTAL(3,$A$1:$A1)<=COUNTA(A:A)-5
  • 红色背景:=COUNTIF(QUERY(A:A,"limit 5 offset "&(COUNT(A:A)-5)),A1)>1

0