最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google sheets - How to split comma-separated values across multiple rows dynamically? - Stack Overflow

programmeradmin1浏览0评论

I have a table in Google Sheets with products (URLs) in one column and a list of comma-separated colors in another column. I want to transform it into a new table where each product is repeated for every individual color, creating a 'product-variant' pair for each combination.

How can I achieve this dynamically, so if the original table changes, the new table updates automatically? I've tried using formulas like SPLIT and ARRAYFORMULA, but I can't get it to work across multiple rows.

Original Table:

Products Colors
/ Red, Blue, Green, Yellow, Black, White
/ Red, Blue, Green, Yellow, Black, White
/ Red, Blue, Cyan, Green, Lime Green, Yellow, Magenta, Black, White

I have a table in Google Sheets with products (URLs) in one column and a list of comma-separated colors in another column. I want to transform it into a new table where each product is repeated for every individual color, creating a 'product-variant' pair for each combination.

How can I achieve this dynamically, so if the original table changes, the new table updates automatically? I've tried using formulas like SPLIT and ARRAYFORMULA, but I can't get it to work across multiple rows.

Original Table:

Products Colors
https://example/product/item-1/ Red, Blue, Green, Yellow, Black, White
https://example/product/item-2/ Red, Blue, Green, Yellow, Black, White
https://example/product/item-3/ Red, Blue, Cyan, Green, Lime Green, Yellow, Magenta, Black, White

Desired Output Table:

Product Variant
https://example/product/item-1/ Red
https://example/product/item-1/ Blue
https://example/product/item-1/ Green
https://example/product/item-1/ Yellow
https://example/product/item-1/ Black
https://example/product/item-1/ White
https://example/product/item-2/ Red
https://example/product/item-2/ Blue
https://example/product/item-2/ Green
... ...
Share Improve this question edited Jan 29 at 12:54 DarkBee 15.5k8 gold badges72 silver badges118 bronze badges asked Jan 29 at 12:52 CastorCastor 8713 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 1

Here's another solution:

=ARRAYFORMULA(
   QUERY(
     SPLIT(
       TOCOL(A2:A & "❅" & SPLIT(B2:B, ", ", ), 3),
       "❅"
     ),
     "WHERE Col2 IS NOT NULL"
   )
 )

You may also use this alternative formula, which should work with your desired output:

=WRAPROWS(TOROW(MAP(TOCOL(A1:A,1),
     TOCOL(B1:B,1),
     LAMBDA(products,
            colors,
            TOROW(BYROW(TRANSPOSE(SPLIT(colors,",")),
                            LAMBDA(x,ARRAYFORMULA(TRIM(SPLIT(JOIN("|",products,x),"|")))))))),1),2)

Sample Output:

Products Colors
https://example/product/item-1/ Red
https://example/product/item-1/ Blue
https://example/product/item-1/ Green
https://example/product/item-1/ Yellow
https://example/product/item-1/ Black
https://example/product/item-1/ White
https://example/product/item-2/ Red
https://example/product/item-2/ Blue
https://example/product/item-2/ Green
https://example/product/item-2/ Yellow
https://example/product/item-2/ Black
https://example/product/item-2/ White
https://example/product/item-3/ Red
https://example/product/item-3/ Blue
https://example/product/item-3/ Cyan
https://example/product/item-3/ Green
https://example/product/item-3/ Lime Green
https://example/product/item-3/ Yellow
https://example/product/item-3/ Magenta
https://example/product/item-3/ Black
https://example/product/item-3/ White

References: WRAPROWS function TOCOL function TOROW function

You may try:

=reduce(A1:B1,A2:A,lambda(Δ,Λ,vstack(Δ,if(Λ="",tocol(,1),let(Σ,tocol(split(index(B:B,row(Λ)),", ",)),ifna(hstack(Λ,Σ),Λ))))))

You can use REDUCE to process each URL separately, VSTACK to accumulate the results, SPLIT to get each of the colors of the column, and BYCOL to process each of the colors:

=REDUCE(TOCOL(,1),A2:A,LAMBDA(previous,url,  IF(url="",previous,
VSTACK(previous,
  TOCOL(BYCOL(SPLIT(OFFSET(url,0,1),", ",0,1),LAMBDA(color,url&color)))))))

发布评论

评论列表(0)

  1. 暂无评论