Skip to content

Instantly share code, notes, and snippets.

@trevorhreed
Created July 7, 2022 05:22
Show Gist options
  • Save trevorhreed/d00bd447dcc005738c5620ad47aaf2c6 to your computer and use it in GitHub Desktop.
Save trevorhreed/d00bd447dcc005738c5620ad47aaf2c6 to your computer and use it in GitHub Desktop.
Format phone numbers in Google Sheets using Google App Sheet
function onOpen() {
var ui = SpreadsheetApp.getUi()
ui.createMenu('Utilities')
.addItem('Format Phone Numbers', 'formatPhone')
.addToUi();
}
function formatPhone() {
const range = SpreadsheetApp.getActive().getActiveRange()
const values = range
.getValues()
.map(row => row.map(value => {
let digits = ('' + value).replace(/[^\d]/g, '')
if (digits.length === 11 && digits[0] === '1') digits = digits.slice(1)
if (digits.length !== 10) return value
return `${digits.slice(0, 3)}-${digits.slice(3, 6)}-${digits.slice(6)}`
}))
range.setValues(values)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment