appscript.dev
Automation Intermediate Docs

Auto-redact sensitive terms in a Doc

Black out names, numbers, or any pattern from a Doc — a redaction pass over a list of terms.

Published Sep 28, 2025

When Northwind turns a client engagement into a public case study, the writing stays but the identifying detail has to go — client names, account numbers, and fee figures. Doing that by hand means scanning the Doc, finding every mention, and hoping nothing was missed. Miss one figure and a confidential deal is in the wild.

This script runs a redaction pass instead. You give it a Doc and a list of terms; it blacks out every one of them, plus any long number or pound figure it finds via pattern matching. Run it on a copy and you get a publishable version of the case study with the sensitive parts replaced by solid blocks.

What you’ll need

  • A Google Doc to redact — ideally a copy of the original, since the script rewrites text in place.
  • A list of the literal terms to remove: client names, project codenames, anything specific to the engagement.
  • Nothing else. The number and currency patterns are built into the script.

The script

// The redaction marker that replaces every matched term.
const REDACTION_MARK = '████';

// Pattern: any run of 6 or more digits — account or reference numbers.
const LONG_NUMBER_PATTERN = '\\b\\d{6,}\\b';

// Pattern: a pound figure, with optional thousands separators and pence.
const CURRENCY_PATTERN =\\d+(?:,\\d{3})*(?:\\.\\d+)?';

/**
 * Redacts a Doc in place: blacks out each literal term in `terms`,
 * then any long number and any pound figure found by pattern.
 */
function redactDoc(docId, terms) {
  const body = DocumentApp.openById(docId).getBody();

  // 1. Replace each literal term — client names, codenames, etc.
  for (const term of terms) {
    body.replaceText(term, REDACTION_MARK);
  }

  // 2. Black out long numbers (account and reference numbers).
  body.replaceText(LONG_NUMBER_PATTERN, REDACTION_MARK);

  // 3. Black out pound figures, keeping the £ so the text still reads.
  body.replaceText(CURRENCY_PATTERN, '£' + REDACTION_MARK);
}

/**
 * Redacts a specific Northwind case-study Doc against its known
 * sensitive terms. Edit the list and the ID for each new case study.
 */
function redactNorthwindStudio() {
  const CASE_STUDY_ID = '1abcCaseStudyId';
  const SENSITIVE_TERMS = ['Northwind & Co', 'AcmeCorp', 'Wagner Architects'];

  redactDoc(CASE_STUDY_ID, SENSITIVE_TERMS);
  Logger.log('Redaction pass complete.');
}

How it works

  1. redactNorthwindStudio holds the things that change per case study — the Doc ID and the list of sensitive terms — and hands them to redactDoc.
  2. redactDoc opens the Doc and gets its body, the element that replaceText operates on.
  3. It loops the literal terms and replaces each one with the redaction marker. replaceText is case-sensitive, so list every spelling you care about.
  4. It runs replaceText with LONG_NUMBER_PATTERN, a regular expression that matches any run of six or more digits — account and reference numbers.
  5. It runs replaceText with CURRENCY_PATTERN to catch pound figures, replacing the digits but keeping the £ so a redacted sentence still reads naturally.
  6. All replacements happen directly in the Doc, so the file you point at is the file that gets redacted.

Example run

A paragraph in the original case-study Doc:

Northwind & Co delivered a brand refresh for AcmeCorp on account 4471203, a project valued at £42,500 over six months.

After running redactNorthwindStudio:

████ delivered a brand refresh for ████ on account ████, a project valued at £████ over six months.

The structure and story survive; every identifying detail is blacked out.

Run it

This is an on-demand job — run it once per Doc, against a copy:

  1. Make a copy of the original Doc (File → Make a copy) and use the copy’s ID. Never run this on the only copy you have.
  2. In redactNorthwindStudio, set CASE_STUDY_ID and edit SENSITIVE_TERMS for the case study in hand.
  3. Select redactNorthwindStudio in the Apps Script editor and click Run.
  4. Approve the authorisation prompt the first time, then open the Doc to check the result.

Watch out for

  • replaceText modifies the Doc in place — there is no undo from the script. Always run on a copy, never the original.
  • Matching is case-sensitive and literal. “AcmeCorp”, “Acme Corp”, and “ACME” are three different terms; list every variant you need to catch.
  • The redaction is text-only. Numbers or names inside images, charts, or embedded drawings are not touched — check those by eye.
  • The number pattern blacks out any run of six or more digits, including harmless ones like long dates or postcodes. Tighten the pattern if it is too greedy for your Doc.
  • Redacted text is replaced, not visually covered. Someone with the original Doc can still see what was there — the protection comes from sharing the redacted copy and nothing else.

Related