Created
July 7, 2022 05:22
-
-
Save trevorhreed/d00bd447dcc005738c5620ad47aaf2c6 to your computer and use it in GitHub Desktop.
Format phone numbers in Google Sheets using Google App Sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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