Normalize an Email Address

Upserts and sheet lookups miss rows when one person typed Ada@Example.com and another stored ada@example.com. normalizeEmail trims whitespace and lowercases so keys match before you write or search. Pair with upsertRowByKey or any email-keyed map.

What you'll need

  • A raw email string from a form, CRM, or sheet cell
  • A place you'll use the normalized value as a lookup key (not necessarily for display)

How to use this snippet

function normalizeEmail(email) { if (email == null) return ''; return String(email).trim().toLowerCase(); } // Before upsert / Map key var key = normalizeEmail(row.Email); if (!key) throw new Error('Missing email'); // Display can stay original; storage key is normalized

Tips

  • This does not validate MX or format — only consistency. Add a loose regex if you need a hard fail on garbage.
  • Don't lowercase display names in the To: header if you're sending mail; normalize for keys, keep original for Gmail when it matters.
  • Empty / null becomes '' so callers can decide whether to skip the row.

Soft tip: if you're wiring a bigger upsert pipeline and want plain-English comments for non-technical teammates, NitroGAS Co-Pilot can help shape the surrounding function. Happy Coding.

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free themes and snippets — plus optional Co-Pilot when you want a boost (1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension