Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Repeating sequence with leading zeros to make all values same length in Google Sheets

The following code gives a repeating sequence of specified numbers from 1 to 12, repeating it 12 times. However, the numbers generated have a different length (1,2,3…10,11,12). How can the formula be modified so that leading zeros are added in order to make all numbers the same length?

transpose(split(REPT(concat(JOIN(",",SEQUENCE(1,12)),","),ROUNDDOWN(ROWS(A1:A)/15)),",",true))

For example, in this example there are numbers with length = 2 and then the desired sequence would be 01,02…10,11,12. However if the sequence was up to 3 or more digits, for example:

...(",",SEQUENCE(1,150)),",")...

Then a desired sequence would be 001…010…150. A sequence going up to 4 would be 0001…1500 etc.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

BASE formula has min_length argument. You can use it to set the LENgth of your sequences. It’s also easy to create the sequence without TRANSPOSE/SPLIT/JOIN/SPLIT with just IF/FLATTEN. For eg, To create sequence of 12, 25 times,

=ARRAYFORMULA(FLATTEN(IF(SEQUENCE(25),BASE(SEQUENCE(1,12),10,LEN(12)))))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading