Create barcode table


#1

Hey,

I am trying to create a barcode like this one: [RP179349376CN]
barcode

As you can see in below table, when going from 9 to 10, an additional digit is added.
My question is how to guarantee the digits used are always the same amount? (the reason in column 6, I deducted manually a zero)

Looking forward to learn from your know how :bulb:

Best,
//JP


#2

LeftPad would do the trick, I think. Do you know in advance how many rows you will potentially have, or is it completely dynamic?

If you know, it’s just LeftPad([Row ID], 2, "0").

If you don’t, you’ll have to do something a little more complex, you’ll have to go through all the ‘Row IDs’ to find the longest one, then count the digits, and use this as your target length:

LeftPad([Row ID], [Table of IDs].Sort(false(),[Row ID]).[Row ID].First().ToText().Length(), "0")

Hope this helps!


#3

Dear Joe,

The first solution :hammer_and_wrench: made it working as expected, thanks a lot. :trophy:

In your second proposal, how can I choose the amount of digits, in my case I should like “9”?

Best,
//JP


#4

LeftPad syntax is string, target length, filler, so if you change the second argument, you’ll get what you want, I think (eg: LeftPad([Row ID], 9, "0"))

Joe


#5

Dear Joe,

Yes, that’s how I did it in the first solution. (see screenshot in previous post)
Just for curiosity and to learn, where to set the length in this formula?

LeftPad([Row ID], [Table of IDs].Sort(false(),[Row ID]).[Row ID].First().ToText().Length(), "0")

Thanks for your time,
//JP


#6

Let me break the formula down a bit:

LeftPad(
  [Row ID], // This is the string you're going to pad. For example, it might be '1'
  [Table of IDs].Sort( // Now I'm going to get the highest Row ID.
    false(), // Sort from largest to smallest (DESC)...
    [Row ID] // ... by the Row ID column
  ).[Row ID].First() // Now get the first row of our sorted list's row ID. In a table with 100 rows, this will be 100.
  .ToText().Length(), // Now, convert the value to a string, and get the length of the string (eg: 100 => "100", which has length 3.
"0") // Now pad the string with the character 0.

So parsed out as a list of ‘instructions’ to Coda:

  1. Get a string from the column called ‘Row ID’
  2. Get the target length of the new padded string by:
    2a. Getting the ‘Table of IDs’
    2b. Sorting it by the Row ID column from largest to smallest
    2c. Getting a list of values from the Row ID column
    2d. Getting the first one
    2e. Converting it to a string
    2f. Getting the length of the string.
  3. Use the character “0” to pad out the string
  4. Take the string from step 1, and add the value from step 3 to the front of it until the string reaches the length from step 2f.

Hope this helps!


#7

Dear Joe,

Wow, thanks for the detailed explanation of the formula :sparkles:, hopefully soon I will be able to reproduce such formulas on my own.

Best,
//JP