0

I have a column with a set of numbers I am prepping for a query. Some of these numbers have leading zeros and I will need to keep it on the list with leading zeros and without leading zeros.

What I have done so far is to create a column of values with leading and without leading zeros. Here is an example of the array when I getValues on the column.

[[1],[2],[001],[002],[1],[2]]

The end result should be... [[1],[2],[001],[002]]

The last two were dropped because they were duplicates and I only need it in the array once.

Here is what I am trying but I am having issues:

var array = sh.getRange(1,sh.getLastColumn(),sh.getLastRow(),1).getValues();
var uniqueArray = removeDuplicates(array)

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

Error: The array comes back as null and then when I try to get uniqueArray.length it will give me TypeError: Cannot read property 'length' of undefined

I've also tried:

var uniqueArray = Array.from(new Set(array));

This seems like it would be less taxing and I like it but it returns all values. It doesn't drop the duplicates.

What am I doing wrong and what is the best approach? How can I fix this?

3
  • It's better to ask a new question. Changing the goal post is frowned upon.. Commented Mar 20, 2020 at 13:14
  • You're correct. I'll do that. Commented Mar 20, 2020 at 13:18
  • I've reverted this one and opened a new one. Here is that question. stackoverflow.com/questions/60775098/… Commented Mar 20, 2020 at 13:25

2 Answers 2

2

Using Sets:

const removeDuplicates = arr2d => [...new Set(arr2d.flat())].map(e => [e]);
console.info(removeDuplicates([[1],[2],[001],[002],[1],[2]]))

Use getDisplayValues to get Strings

Sign up to request clarification or add additional context in comments.

1 Comment

Will this work with 2-Column ranges? I'm going to be amending my question to reflect that. My apologies for not specifying earlier.
1

Try this:

function removeDups(mA){
  //Logger.log('vA:',mA);
  let s=new Set();
  mA.forEach(function(a){
    a.forEach(function(e) {
      s.add(e);//add elements to a set
    });
  });
  //Logger.log('set: %s',s.keys());
  const oA=[...s];//expand the set back to a unique array
  //Logger.log('oA: %s',oA);
  return oA;
}

Note: this requires the new v8 engine

Also I noticed that the logger output is reported much more quickly to the same page as you get when you view executions.

JavaScript Set

Set.add()

6 Comments

This looks good. Question though. I am currently getting the array to input on this by combining two columns into one...(copy and paste onto new column and then copy and paste the other column to the end of that new column.) Is there a more efficient way to do that? I was thinking about getting the range for each in an array and adding one array to the other. Not sure if that is possible?
I don't know. Give it a try and let me know I'll take a look at it.
Ok. I tried your code and I get this error... Exception: The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues. The line that I'm having the issue with is destRange.setValues(uniqueArray);
Is the output of this array a 2 dimensional array? It should go in as a 2d array and come out as one. [[1],[2],[001],[002],[1],[2]]
or [[1,1],[2,1],[001,1],[002,1],[1,1],[2,1]] <- notice the 1 within each.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.