Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Google Sheets Script to Replace Duplicates: How-To

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can replace duplicate values in Google Sheets with a Google Apps Script by iterating over your data, tracking seen values, and overwriting repeats with a placeholder or a new value. The exact script depends on whether you want to blank duplicates, flag them, or substitute a custom string. Below is a working method using the built-in Apps Script editor, plus the common pitfalls and fixes.

What You'll Need

  • A Google account with access to Google Sheets.
  • A spreadsheet with your data already entered.
  • Basic familiarity with copying and pasting code (no prior scripting experience required).
  • Editor access to the spreadsheet if it is shared with you.

Step-by-Step

  1. Open your spreadsheet. Go to Google Sheets and open the file that contains the duplicates you want to replace.
  2. Open the Apps Script editor. In the menu bar, click Extensions > Apps Script. A new tab opens with a code editor. (Menu labels can differ slightly by version or region; if you do not see Extensions, look for Add-ons or Script editor in older layouts.)
  3. Clear the default code. In the editor, select the placeholder function myFunction() {} and delete it so you have a blank file.
  4. Paste the replacement script. Copy the code below into the editor. This script reads a range, replaces every repeated value after the first occurrence with the text you specify, and writes the result back to the sheet.
    function replaceDuplicates() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var range = sheet.getDataRange();
      var values = range.getValues();
      var seen = {};
      var replacement = "REPLACED"; // change this to your preferred text
      for (var i = 0; i < values.length; i++) {
        for (var j = 0; j < values[i].length; j++) {
          var cell = values[i][j];
          if (cell === "" || cell === null) continue;
          var key = String(cell).trim().toLowerCase();
          if (seen[key]) {
            values[i][j] = replacement;
          } else {
            seen[key] = true;
          }
        }
      }
      range.setValues(values);
    }
  5. Adjust the replacement text. Change the line var replacement = "REPLACED"; to whatever you want duplicates to become, such as an empty string "" to blank them out, or a note like "duplicate".
  6. Save the project. Click the save icon (or press Ctrl+S / Cmd+S) and give the project a name when prompted.
  7. Authorize the script. Click the Run button (the triangle icon). Google will ask you to review permissions. Choose your account, click Advanced if prompted, then Go to project (unsafe) and allow the requested access. This is normal for scripts you write yourself.
  8. Check the result. Return to your sheet. Duplicate values should now be replaced. If nothing changed, see Troubleshooting below.
  9. Undo if needed. If the result is not what you wanted, press Ctrl+Z / Cmd+Z in the sheet to undo the write, or restore from version history via File > Version history.

Common Mistakes

  • Running the script on the wrong sheet. The code uses getActiveSheet(), so make sure the correct tab is active before you run it.
  • Forgetting that the script is case-insensitive. The example lowercases values before comparing, so "Apple" and "apple" count as duplicates. Remove .toLowerCase() if you want case-sensitive matching.
  • Assuming it works across columns. This script treats the whole data range as one pool, so a value in column A can mark the same value in column B as a duplicate. Restrict the range if you want per-column logic.
  • Not backing up first. The script overwrites your data in place. Make a copy of the sheet before running it for the first time.
  • Leaving the default replacement text. If you forget to change "REPLACED", your duplicates will literally say REPLACED.

Tips

  • Test on a copy of your spreadsheet before running on live data.
  • To replace duplicates only in a specific column, change sheet.getDataRange() to a fixed range such as sheet.getRange("A2:A100").
  • If you want to highlight rather than overwrite, use setBackground() on the duplicate cells instead of changing their values.
  • You can trigger the script automatically by setting a time-driven trigger in the Apps Script editor under Triggers, but only do this if you understand it will run on a schedule and modify data.
  • Keep a version history snapshot before bulk edits so you can roll back.

Troubleshooting

  • Nothing happens when you run it. Check that the correct sheet tab is active and that your data is not empty. Also confirm you granted authorization.
  • You get an authorization error. Click Review permissions, choose your account, and allow access. If your organization blocks scripts, contact your Workspace admin.
  • The script times out. Very large sheets can exceed the execution limit. Narrow the range or process in chunks.
  • It replaced values you wanted to keep. Undo with Ctrl+Z / Cmd+Z immediately, or restore from File > Version history. Then adjust the range or comparison logic.
  • It only replaced some duplicates. Check for leading or trailing spaces; the script trims for comparison but writes the original value back for the first occurrence. Use TRIM in a helper column if needed.

Frequently Asked Questions

Can I replace duplicates with a blank cell instead of text?

Yes. Set var replacement = ""; and the duplicate cells will be cleared.

Does this work on multiple sheets at once?

Not as written. It targets the active sheet only. You would need to loop over getSheets() to process every tab.

Will it remove entire duplicate rows?

No. It replaces duplicate cell values, not rows. To delete duplicate rows, use Data > Data cleanup > Remove duplicates instead.

Is my data sent anywhere?

No. Apps Script runs on Google's servers within your account, and the code you write stays in your spreadsheet's script project.

Can I undo the script's changes?

Yes, with Ctrl+Z / Cmd+Z right after running, or by restoring a previous version from File > Version history.

Why does the script treat "Apple" and "apple" as duplicates?

Because the example lowercases values before comparing. Remove .toLowerCase() for case-sensitive matching.