Standardise Data by Example with Apps Script

When you have a few examples of how messy values should look, you can turn those examples into repeatable cleaning rules. This guide shows how to build an example-driven standardisation workflow in Google Sheets with Apps Script, then notes where Flookup custom functions can simplify the same job.


When an example-based approach works well

Example-based standardisation is useful when values follow patterns you can describe with a small set of before-and-after pairs, such as product codes, phone numbers, customer names or company suffixes. The goal is to capture the rule once and apply it consistently across the sheet.


Step 1: Create an examples or mapping table

Add a sheet tab named Examples with two columns: Original and Standardised. Fill in a handful of representative pairs. This table becomes the source of truth for exact replacements and a reference for improving your rules.

For pattern-based cases, add a third column such as Rule to document what changed, for example “remove suffix”, “format as E.164” or “title case surname”. Clear notes make the script easier to maintain.


Step 2: Apply examples with Apps Script

Open Extensions > Apps Script, create a script file and use a function that reads your examples, applies exact mappings first, then applies general cleanup rules for values not found in the table.

function standardiseByExample() {
  var ss = SpreadsheetApp.getActive();
  var source = ss.getSheetByName("Data").getRange("A2:A").getValues();
  var examples = ss.getSheetByName("Examples").getRange("A2:B").getValues();
  var map = {};
  examples.forEach(function (row) {
    var from = String(row[0] || "").trim();
    var to = String(row[1] || "").trim();
    if (from) map[from.toLowerCase()] = to;
  });
  var out = source.map(function (row) {
    var value = String(row[0] || "").trim();
    if (!value) return [""];
    var mapped = map[value.toLowerCase()];
    if (mapped) return [mapped];
    var cleaned = value
      .replace(/\s+/g, " ")
      .replace(/[^\w\s\-().]/g, "")
      .trim();
    return [cleaned];
  });
  ss.getSheetByName("Data").getRange("B2:B" + (out.length + 1)).setValues(out);
}

Adjust the source sheet, ranges and cleanup rules to match your data. For production use, write results to a separate output column, test on a copy and keep the original values unchanged.


Step 3: Refine rules from misses

After each run, filter rows where the output is still not in the desired format. Add new example pairs or extend the script with targeted rules, such as removing company suffixes, normalising country codes or reordering names. This incremental loop is often easier to control than trying to handle every case in one pass.


Using Flookup custom functions

If your standardisation can be expressed as text cleaning, normalisation, fuzzy matching or deduplication, Flookup custom functions can reduce the amount of script you need to maintain. For example, NORMALIZE can clean and transform text in-sheet, while FUZZYSIM, FLOOKUP, ULIST and DEDUPE support matching and duplicate workflows. You can also ask the Sheets AI Assistant for formula suggestions based on your ranges and goal.

For large or recurring jobs, Data Cleaning Automation can run supported batch jobs on a schedule.