Google Sheets AutomationDifficulty: Beginner⚡ On Demand

Batch Extract Phone Numbers from Messy Text Using Regex in Sheets

Iterates through customer email text or support notes in Column A, uses regular expressions to isolate 10-digit phone numbers, and writes sanitized phone records to Column B.

Required OAuth Scopes: https://www.googleapis.com/auth/spreadsheets
Code.gs (Apps Script V8 Runtime)
function extractPhoneNumbersFromText() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Notes');
  const data = sheet.getDataRange().getValues();
  const results = [];
  
  const phoneRegex = /(?:\+?\d{1,3}[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}/;
  
  for (let i = 1; i < data.length; i++) {
    const text = String(data[i][0]);
    const match = text.match(phoneRegex);
    results.push([match ? match[0] : 'None Found']);
  }
  
  sheet.getRange(2, 2, results.length, 1).setValues(results);
}

Line-by-Line Code Explanation

Line 6: Standard regular expression matching diverse international phone formats.
Line 8-12: Tests regex against each row string and captures matches.
Line 14: Writes clean telephone numbers to Column B.

Step-by-Step Setup & Deployment

  1. Place raw customer notes in Column A of a tab named "Notes".
  2. Run script to populate clean numbers in Column B.

Best Practices for Quota & Rate Limit Management

When deploying this automation in Google Workspace, adhere to these operational principles:

  • Batch Reads & Writes: Never execute getValue() or setValue() inside loops. Read ranges into memory in 1 call, process arrays in V8 memory, and commit changes with a single setValues() call.
  • Execution Timeouts: Apps Script terminates scripts exceeding 6 minutes. For multi-thousand row datasets, implement batch pagination with resume timestamps stored in PropertiesService.
  • OAuth Permissions: Scope your manifest (appsscript.json) narrowly to prevent security prompts from alarming external team members.

Frequently Asked Questions

Is Google Apps Script completely free to use?

Yes. Google Apps Script is built into all free Google accounts and Google Workspace editions. There are no server fees or subscription costs for running scripts within daily quotas.

Can I run this script on an automated schedule without keeping my computer on?

Yes. Time-driven triggers execute on Google's cloud infrastructure. Your script will run reliably even when your computer is closed or offline.

More Google Sheets Automation Snippets