google-sheetsgoogle-sheets-formulacombinationstransposecartesian-product

Google Sheets - Function to combine cells in a column into two columns with all possible combinations?


Can anyone help me write a Google Sheets function for doing this:

Google Sheets example of what needs to be done:

Google Sheets example of what needs to be done

The first column could be filled with X lines with a name in each one.

From that names, the columns "target" and "source" would be filled with all possible combinations between the names, without repeating a pair (if you already have "Gustavo | Jacinto", you don't need "Jacinto | Gustavo").

I know I can solve this problem using python and other stuff, but I wanted to do this using Google Sheets' functions because it would be faster to get the response I need.


Solution

  • =ARRAYFORMULA(SPLIT(SORT(
     TRANSPOSE(SPLIT(CONCATENATE(REPT(UNIQUE(TRANSPOSE(SPLIT(JOIN(",",TEXTJOIN(",",1,A2:A)),",")))&","&
     TRANSPOSE(UNIQUE(TRANSPOSE(SPLIT(JOIN(",",TEXTJOIN(",",1,A2:A)),",")))), (
     UNIQUE(TRANSPOSE(SPLIT(JOIN(",",TEXTJOIN(",",1,A2:A)),",")))<=
     TRANSPOSE(UNIQUE(TRANSPOSE(SPLIT(JOIN(",",TEXTJOIN(",",1,A2:A)),",")))))*
     REGEXMATCH(CONCATENATE(","&SUBSTITUTE(TEXTJOIN(",",1,A2:A),",",",,")&","&CHAR(9)),"(,"&
     UNIQUE(TRANSPOSE(SPLIT(JOIN(",",TEXTJOIN(",",1,A2:A)),",")))&",[^\t]*,"&
     TRANSPOSE(UNIQUE(TRANSPOSE(SPLIT(JOIN(",",TEXTJOIN(",",1,A2:A)),","))))&",)|(,"&
     TRANSPOSE(UNIQUE(TRANSPOSE(SPLIT(JOIN(",",TEXTJOIN(",",1,A2:A)),","))))&",[^\t]*,"&
     UNIQUE(TRANSPOSE(SPLIT(JOIN(",",TEXTJOIN(",",1,A2:A)),",")))&",)"))&CHAR(9)),CHAR(9)))),","))
    

    0