【问题标题】:Check for duplicate entry with Google Sheet for specific columns使用 Google 表格检查特定列的重复条目
【发布时间】:2021-01-12 20:44:13
【问题描述】:

我有一个 Google 表格,由于 Zapier 电话而定期更新。每隔一段时间就会在工作表中添加一个新行,我想检查工作表的其余部分是否有任何重复项,如果发现则删除重复项。可能的重复项只会出现在某些字段上,例如:

  1. 添加了一个条目:Title = "My super duper night run",Distance = "3.6",Time = "24.3"
  2. 已经有一个条目:Title = "night run", Distance = "3.6", Time = "24.3"
  3. 已经有一个条目:Title = "My super duper night run", Distance = "5.5", Time = "45.1"

在上面的场景中,1号将被保留,2号将被删除,3号将被保留。注意:理想情况下,宏/脚本会在每次更新后运行,但也可以每隔一段时间运行一次,然后在整个列表中搜索重复项,尽管需要保留最新条目并删除旧条目。

我有一些使用 VBA 的经验并构建了基本的表格脚本,但不知道如何构建代码以循环遍历表格(自下而上),然后在找到重复项时删除。

【问题讨论】:

标签: javascript loops google-apps-script google-sheets


【解决方案1】:

假设第 2 列和第 3 列的匹配是重复的,那么这应该可以工作

function deleteDups() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  const startrow=2;
  const rg=sh.getRange(startrow,1,sh.getLastRow()-startrow+1,sh.getLastColumn());
  const vs=rg.getDisplayValues();//this get the data as string
  let uA=[];//unique array
  let d=0;//delete counter
  vs.forEach(function(r,i){
    let p=r[1]+r[2];//adding two strings together is a string
    //uA is an array of strings
    if(uA.indexOf(p)==-1) {
      uA.push(p);//uA is made up of all the unique strings
    }else{
      sh.deleteRow(i+startrow-d++);//the delete counter makes up for the rows that get deleted because the index into the  data remains the same even though the row is now gone.
    }
  });
}

【讨论】:

  • 谢谢 - 会检查一下。
猜你喜欢
  • 2015-03-16
  • 2012-12-06
  • 2021-12-31
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
相关资源
最近更新 更多