CurrentValue in Named Formulas and FormulaMap

Yep - it is a game changer that makes it possible now for all of us to help ourselves. Imagine the benefit of taking all of these out-of-scope concerns and weight off the Codans so they can focus on what really matters.

As a sneak peek - here’s a simple Pack I developed to get my sea-legs.

Garbage In; Data Out

I really like clean data and despite all the efforts to encourage users to enter accurate information, there’s always someone or some process that gums up the works. So I started my Packs journey by framing an idea - what if columns were just smarter than humans?

Here’s a phone number smartifier that can handle lots of garbage while correcting the data instantly. This little gem also uses the Country code to determine how to format the phones numbers. You type in garbage numbers and it fixes them - basic, simple stuff.

Here’s a look at the code for this pack - this is the code that provides a column formatting feature that allows you to smartify the data for a column as opposed to a single function with parameters shown in the example above.

/*

   ***********************************************************
   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();

//
// COLUMN FORMAT: smartify phone number 
//
pack.addColumnFormat({
  name: "SmartifyPhoneNumbers",
  instructions: "A column format that normalizes phone numbers.",
  formulaName: "SmartifyPhoneNumber",
  formulaNamespace: "Deprecated",
});

//
// add the smartify phone number
//
pack.addFormula({
  
  // pack name and description
  name: "SmartifyPhoneNumber",
  description: "A column format that normalizes a phone number.",

  // result type
  resultType: coda.ValueType.String,

  // parameters
  parameters: [
    coda.makeParameter({
      type: coda.ParameterType.String,
      name: "<text>",
      description: "A text string of phone number digits you'd like to normalize."
    }),
  ],

  //
  // execute the function
  //
  execute: function ([thisText]) {
    let newText = "";
    thisText = thisText.replace(/\./ig, ".").replace(/\-/ig, "").replace(/ /ig, "");
    var phoneRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
    if (phoneRegex.test(thisText)) {
        var formattedPhoneNumber = newText = thisText.replace(phoneRegex, "($1) $2-$3");
    } else {
        newText = "ERROR";
    }
    return newText;
  },

});
11 Likes