Skip to content

Instantly share code, notes, and snippets.

@leosoto
Last active November 12, 2020 22:02
Show Gist options
  • Save leosoto/0180c1dd2cb04f5e983f829eabb7fbf4 to your computer and use it in GitHub Desktop.
Save leosoto/0180c1dd2cb04f5e983f829eabb7fbf4 to your computer and use it in GitHub Desktop.
const table = await input.tableAsync("Pick a table");
const fields = [];
let moreFieldsAnswer;
do {
fields.push(await input.fieldAsync('Pick field to deduplicate', table));
moreFieldsAnswer = await input.buttonsAsync("Add more fields to deduplicate?", ['Add more fields', 'No']);
} while (moreFieldsAnswer == 'Add more fields');
const result = await table.selectRecordsAsync({
sorts: fields.map((f) => {return {field: f}})
});
let previousRecord = null;
const duplicatesToRemove = [];
for (let record of result.records) {
if (previousRecord == null) {
previousRecord = record;
continue;
}
let duplicateValuesForField = (field) => {
return previousRecord.getCellValueAsString(field) ==
record.getCellValueAsString(field);
}
if (fields.every(duplicateValuesForField)) {
duplicatesToRemove.push(record);
}
previousRecord = record;
}
if (duplicatesToRemove.length > 0) {
output.text(duplicatesToRemove.length + " duplicates found:");
output.table(duplicatesToRemove);
const removeAnswer = await input.buttonsAsync('Do you really want to remove them?', ['Remove', 'Abort'])
if (removeAnswer == 'Remove') {
for (let i = 0; i < duplicatesToRemove.length; i+= 50) {
await table.deleteRecordsAsync(duplicatesToRemove.slice(i, i + 50));
}
}
} else {
output.text("No duplicates founds");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment