且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

基于唯一文本匹配将多行中的文本合并到一个单元格中?

更新时间:2022-10-17 12:16:13

您不需要脚本;电子表格公式的组合就足够了。为了简单起见,我假设你的数据在A-F列中,输出将在同一张纸的G-H中。

首先,生成一个唯一的前缀列表。假设它们是SKU的前6个字符,则公式(放置在G2中)将是

  = sort(unique(filter (A2:A,6),len(A2:A))))

可选,但在这种情况下有意义)。



然后在下一列中,在单元格H2中使用这个较长的公式,这里用换行符表示。

  = join(|,arrayformula(sku =& filter(A2:A,left(A2:A,6)= G2)$ (b2,B,left(A2:A,6)= G2)
&,size =& filter(C2:C,left A2:A,6)= G2)
&,finish =& filter(D2:D,left(A2:A,6)= G2)
&,look = & filter(E2:E,left(A2:A,6)= G2)
&,application =& filter(E2:E,left(A2:A,6)= G2)
))

尽管长度很短,但非常简单:按SKU前缀过滤每列,预先描述它,然后连接结果。最后一步是加入所有这些结果。

H2中的公式需要拖到H列。

I have a table like so:

Based on this set, I need "summary records" for each unique SKU prefix, e.g. PW0007 and PW0008 in this example). Here's an example:

Just in case this is difficult to read, here's an example of one of the variation outputs (G1) above:

sku=PW0007BG-3x6-M,color=Beige,size=3x6,finish=Matte,look=Wood,application=Wall or Floor|sku=PW0007BK-3x6-M,color=Black,size=3x6,finish=Matte,look=Wood,application=Wall or Floor|sku=PW0007RD-4x8-P,color=Red,size=4x8,finish=Polished,look=Wood,application=Wall or Floor

One more point is that I have the SKU field broken out into its various parts on a separate sheet, such that I can easily do a UNIQUE() and get the Prefix PW0007 and PW0008 if that's the best approach. It's getting the variations for each unique SKU prefix and separating them that I can't begin to imagine. Maybe some kind of SUMPRODUCT?

I'd like to dream this is possible via a formula, but I'm more inclined to think I'll need some kind of script which is a whole 'nother ball of wax for me.

You don't need a script for this; a combination of spreadsheet formulas suffices. For the same of simplicity I assume that your data is in columns A-F and the output will be in G-H of the same sheet.

First, generate a list of unique prefixes. Assuming they are the first 6 characters of SKU, the formula (placed in G2) would be

=sort(unique(filter(left(A2:A, 6), len(A2:A))))

(sort is optional, but makes sense to do in this context). The filter removes empty lines from consideration.

Then in the next column, use this longish formula in cell H2, presented here with line breaks.

=join("|", arrayformula("sku=" & filter(A2:A, left(A2:A, 6) = G2)  
    & ",color=" & filter(B2:B, left(A2:A, 6) = G2)
    & ",size=" & filter(C2:C, left(A2:A, 6) = G2)
    & ",finish=" & filter(D2:D, left(A2:A, 6) = G2)
    & ",look=" & filter(E2:E, left(A2:A,6) = G2)
    & ",application=" & filter(E2:E, left(A2:A,6) = G2)
))

Despite the length, it's pretty straightforward: filter each column by the SKU prefix, prepend its description, and concatenate the results. The final step is to join all such results.

The formula from H2 needs to be dragged down the H column.