最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - SpreadsheetApp how to return unique values from an array - Stack Overflow

matteradmin3PV0评论

Here is the function I made. The var myArray is a list of names. I'm not able to get only unique values from this array and set it to another sheet. I just want the names once.

function array() {          
      var app = SpreadsheetApp;
      var ss = app.getActiveSpreadsheet().getSheetByName("test");
      var destinationSheet = app.getActiveSpreadsheet().getSheetByName("test2");

      var myArray = ss.getRange(2,1,30).getValues();

      destinationSheet.getRange(2, 1, 30).setValues(myArray);   
}

Can someone help me to filter this array?

Here is the function I made. The var myArray is a list of names. I'm not able to get only unique values from this array and set it to another sheet. I just want the names once.

function array() {          
      var app = SpreadsheetApp;
      var ss = app.getActiveSpreadsheet().getSheetByName("test");
      var destinationSheet = app.getActiveSpreadsheet().getSheetByName("test2");

      var myArray = ss.getRange(2,1,30).getValues();

      destinationSheet.getRange(2, 1, 30).setValues(myArray);   
}

Can someone help me to filter this array?

Share Improve this question edited Mar 10, 2022 at 19:27 Wicket 38.7k9 gold badges79 silver badges194 bronze badges asked Dec 4, 2017 at 2:37 Maxime Morin-GagnonMaxime Morin-Gagnon 551 gold badge2 silver badges7 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

Here's an alternative approach (for Google Script)

function myArray() {
var ss, s, a;
ss = SpreadsheetApp.getActive();
s = ss.getSheetByName("test").getRange(2, 1, 30).getValues();
a = [];
a.push(s[0]);
for (var n = 1; n < s.length; n++) {
    if (a.join().indexOf(s[n].join()) == -1) {
        a.push(s[n])
    };
}
ss.getSheetByName("test2").getRange(2, 1, a.length, a[0].length).setValues(a);
}

Javascript Arrays have the method from using which you can create an array form a set of values, be it String, Map or Set.

Add this after the definition of myArray in your code

myArray = Array.from(new Set(myArray));

You can read more about it here.

OR

You can use a simple forEach loop to filter unique values.

Assuming your array contains names only, and getValues returns 2-D arrays, so, your array looks something like - [['name1', 'name2', 'name3']].

var newArray = [];
myArray.forEach(function(x){
    if(newArray.indexOf(x[0]) === -1){
        newArray.push(x[0]);
    }                   
});

To add to @JPV's answer, which is nearly perfect, but does not account for the instance that "ab" follows "abc". In this case the code would not recognize "ab" as a new instance. To get that you'd need to change the code to the following:

function countDistinct() {

var sh = SpreadsheetApp.getActive();
var ss = sh.getSheetByName('Sheet1');
var length = ss.getLastRow()
var data = ss.getRange(1,1,length,1).getValues();
var a = [];
var b = 0;

for (var n = 0; n<length; n++) {

if (a.indexOf(data[n].join()) == -1) {

  a.push(data[n].join());
  b++;

  }
}

Logger.log(a);
Logger.log(b);

}

Variable b would give you the number of unique values, while a would just print the array.

Assuming your array contains names only, and getValues returns 2-D arrays, so, your array looks something like - [['name1', 'name2', 'name3']]. Assuming your array contains names only, and getValues returns 2-D arrays, so, your array looks something like - [['name1', 'name2', 'name3']].

var newArray = [];
myArray.forEach(function(x){
    if(newArray.indexOf(x[0]) === -1){
        newArray.push(x[0]);
    }                   
});

what should i do i have 2 values in array ?

what should i do if have 2 values in array ?

Post a comment

comment list (0)

  1. No comments so far