英文:
Deleting Duplicate Names
问题
我已经能够在午夜时删除我的每日签到和签出Google表格。现在我需要在扫描两次后删除表格中的条目。例如,第一次扫描时日期和姓名会出现,然后在第二次扫描时,第一次扫描和第二次扫描的日期和姓名行都会被删除。例如,某人早上签到,他们的姓名和时间戳会显示在Google表格的表单响应中。他们离开一天或者出去吃午饭,然后再次扫描,他们的姓名和另一个时间戳会显示出来。这将触发一个触发器,将从表单响应中删除这两个条目。
以下是您当前的代码,它将删除第二次输入的条目,但不会删除第一次输入的条目。如果有人能够帮助我删除这两个条目,那将非常有帮助。
function deleteDups() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const startrow = 1;
const datarange = sh.getDataRange();
const lastrow = datarange.getLastRow();
const vs = datarange.getDisplayValues(); // 这将数据获取为字符串
let uA = []; // 唯一字符串数组
let d = 0; // 删除计数器
vs.forEach(function (r, i) {
let p = r[1] + r[2]; // 将两个字符串相加得到一个字符串
// uA 是一个字符串数组
if (uA.indexOf(p) == -1) {
uA.push(p); // uA 由所有唯一字符串组成
} else {
sh.deleteRow(i + startrow - d++); // 删除计数器弥补了被删除的行,因为数据的索引保持不变,尽管行现在已经不存在。
}
});
}
英文:
I was able to get my Daily Check in and out google sheet to delete everyday at midnight. Now I need the sheet to delete entries after scanning in twice. So like the date and name pop on on the first scan and then on the second name the date and name rows for both the first scan and the second scan get deleted. For example the person scans in in the morning, their name with a timestamp shows up on a form response on google sheets. They leave for the day or go out for lunch they scan out and their name with another timestamp shows up. Which activates a trigger that will delete both entries out of the form responses.
function deleteDups() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getActiveSheet();
const startrow=1;
const datarange=sh.getDataRange()
const lastrow=datarange.getLastRow()
const vs=datarange.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.
}
});
}
This is the current code I have and it'll delete the second entry that gets entered but not the first one as well. So if someone could help me be able to delete both that would be super helpful
答案1
得分: 0
这应该可以工作
function deleteDups() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const startRow = 2; // 假设第1行是标题行,如有需要请更改
const dataRange = sh.getRange(startRow, 1, sh.getLastRow() - startRow + 1, sh.getLastColumn());
const data = dataRange.getValues();
const duplicates = [];
data.forEach(function(row, rowIndex) {
const name = row[1]; // 假设名称在B列,如有需要请更改
const timestamp = row[2]; // 假设时间戳在C列,如有需要请更改
const duplicatesIndex = duplicates.findIndex(dup => dup.name === name);
if (duplicatesIndex > -1) {
const duplicate = duplicates[duplicatesIndex];
sh.deleteRow(duplicate.rowIndex + startRow - rowIndex);
sh.deleteRow(rowIndex + startRow - duplicate.rowIndex);
duplicates.splice(duplicatesIndex, 1);
} else {
duplicates.push({ name: name, rowIndex: rowIndex });
}
});
}
英文:
This should work
function deleteDups() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const startRow = 2; // Assuming row 1 is the header row, change it if necessary
const dataRange = sh.getRange(startRow, 1, sh.getLastRow() - startRow + 1, sh.getLastColumn());
const data = dataRange.getValues();
const duplicates = [];
data.forEach(function(row, rowIndex) {
const name = row[1]; // Assuming the name is in column B, change it if necessary
const timestamp = row[2]; // Assuming the timestamp is in column C, change it if necessary
const duplicatesIndex = duplicates.findIndex(dup => dup.name === name);
if (duplicatesIndex > -1) {
const duplicate = duplicates[duplicatesIndex];
sh.deleteRow(duplicate.rowIndex + startRow - rowIndex);
sh.deleteRow(rowIndex + startRow - duplicate.rowIndex);
duplicates.splice(duplicatesIndex, 1);
} else {
duplicates.push({ name: name, rowIndex: rowIndex });
}
});
}
答案2
得分: 0
我相信您的目标如下。
- 您想要在列"B"和"C"重复时删除行。
- 在您的情况下,您希望删除所有重复的行。例如,当第3行和第5行的列"A"到"C"分别为"a3, b3, c3"和"a5, b3, c3"时,您希望删除这两行。
在这种情况下,以下流程如何?
- 通过检查重复的行检索所有行。
- 删除所有重复的行。
按照这个流程,所有重复的行都可以被删除。当这反映在您的脚本中时,以下修改如何?
模式1:
在这种模式下,创建新值并用新值覆盖现有工作表。
function deleteDups2() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const datarange = sh.getDataRange();
const values = [...datarange.getDisplayValues().reduce((m, r) => {
const k = r[1] + r[2];
return m.set(k, m.has(k) ? [...m.get(k), r] : [r]);
}, new Map()).values()].filter(r => r.length == 1).flat(1);
datarange.clearContent().offset(0, 0, values.length, values[0].length).setValues(values);
}
模式2:
在这种模式下,删除重复的行。
function deleteDups3() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const datarange = sh.getDataRange();
const values = [...datarange.getDisplayValues().reduce((m, r, i) => {
const k = r[1] + r[2];
return m.set(k, m.has(k) ? [...m.get(k), i + 1] : [i + 1]);
}, new Map()).values()].filter(r => r.length > 1).flat(1).sort((a, b) => a < b ? 1 : -1);
values.forEach(e => sh.deleteRow(e));
}
参考资料:
英文:
I believe your goal is as follows.
- You want to delete rows when the columns "B" and "C" are duplicated.
- In your situation, you want to delete all duplicated rows. For example, when the columns "A" to "C" of row 3 and row 5 are
a3, b3, c3
anda5, b3, c3
, you want to delete those 2 rows.
In this case, how about the following flow?
- Retrieve all rows by checking the duplicate rows.
- Delete all duplicated rows.
By this flow, all duplicated rows can be deleted. When this is reflected in your script, how about the following modification?
Pattern 1:
In this pattern, new values are created and the existing sheet is overwritten by the new values.
function deleteDups2() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const datarange = sh.getDataRange();
const values = [...datarange.getDisplayValues().reduce((m, r) => {
const k = r[1] + r[2];
return m.set(k, m.has(k) ? [...m.get(k), r] : [r]);
}, new Map()).values()].filter(r => r.length == 1).flat(1);
datarange.clearContent().offset(0, 0, values.length, values[0].length).setValues(values);
}
Pattern 2:
In this pattern, the duplicated rows are deleted.
function deleteDups3() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const datarange = sh.getDataRange();
const values = [...datarange.getDisplayValues().reduce((m, r, i) => {
const k = r[1] + r[2];
return m.set(k, m.has(k) ? [...m.get(k), i + 1] : [i + 1]);
}, new Map()).values()].filter(r => r.length > 1).flat(1).sort((a, b) => a < b ? 1 : -1);
values.forEach(e => sh.deleteRow(e));
}
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论