Calculate hierarchical labels for Google Sheets using native functions

前端 未结 3 1384
猫巷女王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:46

    Here's an answer that does not allow a blank line between items, and requires that you manually type "1" into the first cell (A2). This formula is applied to cell A3, with the assumption that there are at most three levels of hierarchy in columns B, C, and D.

    =IF(
      COUNTA(B3),                                 // If there is a value in the 1st column
      INDEX(SPLIT(A2,"."),1)+1,                   //   find the 1st part of the prior ID, plus 1
      IF(                                         // ...otherwise
        COUNTA(C3),                               // If there's a value in the 2nd column
        INDEX(SPLIT(A2,"."),1)                    //   find the 1st part of the prior ID
          & "."                                   //   add a period and
          & IFERROR(INDEX(SPLIT(A2,"."),2),0)+1,  //   add the 2nd part of the prior ID (or 0), plus 1
        INDEX(SPLIT(A2,"."),1)                    // ...otherwise find the 1st part of the prior ID
          & "."                                   //   add a period and
          & IFERROR(INDEX(SPLIT(A2,"."),2),1)     //   add the 2nd part of the prior ID or 1 and
          & "."                                   //   add a period and
          & IFERROR(INDEX(SPLIT(A2,"."),3)+1,1)   //   add the 3rd part of the prior ID (or 0), plus 1
      )
    ) & ""                                        // Ensure the result is a string ("1.2", not 1.2)
    

    Without comments:

    =IF(COUNTA(B3),INDEX(SPLIT(A2,"."),1)+1,IF(COUNTA(C3),INDEX(SPLIT(A2,"."),1)& "."& IFERROR(INDEX(SPLIT(A2,"."),2),0)+1,INDEX(SPLIT(A2,"."),1)& "."& IFERROR(INDEX(SPLIT(A2,"."),2),1)& "."& IFERROR(INDEX(SPLIT(A2,"."),3)+1,1))) & ""

提交回复
热议问题