Calculate hierarchical labels for Google Sheets using native functions

前端 未结 3 1343
猫巷女王i
猫巷女王i 2021-01-22 04:14

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

3条回答
  •  心在旅途
    2021-01-22 04:53

    Foreword

    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.

    Short answer

    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)&"")
     )
    

    Explanation

    First, prepare your data

    1. Put each task in one row. Include a General task / project to be used as the parent of all the root level tasks.
    2. Add an ID to each task.
    3. Add a reference to the ID of the parent task for each task. Left blank for the General task / project.

    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    |
    +---+--------------+----+-----------+----------+
    

提交回复
热议问题