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?
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:
PS If you want to reject duplicate values, you can add .setAllowInvalid(false)
to the rule definition.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.