Google Spreadsheets:生成4列的所有组合,每列8行

时间:2018-01-01 21:57:40

标签: function google-sheets combinations permutation

我想为Google表格创建一个函数,允许我列出数字1到8的所有可能组合,连接4次(从1111到8888,我认为是8 ^ 4 = 4096)。< / p>

(为了清晰起见,我添加了一个截图)。

到目前为止,我尝试过:

=ArrayFormula(transpose(split(concatenate(A2:A9&B2:B9&C2:C9&D2:D9& char(9)),char(9))))

...但这只给了我8种组合:1111,2222,3333,4444,5555,6666,7777,8888。

我在编程方面略显不好,特别是使用新语言,所以非常感谢任何帮助!

enter image description here

2 个答案:

答案 0 :(得分:2)

这是一个小custom function,可以创建的所有组合(它更容易为行写入):

function combinations(arr) {
  return arr.reduce(function(prod, row) {
    var out = [];
    for (i in row) {
      out = out.concat(prod.map(function(x) {
        return x.concat(row[i]);
      }));
    }
    return out;
  }, [[]]);
}

将其用作=combinations(A2:D9)将创建4 ^ 8个组合,每个长度为8,这不是您想要的。但转换起来很容易:

=combinations(transpose(A2:D9))

上面的函数将组合作为矩形数组返回,因此在您的示例中,输出将是4列宽。如果要在一个单元格中加入组合(因此输出是单个列),请使用此修改版本:

function joincombinations(arr) {
  return arr.reduce(function(prod, row) {
    var out = [];
    for (i in row) {
      out = out.concat(prod.map(function(x) {
        return x.concat(row[i]);
      }));
    }
    return out;
  }, [[]]).map(function(row) {
    return row.join("");
  });
}

用法:=joincombinations(transpose(A2:D9))

答案 1 :(得分:1)

尝试

=arrayformula(if(row(A:A)>4096,"",int((row(A:A)-1)/512)+1&mod(int((row(A:A)-1)/64),8)+1&mod(int((row(A:A)-1)/8),8)+1&mod(int((row(A:A)-1)/1),8)+1))

(表格中至少需要4096行)。