Reputation: 135
Can we set the following data validation rule across a range to prevent duplicate entries?
=COUNTIF(B$6:B,B6)<2
The above custom formula in Data Validation stops duplicate entries in column B from row 6 downwards, so the rule on the 15th column would be:
=COUNTIF(B$6:B,B15)<2
Can we do this programmatically in GAS?
Upvotes: 0
Views: 372
Reputation: 5163
The Data Validation Builder can define a data validation rule across a range:
Sample Code:
function myFunction() {
var cell = SpreadsheetApp.getActive().getRange("B6:B");
var rule = SpreadsheetApp.newDataValidation().requireFormulaSatisfied("=COUNTIF(B$6:B,B6)<2").build();
cell.setDataValidation(rule);
}
Sample Sheet:
P.S. If you want to reject duplicate values, you can add .setAllowInvalid(false)
to the rule definition.
Upvotes: 2