Google会列出唯一的多列并输出多列

时间:2018-06-15 19:32:02

标签: javascript google-sheets unique

我需要一些帮助解决这个问题。我有多列数据,我想制作它,以便我只保留唯一值并将项目返回到各自的列。

1 2 3 6
1 1 4 7  
2 3 5 8

会像这样结束:

1 3 4 6
2   5 7
      8

现在我可以使用=unique()函数处理一个列,但我希望能够放置一个新的数据列,它只会将其中的唯一项目吐出到新表中。

3 个答案:

答案 0 :(得分:2)

这是尝试使用数组公式:假设单元格不包含负数,逗号或管道符号。

=ArrayFormula(transpose(split(transpose(split(join(",",text(unique(transpose(split(textjoin(",",true,{transpose(A1:D3),-transpose(column(A1:D3))}),","))),"0;|")),"|")),",")))

enter image description here

也适用于全列引用

=ArrayFormula(transpose(split(transpose(split(join(",",text(unique(transpose(split(textjoin(",",true,{transpose(A:D),-transpose(column(A:D))}),","))),"0;|")),"|")),",")))

答案 1 :(得分:1)

var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet5");
var sheet2 = SpreadsheetApp.getActive().getSheetByName("Sheet6");
var info = sheet.getDataRange().getValues();
var lastRow = sheet.getLastRow();
var lastCol = sheet.getLastColumn();
var seen = {}; // make object acts as a hash table
var data = info; // make array same size as original array that has the entire sheet

for (var x = 0; x < info[x].length; x++){
    for (var i = 0; i < info.length; i++) {
        if (!(info[i][x] in seen)) { // if item is not in seen
            data[i][x] = info[i][x]; // put item in location
            seen[data[i][x]] = true;}
        else {
           data[i][x] = "";}}} // if its not then add blank item to array

上一个答案的连接限制为50,000个字符,因此它有自己的限制。此选项有助于获取更大的数据集。我认为可以对其进行调整和改进

答案 2 :(得分:0)

  1. 将这些脚本粘贴到脚本编辑器中。
  2. &#13;
    &#13;
    function onOpen() {
        SpreadsheetApp.getUi().createMenu('My Menu')
            .addItem('Show uniques', 'onlyShowUniques')
            .addToUi()
    }
    
    function onlyShowUniques() {
        var r, d, u, t, row, i, j;
        r = SpreadsheetApp.getActive().getActiveRange();
        d = transpose(r.getValues());
        u = [];
        t = [];
        for (var i = 0, rl = d.length; i < rl; i++) {
            row = []
            for (var j = 0, cl = d[0].length; j < cl; j++) {
                if (d[i][j] && (!~u.indexOf(d[i][j]) || i == 0 && j == 0)) {
                    u.push(d[i][j])
                    row.push(d[i][j])
                } else {
                    row.push(null)
                }
                row.sort(function (a, b) {
                    return (a === null) - (b === null) || +(a > b) || -(a < b);
                })
            }
            t.push(row)
        }
        r.setValues(transpose(t))
    }
    
    function transpose(array) {
        return Object.keys(array[0])
            .map(function (col) {
                return array.map(function (row) {
                    return row[col];
                });
            });
    }
    &#13;
    &#13;
    &#13;

    1. 重新打开电子表格,看看是否创建了额外的菜单项(&#39;我的菜单&#39;)。

    2. 选择要清除重复项的范围。

    3. 转到菜单项,然后选择“显示唯一性”#。

    4. 看看是否会带来预期的输出。