Find Duplicate Rows in Google Sheets

Published in: Google Apps Script - Google Sheets

There are many ways to Highlight and Remove Duplicates in Google sheets. You can do it manually using various spreadsheet formulas or you can use Google Apps Script.

This script, uploaded by Carl Kranich to the Google Drive Template Directory, finds duplicate rows in the active sheet and colors them red but unlike other methods, here you have the option to find duplicates based on data of specific columns.

For instance, if the first column is name and the second is age, you can set the value of CHECK_COLUMNS array as 1,2 and the script will only use these 2 columns to catch the duplicate entries. The columns may be contiguous or noncontiguous.

function findDuplicates() {
  // List the columns you want to check by number (A = 1)
  var CHECK_COLUMNS = [2, 3, 5, 6];

  // 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('FindDupes');

  // 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();
  for (i = 1; i < data.length - 1; i++) {
    for (j = i + 1; j < data.length; j++) {
      if (data[i].join() == data[j].join()) {
        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

  // Alert the user with the results
  if (dupes) {
    Browser.msgBox('Possible duplicate(s) found and colored red.');
  } else {
    Browser.msgBox('No duplicates found.');
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Google Sheets

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube