I am trying to modify the following Google App Script to convert phone numbers from
11-digits to 10 digits with hyphens. So the script would need to remove the leading 1 and add the hyphens.
For example: 19734776262 to 973-477-6262
The following script does the opposite. Found on Formatting phone number in place in sheet cell
function myFunction() {
const sheetName = "Sheet1"; // Please set the sheet name.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const range = sheet.getRange(2, 4, sheet.getLastRow() - 1, 1); // Column "D"
const values = range.getValues();
const converted = values.map(([v]) => {
if (/\d{3}-\d{3}-\d{4}/.test(v)) {
let temp = v.replace(/-/g, "");
console.log(v.charAt(0))
const tempV = v.charAt(0) != 1 ? 1 + temp : temp;
return [tempV.length == 11 ? tempV : v];
}
return [v];
});
range.setValues(converted);
}

