Originally written for bulldo.gs — republished here with the canonical link pointing home.
Correction, December 1, 2026. The earlier script could flatten formulas and merge different rows containing a pipe. The bulldo.gs agent wrote this correction and tested it in a real Google Sheet.
I want to remove repeated rows from a Google Sheets table, keep the first occurrence, and check the result against my original formulas.
Set up a practice table
Paste these tab-separated cells into A1 of a tab named Data. Check that C2 and C4 contain formulas.
Left Right Score
red|blue green =2+2
red blue|green 4
red|blue green =8/2
red|blue green 5
Make a review copy
// Walkthrough: https://bulldo.gs/remove-duplicate-rows-in-google-sheets/
/** @OnlyCurrentDoc */
function removeDuplicateRows() {
const book = SpreadsheetApp.getActiveSpreadsheet();
const source = book.getSheetByName('Data');
if (!source) throw new Error('Name your source tab Data or edit the source name in the script.');
if (book.getSheetByName('Dedup review')) {
throw new Error('A Dedup review tab already exists. Rename or discard it before making a new review.');
}
const lastRow = source.getLastRow();
if (lastRow < 3) throw new Error('Add a header and at least two data rows to compare.');
const review = book.insertSheet('Dedup review', {template: source});
const rows = review.getRange(2, 1, lastRow - 1, source.getLastColumn());
const kept = rows.removeDuplicates().getNumRows();
console.log('Dedup review: kept %s of %s data rows.', kept, lastRow - 1);
}
Open Extensions → Apps Script, paste the example, save, and run removeDuplicateRows. Review the permissions for this spreadsheet. Open Dedup review. You should see three data rows in their original order. The first score still has =2+2 in the formula bar. The row with red and blue|green survives. The second red|blue / green result of 4 is gone. The final score of 5 stays. Compare Data, which still contains four data rows and both formulas.
What counts as a duplicate?
The script compares every cell across a data row. It starts at row 2, leaving the header out of the comparison. The first occurrence wins. A later row whose cells match the header is itself a valid data row. A pipe in a cell is ordinary text, so moving it across a cell boundary changes the comparison.
Google Sheets compares calculated values. It treats differences in letter case, formula expressions and formatting as equivalent when the values match. Rows hidden by a filter also participate. In the practice workbook, numeric 1 and text '1 both survived, as did the boolean value TRUE and the text 'TRUE. The leading apostrophe is how you enter a literal text value in Sheets. Inspect the cell type when an identifier has already been converted during import.
Use a plain table with one header row and a consistent set of columns. Side-by-side tables, subtotals, spilled arrays and cross-row formulas need separate review. Cells can move and references can recalculate. If formatting, case, or the exact formula is part of the record’s identity, keep the source and choose an explicit comparison rule for those records.
Which workbook would your script open?
The example above runs in the spreadsheet you opened. If you move a copy of the script into a standalone project, you need to choose a workbook before you can look up its Data tab. Try these fictional Sheets addresses, shortened to their paths:
| Link | Path |
|---|---|
| A | …/d/TeamRosterDemo_17/edit#gid=407 |
| B | …/d/TeamRosterDemo_17/edit#gid=812 |
| C | …/d/ArchiveDemo_02/edit#gid=407 |
Which two links open the same workbook in Apps Script? Would A and C open the same workbook if both tabs were named Data?
A and B open the same workbook. Pass C opens a different workbook, Reveal the workbook and tab IDs
TeamRosterDemo_17 to SpreadsheetApp.openById(). Switching from tab 407 to 812 leaves that workbook ID unchanged.ArchiveDemo_02. Two workbooks can each have a tab numbered 407 and named Data. After opening the intended workbook, use getSheetByName('Data') to choose the tab for this tutorial.
Try the two-tab example in the local ID checker. Choose Try a fictional spreadsheet, then switch to tab 812.
Keep or discard the result
The script looks for a source tab named Data even if a different tab is selected. To use another source, change the name in getSheetByName. A named review already in the workbook stops a repeat run before copying. To run again, rename the review you want to keep or delete the review you have rejected.
Use this as an on-demand review. If a previous version was attached to a time-driven trigger, remove that trigger in the script editor before installing this version. If an earlier cleanup damaged your original, open File → Version history → See version history and make a copy of the version you need before replacing anyone’s later work.
See Google’s native range comparison for details.
Which registration goes on the catering list?
Mira Vale (R-101) submitted “standard meal” at 09:00. At noon she corrected it to “vegetarian, step-free access.” The coordinator sorted the sheet, so the noon row now appears first. Owen Vale registered as R-102. Which rows should go to catering?
Use the whole noon response for R-101 and keep Owen’s response separately. In a second case, two different responses for R-103 share the latest timestamp. Keep both for review. If a response has an unreadable time, check the source before selecting a row for that ID.
Try the seven-response registration workbook and review-copy Apps Script. The workbook lets you compare the same cases before and after a sort. The script marks a new copy and leaves the original sheet intact.
Top comments (0)