Using Google Sheets, I want to automatically number rows like so:
The key is that I want this to use built-in functions only.
I have an
Spreadsheet built-in functions doesn't include an equivalent to JavaScript .map
. The alternative is to use the spreadsheets array handling features and iteration patterns.
A "complete solution" could include the use of built-in functions to automatically transform the user input into a simple table and returning the Work Breakdown Structure number (WBS) . Some people refer to transforming the user input into a simple table as "normalization" but including this will make this post to be too long for the Stack Overflow format, so it will be focused in presenting a short formula to obtain the WBS.
It's worth to say that using formulas for doing the transformation of large data sets into a simple table as part of the continuous spreadsheet calculations, in this case, of WBS, will make the spreadsheet to slow to refresh.
To keep the WBS formula short and simple, first transform the user input into a simple table including task name, id and parent id columns, then use a formula like the following:
=ArrayFormula( IFERROR( INDEX($D$2:$D,MATCH($C2,$B$2:$B,0)) &"." &COUNTIF($C$2:$C2,C2), RANK($B2,FILTER($B$2:B,LEN($C$2:$C)=0),TRUE)&"") )
First, prepare your data
After the above steps the data should look like the following:
+---+--------------+----+-----------+ | | A | B | C | +---+--------------+----+-----------+ | 1 | Task | ID | Parent ID | | 2 | General task | 1 | | | 3 | Substast 1 | 2 | 1 | | 4 | Substast 2 | 3 | 1 | | 5 | Subsubtask 1 | 4 | 2 | | 6 | Subsubtask 2 | 5 | 2 | +---+--------------+----+-----------+
Remark: This also could help to reduce of required processing time of a custom funcion.
Second, add the below formula to D2, then fill down as needed,
=ArrayFormula( IFERROR( INDEX($D$2:$D,MATCH($C2,$B$2:$B,0)) &"." &COUNTIF($C$2:$C2,C2), RANK($B2,FILTER($B$2:B,LEN($C$2:$C)=0),TRUE)&"") )
The result should look like the following:
+---+--------------+----+-----------+----------+ | | A | B | C | D | +---+--------------+----+-----------+----------+ | 1 | Task | ID | Parent ID | WBS | | 2 | General task | 1 | | 1 | | 3 | Substast 1 | 2 | 1 | 1.1 | | 4 | Substast 2 | 3 | 1 | 1.2 | | 5 | Subsubtask 1 | 4 | 2 | 1.1.1 | | 6 | Subsubtask 2 | 5 | 2 | 1.1.2 | +---+--------------+----+-----------+----------+