为每个单元格创建包含多个答案的查询

时间:2015-01-20 10:15:27

标签: google-sheets

所以我有3张2张预定义范围,你可以在例子中看到它们:

# RangeA    # RangeB    # Wanted Result
========    ========    ===============
A | B       A           A
--------    --------    ---------------
1 | a       a           a
2 | a       b           1
3 | a                   2
4 | b                   3
5 | b                   b
6 | b                   4
7 | c                   5
8 | c                   6
9 | c
...

现在我希望有一个Formular来获得我今天已经搜索了很长时间的想要的结果,但是我没有成功。我希望有人可以帮助我。 我希望这个例子足够清楚,可以理解我想做什么。

提前感谢您的时间。

1 个答案:

答案 0 :(得分:1)

我最终用google apps脚本解决了这个问题。 我使用的函数非常简单,只有两个for循环:

/*
 * Merge merges two arrays to get one list of wanted values
 * @param needle {array} is a list of wanted values
 * @param haystack {array} is a list of values and their group
 * @return returns a list of merged values in the format group, value,
 *         value, group ...
 **/
function Merge(needle, haystack) {
  var result = [];
  // Set default values to parameters if parameter is not set.
  needle = needle || [[]];
  haystack = haystack || [[]];
  // Filter the array and remove empty items. # RangeB
  needle = needle.filter(function(item){
    return item[0];
  });
  // Filter the second array and remove empty or incomplete items # RangeA
  haystack = haystack.filter(function(item){
    return item[0] && item[1];
  });
  // Merge both arrays to get the # Wanted Result
  needle.forEach(function(item){
    result.push([item[0]]);
    haystack.forEach(function(needle){
      if(item[0] == needle[1]) {
        result.push([needle[0]]);
      }
    });
  });
  // Check if the result has a length
  if(result.length > 0) {
    return result;
  }
  // else return null to overcome the #error message
  return null;
}