Skip to main content

Link records with duplicate values

info

Select a record and the column to be checked, the script will automatically find the rest of the duplicate records and then associate them to the selected record (you need to create a Two-way link field in advance)

Demo

duplicate-checker

Source code

const datasheet = await space.getActiveDatasheetAsync();

const storeField = await input.fieldAsync(
"Please select the field to store duplicate records, the type needs to be two-way link and the association is the current datasheet:",
datasheet
);
if (storeField.type != "TwoWayLink") {
output.text(
"Failed to run: The selected field type needs to be a Two-way link"
);
} else if (storeField.property.foreignDatasheetId != datasheet.id) {
output.text(
"Failed to run: the selected magical link field must be linked to current datasheet"
);
} else {
console.log(storeField.property);

const needFindRecord = await input.recordAsync(
"Please select the record to check:",
datasheet
);
const needFindField = await input.fieldAsync(
"Please select the fields to check for duplicates:",
datasheet
);

const records = await datasheet.getRecordsAsync();

// Define two data sets that need to be used later, duplicates stores the duplicate record ids found, and valuesMap will store the data used to update the TwoWayLink fields
const duplicates = [];
const valuesMap = {};

for (let record of records) {
//The data that is empty does not belong to the content to be found, so it is skipped
if (record.getCellValueString(needFindField.id) === null) {
continue;
}
//Need to exclude yourself when checking for duplicates
if (record.id === needFindRecord.id) {
continue;
}
//Obtain data according to getCellValueString and judge whether they are equal. If they are equal, it means that the two are repeated
if (
record.getCellValueString(needFindField.id) ===
needFindRecord.getCellValueString(needFindField.id)
) {
duplicates.push(record.id);
}
}

console.log(duplicates);

if (duplicates.length === 0) {
output.text(`No duplicate records found`);
} else {
output.text(
`Found ${duplicates.length} duplicate records, which have been linked in the Two-way link field`
);
valuesMap[storeField.id] = duplicates;
await datasheet.updateRecordAsync(needFindRecord.id, valuesMap);
}
}