本帖最后由 咔咔乱坠 于 2024-10-18 17:12 编辑
2024/10/15公式更新,附件未更新
更新内容:使用函数,把数据去重,居中,对数据弄上从属关系的链条,可适用于父子关系、BOM等
灵感来源:https://club.excelhome.net/thread-1670045-2-1.html
要求版本:excel365
注意事项:此自定义函数包含两个参数,必选参数ref,需为单元格引用,数据源的上侧右侧不能有东西
可选参数center,1为居中,0为不居中,默认为0
- =LAMBDA(ref,[center],LET(D,DROP,V,VSTACK,H,HSTACK,R,REDUCE,S,SEQUENCE,I,INDEX,o,LAMBDA(r,c,OFFSET(ref,r,c)),链,IFS(o(,1)="","",(o(-1,)<>ref)*(o(1,)<>ref),"─",o(-1,)<>ref,"┌",ref<>o(1,),"└",1,"│"),中,IF(center,D(R(0,S(COLUMNS(ref)),LAMBDA(J,K,H(J,LET(序1,SCAN(0,I(ref,,K),LAMBDA(x,y,x+(y<>OFFSET(y,-1,)))),XLOOKUP(D(R(0,UNIQUE(序1),LAMBDA(x,y,V(x,LET(序2,SUM(N(序1=y)),IF(S(序2)=--TEXT(序2/2,0),y))))),1),序1,I(ref,,K),""))))),,1),IF(IFNA(V(1,DROP(ref,1)<>DROP(ref,-1)),1),ref,"")),D(R(0,S(COLUMNS(链)),LAMBDA(x,y,H(x,I(中,,y),I(链,,y)))),,1)))(结构2!A2:D27,1)
复制代码 简化掉一百个字符
- =LAMBDA(ref,[center],LET(o,LAMBDA(r,c,OFFSET(ref,r,c)),链,IFS((o(,1)="")*(COLUMNS(ref)>1),"",(o(-1,)<>ref)*(o(1,)<>ref),"-",o(-1,)<>ref,"┌",ref<>o(1,),"└",1,"│"),DROP(REDUCE(0,SEQUENCE(COLUMNS(ref)),LAMBDA(J,K,LET(a,INDEX(ref,,K),序,SCAN(0,a,LAMBDA(x,y,x+(y<>OFFSET(y,-1,)))),HSTACK(J,IF(DROP(REDUCE(0,FREQUENCY(序,UNIQUE(序)),LAMBDA(x,y,IF(y,VSTACK(x,SEQUENCE(y)=IF(center,ROUND(y/2,),1)),x))),1),a,""),INDEX(链,,K))))),,1)))(结构2!A2:D27,1)
复制代码 |