Copy/paste data validation in Google Spreadsheets

二次信任 提交于 2019-12-05 12:28:23

Indeed, the "list from a range" type of validation treats the reference to the list as absolute rather than relative. I know two workarounds:

Custom formula

Validation based on the custom formula

=not(isna(match(J2, W2:X2, 0)))

is equivalent to "value must be from the range W2:X2", and it will be copied down correctly, the reference to W2:X2 being relative.

Drawback: you don't get an in-cell dropdown list with custom formula validation.

Script

One can use an Apps Script to manage data validation rules. The following script sets data validation rules in each cell of the range J2:J100, where the value is required to be from W:X of the same row.

function validate() {    
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange("J2:J100");
  var valuesColumn = 23;  // begins in W 
  var valuesLength = 2;   // has length 2, so W:X
  var firstRow = range.getRow();
  for (var i = 0; i < range.getHeight(); i++) {
    var rule = SpreadsheetApp.newDataValidation()
               .requireValueInRange(sheet.getRange(firstRow + i, valuesColumn, 1, valuesLength), true)
               .setAllowInvalid(false)
               .build();
    range.offset(i, 0, 1, 1).setDataValidation(rule);
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!