突出显示 Google 工作表中前 x 行的列中的重复值

Highlight Duplicate Values In a Column For Previous x Rows in Google Sheets

所以任务是我需要突出显示或扫描列中的重复值,但对于最后说的 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.");
  }
};

现在,我想要做的是,当我添加新行时,我希望 code/formula 自动再次变为 运行,select 变为 "now previous 5 rows"。就像我添加另一行

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)))

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

您可以通过两个 CF 规则在没有脚本的情况下解决它:

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