简体   繁体   中英

Custom Formula Data Validation

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?

Solution:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM