将某些行连接到一个单元格中,忽略重复项

Concatenate certain rows into a cell ignoring duplicates

我有一个 google 表单,我想在 google sheet 的单独 sheet 中对它的回复进行排序。表格的结果看起来有点像这样。

Id    Job
1     Shelving, Sorting
2     Sorting
1     Cleaning, Shelving
3     Customer Service
2     Shelving, Sorting

我想格式化成

Id    Jobs
1     Cleaning, Shelving, Sorting
2     Shelving, Sorting
3     Customer Service

有没有我可以用来完成此任务的公式,注意它会忽略重复项并将不同的 ID 分组?作业的顺序无关紧要。

这是 Apps 脚本代码,而不是函数。要使用它,您需要使用工具菜单,并打开脚本编辑器。然后 select 下拉列表中的函数名称,然后单击 "Run" 按钮。

要使用此代码,您需要有源和目标 sheet。您需要将代码中的 sheet 名称更改为您的 sheet 名称。在此代码中,源 sheet 被命名为 'Data'。您需要将其更改为您的来源名称 sheet。在此代码中,目标 sheet 被命名为 'Output',并且位于代码的底部。此代码从第二行开始获取数据,并从第二行开始写入输出数据。我用你的价值观测试了它,它有效。

function concatCellData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName('Data');

  var colOneData = sh.getRange(2, 1, sh.getLastRow()-1, 1).getValues();
  var colTwoData = sh.getRange(2, 2, sh.getLastRow()-1, 1).getValues();

  var newData = [],
      newDataColOne = [],
      colOneValue,
      existInNewData = false,
      colB_One,
      colB_Two,
      dataPosition,
      thisValue,
      combinedArrays = [],
      arrayTemp = [];

  for (var i=0;i<colOneData.length;i+=1) {
    colOneValue = colOneData[i][0];
    dataPosition = newDataColOne.indexOf(colOneValue);
    existInNewData = dataPosition !== -1;

    if (!existInNewData) {//If it doesn't exist in the new data, just write the values
      newDataColOne.push(colOneValue);
      newData.push([colOneValue, colTwoData[i][0]]);
      continue;
    };

    colB_One = [];
    colB_Two = [];
    combinedArrays = []
    arrayTemp = [];

    colB_One = colTwoData[i][0].split(",");
    colB_Two = newData[dataPosition][1];
    colB_Two = colB_Two.split(",");

    var combinedArrays = colB_One.concat(colB_Two);

    //Get unique values
    for (var j=0;j<combinedArrays.length;j+=1) {
      thisValue = combinedArrays[j].trim();
      if (arrayTemp.indexOf(thisValue) === -1) {
        arrayTemp.push(thisValue);
      };
    };

    newData[dataPosition] = [colOneValue, arrayTemp.toString()]; //Overwrite existing data

  };

  ss.getSheetByName('Output').getRange(2, 1, newData.length, newData[0].length).setValues(newData);
};

Working example here.

代码如下:

=unique(transpose(split(join(", ",filter(B1:B10,A1:A10=1)),", ")))

哪里

  • filter(B1:B10,A1:A10=1) 给出所有 B 值,其中 A = 1
  • join(",", filter(...)) 使用“,”分隔符加入列表(例如 "apple, orange" 和 "kiwi" 变为 "apple, orange, kiwi"
  • split(join(...)) splits 列表到数组中(例如返回 [apple, orange, kiwi]
  • transpose(split(...)) converts水平列表转垂直列表
  • unique(transpose(...)) 为您提供 unique 值(unique() 仅适用于垂直列表)

在此之后,您需要 transpose 然后 join 列表

请注意,您必须保持分隔符一致(例如始终使用“,”或“,”)