Help formating a number inserted into an ID format

So, when filling a client form, we need to insert the CPF (a type of Brazilian Taxpayer ID Number) for individuals and CNPJ (Brazilian Business Registration Number) for corporate entities.

The formats are as follow
CPF: 999.999.999-99
CNPJ: 99.999.999/9999-99

Tried some stuff in Python as a Trigger formula, didn´t work.

Here’s a suggestion. Make the ID column a Text column, with the following Trigger Formula:

digits = re.sub(r'\D', '', value)
if len(digits) == 11:
  return 'CPF: {}{}{}.{}{}{}.{}{}{}-{}{}'.format(*digits)
if len(digits) == 14:
  return 'CNPJ: {}{}.{}{}{}.{}{}{}/{}{}{}{}-{}{}'.format(*digits)
return value

Set the Trigger Formulas options to Apply it on changes to Current field (data cleaning).

Then typing in an ID with a suitable number of digits should turn it into the correct format.

Here is a document where you can see it in action in column B:
https://docs.getgrist.com/dfNqCQYgfNGr/ID-Formatting/m/fork

Alternatively, column C is a plain-formula approach (formats whatever string is in A, if it has the right number of digits).

1 Like