【问题标题】:Google Sheets - generating all possible combinations of strings from columns谷歌表格 - 从列中生成所有可能的字符串组合
【发布时间】:2018-07-15 13:48:57
【问题描述】:

您好(抱歉问题标题含糊,欢迎提出更好的问题)

我目前有一个如下所示的数据表:

CLASS       Type A  Type B  Type C
A              1       3      7
B              2       4      8
C                               
D               

通过公式(我不知道如何)或脚本,我试图将每个“类”与每种类型结合起来,例如:

A1、A2、A3、A4、A7、A7 和 A13 A14 A17 等和 A137 A138 等(+所有其他可能性)。到目前为止,我只设法获得了“2位数”版本。

到目前为止的代码(在我的数据中,我有一些额外的列告诉我该特定类需要哪些类型):

function expand(){
  //create the sheet to use
  var ss = SpreadsheetApp.getActive().getSheetByName('data');
  //create blank array
  var fin_resultAry = [];
  //extract the range of data
  var main_Range =  ss.getRange('A2:D5').getValues();
  //extract all the variations of data
  var range1 = ss.getRange('E2:E3').getValues();
  var range2 = ss.getRange('F2:F3').getValues();
  var range3 = ss.getRange('G2:G3').getValues();
  
  //create dictionary with variations
  var var_dict = {1: range1, 2: range2, 3: range3}
    
  //main for loop
  for(var k = 0; k<main_Range.length;k++){
    //push the initial no type variation
    fin_resultAry.push(main_Range[k][0]);    
    //create the loop to check what class variations have the set value == true
    for(var i = 1; i<=main_Range[k].length;i++){
      if (main_Range[k][i] == true){
        //for each of the variations add the var string to the initial class and push it
        for(var j = 0; j<var_dict[i].length;j++){
          fin_resultAry.push(main_Range[k][0] + " " + var_dict[i][j][0]);
        }  
      }
    }
  }    
  return fin_resultAry;
  }

另请注意,我的数据有很多“类”和超过 3 种类型,这使得无法使用公式,因为它们仅限于 X 行/字符/等。

【问题讨论】:

  • 我已经使用公式回答了几个类似的问题,你是对的,它们受到工作表大小/单元格大小的限制。不过,方法应该是相同的,对于每列中有 2 个选项的类型,您基本上必须以 3 为基数计算,例如数字 012 表示'不包括类型 A,从类型 B (3) 中选择第一选择,从类型 C (8) 中选择第二选择,然后将其与类一起获得(例如)A38'
  • 我读到的问题是每列只需要 1 个数字,所以我得到 3^3-1 = 26 - 也许 OP 可以澄清一下。
  • 是的,每列只有一个(即在上面的示例中,对于我的目的而言,A12 不是有效的组合)。

标签: javascript arrays google-apps-script google-sheets combinations


【解决方案1】:

下面是“以 3 为基数计数”方法的实现,用于生成不同的组合:

function expand(classes,types) {
 //No. of classes
  var nClasses=classes.length;
  //No. of types;
  var nTypes=types[0].length;
  //No. of 'subtypes' i.e. rows in type columns;
  var nSubTypes=types.length;
  //No. of combinations to go through for types;
  var nCombinations=Math.pow(nSubTypes+1,nTypes)-1;

  var resultArray=[];
  var iRow;
  var comb;
  // Loop over classes
  for (var iClass=0;iClass<nClasses;iClass++)
  {
    var class=classes[iClass];
    // Loop over all combinations of types
    for (var iComb=0;iComb<nCombinations;iComb++)
    {
      var shift=iComb;
      comb=class;
      //Pick out array elements for one combination of types
      for (var iType=0;iType<nTypes;iType++)
      {
        iRow=shift%(nSubTypes+1);

        if (iRow<nSubTypes)
          comb=comb+types[iRow][iType];

        shift=Math.floor(shift/(nSubTypes+1))
      }
    resultArray.push(comb);
    }
  }
  return resultArray.sort();
}

它被称为

=expand(classes,types)

例如用于测试数据

=expand(A2:A5,E2:G3)

【讨论】:

  • 谢谢!调整它以考虑真/假将是一个挑战,但希望我能做到!我已将此标记为正确的解决方案!再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多