Phone Number native format

It would be great to format a column as a phone number. This seems like an easy one.

1 Like

Well, it’s easy when you carve away 21 of the 22 different phone number styles used by earthlings. :wink:

The Codans paved the way to support of all 22 formats and even your own through a custom Pack. Below is example code for one such Pack - you are free to use the code to build your own in Pack Studio.

When used in a formula like this, any values entered into the Phone Number column (such as 123.45-67890) will be transformed into the pattern (123) 456-7890. It doesn’t matter how badly the data is entered, this column formula will normalize the value.

[Smartify]::phoneSmartify(thisRow.[Phone Number], "($1) $2-$3")

/*

   ***********************************************************
   SmartPacks: A collection of smart data entry behaviors
   Copyright (c) 2021 by Global Technologies Corporation
   ALL RIGHTS RESERVED
   ***********************************************************

*/

// import the packs sdk
import * as coda from "@codahq/packs-sdk";

// create the pack
export const pack = coda.newPack();

// ################################################################
//
// PHONESMARTIFY
//
// ################################################################
pack.addColumnFormat({
  name: "Phone Smartify",
  instructions: "This column formula will create consistent phone numbers.",
  formulaName: "phoneSmartify",
});

//
// define the column formula
//
pack.addFormula({

  resultType: coda.ValueType.String,
  name: "phoneSmartify",
  description: "Formats phone number entries.",

  parameters: [

    // phone number
    coda.makeParameter({
      type: coda.ParameterType.String,
      name: "phoneNumber",
      description: "The phone number to format.",
    }),

    // format style
    coda.makeParameter({
      type: coda.ParameterType.String,
      name: "formatStyle",
      description: "The phone number format style. ex, '($1) $2-$3' or '$1.$2.$3'",
      suggestedValue: "($1) $2-$3",
      optional: true,
    }),

  ],

  // execute the formula
  execute: async function ([phoneNumber, formatStyle = "($1) $2-$3"], context) {

    console.log(JSON.stringify(context));

    formatStyle = (formatStyle) ? formatStyle: "($1) $2-$3";
    let newText = phoneNumber.replace(/\./ig, "").replace(/\-/ig, "").replace(/ /ig, "");
    var phoneRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
    var formattedPhoneNumber = newText = newText.replace(phoneRegex, formatStyle);

    console.log('phoneNumber: ' + phoneNumber + " :: " + "formattedPhoneNumber: " + formattedPhoneNumber);

    return formattedPhoneNumber;

  },

});
1 Like

I should have searched Packs before making this post. I found this pack from @Eric_Koleda that does exactly what I need. Thanks!

1 Like