Appscript

How to Remove Duplicate Rows Based on Column A Using a Google Apps Script?

Recently, while working on a Google Sheets solution for one of our clients in the pharmaceutical distribution space, we ran into a frustrating yet common issue — duplicate entries in their Party Master. Every time data was imported from multiple sources, especially via shared sheets or exported reports, duplicates would creep in. And even though the client was trying hard to keep it clean manually, it was just too much to handle at scale.

That’s when I knew it was time to script it out.

If you’re anything like me, you’ve probably had to deal with cleaning data in Google Sheets. Whether it’s from a form response, a client import, or some daily log — duplicates just sneak in! And trust me, manually hunting them down can eat up time like a popup ad on a slow Wi-Fi.

So today, let me show you how I handle this the clean way using Google Apps Script.

Problem Statement

Let’s say you have a sheet named “Parties”, and in column A, you’re storing party names. Now your goal is to:

  • Remove duplicate party names,
  • Keep only the first occurrence, and
  • Do it in a single click.

Sounds good? Let’s roll.

The Script

Here’s a quick and clean script I use:

function deleteDuplicateNamesFromParties() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Parties");
const range = sheet.getRange("A2:A" + sheet.getLastRow());
const values = range.getValues();

const seen = {};
for (let i = values.length - 1; i >= 0; i--) {
const name = values[i][0].toString().trim().toLowerCase();
if (seen[name]) {
sheet.deleteRow(i + 2); // +2 because data starts at row 2
} else {
seen[name] = true;
}
}
}

🛠️ How It Works

  • Step 1: It pulls all values from column A, skipping the header.
  • Step 2: It loops from bottom to top (to avoid shifting issues when deleting).
  • Step 3: If a name has already been “seen,” that row gets deleted.
  • Step 4: Done! Your sheet is cleaned your sheet.