Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could also do it using arrays to handle the whole sheet at once :</p> <pre><code>function removeUnique(){ var col = 2 ; // choose the column you want to check for unique elements var sh = SpreadsheetApp.getActiveSheet(); var ss = SpreadsheetApp.getActiveSpreadsheet(); var data=ss.getDataRange().getValues();// get all data data.sort(function(x,y){ // var xp = Number(x[col]);// use these to sort on numeric values // var yp = Number(y[col]); var xp = x[col];// use these for non-numeric values var yp = y[col]; Logger.log(xp+' '+yp); // just to check the sort is OK return xp == yp ? 0 : xp &lt; yp ? -1 : 1;// sort on column col numeric ascending }); var cc=0; var newdata = new Array(); for(nn=0;nn&lt;data.length-1;++nn){ if(data[nn+1][col]==data[nn][col]||cc&gt;0){ newdata.push(data[nn]); ++cc; if(cc&gt;1){cc=0}} } ss.getDataRange().clearContent(); // clear the sheet sh.getRange(1,1,newdata.length,newdata[0].length).setValues(newdata);// paste new values sorted and without unique elements } </code></pre> <p>EDIT : here is the version that keeps all duplicates (the working one)</p> <pre><code>function removeUnique(){ var col = 2 ; // choose the column you want to check for unique elements var sh = SpreadsheetApp.getActiveSheet(); var ss = SpreadsheetApp.getActiveSpreadsheet(); var data=ss.getDataRange().getValues();// get all data data.sort(function(x,y){ // var xp = Number(x[col]);// use these to sort on numeric values // var yp = Number(y[col]); var xp = x[col];// use these for non-numeric values var yp = y[col]; Logger.log(xp+' '+yp); // just to check the sort is OK return xp == yp ? 0 : xp &lt; yp ? -1 : 1;// sort on column col numeric ascending }); var newdata = new Array(); for(nn=0;nn&lt;data.length-1;++nn){ if(data[nn+1][col]==data[nn][col]){ newdata.push(data[nn]); } } if(data[nn-1][col]==data[nn][col]){newdata.push(data[nn])} ss.getDataRange().clearContent(); // clear the sheet sh.getRange(1,1,newdata.length,newdata[0].length).setValues(newdata);// paste new values sorted and without unique elements } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload