删除阵列Google Apps脚本中的重复值 [英] Remove duplicates values in array Google Apps Script

查看:95
本文介绍了删除阵列Google Apps脚本中的重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从2个数组中删除重复值,并将其合并到一个主数组中。



这些值不能从表单或文档中删除,只是在数组中,这就是为什么我没有使用内置函数中的clear()或clearContents();我还试图修改 GAS教程中的removeDuplicates()函数,但它会将列中的行从A拖放到Z,而不是过滤的行。注意:

参数来自 getClients()来自其他功能,并且工作正常。
$ b newClients 列出来自工作表'Users'和新用户从另一张名为数据的表中列出用户。



这两张表属于同一张电子表格。



newClients和newUsers :均为阵列ys只包含字符串(用户名),并且都有重复的值。



因此,目标被识别并删除这些值,原始和重复。



我认为应该更容易一些,但是我在JS中是新的,所以我试过的所有东西都没有用过。



代码

  function getAllClientsFromData body,project){

var ss = SpreadsheetApp.getActiveSpreadsheet();

//激活表格
var sheet = ss.getSheets()[1,3];

//在工作表数据中访问数据
var rangeC = ss.getRangeByName(clients); // A:2:C273
var rangeU = ss.getRangeByName(names); // A5:A

var clients = rangeC.getValues();
var users = rangeU.getValues();

var lastRow = sheet.getLastRow() - 2;

body + =< h2>+ project +中的+ lastRow +客户端的总数< / h2>
body + =< table style =+ STYLE.TABLE +>;
body + = getClients(ss,clients,users,project);
body + =< / table>;

返回正文;


$ b函数getClients(ss,客户端,用户,项目){

var body =;

var newClients = [];

for(var g = 0; g< clients.length; g ++){

var currentProject = clients [g] [2];

if(clients [g]!=){

newClients.push(clients [g] [0]);

}

} // end for

var newUsers = [];

(var u = 0; u< users.length; u ++){

if(users [u]!=){

newUsers.push(users [u] [0]);

}

} // end for

var allData = newUsers.concat(newClients);

var uniqueData = allData.sort();

body + =< tr>< td style =+ STYLE.TD +> + uniqueData.join(< / td>< tr>< td style =+ STYLE.TD +>)+< / td>< / tr>< / tr>

返回正文;

$ b

更新!



答案可以很好地过滤,但是我得到和以前一样的结果:显示过滤结果。我需要从阵列中删除它们。例如:

  var array = ['aa','bb','aa','ff','pp', 'PP']; 
过滤代码...
var array = ['bb','ff'];

我尝试添加splice()js方法,但是我传递的参数不起作用。 / p>

解决方案

您正在处理的数组不再是二维数组,因为您在排序前提取了字段...所以您可以使用一个非常简单的重复删除函数,如下面的示例所示,还有一些添加了 Logger.log 来查看它是如何工作的。

  function test(){
var array = ['aa','bb','cc','aa','dd','cc']
Logger.log(removeDups(array));
}

函数removeDups(array){
var outArray = [];
array.sort():
outArray.push(array [0]);
for(var n in array){
Logger.log(outArray [outArray.length-1] +'='+ array [n] +'?');
if(outArray [outArray.length-1]!= array [n]){
outArray.push(array [n]);
}
}
返回outArray;
}

在您的代码中,这将取代行

  var uniqueData = allData.sort(); 

会变成:

  var uniqueData = removeDups(allData); 






编辑:



如果字母大小写是问题,您可以修改此代码以忽略它。您应该更改条件排序功能,以便它们都忽略名称中的大小写,但最好保留原始大小写。



可以通过下面的代码来实现:

pre $ function test(){
var array = ['aa',' bb','Cc','AA','dd','CC']; //带大写和小写的例子
Logger.log(removeDups(array));
}

函数removeDups(array){
var outArray = [];
array.sort(lowerCase);
函数lowerCase(a,b){
return a.toLowerCase()> b.toLowerCase()? 1:-1; //排序函数不会看到字母大小写
}
outArray.push(array [0]);
for(var n in array){
Logger.log(outArray [outArray.length-1] +'='+ array [n] +'?');如果(outArray [outArray.length-1] .toLowerCase()!= array [n] .toLowerCase()){
outArray.push(array [n]);
}
}
返回outArray;

记录器结果:






编辑2:



这是另一个只保留唯一值的版本(我在第一个版本中没有正确理解您的请求,因为它)

我只是添加了一个 else if 条件来删除那些元素(我保留不区分大小写的版本,但您可以轻松地将其删除)

  function test(){
var array = ['aa','dd','hh','aa','bb','Cc','cc','cc','bb '','nn','bb','AA','dd','CC']; //带大写和小写的例子
Logger.log('original array ='+ array);
Logger.log('unique result ='+ removeDups2(array));



函数removeDups2(array){
var uniqueArray = []
array.sort(lowerCase);
函数lowerCase(a,b){
return a.toLowerCase()> b.toLowerCase()? 1:-1; //排序函数不会看到字母大小写
}
var temp = array [0];
for(var n = 1; n Logger.log(temp +'='+ array [n] +'?');
if(temp.toLowerCase()!= array [n] .toLowerCase()){
uniqueArray.push(array [n]);
temp = array [n];
} else if(uniqueArray [uniqueArray.length-1] == temp){
uniqueArray.pop(); //如果其中一个重复值
} $ b从结果中移除$ b}
返回uniqueArray;

记录器结果新代码:


I would like to know how to remove duplicates values from 2 arrays, combined into one main array.

This values must NOT be removed from the sheets or document, just in the array, thats why I didnt use clear() or clearContents() built in functions;

Ive also tried to modify the removeDuplicates() function from the GAS tutorials, but it throws me rows inside columns from A to Z, instead filtered rows...a total mess.

Notes:

Parameters from getClients() are from others functions, and works ok.

newClients list clients from the sheet 'Users' and newUsers list users from another sheet called 'Data'.

Boths sheets belongs to the same spreadsheet.

newClients and newUsers: both arrays only contains strings (usernames), and in both there are duplicated values.

So the goal is identified and remove those values, the original and the duplicate.

Should be easier I think, but Im new in JS, so everything Ive been tried, didnt worked.

Thanks

The Code

function getAllClientsFromData(body,project){

  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Active the Sheets
  var sheet= ss.getSheets()[1,3];

  // Access data in Sheet "Data"
  var rangeC = ss.getRangeByName("clients"); //A:2:C273  
  var rangeU = ss.getRangeByName("names"); //A5:A

  var clients = rangeC.getValues();
  var users   = rangeU.getValues();

  var lastRow = sheet.getLastRow()-2;

  body += "<h2>Total of " + lastRow + " clients in " + project + "</h2>" 
  body += "<table style=" + STYLE.TABLE + ">";
  body += getClients(ss,clients,users,project);
  body += "</table>";

  return body;  

}

function getClients(ss,clients,users,project){

  var body = "";

  var newClients = [];

  for( var g = 0; g < clients.length; g++ ){

    var currentProject = clients[g][2];

    if( clients[g]!= "" ){

       newClients.push(clients[g][0]);

    } 

  } // end for

  var newUsers = []; 

  for( var u = 0; u < users.length; u++ ){

    if( users[u] != "" ){    

      newUsers.push(users[u][0]);

    }

  } // end for

  var allData = newUsers.concat(newClients);

  var uniqueData = allData.sort();

  body += "<tr><td style=" + STYLE.TD + ">" + uniqueData.join("</td><tr><td style=" + STYLE.TD + ">") + "</td></tr></tr>";

  return body;

}

UPDATES!

The answers works great filtering, but Im getting the same result as on my previous tries: displaying the filtered results. I need to remove them from the array. example:

var array = ['aa','bb','aa','ff','pp', 'pp'];
filtering code...
var array = ['bb','ff'];

I try to add splice() js method but the params I pass, does not working ok.

解决方案

The array you are working on is not a 2D array anymore since you extracted the fields before sorting... so you can use a very simple duplicate removal function as shown below with an example and some added Logger.log to see how it works.

function test(){
  var array = ['aa','bb','cc','aa','dd','cc']
  Logger.log(removeDups(array));
}

function removeDups(array) {
  var outArray = [];
  array.sort():
  outArray.push(array[0]);
  for(var n in array){
    Logger.log(outArray[outArray.length-1]+'  =  '+array[n]+' ?');
    if(outArray[outArray.length-1]!=array[n]){
      outArray.push(array[n]);
    }
  }
  return outArray;
}

in your code this would replace the line

var uniqueData = allData.sort();

that would become :

var uniqueData = removeDups(allData);


EDIT :

If letter case is an issue, you can modify this code to ignore it. You should change the condition and the sort function so that they both ignore the case in your names but preferably keep the original letter case.

This could be achieved with the code below :

function test(){
  var array = ['aa','bb','Cc','AA','dd','CC'];// an example with Upper and Lower case
  Logger.log(removeDups(array));
}

function removeDups(array) {
  var outArray = [];
  array.sort(lowerCase);
  function lowerCase(a,b){
    return a.toLowerCase()>b.toLowerCase() ? 1 : -1;// sort function that does not "see" letter case
  }
  outArray.push(array[0]);
  for(var n in array){
    Logger.log(outArray[outArray.length-1]+'  =  '+array[n]+' ?');
    if(outArray[outArray.length-1].toLowerCase()!=array[n].toLowerCase()){
      outArray.push(array[n]);
    }
  }
  return outArray;
}

Logger result :


EDIT 2 :

Here is another version that keeps only unique values (I didn't understand correctly your request in the first version as it kept one element from the duplicates...)

I simply added an else if condition to remove the elements that were part of a group of duplicates.(I kept the case insensitive version but you can remove it easily)

function test(){
  var array = ['aa','dd','hh','aa','bb','Cc','cc','cc','bb','nn','bb','AA','dd','CC'];// an example with Upper and Lower case
  Logger.log('original array = '+array);
  Logger.log('unique result = '+removeDups2(array));
}


function removeDups2(array) {
  var uniqueArray = []
  array.sort(lowerCase);
  function lowerCase(a,b){
    return a.toLowerCase()>b.toLowerCase() ? 1 : -1;// sort function that does not "see" letter case
  }
  var temp = array[0];
  for(var n=1 ;n<array.length ; n++){
    Logger.log(temp+'  =  '+array[n]+' ?');
    if(temp.toLowerCase()!=array[n].toLowerCase()){
      uniqueArray.push(array[n]);
      temp = array[n];
    }else if(uniqueArray[uniqueArray.length-1]==temp){
      uniqueArray.pop();// remove it from result if one of the duplicate values
    }
  }
  return uniqueArray;
}

Logger result new code :

这篇关于删除阵列Google Apps脚本中的重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆