Google-sheets – Show a unique combination of first column with a set of columns in Google Sheets

google sheetsgoogle-apps-script

I'd basically like to something like:

Name  Friend_1  Friend_2  Friend_3
----  --------  --------  --------
A     a_1       a_2       a_3
B     b_1       b_2

To:

Name  Friend
----  ------
A     a_1
A     a_2
A     a_3
B     b_1
B     b_2

Some kind of cartesian product of the text in column Name with the text in column Friend_1, Friend_2, Friend_3.

I suspect I'll be using something like TRANSPOSE and CONCAT but I'm not sure.

Best Answer

It looks like this custom function works quite well:

function combinations(input) {
  var output = [];
  for (var i = 0; i < input.length; i++) {
    for (var j = 0; j < input[i].length; j++) {
      other = input[i][j];
      if (other.trim() !== '') {
        output.push([input[i][0].trim(), other.trim()])
    }
  }
  }
  return output;
}