google-sheets - How do I find and delete duplicate values in a range of cells while keeping the first occurrence of a duplicated value in Google Sheets?
问题描述
Ideally, I want to be able to search through an entire sheet (or range of cells) and remove any values that have been repeated in that sheet (for example, if "2" appears in A3
, B8
, and D4
, then I want to keep it in A3
and delete it in B8
and D4
).
I normally see this problem addressed by looking at one column or row for duplicates (using the UNIQUE
function) but not for an entire sheet.
How can I do this?
解决方案
这里有一些代码可以对整个工作表执行此操作。
请注意,它适用于文本和数字,并且会覆盖任何公式。
function removeDuplicates() {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
var traversedValues = [];
var rowCount = values.length;
var colCount = values[0].length;
for (var row = 0; row < rowCount; ++row) {
for (var col = 0; col < colCount; ++col) {
var value = values[row][col];
if (traversedValues.indexOf(value) > -1) {
values[row][col] = null;
} else {
traversedValues.push(value);
}
}
}
range.setValues(values);
}
给你参考
推荐阅读
- google-chrome - 在chrome中使用http地址作为参数的问题
- python - 在python中查找第n个最常见的单词并计数
- java - 如何修复我的linkedHashSet 插入顺序
- javascript - Vuetify:将扩展面板的头部向右对齐
- redux - × TypeError: 中间件不是函数
- php - Illuminate\Routing\Redirect 类的对象或无法转换为字符串
- android - 使用 android.support.design.circulareveal.cardview.CircularRevealCardView 显示效果
- php-7.3 - 打印当月的第二个星期六?
- r - 数据框的 $ 和 [] 函数之间的区别
- grafana - 我应该如何解释这个 grafana 可视化的普罗米修斯直方图桶热图?