Concatenate certain rows into a cell ignoring duplicates

梦想的初衷 提交于 2021-01-28 00:30:10

问题


I have a google form and I would like to sort it's responses in a separate sheet on google sheets. The results of the form look sort of like this.

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

which I would like to format into

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

Is there a formula I can use to accomplish this, noting that it ignores duplicates and groups the different ids? Ordering of the jobs does not matter.


回答1:


Working example here.

The code is like:

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

where

  • filter(B1:B10,A1:A10=1) gives you all the B values where A = 1
  • join(",", filter(...)) joins the list with the ", " separator (e.g. "apple, orange" and "kiwi" becomes "apple, orange, kiwi"
  • split(join(...)) splits the list into an array (e.g. back to [apple, orange, kiwi]
  • transpose(split(...)) converts the horizontal list to vertical list
  • unique(transpose(...)) gives you the unique values (unique() only works with vertical list)

After this, you need to transpose then join the list

Note you must keep the separator consistent (e.g. always "," or ", ")




回答2:


This is Apps Script code instead of a function. To use it, you will need to use the Tools menu, and open the script editor. Then select the function name from the drop down list, and then click the "Run" button.

To use this code, you need to have a source and a destination sheet. You will need to change the sheet names in the code to your sheet names. In this code, the source sheet is named 'Data'. You will need to change that to the name of your source sheet. In this code, the destination sheet is named 'Output', and is at the bottom of the code. This code gets data starting in row two, and writes the output data starting in row two. I tested it with your values and it works.

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);
};


来源:https://stackoverflow.com/questions/35110455/concatenate-certain-rows-into-a-cell-ignoring-duplicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!