How to create all possible pair combinations without duplicates in Google Sheets?

后端 未结 4 1969
臣服心动
臣服心动 2020-12-04 03:21

How to perform iteration over excel/google sheets cells to get pairwise combinations?

\"string1\"

\"string2\"
\"string3\"
...
\"string10\"

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 04:16

    google-sheets

    It is a hard task for native functions. Try a script and use it as a custom function:

    function getTournament(teams_from_range)
    
        {
          // teams_from_range -- 2D Array  
          var teams = [];
          // convert to list
          teams_from_range.forEach(function(row) { row.forEach(function(cell) { teams.push(cell); } ); } );
          return getTournament_(teams);
        }
        
        
        function getTournament_(teams)
        {
          var start = 0;
          var l = teams.length;
          var result = [], game = [];
          
          // loop each value
          for (var i = 0; i < l; i++)
          {
            // loop each value minus current
            start++;
            for (var ii = start; ii < l; ii++)
            {
              game = []
              game.push(teams[i]);
              game.push(teams[ii]);  
              result.push(game);
            }  
          }
          
          return result;
        
        }
    

    Usage:

    =getTournament(A1:A10)

提交回复
热议问题